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/browser/renderer_host/pepper/pepper_udp_socket.h" | 5 #include "content/browser/renderer_host/pepper/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/pepper_message_filter.h" | 11 #include "content/browser/renderer_host/pepper/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 allow_address_reuse_(false), |
| 32 allow_broadcast_(false) { |
31 DCHECK(manager); | 33 DCHECK(manager); |
32 } | 34 } |
33 | 35 |
34 PepperUDPSocket::~PepperUDPSocket() { | 36 PepperUDPSocket::~PepperUDPSocket() { |
35 // Make sure there are no further callbacks from socket_. | 37 // Make sure there are no further callbacks from socket_. |
36 if (socket_.get()) | 38 if (socket_.get()) |
37 socket_->Close(); | 39 socket_->Close(); |
38 } | 40 } |
39 | 41 |
| 42 void PepperUDPSocket::AllowAddressReuse(bool value) { |
| 43 allow_address_reuse_ = value; |
| 44 SendSetSocketFeatureACK(true); |
| 45 } |
| 46 |
| 47 void PepperUDPSocket::AllowBroadcast(bool value) { |
| 48 allow_broadcast_ = value; |
| 49 SendSetSocketFeatureACK(true); |
| 50 } |
| 51 |
40 void PepperUDPSocket::Bind(const PP_NetAddress_Private& addr) { | 52 void PepperUDPSocket::Bind(const PP_NetAddress_Private& addr) { |
41 socket_.reset(new net::UDPServerSocket(NULL, net::NetLog::Source())); | 53 socket_.reset(new net::UDPServerSocket(NULL, net::NetLog::Source())); |
42 | 54 |
43 net::IPEndPoint address; | 55 net::IPEndPoint address; |
44 if (!socket_.get() || | 56 if (!socket_.get() || |
45 !NetAddressPrivateImpl::NetAddressToIPEndPoint(addr, &address)) { | 57 !NetAddressPrivateImpl::NetAddressToIPEndPoint(addr, &address)) { |
46 SendBindACKError(); | 58 SendBindACKError(); |
47 return; | 59 return; |
48 } | 60 } |
49 | 61 |
| 62 if (allow_address_reuse_) |
| 63 socket_->AllowAddressReuse(); |
| 64 if (allow_broadcast_) |
| 65 socket_->AllowBroadcast(); |
| 66 |
50 int result = socket_->Listen(address); | 67 int result = socket_->Listen(address); |
51 | 68 |
52 if (result == net::OK && | 69 if (result == net::OK && |
53 socket_->GetLocalAddress(&bound_address_) != net::OK) { | 70 socket_->GetLocalAddress(&bound_address_) != net::OK) { |
54 SendBindACKError(); | 71 SendBindACKError(); |
55 return; | 72 return; |
56 } | 73 } |
57 | 74 |
58 OnBindCompleted(result); | 75 OnBindCompleted(result); |
59 } | 76 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 sendto_buffer_ = new net::IOBuffer(data_size); | 109 sendto_buffer_ = new net::IOBuffer(data_size); |
93 memcpy(sendto_buffer_->data(), data.data(), data_size); | 110 memcpy(sendto_buffer_->data(), data.data(), data_size); |
94 int result = socket_->SendTo( | 111 int result = socket_->SendTo( |
95 sendto_buffer_, data_size, address, | 112 sendto_buffer_, data_size, address, |
96 base::Bind(&PepperUDPSocket::OnSendToCompleted, base::Unretained(this))); | 113 base::Bind(&PepperUDPSocket::OnSendToCompleted, base::Unretained(this))); |
97 | 114 |
98 if (result != net::ERR_IO_PENDING) | 115 if (result != net::ERR_IO_PENDING) |
99 OnSendToCompleted(result); | 116 OnSendToCompleted(result); |
100 } | 117 } |
101 | 118 |
| 119 void PepperUDPSocket::SendSetSocketFeatureACK(bool succeeded) { |
| 120 manager_->Send(new PpapiMsg_PPBUDPSocket_SetSocketFeatureACK( |
| 121 routing_id_, plugin_dispatcher_id_, socket_id_, succeeded)); |
| 122 } |
| 123 |
102 void PepperUDPSocket::SendRecvFromACKError() { | 124 void PepperUDPSocket::SendRecvFromACKError() { |
103 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; | 125 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; |
104 manager_->Send(new PpapiMsg_PPBUDPSocket_RecvFromACK( | 126 manager_->Send(new PpapiMsg_PPBUDPSocket_RecvFromACK( |
105 routing_id_, plugin_dispatcher_id_, socket_id_, false, std::string(), | 127 routing_id_, plugin_dispatcher_id_, socket_id_, false, std::string(), |
106 addr)); | 128 addr)); |
107 } | 129 } |
108 | 130 |
109 void PepperUDPSocket::SendSendToACKError() { | 131 void PepperUDPSocket::SendSendToACKError() { |
110 manager_->Send(new PpapiMsg_PPBUDPSocket_SendToACK( | 132 manager_->Send(new PpapiMsg_PPBUDPSocket_SendToACK( |
111 routing_id_, plugin_dispatcher_id_, socket_id_, false, 0)); | 133 routing_id_, plugin_dispatcher_id_, socket_id_, false, 0)); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 175 |
154 if (result < 0) { | 176 if (result < 0) { |
155 SendSendToACKError(); | 177 SendSendToACKError(); |
156 } else { | 178 } else { |
157 manager_->Send(new PpapiMsg_PPBUDPSocket_SendToACK( | 179 manager_->Send(new PpapiMsg_PPBUDPSocket_SendToACK( |
158 routing_id_, plugin_dispatcher_id_, socket_id_, true, result)); | 180 routing_id_, plugin_dispatcher_id_, socket_id_, true, result)); |
159 } | 181 } |
160 | 182 |
161 sendto_buffer_ = NULL; | 183 sendto_buffer_ = NULL; |
162 } | 184 } |
OLD | NEW |