platformNet.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _PLATFORM_PLATFORMNET_H_
  23. #define _PLATFORM_PLATFORMNET_H_
  24. #ifndef _TORQUE_TYPES_H_
  25. #include "platform/types.h"
  26. #endif
  27. #include "platform/platformMemory.h"
  28. #ifndef MAXPACKETSIZE
  29. #define MAXPACKETSIZE 1500
  30. #endif
  31. #ifndef TORQUE_NET_DEFAULT_MULTICAST_ADDRESS
  32. #define TORQUE_NET_DEFAULT_MULTICAST_ADDRESS "ff04::7467::656E::6574::776B"
  33. #endif
  34. typedef S32 NetConnectionId;
  35. /// Generic network address
  36. ///
  37. /// This is used to represent IP addresses.
  38. struct NetAddress
  39. {
  40. S32 type; ///< Type of address (IPAddress currently)
  41. /// Acceptable NetAddress types.
  42. enum Type
  43. {
  44. IPAddress,
  45. IPV6Address,
  46. IPBroadcastAddress,
  47. IPV6MulticastAddress
  48. };
  49. union
  50. {
  51. struct {
  52. U8 netNum[4];
  53. } ipv4;
  54. struct {
  55. U8 netNum[16];
  56. U32 netFlow;
  57. U32 netScope;
  58. } ipv6;
  59. struct {
  60. U8 netNum[16];
  61. U8 netFlow[4];
  62. U8 netScope[4];
  63. } ipv6_raw;
  64. } address;
  65. U16 port;
  66. bool isSameAddress(const NetAddress &other) const
  67. {
  68. if (type != other.type)
  69. return false;
  70. switch (type)
  71. {
  72. case NetAddress::IPAddress:
  73. return (dMemcmp(other.address.ipv4.netNum, address.ipv4.netNum, 4) == 0);
  74. break;
  75. case NetAddress::IPV6Address:
  76. return (dMemcmp(other.address.ipv6.netNum, address.ipv6.netNum, 16) == 0);
  77. break;
  78. case NetAddress::IPBroadcastAddress:
  79. return true;
  80. break;
  81. case NetAddress::IPV6MulticastAddress:
  82. return true;
  83. break;
  84. }
  85. return false;
  86. }
  87. bool isSameAddressAndPort(const NetAddress &other) const
  88. {
  89. if (type != other.type)
  90. return false;
  91. switch (type)
  92. {
  93. case NetAddress::IPAddress:
  94. return (dMemcmp(other.address.ipv4.netNum, address.ipv4.netNum, 4) == 0) && other.port == port;
  95. break;
  96. case NetAddress::IPV6Address:
  97. return (dMemcmp(other.address.ipv6.netNum, address.ipv6.netNum, 16) == 0) && other.port == port;
  98. break;
  99. case NetAddress::IPBroadcastAddress:
  100. return true;
  101. break;
  102. case NetAddress::IPV6MulticastAddress:
  103. return true;
  104. break;
  105. }
  106. return false;
  107. }
  108. bool isEqual(const NetAddress &other) const
  109. {
  110. if (type != other.type)
  111. return false;
  112. switch (type)
  113. {
  114. case NetAddress::IPAddress:
  115. return other.port == port && (dMemcmp(other.address.ipv4.netNum, address.ipv4.netNum, 4) == 0);
  116. break;
  117. case NetAddress::IPV6Address:
  118. return other.port == port && other.address.ipv6.netFlow == address.ipv6.netFlow && other.address.ipv6.netScope == address.ipv6.netScope && (dMemcmp(other.address.ipv6.netNum, address.ipv6.netNum, 16) == 0);
  119. break;
  120. case NetAddress::IPBroadcastAddress:
  121. return other.port == port;
  122. break;
  123. case NetAddress::IPV6MulticastAddress:
  124. return other.port == port;
  125. break;
  126. }
  127. return false;
  128. }
  129. U32 getHash() const;
  130. };
  131. class NetSocket
  132. {
  133. protected:
  134. S32 mHandle;
  135. public:
  136. NetSocket() : mHandle(-1) { ; }
  137. inline void setHandle(S32 handleId) { mHandle = handleId; }
  138. inline S32 getHandle() const { return mHandle; }
  139. inline U32 getHash() const { return mHandle; }
  140. bool operator==(const NetSocket &other) const { return mHandle == other.mHandle; }
  141. bool operator!=(const NetSocket &other) const { return mHandle != other.mHandle; }
  142. static NetSocket fromHandle(S32 handleId) { NetSocket ret; ret.mHandle = handleId; return ret; }
  143. static NetSocket INVALID;
  144. };
  145. /// Platform-specific network operations.
  146. struct Net
  147. {
  148. enum Error
  149. {
  150. NoError,
  151. WrongProtocolType,
  152. InvalidPacketProtocol,
  153. WouldBlock,
  154. NotASocket,
  155. UnknownError,
  156. NeedHostLookup
  157. };
  158. enum ConnectionState {
  159. DNSResolved,
  160. DNSFailed,
  161. Connected,
  162. ConnectFailed,
  163. Disconnected
  164. };
  165. static const S32 MaxPacketDataSize = MAXPACKETSIZE;
  166. static bool smMulticastEnabled;
  167. static bool smIpv4Enabled;
  168. static bool smIpv6Enabled;
  169. static bool init();
  170. static void shutdown();
  171. // Unreliable net functions (UDP)
  172. // sendto is for sending data
  173. // all incoming data comes in on packetReceiveEventType
  174. // App can only open one unreliable port... who needs more? ;)
  175. static bool openPort(S32 connectPort, bool doBind = true);
  176. static NetSocket getPort();
  177. static void closePort();
  178. static Error sendto(const NetAddress *address, const U8 *buffer, S32 bufferSize);
  179. // Reliable net functions (TCP)
  180. // all incoming messages come in on the Connected* events
  181. static NetSocket openListenPort(U16 port, NetAddress::Type = NetAddress::IPAddress);
  182. static NetSocket openConnectTo(const char *stringAddress); // does the DNS resolve etc.
  183. static void closeConnectTo(NetSocket socket);
  184. static Error sendtoSocket(NetSocket socket, const U8 *buffer, S32 bufferSize, S32 *bytesWritten=NULL);
  185. static bool compareAddresses(const NetAddress *a1, const NetAddress *a2);
  186. static Net::Error stringToAddress(const char *addressString, NetAddress *address, bool hostLookup=true, int family=0);
  187. static void addressToString(const NetAddress *address, char addressString[256]);
  188. // lower level socked based network functions
  189. static NetSocket openSocket();
  190. static Error closeSocket(NetSocket socket);
  191. static Error send(NetSocket socket, const U8 *buffer, S32 bufferSize, S32 *outBytesWritten=NULL);
  192. static Error recv(NetSocket socket, U8 *buffer, S32 bufferSize, S32 *bytesRead);
  193. static Error connect(NetSocket socket, const NetAddress *address);
  194. static Error listen(NetSocket socket, S32 maxConcurrentListens);
  195. static NetSocket accept(NetSocket acceptSocket, NetAddress *remoteAddress);
  196. static Error bindAddress(const NetAddress &address, NetSocket socket, bool useUDP=false);
  197. static Error setBufferSize(NetSocket socket, S32 bufferSize);
  198. static Error setBroadcast(NetSocket socket, bool broadcastEnable);
  199. static Error setBlocking(NetSocket socket, bool blockingIO);
  200. /// Gets the desired default listen address for a specified address type
  201. static Net::Error getListenAddress(const NetAddress::Type type, NetAddress *address, bool forceDefaults=false);
  202. static void getIdealListenAddress(NetAddress *address);
  203. // Multicast for ipv6 local net browsing
  204. static void enableMulticast();
  205. static void disableMulticast();
  206. static bool isMulticastEnabled();
  207. // Protocol state
  208. static bool isAddressTypeAvailable(NetAddress::Type addressType);
  209. static void process();
  210. private:
  211. static void processListenSocket(NetSocket socket);
  212. };
  213. #endif