platformNet.h 8.8 KB

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