platformNet.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. typedef S32 NetConnectionId;
  31. /// Generic network address
  32. ///
  33. /// This is used to represent IP addresses.
  34. struct NetAddress
  35. {
  36. S32 type; ///< Type of address (IPAddress currently)
  37. /// Acceptable NetAddress types.
  38. enum
  39. {
  40. IPAddress,
  41. };
  42. U8 netNum[4]; ///< For IP: sin_addr<br>
  43. U8 nodeNum[6]; ///< For IP: Not used.<br>
  44. U16 port; ///< For IP: sin_port<br>
  45. };
  46. typedef S32 NetSocket;
  47. const NetSocket InvalidSocket = -1;
  48. /// void event(NetSocket sock, U32 state)
  49. typedef JournaledSignal<void(NetSocket,U32)> ConnectionNotifyEvent;
  50. /// void event(NetSocket listeningPort, NetSocket newConnection, NetAddress originatingAddress)
  51. typedef JournaledSignal<void(NetSocket,NetSocket,NetAddress)> ConnectionAcceptedEvent;
  52. /// void event(NetSocket connection, RawData incomingData)
  53. typedef JournaledSignal<void(NetSocket,RawData)> ConnectionReceiveEvent;
  54. /// void event(NetAddress originator, RawData incomingData)
  55. typedef JournaledSignal<void(NetAddress,RawData)> PacketReceiveEvent;
  56. /// Platform-specific network operations.
  57. struct Net
  58. {
  59. enum Error
  60. {
  61. NoError,
  62. WrongProtocolType,
  63. InvalidPacketProtocol,
  64. WouldBlock,
  65. NotASocket,
  66. UnknownError
  67. };
  68. enum ConnectionState {
  69. DNSResolved,
  70. DNSFailed,
  71. Connected,
  72. ConnectFailed,
  73. Disconnected
  74. };
  75. enum Protocol
  76. {
  77. UDPProtocol,
  78. TCPProtocol
  79. };
  80. static const S32 MaxPacketDataSize = MAXPACKETSIZE;
  81. static ConnectionNotifyEvent smConnectionNotify;
  82. static ConnectionAcceptedEvent smConnectionAccept;
  83. static ConnectionReceiveEvent smConnectionReceive;
  84. static PacketReceiveEvent smPacketReceive;
  85. static bool init();
  86. static void shutdown();
  87. // Unreliable net functions (UDP)
  88. // sendto is for sending data
  89. // all incoming data comes in on packetReceiveEventType
  90. // App can only open one unreliable port... who needs more? ;)
  91. static bool openPort(S32 connectPort, bool doBind = true);
  92. static NetSocket getPort();
  93. static void closePort();
  94. static Error sendto(const NetAddress *address, const U8 *buffer, S32 bufferSize);
  95. // Reliable net functions (TCP)
  96. // all incoming messages come in on the Connected* events
  97. static NetSocket openListenPort(U16 port);
  98. static NetSocket openConnectTo(const char *stringAddress); // does the DNS resolve etc.
  99. static void closeConnectTo(NetSocket socket);
  100. static Error sendtoSocket(NetSocket socket, const U8 *buffer, S32 bufferSize);
  101. static bool compareAddresses(const NetAddress *a1, const NetAddress *a2);
  102. static bool stringToAddress(const char *addressString, NetAddress *address);
  103. static void addressToString(const NetAddress *address, char addressString[256]);
  104. // lower level socked based network functions
  105. static NetSocket openSocket();
  106. static Error closeSocket(NetSocket socket);
  107. static Error send(NetSocket socket, const U8 *buffer, S32 bufferSize);
  108. static Error recv(NetSocket socket, U8 *buffer, S32 bufferSize, S32 *bytesRead);
  109. static Error connect(NetSocket socket, const NetAddress *address);
  110. static Error listen(NetSocket socket, S32 maxConcurrentListens);
  111. static NetSocket accept(NetSocket acceptSocket, NetAddress *remoteAddress);
  112. static Error bind(NetSocket socket, U16 port);
  113. static Error setBufferSize(NetSocket socket, S32 bufferSize);
  114. static Error setBroadcast(NetSocket socket, bool broadcastEnable);
  115. static Error setBlocking(NetSocket socket, bool blockingIO);
  116. private:
  117. static void process();
  118. };
  119. #endif