platformNet.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. #include "platform/platform.h"
  25. #include "core/util/rawData.h"
  26. #include "core/util/journal/journaledSignal.h"
  27. #ifndef MAXPACKETSIZE
  28. #define MAXPACKETSIZE 1500
  29. #endif
  30. #define TORQUE_NET_DEFAULT_MULTICAST_ADDRESS "ff04::7467::656E::6574::776B"
  31. typedef S32 NetConnectionId;
  32. /// Generic network address
  33. ///
  34. /// This is used to represent IP addresses.
  35. struct NetAddress
  36. {
  37. S32 type; ///< Type of address (IPAddress currently)
  38. /// Acceptable NetAddress types.
  39. enum Type
  40. {
  41. IPAddress,
  42. IPV6Address,
  43. IPBroadcastAddress,
  44. IPV6MulticastAddress
  45. };
  46. union
  47. {
  48. struct {
  49. U8 netNum[4];
  50. } ipv4;
  51. struct {
  52. U8 netNum[16];
  53. U32 netFlow;
  54. U32 netScope;
  55. } ipv6;
  56. struct {
  57. U8 netNum[16];
  58. U8 netFlow[4];
  59. U8 netScope[4];
  60. } ipv6_raw;
  61. } address;
  62. U16 port;
  63. bool isSameAddress(const NetAddress &other) const
  64. {
  65. if (type != other.type)
  66. return false;
  67. switch (type)
  68. {
  69. case NetAddress::IPAddress:
  70. return (dMemcmp(other.address.ipv4.netNum, address.ipv4.netNum, 4) == 0);
  71. break;
  72. case NetAddress::IPV6Address:
  73. return (dMemcmp(other.address.ipv6.netNum, address.ipv6.netNum, 16) == 0);
  74. break;
  75. case NetAddress::IPBroadcastAddress:
  76. return true;
  77. break;
  78. case NetAddress::IPV6MulticastAddress:
  79. return true;
  80. break;
  81. }
  82. return false;
  83. }
  84. bool isSameAddressAndPort(const NetAddress &other) const
  85. {
  86. if (type != other.type)
  87. return false;
  88. switch (type)
  89. {
  90. case NetAddress::IPAddress:
  91. return (dMemcmp(other.address.ipv4.netNum, address.ipv4.netNum, 4) == 0) && other.port == port;
  92. break;
  93. case NetAddress::IPV6Address:
  94. return (dMemcmp(other.address.ipv6.netNum, address.ipv6.netNum, 16) == 0) && other.port == port;
  95. break;
  96. case NetAddress::IPBroadcastAddress:
  97. return true;
  98. break;
  99. case NetAddress::IPV6MulticastAddress:
  100. return true;
  101. break;
  102. }
  103. return false;
  104. }
  105. bool isEqual(const NetAddress &other) const
  106. {
  107. if (type != other.type)
  108. return false;
  109. switch (type)
  110. {
  111. case NetAddress::IPAddress:
  112. return other.port == port && (dMemcmp(other.address.ipv4.netNum, address.ipv4.netNum, 4) == 0);
  113. break;
  114. case NetAddress::IPV6Address:
  115. 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);
  116. break;
  117. case NetAddress::IPBroadcastAddress:
  118. return other.port == port;
  119. break;
  120. case NetAddress::IPV6MulticastAddress:
  121. return other.port == port;
  122. break;
  123. }
  124. return false;
  125. }
  126. U32 getHash() const;
  127. };
  128. class NetSocket
  129. {
  130. protected:
  131. S32 mHandle;
  132. public:
  133. NetSocket() : mHandle(-1) { ; }
  134. inline void setHandle(S32 handleId) { mHandle = handleId; }
  135. inline S32 getHandle() const { return mHandle; }
  136. inline U32 getHash() const { return mHandle; }
  137. bool operator==(const NetSocket &other) const { return mHandle == other.mHandle; }
  138. bool operator!=(const NetSocket &other) const { return mHandle != other.mHandle; }
  139. static NetSocket fromHandle(S32 handleId) { NetSocket ret; ret.mHandle = handleId; return ret; }
  140. static NetSocket INVALID;
  141. };
  142. /// void event(NetSocket sock, U32 state)
  143. typedef JournaledSignal<void(NetSocket,U32)> ConnectionNotifyEvent;
  144. /// void event(NetSocket listeningPort, NetSocket newConnection, NetAddress originatingAddress)
  145. typedef JournaledSignal<void(NetSocket,NetSocket,NetAddress)> ConnectionAcceptedEvent;
  146. /// void event(NetSocket connection, RawData incomingData)
  147. typedef JournaledSignal<void(NetSocket,RawData)> ConnectionReceiveEvent;
  148. /// void event(NetAddress originator, RawData incomingData)
  149. typedef JournaledSignal<void(NetAddress,RawData)> PacketReceiveEvent;
  150. /// Platform-specific network operations.
  151. struct Net
  152. {
  153. enum Error
  154. {
  155. NoError,
  156. WrongProtocolType,
  157. InvalidPacketProtocol,
  158. WouldBlock,
  159. NotASocket,
  160. UnknownError,
  161. NeedHostLookup
  162. };
  163. enum ConnectionState {
  164. DNSResolved,
  165. DNSFailed,
  166. Connected,
  167. ConnectFailed,
  168. Disconnected
  169. };
  170. static const S32 MaxPacketDataSize = MAXPACKETSIZE;
  171. static ConnectionNotifyEvent& getConnectionNotifyEvent();
  172. static ConnectionAcceptedEvent& getConnectionAcceptedEvent();
  173. static ConnectionReceiveEvent& getConnectionReceiveEvent();
  174. static PacketReceiveEvent& getPacketReceiveEvent();
  175. static bool smMulticastEnabled;
  176. static bool smIpv4Enabled;
  177. static bool smIpv6Enabled;
  178. static bool init();
  179. static void shutdown();
  180. // Unreliable net functions (UDP)
  181. // sendto is for sending data
  182. // all incoming data comes in on packetReceiveEventType
  183. // App can only open one unreliable port... who needs more? ;)
  184. static bool openPort(S32 connectPort, bool doBind = true);
  185. static NetSocket getPort();
  186. static void closePort();
  187. static Error sendto(const NetAddress *address, const U8 *buffer, S32 bufferSize);
  188. // Reliable net functions (TCP)
  189. // all incoming messages come in on the Connected* events
  190. static NetSocket openListenPort(U16 port, NetAddress::Type = NetAddress::IPAddress);
  191. static NetSocket openConnectTo(const char *stringAddress); // does the DNS resolve etc.
  192. static void closeConnectTo(NetSocket socket);
  193. static Error sendtoSocket(NetSocket socket, const U8 *buffer, S32 bufferSize, S32 *bytesWritten=NULL);
  194. static bool compareAddresses(const NetAddress *a1, const NetAddress *a2);
  195. static Net::Error stringToAddress(const char *addressString, NetAddress *address, bool hostLookup=true, int family=0);
  196. static void addressToString(const NetAddress *address, char addressString[256]);
  197. // lower level socked based network functions
  198. static NetSocket openSocket();
  199. static Error closeSocket(NetSocket socket);
  200. static Error send(NetSocket socket, const U8 *buffer, S32 bufferSize, S32 *outBytesWritten=NULL);
  201. static Error recv(NetSocket socket, U8 *buffer, S32 bufferSize, S32 *bytesRead);
  202. static Error connect(NetSocket socket, const NetAddress *address);
  203. static Error listen(NetSocket socket, S32 maxConcurrentListens);
  204. static NetSocket accept(NetSocket acceptSocket, NetAddress *remoteAddress);
  205. static Error bindAddress(const NetAddress &address, NetSocket socket, bool useUDP=false);
  206. static Error setBufferSize(NetSocket socket, S32 bufferSize);
  207. static Error setBroadcast(NetSocket socket, bool broadcastEnable);
  208. static Error setBlocking(NetSocket socket, bool blockingIO);
  209. /// Gets the desired default listen address for a specified address type
  210. static Net::Error getListenAddress(const NetAddress::Type type, NetAddress *address, bool forceDefaults=false);
  211. static void getIdealListenAddress(NetAddress *address);
  212. // Multicast for ipv6 local net browsing
  213. static void enableMulticast();
  214. static void disableMulticast();
  215. static bool isMulticastEnabled();
  216. // Protocol state
  217. static bool isAddressTypeAvailable(NetAddress::Type addressType);
  218. private:
  219. static void process();
  220. static void processListenSocket(NetSocket socket);
  221. };
  222. #endif