PageRenderTime 71ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/ClassMaster2014/barahon/Cultivation_9_UnixSource/minorGems/network/SocketUDP.h

https://gitlab.com/garheade/linux_camp
C++ Header | 199 lines | 41 code | 48 blank | 110 comment | 4 complexity | 1610101dccb5e91b0b48cb3efc22c733 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * Modification History
  3. *
  4. * 2004-November-3 Jason Rohrer
  5. * Created.
  6. *
  7. * 2004-November-4 Jason Rohrer
  8. * Added default receive timeout (infinite).
  9. *
  10. * 2004-November-9 Jason Rohrer
  11. * Added functions for comparing and copying UDPAddresses.
  12. */
  13. #ifndef SOCKET_UDP_INCLUDED
  14. #define SOCKET_UDP_INCLUDED
  15. /**
  16. * Structure representing a UDP endpoint.
  17. */
  18. struct UDPAddress {
  19. // binary internet address in network byte order
  20. unsigned long mIPAddress;
  21. // port number in network byte order
  22. unsigned short mPort;
  23. };
  24. /**
  25. * Network socket that can be used as an endpoint for sending and receiving
  26. * UDP packets (unreliable datagrams).
  27. *
  28. * Note: Implementation for the functions defined here is provided
  29. * separately for each platform (in the unix/ and win32/ subdirectories).
  30. *
  31. * Socket::initSocketFramework() must be called once before this class
  32. * is used.
  33. *
  34. * @author Jason Rohrer
  35. */
  36. class SocketUDP {
  37. public:
  38. /**
  39. * Constructs a UDP socket and starts listening for incoming datagrams.
  40. *
  41. * @param inReceivePort the port to listen on, in platform-dependent
  42. * byte order.
  43. */
  44. SocketUDP( unsigned short inReceivePort );
  45. ~SocketUDP();
  46. /**
  47. * Makes a UDPAddress structure.
  48. *
  49. * @param inAddress the IP address in ascii numbers-and-dots notation.
  50. * Must be destroyed by caller if non-const.
  51. * @param inPort the port number in platform-specific byte order.
  52. *
  53. * @return an address structure, or NULL if converting the address
  54. * fails.
  55. * Must be destroyed by caller if non-NULL.
  56. */
  57. static struct UDPAddress *makeAddress( const char *inAddress,
  58. unsigned short inPort );
  59. /**
  60. * Extracts address elements from a UDPAddress structure.
  61. *
  62. * @param inAddress the address structure. Must be destroyed by
  63. * caller.
  64. * @param outPort pointer to where the port number, in
  65. * platform-specific byte order, should be returned.
  66. *
  67. * @return the IP address in ascci numbers-and-dots notation.
  68. * Must be destroyed by caller.
  69. */
  70. static char *extractAddress( struct UDPAddress *inAddress,
  71. unsigned short *outPort );
  72. /**
  73. * Compares two UDP addresses.
  74. *
  75. * @param inFirst the first address.
  76. * @param inSecond the second address.
  77. *
  78. * @return true if the addresses are equal, or false if they are
  79. * different.
  80. */
  81. static char compare( struct UDPAddress *inFirst,
  82. struct UDPAddress *inSecond );
  83. /**
  84. * Makes a copy of a UDP address.
  85. *
  86. * @param inAddress the address to copy.
  87. *
  88. * @return a copy of the address. Must be destroyed by caller.
  89. */
  90. static struct UDPAddress *copy( struct UDPAddress *inAddress );
  91. /**
  92. * Sends a datagram through this socket.
  93. *
  94. * Note: the recommended maximum data length is 512 bytes
  95. * to ensure that the datagram can be routed without
  96. * fragmentation through all spec-compliant routers.
  97. * Most routers support larger datagrams, however.
  98. *
  99. * @param inAddress the address to send data through. Must be
  100. * destroyed by caller.
  101. * @param inData the data bytes to send.
  102. * @param inNumBytes the number of bytes to send.
  103. *
  104. * @return the number of bytes sent successfully,
  105. * or -1 for a socket error.
  106. */
  107. int send( struct UDPAddress *inAddress,
  108. unsigned char *inData, unsigned long inNumBytes );
  109. /**
  110. * Receives a datagram from this socket.
  111. *
  112. * @param outAddress pointer to where the address of the remote
  113. * host (the datagram sender) should be returned.
  114. * Will be set to NULL on socket error or timeout.
  115. * Must be destroyed by caller if non-NULL.
  116. * @param outData pointer to where the received data should be
  117. * returned. Will be set to NULL on socket error or timeout.
  118. * Must be destroyed by caller if non-NULL.
  119. * @param inTimeout the timeout for this receive operation in
  120. * milliseconds. Set to -1 for an infinite timeout.
  121. * -2 is returned from this call in the event of a timeout.
  122. * Defaults to -1.
  123. *
  124. * @return the number of bytes received successfully,
  125. * -1 for a socket error, or -2 for a timeout.
  126. */
  127. int receive( struct UDPAddress **outAddress,
  128. unsigned char **outData,
  129. long inTimeout = -1 );
  130. /**
  131. * Used by platform-specific implementations.
  132. */
  133. void *mNativeObjectPointer;
  134. };
  135. inline char SocketUDP::compare( struct UDPAddress *inFirst,
  136. struct UDPAddress *inSecond ) {
  137. if( inFirst->mIPAddress == inSecond->mIPAddress &&
  138. inFirst->mPort == inSecond->mPort ) {
  139. return true;
  140. }
  141. else {
  142. return false;
  143. }
  144. }
  145. inline struct UDPAddress *SocketUDP::copy( struct UDPAddress *inAddress ) {
  146. struct UDPAddress *returnAddress = new struct UDPAddress;
  147. returnAddress->mIPAddress = inAddress->mIPAddress;
  148. returnAddress->mPort = inAddress->mPort;
  149. return returnAddress;
  150. }
  151. #endif