SocketUDP.hpp
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2009 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//
24
25#ifndef SFML_SOCKETUDP_HPP
26#define SFML_SOCKETUDP_HPP
27
29// Headers
31#include <SFML/Network/SocketHelper.hpp>
32#include <vector>
33
34
35namespace sf
36{
37class Packet;
38class IPAddress;
39template <typename> class Selector;
40
45class SFML_API SocketUDP
46{
47public :
48
53 SocketUDP();
54
62 void SetBlocking(bool Blocking);
63
72 bool Bind(unsigned short Port);
73
80 bool Unbind();
81
93 Socket::Status Send(const char* Data, std::size_t Size, const IPAddress& Address, unsigned short Port);
94
108 Socket::Status Receive(char* Data, std::size_t MaxSize, std::size_t& SizeReceived, IPAddress& Address, unsigned short& Port);
109
120 Socket::Status Send(Packet& PacketToSend, const IPAddress& Address, unsigned short Port);
121
133 Socket::Status Receive(Packet& PacketToReceive, IPAddress& Address, unsigned short& Port);
134
141 bool Close();
142
150 bool IsValid() const;
151
158 unsigned short GetPort() const;
159
168 bool operator ==(const SocketUDP& Other) const;
169
178 bool operator !=(const SocketUDP& Other) const;
179
190 bool operator <(const SocketUDP& Other) const;
191
192private :
193
194 friend class Selector<SocketUDP>;
195
203 SocketUDP(SocketHelper::SocketType Descriptor);
204
211 void Create(SocketHelper::SocketType Descriptor = 0);
212
214 // Member data
216 SocketHelper::SocketType mySocket;
217 unsigned short myPort;
218 Uint32 myPendingHeader;
219 Uint32 myPendingHeaderSize;
220 std::vector<char> myPendingPacket;
221 Int32 myPendingPacketSize;
222 bool myIsBlocking;
223};
224
225} // namespace sf
226
227
228#endif // SFML_SOCKETUDP_HPP
IPAddress provides easy manipulation of IP v4 addresses.
Definition IPAddress.hpp:43
Packet wraps data to send / to receive through the network.
Definition Packet.hpp:42
Selector allow reading from multiple sockets without blocking.
Definition Selector.hpp:45
unsigned short GetPort() const
Get the port the socket is currently bound to.
void SetBlocking(bool Blocking)
Change the blocking state of the socket.
Definition SocketUDP.cpp:50
bool Bind(unsigned short Port)
Bind the socket to a specific port.
Definition SocketUDP.cpp:64
bool Unbind()
Unbind the socket from its previous port, if any.
SocketUDP()
Default constructor.
Definition SocketUDP.cpp:41
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.