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