You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_SOCKET_H_
  11. #define RTC_BASE_SOCKET_H_
  12. #include <errno.h>
  13. #if defined(WEBRTC_POSIX)
  14. #include <arpa/inet.h>
  15. #include <netinet/in.h>
  16. #include <sys/socket.h>
  17. #include <sys/types.h>
  18. #define SOCKET_EACCES EACCES
  19. #endif
  20. #if defined(WEBRTC_WIN)
  21. #include "rtc_base/win32.h"
  22. #endif
  23. #include "rtc_base/constructor_magic.h"
  24. #include "rtc_base/socket_address.h"
  25. // Rather than converting errors into a private namespace,
  26. // Reuse the POSIX socket api errors. Note this depends on
  27. // Win32 compatibility.
  28. #if defined(WEBRTC_WIN)
  29. #undef EWOULDBLOCK // Remove errno.h's definition for each macro below.
  30. #define EWOULDBLOCK WSAEWOULDBLOCK
  31. #undef EINPROGRESS
  32. #define EINPROGRESS WSAEINPROGRESS
  33. #undef EALREADY
  34. #define EALREADY WSAEALREADY
  35. #undef ENOTSOCK
  36. #define ENOTSOCK WSAENOTSOCK
  37. #undef EDESTADDRREQ
  38. #define EDESTADDRREQ WSAEDESTADDRREQ
  39. #undef EMSGSIZE
  40. #define EMSGSIZE WSAEMSGSIZE
  41. #undef EPROTOTYPE
  42. #define EPROTOTYPE WSAEPROTOTYPE
  43. #undef ENOPROTOOPT
  44. #define ENOPROTOOPT WSAENOPROTOOPT
  45. #undef EPROTONOSUPPORT
  46. #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
  47. #undef ESOCKTNOSUPPORT
  48. #define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
  49. #undef EOPNOTSUPP
  50. #define EOPNOTSUPP WSAEOPNOTSUPP
  51. #undef EPFNOSUPPORT
  52. #define EPFNOSUPPORT WSAEPFNOSUPPORT
  53. #undef EAFNOSUPPORT
  54. #define EAFNOSUPPORT WSAEAFNOSUPPORT
  55. #undef EADDRINUSE
  56. #define EADDRINUSE WSAEADDRINUSE
  57. #undef EADDRNOTAVAIL
  58. #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
  59. #undef ENETDOWN
  60. #define ENETDOWN WSAENETDOWN
  61. #undef ENETUNREACH
  62. #define ENETUNREACH WSAENETUNREACH
  63. #undef ENETRESET
  64. #define ENETRESET WSAENETRESET
  65. #undef ECONNABORTED
  66. #define ECONNABORTED WSAECONNABORTED
  67. #undef ECONNRESET
  68. #define ECONNRESET WSAECONNRESET
  69. #undef ENOBUFS
  70. #define ENOBUFS WSAENOBUFS
  71. #undef EISCONN
  72. #define EISCONN WSAEISCONN
  73. #undef ENOTCONN
  74. #define ENOTCONN WSAENOTCONN
  75. #undef ESHUTDOWN
  76. #define ESHUTDOWN WSAESHUTDOWN
  77. #undef ETOOMANYREFS
  78. #define ETOOMANYREFS WSAETOOMANYREFS
  79. #undef ETIMEDOUT
  80. #define ETIMEDOUT WSAETIMEDOUT
  81. #undef ECONNREFUSED
  82. #define ECONNREFUSED WSAECONNREFUSED
  83. #undef ELOOP
  84. #define ELOOP WSAELOOP
  85. #undef ENAMETOOLONG
  86. #define ENAMETOOLONG WSAENAMETOOLONG
  87. #undef EHOSTDOWN
  88. #define EHOSTDOWN WSAEHOSTDOWN
  89. #undef EHOSTUNREACH
  90. #define EHOSTUNREACH WSAEHOSTUNREACH
  91. #undef ENOTEMPTY
  92. #define ENOTEMPTY WSAENOTEMPTY
  93. #undef EPROCLIM
  94. #define EPROCLIM WSAEPROCLIM
  95. #undef EUSERS
  96. #define EUSERS WSAEUSERS
  97. #undef EDQUOT
  98. #define EDQUOT WSAEDQUOT
  99. #undef ESTALE
  100. #define ESTALE WSAESTALE
  101. #undef EREMOTE
  102. #define EREMOTE WSAEREMOTE
  103. #define SOCKET_EACCES WSAEACCES
  104. #endif // WEBRTC_WIN
  105. #if defined(WEBRTC_POSIX)
  106. #define INVALID_SOCKET (-1)
  107. #define SOCKET_ERROR (-1)
  108. #define closesocket(s) close(s)
  109. #endif // WEBRTC_POSIX
  110. namespace rtc {
  111. inline bool IsBlockingError(int e) {
  112. return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS);
  113. }
  114. // General interface for the socket implementations of various networks. The
  115. // methods match those of normal UNIX sockets very closely.
  116. class Socket {
  117. public:
  118. virtual ~Socket() {}
  119. // Returns the address to which the socket is bound. If the socket is not
  120. // bound, then the any-address is returned.
  121. virtual SocketAddress GetLocalAddress() const = 0;
  122. // Returns the address to which the socket is connected. If the socket is
  123. // not connected, then the any-address is returned.
  124. virtual SocketAddress GetRemoteAddress() const = 0;
  125. virtual int Bind(const SocketAddress& addr) = 0;
  126. virtual int Connect(const SocketAddress& addr) = 0;
  127. virtual int Send(const void* pv, size_t cb) = 0;
  128. virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) = 0;
  129. // |timestamp| is in units of microseconds.
  130. virtual int Recv(void* pv, size_t cb, int64_t* timestamp) = 0;
  131. virtual int RecvFrom(void* pv,
  132. size_t cb,
  133. SocketAddress* paddr,
  134. int64_t* timestamp) = 0;
  135. virtual int Listen(int backlog) = 0;
  136. virtual Socket* Accept(SocketAddress* paddr) = 0;
  137. virtual int Close() = 0;
  138. virtual int GetError() const = 0;
  139. virtual void SetError(int error) = 0;
  140. inline bool IsBlocking() const { return IsBlockingError(GetError()); }
  141. enum ConnState { CS_CLOSED, CS_CONNECTING, CS_CONNECTED };
  142. virtual ConnState GetState() const = 0;
  143. enum Option {
  144. OPT_DONTFRAGMENT,
  145. OPT_RCVBUF, // receive buffer size
  146. OPT_SNDBUF, // send buffer size
  147. OPT_NODELAY, // whether Nagle algorithm is enabled
  148. OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only.
  149. OPT_DSCP, // DSCP code
  150. OPT_RTP_SENDTIME_EXTN_ID, // This is a non-traditional socket option param.
  151. // This is specific to libjingle and will be used
  152. // if SendTime option is needed at socket level.
  153. };
  154. virtual int GetOption(Option opt, int* value) = 0;
  155. virtual int SetOption(Option opt, int value) = 0;
  156. protected:
  157. Socket() {}
  158. private:
  159. RTC_DISALLOW_COPY_AND_ASSIGN(Socket);
  160. };
  161. } // namespace rtc
  162. #endif // RTC_BASE_SOCKET_H_