OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/p2p/ipc_socket_factory.h" | 5 #include "content/renderer/p2p/ipc_socket_factory.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <deque> | 8 #include <deque> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/debug/trace_event.h" | 11 #include "base/debug/trace_event.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/message_loop/message_loop_proxy.h" | 13 #include "base/message_loop/message_loop_proxy.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "base/threading/non_thread_safe.h" | 15 #include "base/threading/non_thread_safe.h" |
16 #include "content/renderer/media/webrtc_logging.h" | 16 #include "content/renderer/media/webrtc_logging.h" |
17 #include "content/renderer/p2p/host_address_request.h" | 17 #include "content/renderer/p2p/host_address_request.h" |
18 #include "content/renderer/p2p/socket_client_delegate.h" | 18 #include "content/renderer/p2p/socket_client_delegate.h" |
19 #include "content/renderer/p2p/socket_client_impl.h" | 19 #include "content/renderer/p2p/socket_client_impl.h" |
20 #include "content/renderer/p2p/socket_dispatcher.h" | 20 #include "content/renderer/p2p/socket_dispatcher.h" |
21 #include "jingle/glue/utils.h" | 21 #include "jingle/glue/utils.h" |
22 #include "third_party/libjingle/source/talk/base/asyncpacketsocket.h" | 22 #include "third_party/webrtc/base/asyncpacketsocket.h" |
23 | 23 |
24 namespace content { | 24 namespace content { |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 const int kDefaultNonSetOptionValue = -1; | 28 const int kDefaultNonSetOptionValue = -1; |
29 | 29 |
30 bool IsTcpClientSocket(P2PSocketType type) { | 30 bool IsTcpClientSocket(P2PSocketType type) { |
31 return (type == P2P_SOCKET_STUN_TCP_CLIENT) || | 31 return (type == P2P_SOCKET_STUN_TCP_CLIENT) || |
32 (type == P2P_SOCKET_TCP_CLIENT) || | 32 (type == P2P_SOCKET_TCP_CLIENT) || |
33 (type == P2P_SOCKET_STUN_SSLTCP_CLIENT) || | 33 (type == P2P_SOCKET_STUN_SSLTCP_CLIENT) || |
34 (type == P2P_SOCKET_SSLTCP_CLIENT) || | 34 (type == P2P_SOCKET_SSLTCP_CLIENT) || |
35 (type == P2P_SOCKET_TLS_CLIENT) || | 35 (type == P2P_SOCKET_TLS_CLIENT) || |
36 (type == P2P_SOCKET_STUN_TLS_CLIENT); | 36 (type == P2P_SOCKET_STUN_TLS_CLIENT); |
37 } | 37 } |
38 | 38 |
39 bool JingleSocketOptionToP2PSocketOption(talk_base::Socket::Option option, | 39 bool JingleSocketOptionToP2PSocketOption(rtc::Socket::Option option, |
40 P2PSocketOption* ipc_option) { | 40 P2PSocketOption* ipc_option) { |
41 switch (option) { | 41 switch (option) { |
42 case talk_base::Socket::OPT_RCVBUF: | 42 case rtc::Socket::OPT_RCVBUF: |
43 *ipc_option = P2P_SOCKET_OPT_RCVBUF; | 43 *ipc_option = P2P_SOCKET_OPT_RCVBUF; |
44 break; | 44 break; |
45 case talk_base::Socket::OPT_SNDBUF: | 45 case rtc::Socket::OPT_SNDBUF: |
46 *ipc_option = P2P_SOCKET_OPT_SNDBUF; | 46 *ipc_option = P2P_SOCKET_OPT_SNDBUF; |
47 break; | 47 break; |
48 case talk_base::Socket::OPT_DSCP: | 48 case rtc::Socket::OPT_DSCP: |
49 *ipc_option = P2P_SOCKET_OPT_DSCP; | 49 *ipc_option = P2P_SOCKET_OPT_DSCP; |
50 break; | 50 break; |
51 case talk_base::Socket::OPT_DONTFRAGMENT: | 51 case rtc::Socket::OPT_DONTFRAGMENT: |
52 case talk_base::Socket::OPT_NODELAY: | 52 case rtc::Socket::OPT_NODELAY: |
53 case talk_base::Socket::OPT_IPV6_V6ONLY: | 53 case rtc::Socket::OPT_IPV6_V6ONLY: |
54 case talk_base::Socket::OPT_RTP_SENDTIME_EXTN_ID: | 54 case rtc::Socket::OPT_RTP_SENDTIME_EXTN_ID: |
55 return false; // Not supported by the chrome sockets. | 55 return false; // Not supported by the chrome sockets. |
56 default: | 56 default: |
57 NOTREACHED(); | 57 NOTREACHED(); |
58 return false; | 58 return false; |
59 } | 59 } |
60 return true; | 60 return true; |
61 } | 61 } |
62 | 62 |
63 // TODO(miu): This needs tuning. http://crbug.com/237960 | 63 // TODO(miu): This needs tuning. http://crbug.com/237960 |
64 const size_t kMaximumInFlightBytes = 64 * 1024; // 64 KB | 64 const size_t kMaximumInFlightBytes = 64 * 1024; // 64 KB |
65 | 65 |
66 // IpcPacketSocket implements talk_base::AsyncPacketSocket interface | 66 // IpcPacketSocket implements rtc::AsyncPacketSocket interface |
67 // using P2PSocketClient that works over IPC-channel. It must be used | 67 // using P2PSocketClient that works over IPC-channel. It must be used |
68 // on the thread it was created. | 68 // on the thread it was created. |
69 class IpcPacketSocket : public talk_base::AsyncPacketSocket, | 69 class IpcPacketSocket : public rtc::AsyncPacketSocket, |
70 public P2PSocketClientDelegate { | 70 public P2PSocketClientDelegate { |
71 public: | 71 public: |
72 IpcPacketSocket(); | 72 IpcPacketSocket(); |
73 virtual ~IpcPacketSocket(); | 73 virtual ~IpcPacketSocket(); |
74 | 74 |
75 // Always takes ownership of client even if initialization fails. | 75 // Always takes ownership of client even if initialization fails. |
76 bool Init(P2PSocketType type, P2PSocketClientImpl* client, | 76 bool Init(P2PSocketType type, P2PSocketClientImpl* client, |
77 const talk_base::SocketAddress& local_address, | 77 const rtc::SocketAddress& local_address, |
78 const talk_base::SocketAddress& remote_address); | 78 const rtc::SocketAddress& remote_address); |
79 | 79 |
80 // talk_base::AsyncPacketSocket interface. | 80 // rtc::AsyncPacketSocket interface. |
81 virtual talk_base::SocketAddress GetLocalAddress() const OVERRIDE; | 81 virtual rtc::SocketAddress GetLocalAddress() const OVERRIDE; |
82 virtual talk_base::SocketAddress GetRemoteAddress() const OVERRIDE; | 82 virtual rtc::SocketAddress GetRemoteAddress() const OVERRIDE; |
83 virtual int Send(const void *pv, size_t cb, | 83 virtual int Send(const void *pv, size_t cb, |
84 const talk_base::PacketOptions& options) OVERRIDE; | 84 const rtc::PacketOptions& options) OVERRIDE; |
85 virtual int SendTo(const void *pv, size_t cb, | 85 virtual int SendTo(const void *pv, size_t cb, |
86 const talk_base::SocketAddress& addr, | 86 const rtc::SocketAddress& addr, |
87 const talk_base::PacketOptions& options) OVERRIDE; | 87 const rtc::PacketOptions& options) OVERRIDE; |
88 virtual int Close() OVERRIDE; | 88 virtual int Close() OVERRIDE; |
89 virtual State GetState() const OVERRIDE; | 89 virtual State GetState() const OVERRIDE; |
90 virtual int GetOption(talk_base::Socket::Option option, int* value) OVERRIDE; | 90 virtual int GetOption(rtc::Socket::Option option, int* value) OVERRIDE; |
91 virtual int SetOption(talk_base::Socket::Option option, int value) OVERRIDE; | 91 virtual int SetOption(rtc::Socket::Option option, int value) OVERRIDE; |
92 virtual int GetError() const OVERRIDE; | 92 virtual int GetError() const OVERRIDE; |
93 virtual void SetError(int error) OVERRIDE; | 93 virtual void SetError(int error) OVERRIDE; |
94 | 94 |
95 // P2PSocketClientDelegate implementation. | 95 // P2PSocketClientDelegate implementation. |
96 virtual void OnOpen(const net::IPEndPoint& local_address, | 96 virtual void OnOpen(const net::IPEndPoint& local_address, |
97 const net::IPEndPoint& remote_address) OVERRIDE; | 97 const net::IPEndPoint& remote_address) OVERRIDE; |
98 virtual void OnIncomingTcpConnection( | 98 virtual void OnIncomingTcpConnection( |
99 const net::IPEndPoint& address, | 99 const net::IPEndPoint& address, |
100 P2PSocketClient* client) OVERRIDE; | 100 P2PSocketClient* client) OVERRIDE; |
101 virtual void OnSendComplete() OVERRIDE; | 101 virtual void OnSendComplete() OVERRIDE; |
(...skipping 10 matching lines...) Expand all Loading... |
112 IS_CLOSED, | 112 IS_CLOSED, |
113 IS_ERROR, | 113 IS_ERROR, |
114 }; | 114 }; |
115 | 115 |
116 // Update trace of send throttling internal state. This should be called | 116 // Update trace of send throttling internal state. This should be called |
117 // immediately after any changes to |send_bytes_available_| and/or | 117 // immediately after any changes to |send_bytes_available_| and/or |
118 // |in_flight_packet_sizes_|. | 118 // |in_flight_packet_sizes_|. |
119 void TraceSendThrottlingState() const; | 119 void TraceSendThrottlingState() const; |
120 | 120 |
121 void InitAcceptedTcp(P2PSocketClient* client, | 121 void InitAcceptedTcp(P2PSocketClient* client, |
122 const talk_base::SocketAddress& local_address, | 122 const rtc::SocketAddress& local_address, |
123 const talk_base::SocketAddress& remote_address); | 123 const rtc::SocketAddress& remote_address); |
124 | 124 |
125 int DoSetOption(P2PSocketOption option, int value); | 125 int DoSetOption(P2PSocketOption option, int value); |
126 | 126 |
127 P2PSocketType type_; | 127 P2PSocketType type_; |
128 | 128 |
129 // Message loop on which this socket was created and being used. | 129 // Message loop on which this socket was created and being used. |
130 base::MessageLoop* message_loop_; | 130 base::MessageLoop* message_loop_; |
131 | 131 |
132 // Corresponding P2P socket client. | 132 // Corresponding P2P socket client. |
133 scoped_refptr<P2PSocketClient> client_; | 133 scoped_refptr<P2PSocketClient> client_; |
134 | 134 |
135 // Local address is allocated by the browser process, and the | 135 // Local address is allocated by the browser process, and the |
136 // renderer side doesn't know the address until it receives OnOpen() | 136 // renderer side doesn't know the address until it receives OnOpen() |
137 // event from the browser. | 137 // event from the browser. |
138 talk_base::SocketAddress local_address_; | 138 rtc::SocketAddress local_address_; |
139 | 139 |
140 // Remote address for client TCP connections. | 140 // Remote address for client TCP connections. |
141 talk_base::SocketAddress remote_address_; | 141 rtc::SocketAddress remote_address_; |
142 | 142 |
143 // Current state of the object. | 143 // Current state of the object. |
144 InternalState state_; | 144 InternalState state_; |
145 | 145 |
146 // Track the number of bytes allowed to be sent non-blocking. This is used to | 146 // Track the number of bytes allowed to be sent non-blocking. This is used to |
147 // throttle the sending of packets to the browser process. For each packet | 147 // throttle the sending of packets to the browser process. For each packet |
148 // sent, the value is decreased. As callbacks to OnSendComplete() (as IPCs | 148 // sent, the value is decreased. As callbacks to OnSendComplete() (as IPCs |
149 // from the browser process) are made, the value is increased back. This | 149 // from the browser process) are made, the value is increased back. This |
150 // allows short bursts of high-rate sending without dropping packets, but | 150 // allows short bursts of high-rate sending without dropping packets, but |
151 // quickly restricts the client to a sustainable steady-state rate. | 151 // quickly restricts the client to a sustainable steady-state rate. |
(...skipping 10 matching lines...) Expand all Loading... |
162 | 162 |
163 DISALLOW_COPY_AND_ASSIGN(IpcPacketSocket); | 163 DISALLOW_COPY_AND_ASSIGN(IpcPacketSocket); |
164 }; | 164 }; |
165 | 165 |
166 // Simple wrapper around P2PAsyncAddressResolver. The main purpose of this | 166 // Simple wrapper around P2PAsyncAddressResolver. The main purpose of this |
167 // class is to send SignalDone, after OnDone callback from | 167 // class is to send SignalDone, after OnDone callback from |
168 // P2PAsyncAddressResolver. Libjingle sig slots are not thread safe. In case | 168 // P2PAsyncAddressResolver. Libjingle sig slots are not thread safe. In case |
169 // of MT sig slots clients must call disconnect. This class is to make sure | 169 // of MT sig slots clients must call disconnect. This class is to make sure |
170 // we destruct from the same thread on which is created. | 170 // we destruct from the same thread on which is created. |
171 class AsyncAddressResolverImpl : public base::NonThreadSafe, | 171 class AsyncAddressResolverImpl : public base::NonThreadSafe, |
172 public talk_base::AsyncResolverInterface { | 172 public rtc::AsyncResolverInterface { |
173 public: | 173 public: |
174 AsyncAddressResolverImpl(P2PSocketDispatcher* dispatcher); | 174 AsyncAddressResolverImpl(P2PSocketDispatcher* dispatcher); |
175 virtual ~AsyncAddressResolverImpl(); | 175 virtual ~AsyncAddressResolverImpl(); |
176 | 176 |
177 // talk_base::AsyncResolverInterface interface. | 177 // rtc::AsyncResolverInterface interface. |
178 virtual void Start(const talk_base::SocketAddress& addr) OVERRIDE; | 178 virtual void Start(const rtc::SocketAddress& addr) OVERRIDE; |
179 virtual bool GetResolvedAddress( | 179 virtual bool GetResolvedAddress( |
180 int family, talk_base::SocketAddress* addr) const OVERRIDE; | 180 int family, rtc::SocketAddress* addr) const OVERRIDE; |
181 virtual int GetError() const OVERRIDE; | 181 virtual int GetError() const OVERRIDE; |
182 virtual void Destroy(bool wait) OVERRIDE; | 182 virtual void Destroy(bool wait) OVERRIDE; |
183 | 183 |
184 private: | 184 private: |
185 virtual void OnAddressResolved(const net::IPAddressList& addresses); | 185 virtual void OnAddressResolved(const net::IPAddressList& addresses); |
186 | 186 |
187 scoped_refptr<P2PAsyncAddressResolver> resolver_; | 187 scoped_refptr<P2PAsyncAddressResolver> resolver_; |
188 int port_; // Port number in |addr| from Start() method. | 188 int port_; // Port number in |addr| from Start() method. |
189 std::vector<talk_base::IPAddress> addresses_; // Resolved addresses. | 189 std::vector<rtc::IPAddress> addresses_; // Resolved addresses. |
190 }; | 190 }; |
191 | 191 |
192 IpcPacketSocket::IpcPacketSocket() | 192 IpcPacketSocket::IpcPacketSocket() |
193 : type_(P2P_SOCKET_UDP), | 193 : type_(P2P_SOCKET_UDP), |
194 message_loop_(base::MessageLoop::current()), | 194 message_loop_(base::MessageLoop::current()), |
195 state_(IS_UNINITIALIZED), | 195 state_(IS_UNINITIALIZED), |
196 send_bytes_available_(kMaximumInFlightBytes), | 196 send_bytes_available_(kMaximumInFlightBytes), |
197 writable_signal_expected_(false), | 197 writable_signal_expected_(false), |
198 error_(0) { | 198 error_(0) { |
199 COMPILE_ASSERT(kMaximumInFlightBytes > 0, would_send_at_zero_rate); | 199 COMPILE_ASSERT(kMaximumInFlightBytes > 0, would_send_at_zero_rate); |
(...skipping 10 matching lines...) Expand all Loading... |
210 | 210 |
211 void IpcPacketSocket::TraceSendThrottlingState() const { | 211 void IpcPacketSocket::TraceSendThrottlingState() const { |
212 TRACE_COUNTER_ID1("p2p", "P2PSendBytesAvailable", local_address_.port(), | 212 TRACE_COUNTER_ID1("p2p", "P2PSendBytesAvailable", local_address_.port(), |
213 send_bytes_available_); | 213 send_bytes_available_); |
214 TRACE_COUNTER_ID1("p2p", "P2PSendPacketsInFlight", local_address_.port(), | 214 TRACE_COUNTER_ID1("p2p", "P2PSendPacketsInFlight", local_address_.port(), |
215 in_flight_packet_sizes_.size()); | 215 in_flight_packet_sizes_.size()); |
216 } | 216 } |
217 | 217 |
218 bool IpcPacketSocket::Init(P2PSocketType type, | 218 bool IpcPacketSocket::Init(P2PSocketType type, |
219 P2PSocketClientImpl* client, | 219 P2PSocketClientImpl* client, |
220 const talk_base::SocketAddress& local_address, | 220 const rtc::SocketAddress& local_address, |
221 const talk_base::SocketAddress& remote_address) { | 221 const rtc::SocketAddress& remote_address) { |
222 DCHECK_EQ(base::MessageLoop::current(), message_loop_); | 222 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
223 DCHECK_EQ(state_, IS_UNINITIALIZED); | 223 DCHECK_EQ(state_, IS_UNINITIALIZED); |
224 | 224 |
225 type_ = type; | 225 type_ = type; |
226 client_ = client; | 226 client_ = client; |
227 local_address_ = local_address; | 227 local_address_ = local_address; |
228 remote_address_ = remote_address; | 228 remote_address_ = remote_address; |
229 state_ = IS_OPENING; | 229 state_ = IS_OPENING; |
230 | 230 |
231 net::IPEndPoint local_endpoint; | 231 net::IPEndPoint local_endpoint; |
(...skipping 16 matching lines...) Expand all Loading... |
248 remote_address.PortAsString(); | 248 remote_address.PortAsString(); |
249 P2PHostAndIPEndPoint remote_info(remote_hostname, remote_endpoint); | 249 P2PHostAndIPEndPoint remote_info(remote_hostname, remote_endpoint); |
250 | 250 |
251 client->Init(type, local_endpoint, remote_info, this); | 251 client->Init(type, local_endpoint, remote_info, this); |
252 | 252 |
253 return true; | 253 return true; |
254 } | 254 } |
255 | 255 |
256 void IpcPacketSocket::InitAcceptedTcp( | 256 void IpcPacketSocket::InitAcceptedTcp( |
257 P2PSocketClient* client, | 257 P2PSocketClient* client, |
258 const talk_base::SocketAddress& local_address, | 258 const rtc::SocketAddress& local_address, |
259 const talk_base::SocketAddress& remote_address) { | 259 const rtc::SocketAddress& remote_address) { |
260 DCHECK_EQ(base::MessageLoop::current(), message_loop_); | 260 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
261 DCHECK_EQ(state_, IS_UNINITIALIZED); | 261 DCHECK_EQ(state_, IS_UNINITIALIZED); |
262 | 262 |
263 client_ = client; | 263 client_ = client; |
264 local_address_ = local_address; | 264 local_address_ = local_address; |
265 remote_address_ = remote_address; | 265 remote_address_ = remote_address; |
266 state_ = IS_OPEN; | 266 state_ = IS_OPEN; |
267 TraceSendThrottlingState(); | 267 TraceSendThrottlingState(); |
268 client_->SetDelegate(this); | 268 client_->SetDelegate(this); |
269 } | 269 } |
270 | 270 |
271 // talk_base::AsyncPacketSocket interface. | 271 // rtc::AsyncPacketSocket interface. |
272 talk_base::SocketAddress IpcPacketSocket::GetLocalAddress() const { | 272 rtc::SocketAddress IpcPacketSocket::GetLocalAddress() const { |
273 DCHECK_EQ(base::MessageLoop::current(), message_loop_); | 273 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
274 return local_address_; | 274 return local_address_; |
275 } | 275 } |
276 | 276 |
277 talk_base::SocketAddress IpcPacketSocket::GetRemoteAddress() const { | 277 rtc::SocketAddress IpcPacketSocket::GetRemoteAddress() const { |
278 DCHECK_EQ(base::MessageLoop::current(), message_loop_); | 278 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
279 return remote_address_; | 279 return remote_address_; |
280 } | 280 } |
281 | 281 |
282 int IpcPacketSocket::Send(const void *data, size_t data_size, | 282 int IpcPacketSocket::Send(const void *data, size_t data_size, |
283 const talk_base::PacketOptions& options) { | 283 const rtc::PacketOptions& options) { |
284 DCHECK_EQ(base::MessageLoop::current(), message_loop_); | 284 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
285 return SendTo(data, data_size, remote_address_, options); | 285 return SendTo(data, data_size, remote_address_, options); |
286 } | 286 } |
287 | 287 |
288 int IpcPacketSocket::SendTo(const void *data, size_t data_size, | 288 int IpcPacketSocket::SendTo(const void *data, size_t data_size, |
289 const talk_base::SocketAddress& address, | 289 const rtc::SocketAddress& address, |
290 const talk_base::PacketOptions& options) { | 290 const rtc::PacketOptions& options) { |
291 DCHECK_EQ(base::MessageLoop::current(), message_loop_); | 291 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
292 | 292 |
293 switch (state_) { | 293 switch (state_) { |
294 case IS_UNINITIALIZED: | 294 case IS_UNINITIALIZED: |
295 NOTREACHED(); | 295 NOTREACHED(); |
296 return EWOULDBLOCK; | 296 return EWOULDBLOCK; |
297 case IS_OPENING: | 297 case IS_OPENING: |
298 return EWOULDBLOCK; | 298 return EWOULDBLOCK; |
299 case IS_CLOSED: | 299 case IS_CLOSED: |
300 return ENOTCONN; | 300 return ENOTCONN; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 | 348 |
349 int IpcPacketSocket::Close() { | 349 int IpcPacketSocket::Close() { |
350 DCHECK_EQ(base::MessageLoop::current(), message_loop_); | 350 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
351 | 351 |
352 client_->Close(); | 352 client_->Close(); |
353 state_ = IS_CLOSED; | 353 state_ = IS_CLOSED; |
354 | 354 |
355 return 0; | 355 return 0; |
356 } | 356 } |
357 | 357 |
358 talk_base::AsyncPacketSocket::State IpcPacketSocket::GetState() const { | 358 rtc::AsyncPacketSocket::State IpcPacketSocket::GetState() const { |
359 DCHECK_EQ(base::MessageLoop::current(), message_loop_); | 359 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
360 | 360 |
361 switch (state_) { | 361 switch (state_) { |
362 case IS_UNINITIALIZED: | 362 case IS_UNINITIALIZED: |
363 NOTREACHED(); | 363 NOTREACHED(); |
364 return STATE_CLOSED; | 364 return STATE_CLOSED; |
365 | 365 |
366 case IS_OPENING: | 366 case IS_OPENING: |
367 return STATE_BINDING; | 367 return STATE_BINDING; |
368 | 368 |
369 case IS_OPEN: | 369 case IS_OPEN: |
370 if (IsTcpClientSocket(type_)) { | 370 if (IsTcpClientSocket(type_)) { |
371 return STATE_CONNECTED; | 371 return STATE_CONNECTED; |
372 } else { | 372 } else { |
373 return STATE_BOUND; | 373 return STATE_BOUND; |
374 } | 374 } |
375 | 375 |
376 case IS_CLOSED: | 376 case IS_CLOSED: |
377 case IS_ERROR: | 377 case IS_ERROR: |
378 return STATE_CLOSED; | 378 return STATE_CLOSED; |
379 } | 379 } |
380 | 380 |
381 NOTREACHED(); | 381 NOTREACHED(); |
382 return STATE_CLOSED; | 382 return STATE_CLOSED; |
383 } | 383 } |
384 | 384 |
385 int IpcPacketSocket::GetOption(talk_base::Socket::Option option, int* value) { | 385 int IpcPacketSocket::GetOption(rtc::Socket::Option option, int* value) { |
386 P2PSocketOption p2p_socket_option = P2P_SOCKET_OPT_MAX; | 386 P2PSocketOption p2p_socket_option = P2P_SOCKET_OPT_MAX; |
387 if (!JingleSocketOptionToP2PSocketOption(option, &p2p_socket_option)) { | 387 if (!JingleSocketOptionToP2PSocketOption(option, &p2p_socket_option)) { |
388 // unsupported option. | 388 // unsupported option. |
389 return -1; | 389 return -1; |
390 } | 390 } |
391 | 391 |
392 *value = options_[p2p_socket_option]; | 392 *value = options_[p2p_socket_option]; |
393 return 0; | 393 return 0; |
394 } | 394 } |
395 | 395 |
396 int IpcPacketSocket::SetOption(talk_base::Socket::Option option, int value) { | 396 int IpcPacketSocket::SetOption(rtc::Socket::Option option, int value) { |
397 DCHECK_EQ(base::MessageLoop::current(), message_loop_); | 397 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
398 | 398 |
399 P2PSocketOption p2p_socket_option = P2P_SOCKET_OPT_MAX; | 399 P2PSocketOption p2p_socket_option = P2P_SOCKET_OPT_MAX; |
400 if (!JingleSocketOptionToP2PSocketOption(option, &p2p_socket_option)) { | 400 if (!JingleSocketOptionToP2PSocketOption(option, &p2p_socket_option)) { |
401 // Option is not supported. | 401 // Option is not supported. |
402 return -1; | 402 return -1; |
403 } | 403 } |
404 | 404 |
405 options_[p2p_socket_option] = value; | 405 options_[p2p_socket_option] = value; |
406 | 406 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 DoSetOption(static_cast<P2PSocketOption> (i), options_[i]); | 449 DoSetOption(static_cast<P2PSocketOption> (i), options_[i]); |
450 } | 450 } |
451 | 451 |
452 SignalAddressReady(this, local_address_); | 452 SignalAddressReady(this, local_address_); |
453 if (IsTcpClientSocket(type_)) { | 453 if (IsTcpClientSocket(type_)) { |
454 SignalConnect(this); | 454 SignalConnect(this); |
455 // If remote address is unresolved, set resolved remote IP address received | 455 // If remote address is unresolved, set resolved remote IP address received |
456 // in the callback. This address will be used while sending the packets | 456 // in the callback. This address will be used while sending the packets |
457 // over the network. | 457 // over the network. |
458 if (remote_address_.IsUnresolvedIP()) { | 458 if (remote_address_.IsUnresolvedIP()) { |
459 talk_base::SocketAddress jingle_socket_address; | 459 rtc::SocketAddress jingle_socket_address; |
460 if (!jingle_glue::IPEndPointToSocketAddress( | 460 if (!jingle_glue::IPEndPointToSocketAddress( |
461 remote_address, &jingle_socket_address)) { | 461 remote_address, &jingle_socket_address)) { |
462 NOTREACHED(); | 462 NOTREACHED(); |
463 } | 463 } |
464 // Set only the IP address. | 464 // Set only the IP address. |
465 remote_address_.SetResolvedIP(jingle_socket_address.ipaddr()); | 465 remote_address_.SetResolvedIP(jingle_socket_address.ipaddr()); |
466 } | 466 } |
467 } | 467 } |
468 } | 468 } |
469 | 469 |
470 void IpcPacketSocket::OnIncomingTcpConnection( | 470 void IpcPacketSocket::OnIncomingTcpConnection( |
471 const net::IPEndPoint& address, | 471 const net::IPEndPoint& address, |
472 P2PSocketClient* client) { | 472 P2PSocketClient* client) { |
473 DCHECK_EQ(base::MessageLoop::current(), message_loop_); | 473 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
474 | 474 |
475 scoped_ptr<IpcPacketSocket> socket(new IpcPacketSocket()); | 475 scoped_ptr<IpcPacketSocket> socket(new IpcPacketSocket()); |
476 | 476 |
477 talk_base::SocketAddress remote_address; | 477 rtc::SocketAddress remote_address; |
478 if (!jingle_glue::IPEndPointToSocketAddress(address, &remote_address)) { | 478 if (!jingle_glue::IPEndPointToSocketAddress(address, &remote_address)) { |
479 // Always expect correct IPv4 address to be allocated. | 479 // Always expect correct IPv4 address to be allocated. |
480 NOTREACHED(); | 480 NOTREACHED(); |
481 } | 481 } |
482 socket->InitAcceptedTcp(client, local_address_, remote_address); | 482 socket->InitAcceptedTcp(client, local_address_, remote_address); |
483 SignalNewConnection(this, socket.release()); | 483 SignalNewConnection(this, socket.release()); |
484 } | 484 } |
485 | 485 |
486 void IpcPacketSocket::OnSendComplete() { | 486 void IpcPacketSocket::OnSendComplete() { |
487 DCHECK_EQ(base::MessageLoop::current(), message_loop_); | 487 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
(...skipping 24 matching lines...) Expand all Loading... |
512 if (!was_closed) { | 512 if (!was_closed) { |
513 SignalClose(this, 0); | 513 SignalClose(this, 0); |
514 } | 514 } |
515 } | 515 } |
516 | 516 |
517 void IpcPacketSocket::OnDataReceived(const net::IPEndPoint& address, | 517 void IpcPacketSocket::OnDataReceived(const net::IPEndPoint& address, |
518 const std::vector<char>& data, | 518 const std::vector<char>& data, |
519 const base::TimeTicks& timestamp) { | 519 const base::TimeTicks& timestamp) { |
520 DCHECK_EQ(base::MessageLoop::current(), message_loop_); | 520 DCHECK_EQ(base::MessageLoop::current(), message_loop_); |
521 | 521 |
522 talk_base::SocketAddress address_lj; | 522 rtc::SocketAddress address_lj; |
523 if (!jingle_glue::IPEndPointToSocketAddress(address, &address_lj)) { | 523 if (!jingle_glue::IPEndPointToSocketAddress(address, &address_lj)) { |
524 // We should always be able to convert address here because we | 524 // We should always be able to convert address here because we |
525 // don't expect IPv6 address on IPv4 connections. | 525 // don't expect IPv6 address on IPv4 connections. |
526 NOTREACHED(); | 526 NOTREACHED(); |
527 return; | 527 return; |
528 } | 528 } |
529 | 529 |
530 talk_base::PacketTime packet_time(timestamp.ToInternalValue(), 0); | 530 rtc::PacketTime packet_time(timestamp.ToInternalValue(), 0); |
531 SignalReadPacket(this, &data[0], data.size(), address_lj, | 531 SignalReadPacket(this, &data[0], data.size(), address_lj, |
532 packet_time); | 532 packet_time); |
533 } | 533 } |
534 | 534 |
535 AsyncAddressResolverImpl::AsyncAddressResolverImpl( | 535 AsyncAddressResolverImpl::AsyncAddressResolverImpl( |
536 P2PSocketDispatcher* dispatcher) | 536 P2PSocketDispatcher* dispatcher) |
537 : resolver_(new P2PAsyncAddressResolver(dispatcher)) { | 537 : resolver_(new P2PAsyncAddressResolver(dispatcher)) { |
538 } | 538 } |
539 | 539 |
540 AsyncAddressResolverImpl::~AsyncAddressResolverImpl() { | 540 AsyncAddressResolverImpl::~AsyncAddressResolverImpl() { |
541 } | 541 } |
542 | 542 |
543 void AsyncAddressResolverImpl::Start(const talk_base::SocketAddress& addr) { | 543 void AsyncAddressResolverImpl::Start(const rtc::SocketAddress& addr) { |
544 DCHECK(CalledOnValidThread()); | 544 DCHECK(CalledOnValidThread()); |
545 // Copy port number from |addr|. |port_| must be copied | 545 // Copy port number from |addr|. |port_| must be copied |
546 // when resolved address is returned in GetResolvedAddress. | 546 // when resolved address is returned in GetResolvedAddress. |
547 port_ = addr.port(); | 547 port_ = addr.port(); |
548 | 548 |
549 resolver_->Start(addr, base::Bind( | 549 resolver_->Start(addr, base::Bind( |
550 &AsyncAddressResolverImpl::OnAddressResolved, | 550 &AsyncAddressResolverImpl::OnAddressResolved, |
551 base::Unretained(this))); | 551 base::Unretained(this))); |
552 } | 552 } |
553 | 553 |
554 bool AsyncAddressResolverImpl::GetResolvedAddress( | 554 bool AsyncAddressResolverImpl::GetResolvedAddress( |
555 int family, talk_base::SocketAddress* addr) const { | 555 int family, rtc::SocketAddress* addr) const { |
556 DCHECK(CalledOnValidThread()); | 556 DCHECK(CalledOnValidThread()); |
557 | 557 |
558 if (addresses_.empty()) | 558 if (addresses_.empty()) |
559 return false; | 559 return false; |
560 | 560 |
561 for (size_t i = 0; i < addresses_.size(); ++i) { | 561 for (size_t i = 0; i < addresses_.size(); ++i) { |
562 if (family == addresses_[i].family()) { | 562 if (family == addresses_[i].family()) { |
563 addr->SetResolvedIP(addresses_[i]); | 563 addr->SetResolvedIP(addresses_[i]); |
564 addr->SetPort(port_); | 564 addr->SetPort(port_); |
565 return true; | 565 return true; |
(...skipping 12 matching lines...) Expand all Loading... |
578 resolver_->Cancel(); | 578 resolver_->Cancel(); |
579 // Libjingle doesn't need this object any more and it's not going to delete | 579 // Libjingle doesn't need this object any more and it's not going to delete |
580 // it explicitly. | 580 // it explicitly. |
581 delete this; | 581 delete this; |
582 } | 582 } |
583 | 583 |
584 void AsyncAddressResolverImpl::OnAddressResolved( | 584 void AsyncAddressResolverImpl::OnAddressResolved( |
585 const net::IPAddressList& addresses) { | 585 const net::IPAddressList& addresses) { |
586 DCHECK(CalledOnValidThread()); | 586 DCHECK(CalledOnValidThread()); |
587 for (size_t i = 0; i < addresses.size(); ++i) { | 587 for (size_t i = 0; i < addresses.size(); ++i) { |
588 talk_base::SocketAddress socket_address; | 588 rtc::SocketAddress socket_address; |
589 if (!jingle_glue::IPEndPointToSocketAddress( | 589 if (!jingle_glue::IPEndPointToSocketAddress( |
590 net::IPEndPoint(addresses[i], 0), &socket_address)) { | 590 net::IPEndPoint(addresses[i], 0), &socket_address)) { |
591 NOTREACHED(); | 591 NOTREACHED(); |
592 } | 592 } |
593 addresses_.push_back(socket_address.ipaddr()); | 593 addresses_.push_back(socket_address.ipaddr()); |
594 } | 594 } |
595 SignalDone(this); | 595 SignalDone(this); |
596 } | 596 } |
597 | 597 |
598 } // namespace | 598 } // namespace |
599 | 599 |
600 IpcPacketSocketFactory::IpcPacketSocketFactory( | 600 IpcPacketSocketFactory::IpcPacketSocketFactory( |
601 P2PSocketDispatcher* socket_dispatcher) | 601 P2PSocketDispatcher* socket_dispatcher) |
602 : socket_dispatcher_(socket_dispatcher) { | 602 : socket_dispatcher_(socket_dispatcher) { |
603 } | 603 } |
604 | 604 |
605 IpcPacketSocketFactory::~IpcPacketSocketFactory() { | 605 IpcPacketSocketFactory::~IpcPacketSocketFactory() { |
606 } | 606 } |
607 | 607 |
608 talk_base::AsyncPacketSocket* IpcPacketSocketFactory::CreateUdpSocket( | 608 rtc::AsyncPacketSocket* IpcPacketSocketFactory::CreateUdpSocket( |
609 const talk_base::SocketAddress& local_address, int min_port, int max_port) { | 609 const rtc::SocketAddress& local_address, int min_port, int max_port) { |
610 talk_base::SocketAddress crome_address; | 610 rtc::SocketAddress crome_address; |
611 P2PSocketClientImpl* socket_client = | 611 P2PSocketClientImpl* socket_client = |
612 new P2PSocketClientImpl(socket_dispatcher_); | 612 new P2PSocketClientImpl(socket_dispatcher_); |
613 scoped_ptr<IpcPacketSocket> socket(new IpcPacketSocket()); | 613 scoped_ptr<IpcPacketSocket> socket(new IpcPacketSocket()); |
614 // TODO(sergeyu): Respect local_address and port limits here (need | 614 // TODO(sergeyu): Respect local_address and port limits here (need |
615 // to pass them over IPC channel to the browser). | 615 // to pass them over IPC channel to the browser). |
616 if (!socket->Init(P2P_SOCKET_UDP, socket_client, | 616 if (!socket->Init(P2P_SOCKET_UDP, socket_client, |
617 local_address, talk_base::SocketAddress())) { | 617 local_address, rtc::SocketAddress())) { |
618 return NULL; | 618 return NULL; |
619 } | 619 } |
620 return socket.release(); | 620 return socket.release(); |
621 } | 621 } |
622 | 622 |
623 talk_base::AsyncPacketSocket* IpcPacketSocketFactory::CreateServerTcpSocket( | 623 rtc::AsyncPacketSocket* IpcPacketSocketFactory::CreateServerTcpSocket( |
624 const talk_base::SocketAddress& local_address, int min_port, int max_port, | 624 const rtc::SocketAddress& local_address, int min_port, int max_port, |
625 int opts) { | 625 int opts) { |
626 // TODO(sergeyu): Implement SSL support. | 626 // TODO(sergeyu): Implement SSL support. |
627 if (opts & talk_base::PacketSocketFactory::OPT_SSLTCP) | 627 if (opts & rtc::PacketSocketFactory::OPT_SSLTCP) |
628 return NULL; | 628 return NULL; |
629 | 629 |
630 P2PSocketType type = (opts & talk_base::PacketSocketFactory::OPT_STUN) ? | 630 P2PSocketType type = (opts & rtc::PacketSocketFactory::OPT_STUN) ? |
631 P2P_SOCKET_STUN_TCP_SERVER : P2P_SOCKET_TCP_SERVER; | 631 P2P_SOCKET_STUN_TCP_SERVER : P2P_SOCKET_TCP_SERVER; |
632 P2PSocketClientImpl* socket_client = | 632 P2PSocketClientImpl* socket_client = |
633 new P2PSocketClientImpl(socket_dispatcher_); | 633 new P2PSocketClientImpl(socket_dispatcher_); |
634 scoped_ptr<IpcPacketSocket> socket(new IpcPacketSocket()); | 634 scoped_ptr<IpcPacketSocket> socket(new IpcPacketSocket()); |
635 if (!socket->Init(type, socket_client, local_address, | 635 if (!socket->Init(type, socket_client, local_address, |
636 talk_base::SocketAddress())) { | 636 rtc::SocketAddress())) { |
637 return NULL; | 637 return NULL; |
638 } | 638 } |
639 return socket.release(); | 639 return socket.release(); |
640 } | 640 } |
641 | 641 |
642 talk_base::AsyncPacketSocket* IpcPacketSocketFactory::CreateClientTcpSocket( | 642 rtc::AsyncPacketSocket* IpcPacketSocketFactory::CreateClientTcpSocket( |
643 const talk_base::SocketAddress& local_address, | 643 const rtc::SocketAddress& local_address, |
644 const talk_base::SocketAddress& remote_address, | 644 const rtc::SocketAddress& remote_address, |
645 const talk_base::ProxyInfo& proxy_info, | 645 const rtc::ProxyInfo& proxy_info, |
646 const std::string& user_agent, int opts) { | 646 const std::string& user_agent, int opts) { |
647 P2PSocketType type; | 647 P2PSocketType type; |
648 if (opts & talk_base::PacketSocketFactory::OPT_SSLTCP) { | 648 if (opts & rtc::PacketSocketFactory::OPT_SSLTCP) { |
649 type = (opts & talk_base::PacketSocketFactory::OPT_STUN) ? | 649 type = (opts & rtc::PacketSocketFactory::OPT_STUN) ? |
650 P2P_SOCKET_STUN_SSLTCP_CLIENT : P2P_SOCKET_SSLTCP_CLIENT; | 650 P2P_SOCKET_STUN_SSLTCP_CLIENT : P2P_SOCKET_SSLTCP_CLIENT; |
651 } else if (opts & talk_base::PacketSocketFactory::OPT_TLS) { | 651 } else if (opts & rtc::PacketSocketFactory::OPT_TLS) { |
652 type = (opts & talk_base::PacketSocketFactory::OPT_STUN) ? | 652 type = (opts & rtc::PacketSocketFactory::OPT_STUN) ? |
653 P2P_SOCKET_STUN_TLS_CLIENT : P2P_SOCKET_TLS_CLIENT; | 653 P2P_SOCKET_STUN_TLS_CLIENT : P2P_SOCKET_TLS_CLIENT; |
654 } else { | 654 } else { |
655 type = (opts & talk_base::PacketSocketFactory::OPT_STUN) ? | 655 type = (opts & rtc::PacketSocketFactory::OPT_STUN) ? |
656 P2P_SOCKET_STUN_TCP_CLIENT : P2P_SOCKET_TCP_CLIENT; | 656 P2P_SOCKET_STUN_TCP_CLIENT : P2P_SOCKET_TCP_CLIENT; |
657 } | 657 } |
658 P2PSocketClientImpl* socket_client = | 658 P2PSocketClientImpl* socket_client = |
659 new P2PSocketClientImpl(socket_dispatcher_); | 659 new P2PSocketClientImpl(socket_dispatcher_); |
660 scoped_ptr<IpcPacketSocket> socket(new IpcPacketSocket()); | 660 scoped_ptr<IpcPacketSocket> socket(new IpcPacketSocket()); |
661 if (!socket->Init(type, socket_client, local_address, remote_address)) | 661 if (!socket->Init(type, socket_client, local_address, remote_address)) |
662 return NULL; | 662 return NULL; |
663 return socket.release(); | 663 return socket.release(); |
664 } | 664 } |
665 | 665 |
666 talk_base::AsyncResolverInterface* | 666 rtc::AsyncResolverInterface* |
667 IpcPacketSocketFactory::CreateAsyncResolver() { | 667 IpcPacketSocketFactory::CreateAsyncResolver() { |
668 scoped_ptr<AsyncAddressResolverImpl> resolver( | 668 scoped_ptr<AsyncAddressResolverImpl> resolver( |
669 new AsyncAddressResolverImpl(socket_dispatcher_)); | 669 new AsyncAddressResolverImpl(socket_dispatcher_)); |
670 return resolver.release(); | 670 return resolver.release(); |
671 } | 671 } |
672 | 672 |
673 } // namespace content | 673 } // namespace content |
OLD | NEW |