1 ////////////////////////////////////////////////////////////
\r
3 // SFML - Simple and Fast Multimedia Library
\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
\r
6 // This software is provided 'as-is', without any express or implied warranty.
\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.
\r
9 // Permission is granted to anyone to use this software for any purpose,
\r
10 // including commercial applications, and to alter it and redistribute it freely,
\r
11 // subject to the following restrictions:
\r
13 // 1. The origin of this software must not be misrepresented;
\r
14 // you must not claim that you wrote the original software.
\r
15 // If you use this software in a product, an acknowledgment
\r
16 // in the product documentation would be appreciated but is not required.
\r
18 // 2. Altered source versions must be plainly marked as such,
\r
19 // and must not be misrepresented as being the original software.
\r
21 // 3. This notice may not be removed or altered from any source distribution.
\r
23 ////////////////////////////////////////////////////////////
\r
25 #ifndef SFML_SOCKETTCP_HPP
\r
26 #define SFML_SOCKETTCP_HPP
\r
28 ////////////////////////////////////////////////////////////
\r
30 ////////////////////////////////////////////////////////////
\r
31 #include <SFML/Network/SocketHelper.hpp>
\r
39 template <typename> class Selector;
\r
41 ////////////////////////////////////////////////////////////
\r
42 /// SocketTCP wraps a socket using TCP protocol to
\r
43 /// send data safely (but a bit slower)
\r
44 ////////////////////////////////////////////////////////////
\r
45 class SFML_API SocketTCP
\r
49 ////////////////////////////////////////////////////////////
\r
50 /// Default constructor
\r
52 ////////////////////////////////////////////////////////////
\r
55 ////////////////////////////////////////////////////////////
\r
56 /// Change the blocking state of the socket.
\r
57 /// The default behaviour of a socket is blocking
\r
59 /// \param Blocking : Pass true to set the socket as blocking, or false for non-blocking
\r
61 ////////////////////////////////////////////////////////////
\r
62 void SetBlocking(bool Blocking);
\r
64 ////////////////////////////////////////////////////////////
\r
65 /// Connect to another computer on a specified port
\r
67 /// \param Port : Port to use for transfers (warning : ports < 1024 are reserved)
\r
68 /// \param HostAddress : IP Address of the host to connect to
\r
69 /// \param Timeout : Maximum time to wait, in seconds (0 by default : no timeout) (this parameter is ignored for non-blocking sockets)
\r
71 /// \return True if operation has been successful
\r
73 ////////////////////////////////////////////////////////////
\r
74 Socket::Status Connect(unsigned short Port, const IPAddress& HostAddress, float Timeout = 0.f);
\r
76 ////////////////////////////////////////////////////////////
\r
77 /// Listen to a specified port for incoming data or connections
\r
79 /// \param Port : Port to listen to
\r
81 /// \return True if operation has been successful
\r
83 ////////////////////////////////////////////////////////////
\r
84 bool Listen(unsigned short Port);
\r
86 ////////////////////////////////////////////////////////////
\r
87 /// Wait for a connection (must be listening to a port).
\r
88 /// This function will block if the socket is blocking
\r
90 /// \param Connected : Socket containing the connection with the connected client
\r
91 /// \param Address : Pointer to an address to fill with client infos (NULL by default)
\r
93 /// \return Status code
\r
95 ////////////////////////////////////////////////////////////
\r
96 Socket::Status Accept(SocketTCP& Connected, IPAddress* Address = NULL);
\r
98 ////////////////////////////////////////////////////////////
\r
99 /// Send an array of bytes to the host (must be connected first)
\r
101 /// \param Data : Pointer to the bytes to send
\r
102 /// \param Size : Number of bytes to send
\r
104 /// \return Status code
\r
106 ////////////////////////////////////////////////////////////
\r
107 Socket::Status Send(const char* Data, std::size_t Size);
\r
109 ////////////////////////////////////////////////////////////
\r
110 /// Receive an array of bytes from the host (must be connected first).
\r
111 /// This function will block if the socket is blocking
\r
113 /// \param Data : Pointer to a byte array to fill (make sure it is big enough)
\r
114 /// \param MaxSize : Maximum number of bytes to read
\r
115 /// \param SizeReceived : Number of bytes received
\r
117 /// \return Status code
\r
119 ////////////////////////////////////////////////////////////
\r
120 Socket::Status Receive(char* Data, std::size_t MaxSize, std::size_t& SizeReceived);
\r
122 ////////////////////////////////////////////////////////////
\r
123 /// Send a packet of data to the host (must be connected first)
\r
125 /// \param PacketToSend : Packet to send
\r
127 /// \return Status code
\r
129 ////////////////////////////////////////////////////////////
\r
130 Socket::Status Send(Packet& PacketToSend);
\r
132 ////////////////////////////////////////////////////////////
\r
133 /// Receive a packet from the host (must be connected first).
\r
134 /// This function will block if the socket is blocking
\r
136 /// \param PacketToReceive : Packet to fill with received data
\r
138 /// \return Status code
\r
140 ////////////////////////////////////////////////////////////
\r
141 Socket::Status Receive(Packet& PacketToReceive);
\r
143 ////////////////////////////////////////////////////////////
\r
144 /// Close the socket
\r
146 /// \return True if operation has been successful
\r
148 ////////////////////////////////////////////////////////////
\r
151 ////////////////////////////////////////////////////////////
\r
152 /// Check if the socket is in a valid state ; this function
\r
153 /// can be called any time to check if the socket is OK
\r
155 /// \return True if the socket is valid
\r
157 ////////////////////////////////////////////////////////////
\r
158 bool IsValid() const;
\r
160 ////////////////////////////////////////////////////////////
\r
161 /// Comparison operator ==
\r
163 /// \param Other : Socket to compare
\r
165 /// \return True if *this == Other
\r
167 ////////////////////////////////////////////////////////////
\r
168 bool operator ==(const SocketTCP& Other) const;
\r
170 ////////////////////////////////////////////////////////////
\r
171 /// Comparison operator !=
\r
173 /// \param Other : Socket to compare
\r
175 /// \return True if *this != Other
\r
177 ////////////////////////////////////////////////////////////
\r
178 bool operator !=(const SocketTCP& Other) const;
\r
180 ////////////////////////////////////////////////////////////
\r
181 /// Comparison operator <.
\r
182 /// Provided for compatibility with standard containers, as
\r
183 /// comparing two sockets doesn't make much sense...
\r
185 /// \param Other : Socket to compare
\r
187 /// \return True if *this < Other
\r
189 ////////////////////////////////////////////////////////////
\r
190 bool operator <(const SocketTCP& Other) const;
\r
194 friend class Selector<SocketTCP>;
\r
196 ////////////////////////////////////////////////////////////
\r
197 /// Construct the socket from a socket descriptor
\r
198 /// (for internal use only)
\r
200 /// \param Descriptor : Socket descriptor
\r
202 ////////////////////////////////////////////////////////////
\r
203 SocketTCP(SocketHelper::SocketType Descriptor);
\r
205 ////////////////////////////////////////////////////////////
\r
206 /// Create the socket
\r
208 /// \param Descriptor : System socket descriptor to use (0 by default -- create a new socket)
\r
210 ////////////////////////////////////////////////////////////
\r
211 void Create(SocketHelper::SocketType Descriptor = 0);
\r
213 ////////////////////////////////////////////////////////////
\r
215 ////////////////////////////////////////////////////////////
\r
216 SocketHelper::SocketType mySocket; ///< Socket descriptor
\r
217 Uint32 myPendingHeader; ///< Data of the current pending packet header, if any
\r
218 Uint32 myPendingHeaderSize; ///< Size of the current pending packet header, if any
\r
219 std::vector<char> myPendingPacket; ///< Data of the current pending packet, if any
\r
220 Int32 myPendingPacketSize; ///< Size of the current pending packet, if any
\r
221 bool myIsBlocking; ///< Is the socket blocking or non-blocking ?
\r
227 #endif // SFML_SOCKETTCP_HPP
\r