5a5bdb3fa989685df8396ae21fa6a54f02c94734
[TDDC76_proj.git] /
1 ////////////////////////////////////////////////////////////
2 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
23 ////////////////////////////////////////////////////////////
24
25 #ifndef SFML_TCPLISTENER_HPP
26 #define SFML_TCPLISTENER_HPP
27
28 ////////////////////////////////////////////////////////////
29 // Headers
30 ////////////////////////////////////////////////////////////
31 #include <SFML/Network/Export.hpp>
32 #include <SFML/Network/Socket.hpp>
33
34
35 namespace sf
36 {
37 class TcpSocket;
38
39 ////////////////////////////////////////////////////////////
40 /// \brief Socket that listens to new TCP connections
41 ///
42 ////////////////////////////////////////////////////////////
43 class SFML_NETWORK_API TcpListener : public Socket
44 {
45 public :
46
47 ////////////////////////////////////////////////////////////
48 /// \brief Default constructor
49 ///
50 ////////////////////////////////////////////////////////////
51 TcpListener();
52
53 ////////////////////////////////////////////////////////////
54 /// \brief Get the port to which the socket is bound locally
55 ///
56 /// If the socket is not listening to a port, this function
57 /// returns 0.
58 ///
59 /// \return Port to which the socket is bound
60 ///
61 /// \see listen
62 ///
63 ////////////////////////////////////////////////////////////
64 unsigned short getLocalPort() const;
65
66 ////////////////////////////////////////////////////////////
67 /// \brief Start listening for connections
68 ///
69 /// This functions makes the socket listen to the specified
70 /// port, waiting for new connections.
71 /// If the socket was previously listening to another port,
72 /// it will be stopped first and bound to the new port.
73 ///
74 /// \param port Port to listen for new connections
75 ///
76 /// \return Status code
77 ///
78 /// \see accept, close
79 ///
80 ////////////////////////////////////////////////////////////
81 Status listen(unsigned short port);
82
83 ////////////////////////////////////////////////////////////
84 /// \brief Stop listening and close the socket
85 ///
86 /// This function gracefully stops the listener. If the
87 /// socket is not listening, this function has no effect.
88 ///
89 /// \see listen
90 ///
91 ////////////////////////////////////////////////////////////
92 void close();
93
94 ////////////////////////////////////////////////////////////
95 /// \brief Accept a new connection
96 ///
97 /// If the socket is in blocking mode, this function will
98 /// not return until a connection is actually received.
99 ///
100 /// \param socket Socket that will hold the new connection
101 ///
102 /// \return Status code
103 ///
104 /// \see listen
105 ///
106 ////////////////////////////////////////////////////////////
107 Status accept(TcpSocket& socket);
108 };
109
110
111 } // namespace sf
112
113
114 #endif // SFML_TCPLISTENER_HPP
115
116
117 ////////////////////////////////////////////////////////////
118 /// \class sf::TcpListener
119 /// \ingroup network
120 ///
121 /// A listener socket is a special type of socket that listens to
122 /// a given port and waits for connections on that port.
123 /// This is all it can do.
124 ///
125 /// When a new connection is received, you must call accept and
126 /// the listener returns a new instance of sf::TcpSocket that
127 /// is properly initialized and can be used to communicate with
128 /// the new client.
129 ///
130 /// Listener sockets are specific to the TCP protocol,
131 /// UDP sockets are connectionless and can therefore communicate
132 /// directly. As a consequence, a listener socket will always
133 /// return the new connections as sf::TcpSocket instances.
134 ///
135 /// A listener is automatically closed on destruction, like all
136 /// other types of socket. However if you want to stop listening
137 /// before the socket is destroyed, you can call its close()
138 /// function.
139 ///
140 /// Usage example:
141 /// \code
142 /// // Create a listener socket and make it wait for new
143 /// // connections on port 55001
144 /// sf::TcpListener listener;
145 /// listener.listen(55001);
146 ///
147 /// // Endless loop that waits for new connections
148 /// while (running)
149 /// {
150 /// sf::TcpSocket client;
151 /// if (listener.accept(client) == sf::Socket::Done)
152 /// {
153 /// // A new client just connected!
154 /// std::cout << "New connection received from " << client.getRemoteAddress() << std::endl;
155 /// doSomethingWith(client);
156 /// }
157 /// }
158 /// \endcode
159 ///
160 /// \see sf::TcpSocket, sf::Socket
161 ///
162 ////////////////////////////////////////////////////////////