SocketLayer.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file
  11. /// \brief SocketLayer class implementation
  12. ///
  13. #ifndef __SOCKET_LAYER_H
  14. #define __SOCKET_LAYER_H
  15. #include "RakMemoryOverride.h"
  16. #include "RakNetTypes.h"
  17. #include "RakNetSmartPtr.h"
  18. //#include "RakNetSocket.h"
  19. #include "Export.h"
  20. #include "MTUSize.h"
  21. #include "RakString.h"
  22. //#include "ClientContextStruct.h"
  23. namespace RakNet
  24. {
  25. /// Forward declarations
  26. class RakPeer;
  27. /*
  28. class RAK_DLL_EXPORT SocketLayerOverride
  29. {
  30. public:
  31. SocketLayerOverride() {}
  32. virtual ~SocketLayerOverride() {}
  33. /// Called when SendTo would otherwise occur.
  34. virtual int RakNetSendTo( const char *data, int length, const SystemAddress &systemAddress )=0;
  35. /// Called when RecvFrom would otherwise occur. Return number of bytes read. Write data into dataOut
  36. // Return -1 to use RakNet's normal recvfrom, 0 to abort RakNet's normal recvfrom, and positive to return data
  37. virtual int RakNetRecvFrom( char dataOut[ MAXIMUM_MTU_SIZE ], SystemAddress *senderOut, bool calledFromMainThread )=0;
  38. };
  39. */
  40. // A platform independent implementation of Berkeley sockets, with settings used by RakNet
  41. class RAK_DLL_EXPORT SocketLayer
  42. {
  43. public:
  44. /// Default Constructor
  45. SocketLayer();
  46. // Destructor
  47. ~SocketLayer();
  48. /*
  49. /// Creates a bound socket to listen for incoming connections on the specified port
  50. /// \param[in] port the port number
  51. /// \param[in] blockingSocket
  52. /// \return A new socket used for accepting clients
  53. static RakNetSocket* CreateBoundSocket( RakPeer *peer, unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned int sleepOn10048, unsigned int extraSocketOptions, unsigned short socketFamily, _PP_Instance_ chromeInstance );
  54. #if defined(WINDOWS_STORE_RT)
  55. static RakNetSocket* CreateWindowsStore8Socket( RakPeer *peer, unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned int sleepOn10048, unsigned int extraSocketOptions, _PP_Instance_ chromeInstance );
  56. #endif
  57. static RakNetSocket* CreateBoundSocket_IPV4( RakPeer *peer, unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned int sleepOn10048, unsigned int extraSocketOptions, _PP_Instance_ chromeInstance );
  58. #if RAKNET_SUPPORT_IPV6==1
  59. static RakNetSocket* CreateBoundSocket_SupportIPV4And6( RakPeer *peer, unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned int sleepOn10048, unsigned int extraSocketOptions, unsigned short socketFamily, _PP_Instance_ chromeInstance );
  60. #endif
  61. static RakNetSocket* CreateBoundSocket_PS3Lobby( unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned short socketFamily );
  62. static RakNetSocket* CreateBoundSocket_PSP2( unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned short socketFamily );
  63. */
  64. /*
  65. #ifndef WINDOWS_STORE_RT
  66. /// Returns if this specified port is in use, for UDP
  67. /// \param[in] port the port number
  68. /// \return If this port is already in use
  69. //static bool IsPortInUse_Old(unsigned short port, const char *hostAddress);
  70. //static bool IsPortInUse(unsigned short port, const char *hostAddress, unsigned short socketFamily );
  71. static bool IsSocketFamilySupported(const char *hostAddress, unsigned short socketFamily);
  72. #endif
  73. */
  74. // static const char* DomainNameToIP_Old( const char *domainName );
  75. // static const char* DomainNameToIP( const char *domainName );
  76. /// Write \a data of length \a length to \a writeSocket
  77. /// \param[in] writeSocket The socket to write to
  78. /// \param[in] data The data to write
  79. /// \param[in] length The length of \a data
  80. // static void Write( RakNetSocket*writeSocket, const char* data, const int length );
  81. /// Read data from a socket
  82. /// \param[in] s the socket
  83. /// \param[in] rakPeer The instance of rakPeer containing the recvFrom C callback
  84. /// \param[in] errorCode An error code if an error occured .
  85. /// \param[in] connectionSocketIndex Which of the sockets in RakPeer we are using
  86. /// \return Returns true if you successfully read data, false on error.
  87. // static void RecvFromBlocking_IPV4( RakNetSocket *s, RakPeer *rakPeer, char *dataOut, int *bytesReadOut, SystemAddress *systemAddressOut, RakNet::TimeUS *timeRead );
  88. // #if RAKNET_SUPPORT_IPV6==1
  89. // static void RecvFromBlockingIPV4And6( RakNetSocket *s, RakPeer *rakPeer, char *dataOut, int *bytesReadOut, SystemAddress *systemAddressOut, RakNet::TimeUS *timeRead );
  90. // #endif
  91. // static void RecvFromBlocking( RakNetSocket *s, RakPeer *rakPeer, char *dataOut, int *bytesReadOut, SystemAddress *systemAddressOut, RakNet::TimeUS *timeRead );
  92. #if defined(WINDOWS_STORE_RT)
  93. // static void RecvFromBlocking_WindowsStore8( RakNetSocket *s, RakPeer *rakPeer, char *dataOut, int *bytesReadOut, SystemAddress *systemAddressOut, RakNet::TimeUS *timeRead );
  94. #endif
  95. /// Given a socket and IP, retrieves the subnet mask, on linux the socket is unused
  96. /// \param[in] inSock the socket
  97. /// \param[in] inIpString The ip of the interface you wish to retrieve the subnet mask from
  98. /// \return Returns the ip dotted subnet mask if successful, otherwise returns empty string ("")
  99. static RakNet::RakString GetSubNetForSocketAndIp(__UDPSOCKET__ inSock, RakNet::RakString inIpString);
  100. /// Sets the socket flags to nonblocking
  101. /// \param[in] listenSocket the socket to set
  102. // static void SetNonBlocking( RakNetSocket* listenSocket);
  103. /// Retrieve all local IP address in a string format.
  104. /// \param[in] s The socket whose port we are referring to
  105. /// \param[in] ipList An array of ip address in dotted notation.
  106. static void GetMyIP( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] );
  107. /// Call sendto (UDP obviously)
  108. /// \param[in] s the socket
  109. /// \param[in] data The byte buffer to send
  110. /// \param[in] length The length of the \a data in bytes
  111. /// \param[in] ip The address of the remote host in dotted notation.
  112. /// \param[in] port The port number to send to.
  113. /// \return 0 on success, nonzero on failure.
  114. // static int SendTo( UDPSOCKET s, const char *data, int length, const char ip[ 16 ], unsigned short port, unsigned short remotePortRakNetWasStartedOn_PS3, unsigned int extraSocketOptions, const char *file, const long line );
  115. /// Call sendto' (UDP obviously)
  116. /// It won't reach the recipient, except on a LAN
  117. /// However, this is good for opening routers / firewalls
  118. /// \param[in] s the socket
  119. /// \param[in] data The byte buffer to send
  120. /// \param[in] length The length of the \a data in bytes
  121. /// \param[in] ip The address of the remote host in dotted notation.
  122. /// \param[in] port The port number to send to.
  123. /// \param[in] ttl Max hops of datagram
  124. /// \return 0 on success, nonzero on failure.
  125. // static int SendToTTL( RakNetSocket *s, const char *data, int length, SystemAddress &systemAddress, int ttl );
  126. /// Call sendto (UDP obviously)
  127. /// \param[in] s the socket
  128. /// \param[in] data The byte buffer to send
  129. /// \param[in] length The length of the \a data in bytes
  130. /// \param[in] binaryAddress The address of the remote host in binary format.
  131. /// \param[in] port The port number to send to.
  132. /// \return 0 on success, nonzero on failure.
  133. // static int SendTo( RakNetSocket *s, const char *data, int length, SystemAddress systemAddress, const char *file, const long line );
  134. // static unsigned short GetLocalPort(RakNetSocket *s);
  135. static unsigned short GetLocalPort( __UDPSOCKET__ s);
  136. // static void GetSystemAddress_Old ( RakNetSocket *s, SystemAddress *systemAddressOut );
  137. static void GetSystemAddress_Old ( __UDPSOCKET__ s, SystemAddress *systemAddressOut );
  138. // static void GetSystemAddress ( RakNetSocket *s, SystemAddress *systemAddressOut );
  139. static void GetSystemAddress ( __UDPSOCKET__ s, SystemAddress *systemAddressOut );
  140. // static void SetSocketLayerOverride(SocketLayerOverride *_slo);
  141. // static SocketLayerOverride* GetSocketLayerOverride(void) {return slo;}
  142. // static int SendTo_PS3Lobby( RakNetSocket *s, const char *data, int length, const SystemAddress &systemAddress );
  143. // static int SendTo_PSP2( RakNetSocket *s, const char *data, int length, const SystemAddress &systemAddress );
  144. // static int SendTo_360( RakNetSocket *s, const char *data, int length, const char *voiceData, int voiceLength, const SystemAddress &systemAddress );
  145. // static int SendTo_PC( RakNetSocket *s, const char *data, int length, const SystemAddress &systemAddress, const char *file, const long line );
  146. // #if defined(WINDOWS_STORE_RT)
  147. // static int SendTo_WindowsStore8( RakNetSocket *s, const char *data, int length, const SystemAddress &systemAddress, const char *file, const long line );
  148. // #endif
  149. //
  150. // static void SetDoNotFragment( RakNetSocket* listenSocket, int opt );
  151. // static void SetSocketOptions( RakNetSocket* listenSocket, bool blockingSocket, bool setBroadcast);
  152. static void SetSocketOptions( __UDPSOCKET__ listenSocket, bool blockingSocket, bool setBroadcast);
  153. // AF_INET (default). For IPV6, use AF_INET6. To autoselect, use AF_UNSPEC.
  154. static bool GetFirstBindableIP(char firstBindable[128], int ipProto);
  155. private:
  156. // static SocketLayerOverride *slo;
  157. };
  158. } // namespace RakNet
  159. #endif
粤ICP备19079148号