PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/include/SFML/Network/UdpSocket.hpp

http://github.com/LaurentGomila/SFML
C++ Header | 291 lines | 29 code | 21 blank | 241 comment | 0 complexity | 33af4aa4c5607a15ae87f853fe9a4eee MD5 | raw file
  1. ////////////////////////////////////////////////////////////
  2. //
  3. // SFML - Simple and Fast Multimedia Library
  4. // Copyright (C) 2007-2019 Laurent Gomila (laurent@sfml-dev.org)
  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. //
  23. ////////////////////////////////////////////////////////////
  24. #ifndef SFML_UDPSOCKET_HPP
  25. #define SFML_UDPSOCKET_HPP
  26. ////////////////////////////////////////////////////////////
  27. // Headers
  28. ////////////////////////////////////////////////////////////
  29. #include <SFML/Network/Export.hpp>
  30. #include <SFML/Network/Socket.hpp>
  31. #include <SFML/Network/IpAddress.hpp>
  32. #include <vector>
  33. namespace sf
  34. {
  35. class Packet;
  36. ////////////////////////////////////////////////////////////
  37. /// \brief Specialized socket using the UDP protocol
  38. ///
  39. ////////////////////////////////////////////////////////////
  40. class SFML_NETWORK_API UdpSocket : public Socket
  41. {
  42. public:
  43. ////////////////////////////////////////////////////////////
  44. // Constants
  45. ////////////////////////////////////////////////////////////
  46. enum
  47. {
  48. MaxDatagramSize = 65507 //!< The maximum number of bytes that can be sent in a single UDP datagram
  49. };
  50. ////////////////////////////////////////////////////////////
  51. /// \brief Default constructor
  52. ///
  53. ////////////////////////////////////////////////////////////
  54. UdpSocket();
  55. ////////////////////////////////////////////////////////////
  56. /// \brief Get the port to which the socket is bound locally
  57. ///
  58. /// If the socket is not bound to a port, this function
  59. /// returns 0.
  60. ///
  61. /// \return Port to which the socket is bound
  62. ///
  63. /// \see bind
  64. ///
  65. ////////////////////////////////////////////////////////////
  66. unsigned short getLocalPort() const;
  67. ////////////////////////////////////////////////////////////
  68. /// \brief Bind the socket to a specific port
  69. ///
  70. /// Binding the socket to a port is necessary for being
  71. /// able to receive data on that port.
  72. /// You can use the special value Socket::AnyPort to tell the
  73. /// system to automatically pick an available port, and then
  74. /// call getLocalPort to retrieve the chosen port.
  75. ///
  76. /// Since the socket can only be bound to a single port at
  77. /// any given moment, if it is already bound when this
  78. /// function is called, it will be unbound from the previous
  79. /// port before being bound to the new one.
  80. ///
  81. /// \param port Port to bind the socket to
  82. /// \param address Address of the interface to bind to
  83. ///
  84. /// \return Status code
  85. ///
  86. /// \see unbind, getLocalPort
  87. ///
  88. ////////////////////////////////////////////////////////////
  89. Status bind(unsigned short port, const IpAddress& address = IpAddress::Any);
  90. ////////////////////////////////////////////////////////////
  91. /// \brief Unbind the socket from the local port to which it is bound
  92. ///
  93. /// The port that the socket was previously bound to is immediately
  94. /// made available to the operating system after this function is called.
  95. /// This means that a subsequent call to bind() will be able to re-bind
  96. /// the port if no other process has done so in the mean time.
  97. /// If the socket is not bound to a port, this function has no effect.
  98. ///
  99. /// \see bind
  100. ///
  101. ////////////////////////////////////////////////////////////
  102. void unbind();
  103. ////////////////////////////////////////////////////////////
  104. /// \brief Send raw data to a remote peer
  105. ///
  106. /// Make sure that \a size is not greater than
  107. /// UdpSocket::MaxDatagramSize, otherwise this function will
  108. /// fail and no data will be sent.
  109. ///
  110. /// \param data Pointer to the sequence of bytes to send
  111. /// \param size Number of bytes to send
  112. /// \param remoteAddress Address of the receiver
  113. /// \param remotePort Port of the receiver to send the data to
  114. ///
  115. /// \return Status code
  116. ///
  117. /// \see receive
  118. ///
  119. ////////////////////////////////////////////////////////////
  120. Status send(const void* data, std::size_t size, const IpAddress& remoteAddress, unsigned short remotePort);
  121. ////////////////////////////////////////////////////////////
  122. /// \brief Receive raw data from a remote peer
  123. ///
  124. /// In blocking mode, this function will wait until some
  125. /// bytes are actually received.
  126. /// Be careful to use a buffer which is large enough for
  127. /// the data that you intend to receive, if it is too small
  128. /// then an error will be returned and *all* the data will
  129. /// be lost.
  130. ///
  131. /// \param data Pointer to the array to fill with the received bytes
  132. /// \param size Maximum number of bytes that can be received
  133. /// \param received This variable is filled with the actual number of bytes received
  134. /// \param remoteAddress Address of the peer that sent the data
  135. /// \param remotePort Port of the peer that sent the data
  136. ///
  137. /// \return Status code
  138. ///
  139. /// \see send
  140. ///
  141. ////////////////////////////////////////////////////////////
  142. Status receive(void* data, std::size_t size, std::size_t& received, IpAddress& remoteAddress, unsigned short& remotePort);
  143. ////////////////////////////////////////////////////////////
  144. /// \brief Send a formatted packet of data to a remote peer
  145. ///
  146. /// Make sure that the packet size is not greater than
  147. /// UdpSocket::MaxDatagramSize, otherwise this function will
  148. /// fail and no data will be sent.
  149. ///
  150. /// \param packet Packet to send
  151. /// \param remoteAddress Address of the receiver
  152. /// \param remotePort Port of the receiver to send the data to
  153. ///
  154. /// \return Status code
  155. ///
  156. /// \see receive
  157. ///
  158. ////////////////////////////////////////////////////////////
  159. Status send(Packet& packet, const IpAddress& remoteAddress, unsigned short remotePort);
  160. ////////////////////////////////////////////////////////////
  161. /// \brief Receive a formatted packet of data from a remote peer
  162. ///
  163. /// In blocking mode, this function will wait until the whole packet
  164. /// has been received.
  165. ///
  166. /// \param packet Packet to fill with the received data
  167. /// \param remoteAddress Address of the peer that sent the data
  168. /// \param remotePort Port of the peer that sent the data
  169. ///
  170. /// \return Status code
  171. ///
  172. /// \see send
  173. ///
  174. ////////////////////////////////////////////////////////////
  175. Status receive(Packet& packet, IpAddress& remoteAddress, unsigned short& remotePort);
  176. private:
  177. ////////////////////////////////////////////////////////////
  178. // Member data
  179. ////////////////////////////////////////////////////////////
  180. std::vector<char> m_buffer; //!< Temporary buffer holding the received data in Receive(Packet)
  181. };
  182. } // namespace sf
  183. #endif // SFML_UDPSOCKET_HPP
  184. ////////////////////////////////////////////////////////////
  185. /// \class sf::UdpSocket
  186. /// \ingroup network
  187. ///
  188. /// A UDP socket is a connectionless socket. Instead of
  189. /// connecting once to a remote host, like TCP sockets,
  190. /// it can send to and receive from any host at any time.
  191. ///
  192. /// It is a datagram protocol: bounded blocks of data (datagrams)
  193. /// are transfered over the network rather than a continuous
  194. /// stream of data (TCP). Therefore, one call to send will always
  195. /// match one call to receive (if the datagram is not lost),
  196. /// with the same data that was sent.
  197. ///
  198. /// The UDP protocol is lightweight but unreliable. Unreliable
  199. /// means that datagrams may be duplicated, be lost or
  200. /// arrive reordered. However, if a datagram arrives, its
  201. /// data is guaranteed to be valid.
  202. ///
  203. /// UDP is generally used for real-time communication
  204. /// (audio or video streaming, real-time games, etc.) where
  205. /// speed is crucial and lost data doesn't matter much.
  206. ///
  207. /// Sending and receiving data can use either the low-level
  208. /// or the high-level functions. The low-level functions
  209. /// process a raw sequence of bytes, whereas the high-level
  210. /// interface uses packets (see sf::Packet), which are easier
  211. /// to use and provide more safety regarding the data that is
  212. /// exchanged. You can look at the sf::Packet class to get
  213. /// more details about how they work.
  214. ///
  215. /// It is important to note that UdpSocket is unable to send
  216. /// datagrams bigger than MaxDatagramSize. In this case, it
  217. /// returns an error and doesn't send anything. This applies
  218. /// to both raw data and packets. Indeed, even packets are
  219. /// unable to split and recompose data, due to the unreliability
  220. /// of the protocol (dropped, mixed or duplicated datagrams may
  221. /// lead to a big mess when trying to recompose a packet).
  222. ///
  223. /// If the socket is bound to a port, it is automatically
  224. /// unbound from it when the socket is destroyed. However,
  225. /// you can unbind the socket explicitly with the Unbind
  226. /// function if necessary, to stop receiving messages or
  227. /// make the port available for other sockets.
  228. ///
  229. /// Usage example:
  230. /// \code
  231. /// // ----- The client -----
  232. ///
  233. /// // Create a socket and bind it to the port 55001
  234. /// sf::UdpSocket socket;
  235. /// socket.bind(55001);
  236. ///
  237. /// // Send a message to 192.168.1.50 on port 55002
  238. /// std::string message = "Hi, I am " + sf::IpAddress::getLocalAddress().toString();
  239. /// socket.send(message.c_str(), message.size() + 1, "192.168.1.50", 55002);
  240. ///
  241. /// // Receive an answer (most likely from 192.168.1.50, but could be anyone else)
  242. /// char buffer[1024];
  243. /// std::size_t received = 0;
  244. /// sf::IpAddress sender;
  245. /// unsigned short port;
  246. /// socket.receive(buffer, sizeof(buffer), received, sender, port);
  247. /// std::cout << sender.ToString() << " said: " << buffer << std::endl;
  248. ///
  249. /// // ----- The server -----
  250. ///
  251. /// // Create a socket and bind it to the port 55002
  252. /// sf::UdpSocket socket;
  253. /// socket.bind(55002);
  254. ///
  255. /// // Receive a message from anyone
  256. /// char buffer[1024];
  257. /// std::size_t received = 0;
  258. /// sf::IpAddress sender;
  259. /// unsigned short port;
  260. /// socket.receive(buffer, sizeof(buffer), received, sender, port);
  261. /// std::cout << sender.ToString() << " said: " << buffer << std::endl;
  262. ///
  263. /// // Send an answer
  264. /// std::string message = "Welcome " + sender.toString();
  265. /// socket.send(message.c_str(), message.size() + 1, sender, port);
  266. /// \endcode
  267. ///
  268. /// \see sf::Socket, sf::TcpSocket, sf::Packet
  269. ///
  270. ////////////////////////////////////////////////////////////