28#include <SFML/Network/SocketTCP.hpp>
29#include <SFML/Network/IPAddress.hpp>
30#include <SFML/Network/Packet.hpp>
31#include <SFML/Network/SocketHelper.hpp>
38 #pragma warning(disable : 4127)
122 if (Status == Socket::NotReady)
132 Time.tv_usec = (
static_cast<long>(
Timeout * 1000) % 1000) * 1000;
143 Status = Socket::Done;
186 std::cerr <<
"Failed to bind socket to port " <<
Port << std::endl;
191 if (
listen(mySocket, 0) == -1)
194 std::cerr <<
"Failed to listen to port " <<
Port << std::endl;
239 return Socket::Error;
262 std::cerr <<
"Cannot send data over the network (invalid parameters)" << std::endl;
263 return Socket::Error;
279 return Socket::Error;
295 return Socket::Disconnected;
305 std::cerr <<
"Cannot receive data from the network (invalid parameters)" << std::endl;
306 return Socket::Error;
345 if (myPendingPacketSize < 0)
349 while (myPendingHeaderSize <
sizeof(myPendingHeader))
351 char*
Data =
reinterpret_cast<char*
>(&myPendingHeader) + myPendingHeaderSize;
352 Socket::Status Status =
Receive(
Data,
sizeof(myPendingHeader) - myPendingHeaderSize,
Received);
355 if (Status != Socket::Done)
360 myPendingHeaderSize = 0;
375 if (Status != Socket::Done)
378 if (Status == Socket::NotReady)
386 myPendingPacket.resize(myPendingPacket.size() +
Received);
387 char*
Begin = &myPendingPacket[0] + myPendingPacket.size() -
Received;
394 if (!myPendingPacket.empty())
395 PacketToReceive.OnReceive(&myPendingPacket[0], myPendingPacket.size());
396 myPendingPacket.clear();
397 myPendingPacketSize = -1;
412 std::cerr <<
"Failed to close socket" << std::endl;
440 return mySocket ==
Other.mySocket;
449 return mySocket !=
Other.mySocket;
460 return mySocket <
Other.mySocket;
477void SocketTCP::Create(SocketHelper::SocketType Descriptor)
480 mySocket = Descriptor ? Descriptor : socket(PF_INET, SOCK_STREAM, 0);
484 myPendingHeaderSize = 0;
485 myPendingPacket.clear();
486 myPendingPacketSize = -1;
493 if (setsockopt(mySocket, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<char*
>(&Yes),
sizeof(Yes)) == -1)
495 std::cerr <<
"Failed to set socket option \"SO_REUSEADDR\" ; "
496 <<
"binding to a same port may fail if too fast" << std::endl;
500 if (setsockopt(mySocket, IPPROTO_TCP, TCP_NODELAY,
reinterpret_cast<char*
>(&Yes),
sizeof(Yes)) == -1)
502 std::cerr <<
"Failed to set socket option \"TCP_NODELAY\" ; "
503 <<
"all your TCP packets will be buffered" << std::endl;
IPAddress provides easy manipulation of IP v4 addresses.
Packet wraps data to send / to receive through the network.
Selector allow reading from multiple sockets without blocking.
static void SetBlocking(SocketType Socket, bool Block)
Set a socket as blocking or non-blocking.
static Socket::Status GetErrorStatus()
Get the last socket error status.
static SocketType InvalidSocket()
Return the value of the invalid socket.
static bool Close(SocketType Socket)
Close / destroy a socket.
SocketTCP wraps a socket using TCP protocol to send data safely (but a bit slower)
SocketTCP()
Default constructor.
bool IsValid() const
Check if the socket is in a valid state ; this function can be called any time to check if the socket...
bool Listen(unsigned short Port)
Listen to a specified port for incoming data or connections.
Socket::Status Receive(char *Data, std::size_t MaxSize, std::size_t &SizeReceived)
Receive an array of bytes from the host (must be connected first).
Socket::Status Send(const char *Data, std::size_t Size)
Send an array of bytes to the host (must be connected first)
bool Close()
Close the socket.
Socket::Status Accept(SocketTCP &Connected, IPAddress *Address=NULL)
Wait for a connection (must be listening to a port).
void SetBlocking(bool Blocking)
Change the blocking state of the socket.
bool operator!=(const SocketTCP &Other) const
Comparison operator !=.
bool operator==(const SocketTCP &Other) const
Comparison operator ==.
bool operator<(const SocketTCP &Other) const
Comparison operator <.
Socket::Status Connect(unsigned short Port, const IPAddress &HostAddress, float Timeout=0.f)
Connect to another computer on a specified port.
Vector2 is an utility class for manipulating 2 dimensional vectors.