| 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 |
| 11 | #ifndef RTC_BASE_SOCKET_H_ |
| 12 | #define RTC_BASE_SOCKET_H_ |
| 13 |
| 14 | #include <errno.h> |
| 15 |
| 16 | #include "absl/types/optional.h" |
| 17 | #include "rtc_base/checks.h" |
| 18 |
| 19 | #if defined(WEBRTC_POSIX) |
| 20 | #include <arpa/inet.h> |
| 21 | #include <netinet/in.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/types.h> |
| 24 | #define SOCKET_EACCES EACCES |
| 25 | #endif |
| 26 |
| 27 | #if defined(WEBRTC_WIN) |
| 28 | #include "rtc_base/win32.h" |
| 29 | #endif |
| 30 |
| 31 | #include "api/units/timestamp.h" |
| 32 | #include "rtc_base/buffer.h" |
| 33 | #include "rtc_base/socket_address.h" |
| 34 | #include "rtc_base/system/rtc_export.h" |
| 35 | #include "rtc_base/third_party/sigslot/sigslot.h" |
| 36 |
| 37 | // Rather than converting errors into a private namespace, |
| 38 | // Reuse the POSIX socket api errors. Note this depends on |
| 39 | // Win32 compatibility. |
| 40 |
| 41 | #if defined(WEBRTC_WIN) |
| 42 | #undef EWOULDBLOCK // Remove errno.h's definition for each macro below. |
| 43 | #define EWOULDBLOCK WSAEWOULDBLOCK |
| 44 | #undef EINPROGRESS |
| 45 | #define EINPROGRESS WSAEINPROGRESS |
| 46 | #undef EALREADY |
| 47 | #define EALREADY WSAEALREADY |
| 48 | #undef EMSGSIZE |
| 49 | #define EMSGSIZE WSAEMSGSIZE |
| 50 | #undef EADDRINUSE |
| 51 | #define EADDRINUSE WSAEADDRINUSE |
| 52 | #undef EADDRNOTAVAIL |
| 53 | #define EADDRNOTAVAIL WSAEADDRNOTAVAIL |
| 54 | #undef ENETDOWN |
| 55 | #define ENETDOWN WSAENETDOWN |
| 56 | #undef ECONNABORTED |
| 57 | #define ECONNABORTED WSAECONNABORTED |
| 58 | #undef ENOBUFS |
| 59 | #define ENOBUFS WSAENOBUFS |
| 60 | #undef EISCONN |
| 61 | #define EISCONN WSAEISCONN |
| 62 | #undef ENOTCONN |
| 63 | #define ENOTCONN WSAENOTCONN |
| 64 | #undef ECONNREFUSED |
| 65 | #define ECONNREFUSED WSAECONNREFUSED |
| 66 | #undef EHOSTUNREACH |
| 67 | #define EHOSTUNREACH WSAEHOSTUNREACH |
| 68 | #undef ENETUNREACH |
| 69 | #define ENETUNREACH WSAENETUNREACH |
| 70 | #define SOCKET_EACCES WSAEACCES |
| 71 | #endif // WEBRTC_WIN |
| 72 |
| 73 | #if defined(WEBRTC_POSIX) |
| 74 | #define INVALID_SOCKET (-1) |
| 75 | #define SOCKET_ERROR (-1) |
| 76 | #define closesocket(s) close(s) |
| 77 | #endif // WEBRTC_POSIX |
| 78 |
| 79 | namespace rtc { |
| 80 |
| 81 | inline bool IsBlockingError(int e) { |
| 82 | return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS); |
| 83 | } |
| 84 |
| 85 | // General interface for the socket implementations of various networks. The |
| 86 | // methods match those of normal UNIX sockets very closely. |
| 87 | class RTC_EXPORT Socket { |
| 88 | public: |
| 89 | struct ReceiveBuffer { |
| 90 | ReceiveBuffer(Buffer& payload) : payload(payload) {} |
| 91 |
| 92 | absl::optional<webrtc::Timestamp> arrival_time; |
| 93 | SocketAddress source_address; |
| 94 | Buffer& payload; |
| 95 | }; |
| 96 | virtual ~Socket() {} |
| 97 |
| 98 | Socket(const Socket&) = delete; |
| 99 | Socket& operator=(const Socket&) = delete; |
| 100 |
| 101 | // Returns the address to which the socket is bound. If the socket is not |
| 102 | // bound, then the any-address is returned. |
| 103 | virtual SocketAddress GetLocalAddress() const = 0; |
| 104 |
| 105 | // Returns the address to which the socket is connected. If the socket is |
| 106 | // not connected, then the any-address is returned. |
| 107 | virtual SocketAddress GetRemoteAddress() const = 0; |
| 108 |
| 109 | virtual int Bind(const SocketAddress& addr) = 0; |
| 110 | virtual int Connect(const SocketAddress& addr) = 0; |
| 111 | virtual int Send(const void* pv, size_t cb) = 0; |
| 112 | virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) = 0; |
| 113 | // `timestamp` is in units of microseconds. |
| 114 | virtual int Recv(void* pv, size_t cb, int64_t* timestamp) = 0; |
| 115 | // TODO(webrtc:15368): Deprecate and remove. |
| 116 | virtual int RecvFrom(void* pv, |
| 117 | size_t cb, |
| 118 | SocketAddress* paddr, |
| 119 | int64_t* timestamp) { |
| 120 | // Not implemented. Use RecvFrom(ReceiveBuffer& buffer). |
| 121 | RTC_CHECK_NOTREACHED(); |
| 122 | } |
| 123 | // Intended to replace RecvFrom(void* ...). |
| 124 | // Default implementation calls RecvFrom(void* ...) with 64Kbyte buffer. |
| 125 | // Returns number of bytes received or a negative value on error. |
| 126 | virtual int RecvFrom(ReceiveBuffer& buffer); |
| 127 | virtual int Listen(int backlog) = 0; |
| 128 | virtual Socket* Accept(SocketAddress* paddr) = 0; |
| 129 | virtual int Close() = 0; |
| 130 | virtual int GetError() const = 0; |
| 131 | virtual void SetError(int error) = 0; |
| 132 | inline bool IsBlocking() const { return IsBlockingError(GetError()); } |
| 133 |
| 134 | enum ConnState { CS_CLOSED, CS_CONNECTING, CS_CONNECTED }; |
| 135 | virtual ConnState GetState() const = 0; |
| 136 |
| 137 | enum Option { |
| 138 | OPT_DONTFRAGMENT, |
| 139 | OPT_RCVBUF, // receive buffer size |
| 140 | OPT_SNDBUF, // send buffer size |
| 141 | OPT_NODELAY, // whether Nagle algorithm is enabled |
| 142 | OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only. |
| 143 | OPT_DSCP, // DSCP code |
| 144 | OPT_RTP_SENDTIME_EXTN_ID, // This is a non-traditional socket option param. |
| 145 | // This is specific to libjingle and will be used |
| 146 | // if SendTime option is needed at socket level. |
| 147 | }; |
| 148 | virtual int GetOption(Option opt, int* value) = 0; |
| 149 | virtual int SetOption(Option opt, int value) = 0; |
| 150 |
| 151 | // SignalReadEvent and SignalWriteEvent use multi_threaded_local to allow |
| 152 | // access concurrently from different thread. |
| 153 | // For example SignalReadEvent::connect will be called in AsyncUDPSocket ctor |
| 154 | // but at the same time the SocketDispatcher may be signaling the read event. |
| 155 | // ready to read |
| 156 | sigslot::signal1<Socket*, sigslot::multi_threaded_local> SignalReadEvent; |
| 157 | // ready to write |
| 158 | sigslot::signal1<Socket*, sigslot::multi_threaded_local> SignalWriteEvent; |
| 159 | sigslot::signal1<Socket*> SignalConnectEvent; // connected |
| 160 | sigslot::signal2<Socket*, int> SignalCloseEvent; // closed |
| 161 |
| 162 | protected: |
| 163 | Socket() {} |
| 164 | }; |
| 165 |
| 166 | } // namespace rtc |
| 167 |
| 168 | #endif // RTC_BASE_SOCKET_H_ |
| 169 |