28#include <SFML/Network/SocketUDP.hpp>
29#include <SFML/Network/IPAddress.hpp>
30#include <SFML/Network/Packet.hpp>
84 std::cerr <<
"Failed to bind the socket to port " <<
Port << std::endl;
152 std::cerr <<
"Cannot send data over the network (invalid parameters)" << std::endl;
153 return Socket::Error;
170 std::cerr <<
"Failed to receive data ; the UDP socket first needs to be bound to a port" << std::endl;
171 return Socket::Error;
210 std::cerr <<
"Cannot receive data from the network (invalid parameters)" << std::endl;
211 return Socket::Error;
250 if (myPendingPacketSize < 0)
254 while (myPendingHeaderSize <
sizeof(myPendingHeader))
256 char*
Data =
reinterpret_cast<char*
>(&myPendingHeader) + myPendingHeaderSize;
260 if (Status != Socket::Done)
265 myPendingHeaderSize = 0;
285 if (Status != Socket::Done)
288 if (Status == Socket::NotReady)
296 myPendingPacket.resize(myPendingPacket.size() +
Received);
297 char*
Begin = &myPendingPacket[0] + myPendingPacket.size() -
Received;
304 if (!myPendingPacket.empty())
305 PacketToReceive.OnReceive(&myPendingPacket[0], myPendingPacket.size());
306 myPendingPacket.clear();
307 myPendingPacketSize = -1;
322 std::cerr <<
"Failed to close socket" << std::endl;
360 return mySocket ==
Other.mySocket;
369 return mySocket !=
Other.mySocket;
380 return mySocket <
Other.mySocket;
397void SocketUDP::Create(SocketHelper::SocketType Descriptor)
400 mySocket = Descriptor ? Descriptor : socket(PF_INET, SOCK_DGRAM, 0);
407 myPendingHeaderSize = 0;
408 myPendingPacket.clear();
409 myPendingPacketSize = -1;
416 if (setsockopt(mySocket, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<char*
>(&Yes),
sizeof(Yes)) == -1)
418 std::cerr <<
"Failed to set socket option \"reuse address\" ; "
419 <<
"binding to a same port may fail if too fast" << std::endl;
423 if (setsockopt(mySocket, SOL_SOCKET, SO_BROADCAST,
reinterpret_cast<char*
>(&Yes),
sizeof(Yes)) == -1)
425 std::cerr <<
"Failed to enable broadcast on UDP socket" << std::endl;
IPAddress provides easy manipulation of IP v4 addresses.
Packet wraps data to send / to receive through the network.
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.
SocketUDP wraps a socket using UDP protocol to send data fastly (but with less safety)
unsigned short GetPort() const
Get the port the socket is currently bound to.
void SetBlocking(bool Blocking)
Change the blocking state of the socket.
bool Bind(unsigned short Port)
Bind the socket to a specific port.
bool Unbind()
Unbind the socket from its previous port, if any.
bool operator==(const SocketUDP &Other) const
Comparison operator ==.
SocketUDP()
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...
Socket::Status Send(const char *Data, std::size_t Size, const IPAddress &Address, unsigned short Port)
Send an array of bytes.
Socket::Status Receive(char *Data, std::size_t MaxSize, std::size_t &SizeReceived, IPAddress &Address, unsigned short &Port)
Receive an array of bytes.
bool Close()
Close the socket.
bool operator<(const SocketUDP &Other) const
Comparison operator <.
bool operator!=(const SocketUDP &Other) const
Comparison operator !=.
Vector2 is an utility class for manipulating 2 dimensional vectors.