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_SOCKETUDP_HPP
\r
26 #define SFML_SOCKETUDP_HPP
\r
28 ////////////////////////////////////////////////////////////
\r
30 ////////////////////////////////////////////////////////////
\r
31 #include <SFML/Network/SocketHelper.hpp>
\r
39 template <typename> class Selector;
\r
41 ////////////////////////////////////////////////////////////
\r
42 /// SocketUDP wraps a socket using UDP protocol to
\r
43 /// send data fastly (but with less safety)
\r
44 ////////////////////////////////////////////////////////////
\r
45 class SFML_API SocketUDP
\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 /// Bind the socket to a specific port
\r
67 /// \param Port : Port to bind the socket to
\r
69 /// \return True if operation has been successful
\r
71 ////////////////////////////////////////////////////////////
\r
72 bool Bind(unsigned short Port);
\r
74 ////////////////////////////////////////////////////////////
\r
75 /// Unbind the socket from its previous port, if any
\r
77 /// \return True if operation has been successful
\r
79 ////////////////////////////////////////////////////////////
\r
82 ////////////////////////////////////////////////////////////
\r
83 /// Send an array of bytes
\r
85 /// \param Data : Pointer to the bytes to send
\r
86 /// \param Size : Number of bytes to send
\r
87 /// \param Address : Address of the computer to send the packet to
\r
88 /// \param Port : Port to send the data to
\r
90 /// \return Status code
\r
92 ////////////////////////////////////////////////////////////
\r
93 Socket::Status Send(const char* Data, std::size_t Size, const IPAddress& Address, unsigned short Port);
\r
95 ////////////////////////////////////////////////////////////
\r
96 /// Receive an array of bytes.
\r
97 /// This function will block if the socket is blocking
\r
99 /// \param Data : Pointer to a byte array to fill (make sure it is big enough)
\r
100 /// \param MaxSize : Maximum number of bytes to read
\r
101 /// \param SizeReceived : Number of bytes received
\r
102 /// \param Address : Address of the computer which sent the data
\r
103 /// \param Port : Port on which the remote computer sent the data
\r
105 /// \return Status code
\r
107 ////////////////////////////////////////////////////////////
\r
108 Socket::Status Receive(char* Data, std::size_t MaxSize, std::size_t& SizeReceived, IPAddress& Address, unsigned short& Port);
\r
110 ////////////////////////////////////////////////////////////
\r
111 /// Send a packet of data
\r
113 /// \param PacketToSend : Packet to send
\r
114 /// \param Address : Address of the computer to send the packet to
\r
115 /// \param Port : Port to send the data to
\r
117 /// \return Status code
\r
119 ////////////////////////////////////////////////////////////
\r
120 Socket::Status Send(Packet& PacketToSend, const IPAddress& Address, unsigned short Port);
\r
122 ////////////////////////////////////////////////////////////
\r
123 /// Receive a packet.
\r
124 /// This function will block if the socket is blocking
\r
126 /// \param PacketToReceive : Packet to fill with received data
\r
127 /// \param Address : Address of the computer which sent the packet
\r
128 /// \param Port : Port on which the remote computer sent the data
\r
130 /// \return Status code
\r
132 ////////////////////////////////////////////////////////////
\r
133 Socket::Status Receive(Packet& PacketToReceive, IPAddress& Address, unsigned short& Port);
\r
135 ////////////////////////////////////////////////////////////
\r
136 /// Close the socket
\r
138 /// \return True if operation has been successful
\r
140 ////////////////////////////////////////////////////////////
\r
143 ////////////////////////////////////////////////////////////
\r
144 /// Check if the socket is in a valid state ; this function
\r
145 /// can be called any time to check if the socket is OK
\r
147 /// \return True if the socket is valid
\r
149 ////////////////////////////////////////////////////////////
\r
150 bool IsValid() const;
\r
152 ////////////////////////////////////////////////////////////
\r
153 /// Get the port the socket is currently bound to
\r
155 /// \return Current port (0 means the socket is not bound)
\r
157 ////////////////////////////////////////////////////////////
\r
158 unsigned short GetPort() 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 SocketUDP& 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 SocketUDP& 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 SocketUDP& Other) const;
\r
194 friend class Selector<SocketUDP>;
\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 SocketUDP(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 identifier
\r
217 unsigned short myPort; ///< Port to which the socket is bound
\r
218 Uint32 myPendingHeader; ///< Data of the current pending packet header, if any
\r
219 Uint32 myPendingHeaderSize; ///< Size of the current pending packet header, if any
\r
220 std::vector<char> myPendingPacket; ///< Data of the current pending packet, if any
\r
221 Int32 myPendingPacketSize; ///< Size of the current pending packet, if any
\r
222 bool myIsBlocking; ///< Is the socket blocking or non-blocking ?
\r
228 #endif // SFML_SOCKETUDP_HPP
\r