OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/renderer_host/pepper_udp_socket.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include "base/compiler_specific.h" | |
10 #include "base/logging.h" | |
11 #include "content/browser/renderer_host/pepper_message_filter.h" | |
12 #include "content/public/browser/browser_thread.h" | |
13 #include "net/base/io_buffer.h" | |
14 #include "net/base/net_errors.h" | |
15 #include "net/udp/udp_server_socket.h" | |
16 #include "ppapi/proxy/ppapi_messages.h" | |
17 #include "ppapi/shared_impl/private/net_address_private_impl.h" | |
18 | |
19 using content::BrowserThread; | |
20 using ppapi::NetAddressPrivateImpl; | |
21 | |
22 PepperUDPSocket::PepperUDPSocket( | |
23 PepperMessageFilter* manager, | |
24 int32 routing_id, | |
25 uint32 plugin_dispatcher_id, | |
26 uint32 socket_id) | |
27 : manager_(manager), | |
28 routing_id_(routing_id), | |
29 plugin_dispatcher_id_(plugin_dispatcher_id), | |
30 socket_id_(socket_id) { | |
31 DCHECK(manager); | |
32 } | |
33 | |
34 PepperUDPSocket::~PepperUDPSocket() { | |
35 // Make sure there are no further callbacks from socket_. | |
36 if (socket_.get()) | |
37 socket_->Close(); | |
38 } | |
39 | |
40 void PepperUDPSocket::Bind(const PP_NetAddress_Private& addr) { | |
41 socket_.reset(new net::UDPServerSocket(NULL, net::NetLog::Source())); | |
42 | |
43 net::IPEndPoint address; | |
44 if (!socket_.get() || | |
45 !NetAddressPrivateImpl::NetAddressToIPEndPoint(addr, &address)) { | |
46 SendBindACKError(); | |
47 return; | |
48 } | |
49 | |
50 int result = socket_->Listen(address); | |
51 | |
52 if (result == net::OK && | |
53 socket_->GetLocalAddress(&bound_address_) != net::OK) { | |
54 SendBindACKError(); | |
55 return; | |
56 } | |
57 | |
58 OnBindCompleted(result); | |
59 } | |
60 | |
61 void PepperUDPSocket::RecvFrom(int32_t num_bytes) { | |
62 if (recvfrom_buffer_.get()) { | |
63 SendRecvFromACKError(); | |
64 return; | |
65 } | |
66 | |
67 recvfrom_buffer_ = new net::IOBuffer(num_bytes); | |
68 int result = socket_->RecvFrom( | |
69 recvfrom_buffer_, num_bytes, &recvfrom_address_, | |
70 base::Bind(&PepperUDPSocket::OnRecvFromCompleted, | |
71 base::Unretained(this))); | |
72 | |
73 if (result != net::ERR_IO_PENDING) | |
74 OnRecvFromCompleted(result); | |
75 } | |
76 | |
77 void PepperUDPSocket::SendTo(const std::string& data, | |
78 const PP_NetAddress_Private& addr) { | |
79 if (sendto_buffer_.get() || data.empty()) { | |
80 SendSendToACKError(); | |
81 return; | |
82 } | |
83 | |
84 net::IPEndPoint address; | |
85 if (!NetAddressPrivateImpl::NetAddressToIPEndPoint(addr, &address)) { | |
86 SendSendToACKError(); | |
87 return; | |
88 } | |
89 | |
90 int data_size = data.size(); | |
91 | |
92 sendto_buffer_ = new net::IOBuffer(data_size); | |
93 memcpy(sendto_buffer_->data(), data.data(), data_size); | |
94 int result = socket_->SendTo( | |
95 sendto_buffer_, data_size, address, | |
96 base::Bind(&PepperUDPSocket::OnSendToCompleted, base::Unretained(this))); | |
97 | |
98 if (result != net::ERR_IO_PENDING) | |
99 OnSendToCompleted(result); | |
100 } | |
101 | |
102 void PepperUDPSocket::SendRecvFromACKError() { | |
103 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; | |
104 manager_->Send(new PpapiMsg_PPBUDPSocket_RecvFromACK( | |
105 routing_id_, plugin_dispatcher_id_, socket_id_, false, std::string(), | |
106 addr)); | |
107 } | |
108 | |
109 void PepperUDPSocket::SendSendToACKError() { | |
110 manager_->Send(new PpapiMsg_PPBUDPSocket_SendToACK( | |
111 routing_id_, plugin_dispatcher_id_, socket_id_, false, 0)); | |
112 } | |
113 | |
114 void PepperUDPSocket::SendBindACKError() { | |
115 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; | |
116 manager_->Send(new PpapiMsg_PPBUDPSocket_BindACK( | |
117 routing_id_, plugin_dispatcher_id_, socket_id_, false, addr)); | |
118 } | |
119 | |
120 void PepperUDPSocket::OnBindCompleted(int result) { | |
121 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; | |
122 if (result < 0 || | |
123 !NetAddressPrivateImpl::IPEndPointToNetAddress(bound_address_, | |
124 &addr)) { | |
125 SendBindACKError(); | |
126 } else { | |
127 manager_->Send(new PpapiMsg_PPBUDPSocket_BindACK( | |
128 routing_id_, plugin_dispatcher_id_, socket_id_, true, addr)); | |
129 } | |
130 } | |
131 | |
132 void PepperUDPSocket::OnRecvFromCompleted(int result) { | |
133 DCHECK(recvfrom_buffer_.get()); | |
134 | |
135 // Convert IPEndPoint we get back from RecvFrom to a PP_NetAddress_Private, | |
136 // to send back. | |
137 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; | |
138 if (result < 0 || | |
139 !NetAddressPrivateImpl::IPEndPointToNetAddress(recvfrom_address_, | |
140 &addr)) { | |
141 SendRecvFromACKError(); | |
142 } else { | |
143 manager_->Send(new PpapiMsg_PPBUDPSocket_RecvFromACK( | |
144 routing_id_, plugin_dispatcher_id_, socket_id_, true, | |
145 std::string(recvfrom_buffer_->data(), result), addr)); | |
146 } | |
147 | |
148 recvfrom_buffer_ = NULL; | |
149 } | |
150 | |
151 void PepperUDPSocket::OnSendToCompleted(int result) { | |
152 DCHECK(sendto_buffer_.get()); | |
153 | |
154 if (result < 0) { | |
155 SendSendToACKError(); | |
156 } else { | |
157 manager_->Send(new PpapiMsg_PPBUDPSocket_SendToACK( | |
158 routing_id_, plugin_dispatcher_id_, socket_id_, true, result)); | |
159 } | |
160 | |
161 sendto_buffer_ = NULL; | |
162 } | |
OLD | NEW |