OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "network/public/cpp/udp_socket_wrapper.h" | 5 #include "network/public/cpp/udp_socket_wrapper.h" |
6 | 6 |
7 #include <assert.h> | 7 #include <assert.h> |
8 | 8 |
9 #include "third_party/mojo/src/mojo/public/cpp/environment/logging.h" | 9 #include "third_party/mojo/src/mojo/public/cpp/environment/logging.h" |
10 | 10 |
(...skipping 22 matching lines...) Expand all Loading... |
33 : delegate_(delegate), | 33 : delegate_(delegate), |
34 forward_callback_(forward_callback) { | 34 forward_callback_(forward_callback) { |
35 } | 35 } |
36 | 36 |
37 UDPSocketWrapper::SendCallbackHandler::~SendCallbackHandler() {} | 37 UDPSocketWrapper::SendCallbackHandler::~SendCallbackHandler() {} |
38 | 38 |
39 void UDPSocketWrapper::SendCallbackHandler::Run(NetworkErrorPtr result) const { | 39 void UDPSocketWrapper::SendCallbackHandler::Run(NetworkErrorPtr result) const { |
40 delegate_->OnSendToCompleted(result.Pass(), forward_callback_); | 40 delegate_->OnSendToCompleted(result.Pass(), forward_callback_); |
41 } | 41 } |
42 | 42 |
| 43 UDPSocketWrapper::ReceiverBindingCallback::ReceiverBindingCallback( |
| 44 UDPSocketWrapper* delegate, |
| 45 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& wrapper_callback) |
| 46 : delegate_(delegate), wrapper_callback_(wrapper_callback) { |
| 47 } |
| 48 |
| 49 UDPSocketWrapper::ReceiverBindingCallback::~ReceiverBindingCallback() { |
| 50 } |
| 51 |
| 52 void UDPSocketWrapper::ReceiverBindingCallback::Run( |
| 53 NetworkErrorPtr result, |
| 54 NetAddressPtr addr, |
| 55 InterfaceRequest<UDPSocketReceiver> request) const { |
| 56 delegate_->StartReceivingData(request.Pass()); |
| 57 wrapper_callback_.Run(result.Pass(), addr.Pass()); |
| 58 } |
| 59 |
43 UDPSocketWrapper::ReceivedData::ReceivedData() {} | 60 UDPSocketWrapper::ReceivedData::ReceivedData() {} |
44 UDPSocketWrapper::ReceivedData::~ReceivedData() {} | 61 UDPSocketWrapper::ReceivedData::~ReceivedData() {} |
45 | 62 |
46 UDPSocketWrapper::SendRequest::SendRequest() {} | 63 UDPSocketWrapper::SendRequest::SendRequest() {} |
47 UDPSocketWrapper::SendRequest::~SendRequest() {} | 64 UDPSocketWrapper::SendRequest::~SendRequest() {} |
48 | 65 |
49 UDPSocketWrapper::UDPSocketWrapper(UDPSocketPtr socket) | 66 UDPSocketWrapper::UDPSocketWrapper(UDPSocketPtr socket) |
50 : socket_(socket.Pass()), | 67 : binding_(this), |
51 max_receive_queue_size_(kDefaultReceiveQueueSlots), | 68 socket_(socket.Pass()), |
52 max_pending_sends_(1), | 69 max_receive_queue_size_(kDefaultReceiveQueueSlots), |
53 current_pending_sends_(0) { | 70 max_pending_sends_(1), |
| 71 current_pending_sends_(0) { |
54 Initialize(0); | 72 Initialize(0); |
55 } | 73 } |
56 | 74 |
57 UDPSocketWrapper::UDPSocketWrapper(UDPSocketPtr socket, | 75 UDPSocketWrapper::UDPSocketWrapper(UDPSocketPtr socket, |
58 uint32_t receive_queue_slots, | 76 uint32_t receive_queue_slots, |
59 uint32_t requested_max_pending_sends) | 77 uint32_t requested_max_pending_sends) |
60 : socket_(socket.Pass()), | 78 : binding_(this), |
61 max_receive_queue_size_(receive_queue_slots), | 79 socket_(socket.Pass()), |
62 max_pending_sends_(1), | 80 max_receive_queue_size_(receive_queue_slots), |
63 current_pending_sends_(0) { | 81 max_pending_sends_(1), |
| 82 current_pending_sends_(0) { |
64 Initialize(requested_max_pending_sends); | 83 Initialize(requested_max_pending_sends); |
65 } | 84 } |
66 | 85 |
67 UDPSocketWrapper::~UDPSocketWrapper() { | 86 UDPSocketWrapper::~UDPSocketWrapper() { |
68 while (!receive_queue_.empty()) { | 87 while (!receive_queue_.empty()) { |
69 delete receive_queue_.front(); | 88 delete receive_queue_.front(); |
70 receive_queue_.pop(); | 89 receive_queue_.pop(); |
71 } | 90 } |
72 while (!send_requests_.empty()) { | 91 while (!send_requests_.empty()) { |
73 delete send_requests_.front(); | 92 delete send_requests_.front(); |
74 send_requests_.pop(); | 93 send_requests_.pop(); |
75 } | 94 } |
76 } | 95 } |
77 | 96 |
78 void UDPSocketWrapper::AllowAddressReuse(const ErrorCallback& callback) { | 97 void UDPSocketWrapper::AllowAddressReuse(const ErrorCallback& callback) { |
79 socket_->AllowAddressReuse(callback); | 98 socket_->AllowAddressReuse(callback); |
80 } | 99 } |
81 | 100 |
82 void UDPSocketWrapper::Bind( | 101 void UDPSocketWrapper::Bind( |
83 NetAddressPtr addr, | 102 NetAddressPtr addr, |
84 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback) { | 103 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback) { |
85 socket_->Bind(addr.Pass(), callback); | 104 socket_->Bind( |
| 105 addr.Pass(), |
| 106 BindOrConnectCallback(static_cast<BindOrConnectCallback::Runnable*>( |
| 107 new ReceiverBindingCallback(this, callback)))); |
86 } | 108 } |
87 | 109 |
88 void UDPSocketWrapper::Connect( | 110 void UDPSocketWrapper::Connect( |
89 NetAddressPtr remote_addr, | 111 NetAddressPtr remote_addr, |
90 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback) { | 112 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback) { |
91 socket_->Connect(remote_addr.Pass(), callback); | 113 socket_->Connect( |
| 114 remote_addr.Pass(), |
| 115 BindOrConnectCallback(static_cast<BindOrConnectCallback::Runnable*>( |
| 116 new ReceiverBindingCallback(this, callback)))); |
92 } | 117 } |
93 | 118 |
94 void UDPSocketWrapper::SetSendBufferSize(uint32_t size, | 119 void UDPSocketWrapper::SetSendBufferSize(uint32_t size, |
95 const ErrorCallback& callback) { | 120 const ErrorCallback& callback) { |
96 socket_->SetSendBufferSize(size, callback); | 121 socket_->SetSendBufferSize(size, callback); |
97 } | 122 } |
98 | 123 |
99 void UDPSocketWrapper::SetReceiveBufferSize(uint32_t size, | 124 void UDPSocketWrapper::SetReceiveBufferSize(uint32_t size, |
100 const ErrorCallback& callback) { | 125 const ErrorCallback& callback) { |
101 socket_->SetReceiveBufferSize(size, callback); | 126 socket_->SetReceiveBufferSize(size, callback); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 | 177 |
153 MOJO_DCHECK(receive_queue_.size() < max_receive_queue_size_); | 178 MOJO_DCHECK(receive_queue_.size() < max_receive_queue_size_); |
154 ReceivedData* received_data = new ReceivedData(); | 179 ReceivedData* received_data = new ReceivedData(); |
155 received_data->result = result.Pass(); | 180 received_data->result = result.Pass(); |
156 received_data->src_addr = src_addr.Pass(); | 181 received_data->src_addr = src_addr.Pass(); |
157 received_data->data = data.Pass(); | 182 received_data->data = data.Pass(); |
158 receive_queue_.push(received_data); | 183 receive_queue_.push(received_data); |
159 } | 184 } |
160 | 185 |
161 void UDPSocketWrapper::Initialize(uint32_t requested_max_pending_sends) { | 186 void UDPSocketWrapper::Initialize(uint32_t requested_max_pending_sends) { |
162 socket_.set_client(this); | |
163 socket_->NegotiateMaxPendingSendRequests( | 187 socket_->NegotiateMaxPendingSendRequests( |
164 requested_max_pending_sends, | 188 requested_max_pending_sends, |
165 Callback<void(uint32_t)>( | 189 Callback<void(uint32_t)>( |
166 static_cast< Callback<void(uint32_t)>::Runnable*>( | 190 static_cast< Callback<void(uint32_t)>::Runnable*>( |
167 new NegotiateCallbackHandler(this)))); | 191 new NegotiateCallbackHandler(this)))); |
168 socket_->ReceiveMore(max_receive_queue_size_); | |
169 } | 192 } |
170 | 193 |
171 void UDPSocketWrapper::OnNegotiateMaxPendingSendRequestsCompleted( | 194 void UDPSocketWrapper::OnNegotiateMaxPendingSendRequestsCompleted( |
172 uint32_t actual_size) { | 195 uint32_t actual_size) { |
173 MOJO_DCHECK(max_pending_sends_ == 1); | 196 MOJO_DCHECK(max_pending_sends_ == 1); |
174 | 197 |
175 if (actual_size == 0) { | 198 if (actual_size == 0) { |
176 assert(false); | 199 assert(false); |
177 return; | 200 return; |
178 } | 201 } |
(...skipping 24 matching lines...) Expand all Loading... |
203 socket_->SendTo( | 226 socket_->SendTo( |
204 request->dest_addr.Pass(), request->data.Pass(), | 227 request->dest_addr.Pass(), request->data.Pass(), |
205 ErrorCallback(static_cast<ErrorCallback::Runnable*>( | 228 ErrorCallback(static_cast<ErrorCallback::Runnable*>( |
206 new SendCallbackHandler(this, request->callback)))); | 229 new SendCallbackHandler(this, request->callback)))); |
207 | 230 |
208 delete request; | 231 delete request; |
209 | 232 |
210 return true; | 233 return true; |
211 } | 234 } |
212 | 235 |
| 236 void UDPSocketWrapper::StartReceivingData( |
| 237 InterfaceRequest<UDPSocketReceiver> request) { |
| 238 binding_.Bind(request.Pass()); |
| 239 socket_->ReceiveMore(max_receive_queue_size_); |
| 240 } |
| 241 |
213 } // namespace mojo | 242 } // namespace mojo |
OLD | NEW |