OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/browser/renderer_host/pepper_udp_socket.h" | 5 #include "content/browser/renderer_host/pepper_udp_socket.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "content/browser/renderer_host/pepper_message_filter.h" | 11 #include "content/browser/renderer_host/pepper_message_filter.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 #include "net/udp/udp_server_socket.h" | 15 #include "net/udp/udp_server_socket.h" |
16 #include "ppapi/proxy/ppapi_messages.h" | 16 #include "ppapi/proxy/ppapi_messages.h" |
17 #include "ppapi/shared_impl/private/net_address_private_impl.h" | 17 #include "ppapi/shared_impl/private/net_address_private_impl.h" |
18 | 18 |
19 using content::BrowserThread; | 19 using content::BrowserThread; |
20 using ppapi::NetAddressPrivateImpl; | 20 using ppapi::NetAddressPrivateImpl; |
21 | 21 |
22 PepperUDPSocket::PepperUDPSocket( | 22 PepperUDPSocket::PepperUDPSocket( |
23 PepperMessageFilter* manager, | 23 PepperMessageFilter* manager, |
24 int32 routing_id, | 24 int32 routing_id, |
25 uint32 plugin_dispatcher_id, | 25 uint32 plugin_dispatcher_id, |
26 uint32 socket_id) | 26 uint32 socket_id) |
27 : manager_(manager), | 27 : manager_(manager), |
28 routing_id_(routing_id), | 28 routing_id_(routing_id), |
29 plugin_dispatcher_id_(plugin_dispatcher_id), | 29 plugin_dispatcher_id_(plugin_dispatcher_id), |
30 socket_id_(socket_id) { | 30 socket_id_(socket_id), |
| 31 bound_port_(0) { |
31 DCHECK(manager); | 32 DCHECK(manager); |
32 } | 33 } |
33 | 34 |
34 PepperUDPSocket::~PepperUDPSocket() { | 35 PepperUDPSocket::~PepperUDPSocket() { |
35 // Make sure there are no further callbacks from socket_. | 36 // Make sure there are no further callbacks from socket_. |
36 if (socket_.get()) | 37 if (socket_.get()) |
37 socket_->Close(); | 38 socket_->Close(); |
38 } | 39 } |
39 | 40 |
40 void PepperUDPSocket::Bind(const PP_NetAddress_Private& addr) { | 41 void PepperUDPSocket::Bind(const PP_NetAddress_Private& addr) { |
41 socket_.reset(new net::UDPServerSocket(NULL, net::NetLog::Source())); | 42 socket_.reset(new net::UDPServerSocket(NULL, net::NetLog::Source())); |
42 | 43 |
| 44 // Reset |bound_port_| to zero. |
| 45 bound_port_ = 0; |
| 46 |
43 net::IPEndPoint address; | 47 net::IPEndPoint address; |
44 if (!socket_.get() || | 48 if (!socket_.get() || |
45 !NetAddressPrivateImpl::NetAddressToIPEndPoint(addr, &address)) { | 49 !NetAddressPrivateImpl::NetAddressToIPEndPoint(addr, &address)) { |
46 SendBindACK(false); | 50 SendBindACK(false); |
47 return; | 51 return; |
48 } | 52 } |
49 | 53 |
50 int result = socket_->Listen(address); | 54 int result = socket_->Listen(address); |
51 | 55 |
| 56 // If we are successful, get the local port that we bound to. |
| 57 if (result == net::OK) { |
| 58 net::IPEndPoint current_address; |
| 59 socket_->GetLocalAddress(¤t_address); |
| 60 bound_port_ = current_address.port(); |
| 61 } |
| 62 |
52 SendBindACK(result == net::OK); | 63 SendBindACK(result == net::OK); |
53 } | 64 } |
54 | 65 |
55 void PepperUDPSocket::RecvFrom(int32_t num_bytes) { | 66 void PepperUDPSocket::RecvFrom(int32_t num_bytes) { |
56 if (recvfrom_buffer_.get()) { | 67 if (recvfrom_buffer_.get()) { |
57 SendRecvFromACKError(); | 68 SendRecvFromACKError(); |
58 return; | 69 return; |
59 } | 70 } |
60 | 71 |
61 recvfrom_buffer_ = new net::IOBuffer(num_bytes); | 72 recvfrom_buffer_ = new net::IOBuffer(num_bytes); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 addr)); | 111 addr)); |
101 } | 112 } |
102 | 113 |
103 void PepperUDPSocket::SendSendToACKError() { | 114 void PepperUDPSocket::SendSendToACKError() { |
104 manager_->Send(new PpapiMsg_PPBUDPSocket_SendToACK( | 115 manager_->Send(new PpapiMsg_PPBUDPSocket_SendToACK( |
105 routing_id_, plugin_dispatcher_id_, socket_id_, false, 0)); | 116 routing_id_, plugin_dispatcher_id_, socket_id_, false, 0)); |
106 } | 117 } |
107 | 118 |
108 void PepperUDPSocket::SendBindACK(bool result) { | 119 void PepperUDPSocket::SendBindACK(bool result) { |
109 manager_->Send(new PpapiMsg_PPBUDPSocket_BindACK( | 120 manager_->Send(new PpapiMsg_PPBUDPSocket_BindACK( |
110 routing_id_, plugin_dispatcher_id_, socket_id_, result)); | 121 routing_id_, plugin_dispatcher_id_, socket_id_, result, bound_port_)); |
111 } | 122 } |
112 | 123 |
113 void PepperUDPSocket::OnRecvFromCompleted(int result) { | 124 void PepperUDPSocket::OnRecvFromCompleted(int result) { |
114 DCHECK(recvfrom_buffer_.get()); | 125 DCHECK(recvfrom_buffer_.get()); |
115 | 126 |
116 // Convert IPEndPoint we get back from RecvFrom to a PP_NetAddress_Private, | 127 // Convert IPEndPoint we get back from RecvFrom to a PP_NetAddress_Private, |
117 // to send back. | 128 // to send back. |
118 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; | 129 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; |
119 if (result < 0 || | 130 if (result < 0 || |
120 !NetAddressPrivateImpl::IPEndPointToNetAddress(recvfrom_address_, | 131 !NetAddressPrivateImpl::IPEndPointToNetAddress(recvfrom_address_, |
121 &addr)) { | 132 &addr)) { |
122 SendRecvFromACKError(); | 133 SendRecvFromACKError(); |
123 } else { | 134 } else { |
124 manager_->Send(new PpapiMsg_PPBUDPSocket_RecvFromACK( | 135 manager_->Send(new PpapiMsg_PPBUDPSocket_RecvFromACK( |
125 routing_id_, plugin_dispatcher_id_, socket_id_, true, | 136 routing_id_, plugin_dispatcher_id_, socket_id_, true, |
126 std::string(recvfrom_buffer_->data(), result), addr)); | 137 std::string(recvfrom_buffer_->data(), result), addr)); |
127 } | 138 } |
128 | 139 |
129 recvfrom_buffer_ = NULL; | 140 recvfrom_buffer_ = NULL; |
130 } | 141 } |
131 | 142 |
132 void PepperUDPSocket::OnSendToCompleted(int result) { | 143 void PepperUDPSocket::OnSendToCompleted(int result) { |
133 DCHECK(sendto_buffer_.get()); | 144 DCHECK(sendto_buffer_.get()); |
134 | 145 |
135 manager_->Send(new PpapiMsg_PPBUDPSocket_SendToACK( | 146 manager_->Send(new PpapiMsg_PPBUDPSocket_SendToACK( |
136 routing_id_, plugin_dispatcher_id_, socket_id_, true, result)); | 147 routing_id_, plugin_dispatcher_id_, socket_id_, true, result)); |
137 | 148 |
138 sendto_buffer_ = NULL; | 149 sendto_buffer_ = NULL; |
139 } | 150 } |
OLD | NEW |