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 ALLOW_THIS_IN_INITIALIZER_LIST( | |
32 recvfrom_callback_(this, &PepperUDPSocket::OnRecvFromCompleted)), | |
33 ALLOW_THIS_IN_INITIALIZER_LIST( | |
34 sendto_callback_(this, &PepperUDPSocket::OnSendToCompleted)) { | |
35 DCHECK(manager); | 31 DCHECK(manager); |
36 } | 32 } |
37 | 33 |
38 PepperUDPSocket::~PepperUDPSocket() { | 34 PepperUDPSocket::~PepperUDPSocket() { |
39 // Make sure there are no further callbacks from socket_. | 35 // Make sure there are no further callbacks from socket_. |
40 if (socket_.get()) | 36 if (socket_.get()) |
41 socket_->Close(); | 37 socket_->Close(); |
42 } | 38 } |
43 | 39 |
44 void PepperUDPSocket::Bind(const PP_NetAddress_Private& addr) { | 40 void PepperUDPSocket::Bind(const PP_NetAddress_Private& addr) { |
(...skipping 11 matching lines...) Expand all Loading... |
56 SendBindACK(result == net::OK); | 52 SendBindACK(result == net::OK); |
57 } | 53 } |
58 | 54 |
59 void PepperUDPSocket::RecvFrom(int32_t num_bytes) { | 55 void PepperUDPSocket::RecvFrom(int32_t num_bytes) { |
60 if (recvfrom_buffer_.get()) { | 56 if (recvfrom_buffer_.get()) { |
61 SendRecvFromACKError(); | 57 SendRecvFromACKError(); |
62 return; | 58 return; |
63 } | 59 } |
64 | 60 |
65 recvfrom_buffer_ = new net::IOBuffer(num_bytes); | 61 recvfrom_buffer_ = new net::IOBuffer(num_bytes); |
66 int result = socket_->RecvFrom(recvfrom_buffer_, | 62 int result = socket_->RecvFrom( |
67 num_bytes, | 63 recvfrom_buffer_, num_bytes, &recvfrom_address_, |
68 &recvfrom_address_, | 64 base::Bind(&PepperUDPSocket::OnRecvFromCompleted, |
69 &recvfrom_callback_); | 65 base::Unretained(this))); |
70 | 66 |
71 if (result != net::ERR_IO_PENDING) | 67 if (result != net::ERR_IO_PENDING) |
72 OnRecvFromCompleted(result); | 68 OnRecvFromCompleted(result); |
73 } | 69 } |
74 | 70 |
75 void PepperUDPSocket::SendTo(const std::string& data, | 71 void PepperUDPSocket::SendTo(const std::string& data, |
76 const PP_NetAddress_Private& addr) { | 72 const PP_NetAddress_Private& addr) { |
77 if (sendto_buffer_.get() || data.empty()) { | 73 if (sendto_buffer_.get() || data.empty()) { |
78 SendSendToACKError(); | 74 SendSendToACKError(); |
79 return; | 75 return; |
80 } | 76 } |
81 | 77 |
82 net::IPEndPoint address; | 78 net::IPEndPoint address; |
83 if (!NetAddressPrivateImpl::NetAddressToIPEndPoint(addr, &address)) { | 79 if (!NetAddressPrivateImpl::NetAddressToIPEndPoint(addr, &address)) { |
84 SendSendToACKError(); | 80 SendSendToACKError(); |
85 return; | 81 return; |
86 } | 82 } |
87 | 83 |
88 int data_size = data.size(); | 84 int data_size = data.size(); |
89 | 85 |
90 sendto_buffer_ = new net::IOBuffer(data_size); | 86 sendto_buffer_ = new net::IOBuffer(data_size); |
91 memcpy(sendto_buffer_->data(), data.data(), data_size); | 87 memcpy(sendto_buffer_->data(), data.data(), data_size); |
92 int result = socket_->SendTo(sendto_buffer_, | 88 int result = socket_->SendTo( |
93 data_size, | 89 sendto_buffer_, data_size, address, |
94 address, | 90 base::Bind(&PepperUDPSocket::OnSendToCompleted, base::Unretained(this))); |
95 &sendto_callback_); | |
96 | 91 |
97 if (result != net::ERR_IO_PENDING) | 92 if (result != net::ERR_IO_PENDING) |
98 OnSendToCompleted(result); | 93 OnSendToCompleted(result); |
99 } | 94 } |
100 | 95 |
101 void PepperUDPSocket::SendRecvFromACKError() { | 96 void PepperUDPSocket::SendRecvFromACKError() { |
102 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; | 97 PP_NetAddress_Private addr = NetAddressPrivateImpl::kInvalidNetAddress; |
103 manager_->Send(new PpapiMsg_PPBUDPSocket_RecvFromACK( | 98 manager_->Send(new PpapiMsg_PPBUDPSocket_RecvFromACK( |
104 routing_id_, plugin_dispatcher_id_, socket_id_, false, std::string(), | 99 routing_id_, plugin_dispatcher_id_, socket_id_, false, std::string(), |
105 addr)); | 100 addr)); |
(...skipping 29 matching lines...) Expand all Loading... |
135 } | 130 } |
136 | 131 |
137 void PepperUDPSocket::OnSendToCompleted(int result) { | 132 void PepperUDPSocket::OnSendToCompleted(int result) { |
138 DCHECK(sendto_buffer_.get()); | 133 DCHECK(sendto_buffer_.get()); |
139 | 134 |
140 manager_->Send(new PpapiMsg_PPBUDPSocket_SendToACK( | 135 manager_->Send(new PpapiMsg_PPBUDPSocket_SendToACK( |
141 routing_id_, plugin_dispatcher_id_, socket_id_, true, result)); | 136 routing_id_, plugin_dispatcher_id_, socket_id_, true, result)); |
142 | 137 |
143 sendto_buffer_ = NULL; | 138 sendto_buffer_ = NULL; |
144 } | 139 } |
OLD | NEW |