Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: net/udp/udp_socket_win.cc

Issue 8200011: Add NetLog support to UDP sockets. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« net/udp/udp_socket_libevent.cc ('K') | « net/udp/udp_socket_win.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "net/udp/udp_socket_win.h" 5 #include "net/udp/udp_socket_win.h"
6 6
7 #include <mstcpip.h> 7 #include <mstcpip.h>
8 8
9 #include "base/eintr_wrapper.h" 9 #include "base/eintr_wrapper.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/metrics/stats_counters.h" 12 #include "base/metrics/stats_counters.h"
13 #include "base/rand_util.h" 13 #include "base/rand_util.h"
14 #include "net/base/address_list_net_log_param.h"
14 #include "net/base/io_buffer.h" 15 #include "net/base/io_buffer.h"
15 #include "net/base/ip_endpoint.h" 16 #include "net/base/ip_endpoint.h"
16 #include "net/base/net_errors.h" 17 #include "net/base/net_errors.h"
17 #include "net/base/net_log.h" 18 #include "net/base/net_log.h"
18 #include "net/base/net_util.h" 19 #include "net/base/net_util.h"
19 #include "net/base/winsock_init.h" 20 #include "net/base/winsock_init.h"
20 #include "net/base/winsock_util.h" 21 #include "net/base/winsock_util.h"
22 #include "net/udp/udp_data_transfer_param.h"
21 23
22 namespace { 24 namespace {
23 25
24 static const int kBindRetries = 10; 26 static const int kBindRetries = 10;
25 static const int kPortStart = 1024; 27 static const int kPortStart = 1024;
26 static const int kPortEnd = 65535; 28 static const int kPortEnd = 65535;
27 29
28 } // namespace net 30 } // namespace net
29 31
30 namespace net { 32 namespace net {
(...skipping 13 matching lines...) Expand all
44 net::NetLog* net_log, 46 net::NetLog* net_log,
45 const net::NetLog::Source& source) 47 const net::NetLog::Source& source)
46 : socket_(INVALID_SOCKET), 48 : socket_(INVALID_SOCKET),
47 bind_type_(bind_type), 49 bind_type_(bind_type),
48 rand_int_cb_(rand_int_cb), 50 rand_int_cb_(rand_int_cb),
49 ALLOW_THIS_IN_INITIALIZER_LIST(read_delegate_(this)), 51 ALLOW_THIS_IN_INITIALIZER_LIST(read_delegate_(this)),
50 ALLOW_THIS_IN_INITIALIZER_LIST(write_delegate_(this)), 52 ALLOW_THIS_IN_INITIALIZER_LIST(write_delegate_(this)),
51 recv_from_address_(NULL), 53 recv_from_address_(NULL),
52 read_callback_(NULL), 54 read_callback_(NULL),
53 write_callback_(NULL), 55 write_callback_(NULL),
54 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { 56 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)) {
55 EnsureWinsockInit(); 57 EnsureWinsockInit();
56 scoped_refptr<NetLog::EventParameters> params; 58 scoped_refptr<NetLog::EventParameters> params;
57 if (source.is_valid()) 59 if (source.is_valid())
58 params = new NetLogSourceParameter("source_dependency", source); 60 params = new NetLogSourceParameter("source_dependency", source);
59 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); 61 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params);
60 memset(&read_overlapped_, 0, sizeof(read_overlapped_)); 62 memset(&read_overlapped_, 0, sizeof(read_overlapped_));
61 read_overlapped_.hEvent = WSACreateEvent(); 63 read_overlapped_.hEvent = WSACreateEvent();
62 memset(&write_overlapped_, 0, sizeof(write_overlapped_)); 64 memset(&write_overlapped_, 0, sizeof(write_overlapped_));
63 write_overlapped_.hEvent = WSACreateEvent(); 65 write_overlapped_.hEvent = WSACreateEvent();
64 if (bind_type == DatagramSocket::RANDOM_BIND) 66 if (bind_type == DatagramSocket::RANDOM_BIND)
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 176
175 int UDPSocketWin::SendToOrWrite(IOBuffer* buf, 177 int UDPSocketWin::SendToOrWrite(IOBuffer* buf,
176 int buf_len, 178 int buf_len,
177 const IPEndPoint* address, 179 const IPEndPoint* address,
178 OldCompletionCallback* callback) { 180 OldCompletionCallback* callback) {
179 DCHECK(CalledOnValidThread()); 181 DCHECK(CalledOnValidThread());
180 DCHECK_NE(INVALID_SOCKET, socket_); 182 DCHECK_NE(INVALID_SOCKET, socket_);
181 DCHECK(!write_callback_); 183 DCHECK(!write_callback_);
182 DCHECK(callback); // Synchronous operation not supported. 184 DCHECK(callback); // Synchronous operation not supported.
183 DCHECK_GT(buf_len, 0); 185 DCHECK_GT(buf_len, 0);
186 DCHECK(!send_to_address_.get());
184 187
185 int nwrite = InternalSendTo(buf, buf_len, address); 188 int nwrite = InternalSendTo(buf, buf_len, address);
186 if (nwrite != ERR_IO_PENDING) 189 if (nwrite != ERR_IO_PENDING)
187 return nwrite; 190 return nwrite;
188 191
192 if (address)
193 send_to_address_.reset(new IPEndPoint(*address));
189 write_iobuffer_ = buf; 194 write_iobuffer_ = buf;
190 write_callback_ = callback; 195 write_callback_ = callback;
191 return ERR_IO_PENDING; 196 return ERR_IO_PENDING;
192 } 197 }
193 198
194 int UDPSocketWin::Connect(const IPEndPoint& address) { 199 int UDPSocketWin::Connect(const IPEndPoint& address) {
200 net_log_.BeginEvent(
201 NetLog::TYPE_UDP_CONNECT,
202 make_scoped_refptr(new NetLogStringParameter("address",
203 address.ToString())));
204 int rv = InternalConnect(address);
205 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UDP_CONNECT, rv);
206 return rv;
207 }
208
209 int UDPSocketWin::InternalConnect(const IPEndPoint& address) {
195 DCHECK(!is_connected()); 210 DCHECK(!is_connected());
196 DCHECK(!remote_address_.get()); 211 DCHECK(!remote_address_.get());
197 int rv = CreateSocket(address); 212 int rv = CreateSocket(address);
198 if (rv < 0) 213 if (rv < 0)
199 return rv; 214 return rv;
200 215
201 if (bind_type_ == DatagramSocket::RANDOM_BIND) 216 if (bind_type_ == DatagramSocket::RANDOM_BIND)
202 rv = RandomBind(address); 217 rv = RandomBind(address);
203 // else connect() does the DatagramSocket::DEFAULT_BIND 218 // else connect() does the DatagramSocket::DEFAULT_BIND
204 219
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 write_callback_ = NULL; 273 write_callback_ = NULL;
259 c->Run(rv); 274 c->Run(rv);
260 } 275 }
261 276
262 void UDPSocketWin::DidCompleteRead() { 277 void UDPSocketWin::DidCompleteRead() {
263 DWORD num_bytes, flags; 278 DWORD num_bytes, flags;
264 BOOL ok = WSAGetOverlappedResult(socket_, &read_overlapped_, 279 BOOL ok = WSAGetOverlappedResult(socket_, &read_overlapped_,
265 &num_bytes, FALSE, &flags); 280 &num_bytes, FALSE, &flags);
266 WSAResetEvent(read_overlapped_.hEvent); 281 WSAResetEvent(read_overlapped_.hEvent);
267 int result = ok ? num_bytes : MapSystemError(WSAGetLastError()); 282 int result = ok ? num_bytes : MapSystemError(WSAGetLastError());
268 if (ok) { 283 // Convert address.
269 if (!ProcessSuccessfulRead(num_bytes, recv_from_address_)) 284 if (recv_from_address_ && result >= 0) {
285 if (!ReceiveAddressToIPEndpoint(recv_from_address_))
270 result = ERR_FAILED; 286 result = ERR_FAILED;
271 } 287 }
288 LogRead(result, read_iobuffer_->data());
272 read_iobuffer_ = NULL; 289 read_iobuffer_ = NULL;
273 recv_from_address_ = NULL; 290 recv_from_address_ = NULL;
274 DoReadCallback(result); 291 DoReadCallback(result);
275 } 292 }
276 293
277 bool UDPSocketWin::ProcessSuccessfulRead(int num_bytes, IPEndPoint* address) { 294 int UDPSocketWin::LogRead(int result,
278 base::StatsCounter read_bytes("udp.read_bytes"); 295 const char* bytes) const {
279 read_bytes.Add(num_bytes); 296 if (result < 0) {
280 297 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_RECEIVE_ERROR, result);
281 // Convert address. 298 return result;
282 if (address) {
283 struct sockaddr* addr =
284 reinterpret_cast<struct sockaddr*>(&recv_addr_storage_);
285 if (!address->FromSockAddr(addr, recv_addr_len_))
286 return false;
287 } 299 }
288 300
289 return true; 301 if (net_log_.IsLoggingAllEvents()) {
302 // Get address for logging, if |address| is NULL.
303 IPEndPoint address;
304 bool is_address_valid = ReceiveAddressToIPEndpoint(&address);
305 net_log_.AddEvent(
306 NetLog::TYPE_UDP_BYTES_RECEIVED,
307 make_scoped_refptr(
308 new UDPDataTransferNetLogParam(
309 result, bytes, net_log_.IsLoggingBytes(),
310 is_address_valid ? &address : NULL)));
311 }
312
313 base::StatsCounter read_bytes("udp.read_bytes");
314 read_bytes.Add(result);
315 return result;
290 } 316 }
291 317
292 void UDPSocketWin::DidCompleteWrite() { 318 void UDPSocketWin::DidCompleteWrite() {
293 DWORD num_bytes, flags; 319 DWORD num_bytes, flags;
294 BOOL ok = WSAGetOverlappedResult(socket_, &write_overlapped_, 320 BOOL ok = WSAGetOverlappedResult(socket_, &write_overlapped_,
295 &num_bytes, FALSE, &flags); 321 &num_bytes, FALSE, &flags);
296 WSAResetEvent(write_overlapped_.hEvent); 322 WSAResetEvent(write_overlapped_.hEvent);
297 int result = ok ? num_bytes : MapSystemError(WSAGetLastError()); 323 int result = ok ? num_bytes : MapSystemError(WSAGetLastError());
298 if (ok) 324 result = LogWrite(result, write_iobuffer_->data(), send_to_address_.get());
299 ProcessSuccessfulWrite(num_bytes); 325
326 send_to_address_.reset();
300 write_iobuffer_ = NULL; 327 write_iobuffer_ = NULL;
301 DoWriteCallback(result); 328 DoWriteCallback(result);
302 } 329 }
303 330
304 void UDPSocketWin::ProcessSuccessfulWrite(int num_bytes) { 331 int UDPSocketWin::LogWrite(int result,
332 const char* bytes,
333 const IPEndPoint* address) const {
334 if (result < 0) {
335 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_SEND_ERROR, result);
336 return result;
337 }
338
339 if (net_log_.IsLoggingAllEvents()) {
340 net_log_.AddEvent(
341 NetLog::TYPE_UDP_BYTES_SENT,
342 make_scoped_refptr(
343 new UDPDataTransferNetLogParam(result, bytes,
344 net_log_.IsLoggingBytes(),
345 address)));
346 }
347
305 base::StatsCounter write_bytes("udp.write_bytes"); 348 base::StatsCounter write_bytes("udp.write_bytes");
306 write_bytes.Add(num_bytes); 349 write_bytes.Add(result);
350 return result;
307 } 351 }
308 352
309 int UDPSocketWin::InternalRecvFrom(IOBuffer* buf, int buf_len, 353 int UDPSocketWin::InternalRecvFrom(IOBuffer* buf, int buf_len,
310 IPEndPoint* address) { 354 IPEndPoint* address) {
311 recv_addr_len_ = sizeof(recv_addr_storage_); 355 recv_addr_len_ = sizeof(recv_addr_storage_);
312 struct sockaddr* addr = 356 struct sockaddr* addr =
313 reinterpret_cast<struct sockaddr*>(&recv_addr_storage_); 357 reinterpret_cast<struct sockaddr*>(&recv_addr_storage_);
314 358
315 WSABUF read_buffer; 359 WSABUF read_buffer;
316 read_buffer.buf = buf->data(); 360 read_buffer.buf = buf->data();
317 read_buffer.len = buf_len; 361 read_buffer.len = buf_len;
318 362
319 DWORD flags = 0; 363 DWORD flags = 0;
320 DWORD num; 364 DWORD num;
321 AssertEventNotSignaled(read_overlapped_.hEvent); 365 AssertEventNotSignaled(read_overlapped_.hEvent);
322 int rv = WSARecvFrom(socket_, &read_buffer, 1, &num, &flags, addr, 366 int rv = WSARecvFrom(socket_, &read_buffer, 1, &num, &flags, addr,
323 &recv_addr_len_, &read_overlapped_, NULL); 367 &recv_addr_len_, &read_overlapped_, NULL);
324 if (rv == 0) { 368 if (rv == 0) {
325 if (ResetEventIfSignaled(read_overlapped_.hEvent)) { 369 if (ResetEventIfSignaled(read_overlapped_.hEvent)) {
326 if (!ProcessSuccessfulRead(num, address)) 370 int result = num;
327 return ERR_FAILED; 371 // Convert address.
328 return static_cast<int>(num); 372 if (address && result >= 0) {
373 if (!ReceiveAddressToIPEndpoint(address))
374 result = ERR_FAILED;
375 }
376 return LogRead(result, buf->data());
329 } 377 }
330 } else { 378 } else {
331 int os_error = WSAGetLastError(); 379 int os_error = WSAGetLastError();
332 if (os_error != WSA_IO_PENDING) 380 if (os_error != WSA_IO_PENDING)
333 return MapSystemError(os_error); 381 return LogRead(MapSystemError(os_error), NULL);
334 } 382 }
335 read_watcher_.StartWatching(read_overlapped_.hEvent, &read_delegate_); 383 read_watcher_.StartWatching(read_overlapped_.hEvent, &read_delegate_);
336 return ERR_IO_PENDING; 384 return ERR_IO_PENDING;
337 } 385 }
338 386
339 int UDPSocketWin::InternalSendTo(IOBuffer* buf, int buf_len, 387 int UDPSocketWin::InternalSendTo(IOBuffer* buf, int buf_len,
340 const IPEndPoint* address) { 388 const IPEndPoint* address) {
341 struct sockaddr_storage addr_storage; 389 struct sockaddr_storage addr_storage;
342 size_t addr_len = sizeof(addr_storage); 390 size_t addr_len = sizeof(addr_storage);
343 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); 391 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage);
344 392
345 // Convert address. 393 // Convert address.
346 if (!address) { 394 if (!address) {
347 addr = NULL; 395 addr = NULL;
348 addr_len = 0; 396 addr_len = 0;
349 } else { 397 } else {
350 if (!address->ToSockAddr(addr, &addr_len)) 398 if (!address->ToSockAddr(addr, &addr_len))
351 return ERR_FAILED; 399 return LogWrite(ERR_FAILED, NULL, NULL);
352 } 400 }
353 401
354 WSABUF write_buffer; 402 WSABUF write_buffer;
355 write_buffer.buf = buf->data(); 403 write_buffer.buf = buf->data();
356 write_buffer.len = buf_len; 404 write_buffer.len = buf_len;
357 405
358 DWORD flags = 0; 406 DWORD flags = 0;
359 DWORD num; 407 DWORD num;
360 AssertEventNotSignaled(write_overlapped_.hEvent); 408 AssertEventNotSignaled(write_overlapped_.hEvent);
361 int rv = WSASendTo(socket_, &write_buffer, 1, &num, flags, 409 int rv = WSASendTo(socket_, &write_buffer, 1, &num, flags,
362 addr, addr_len, &write_overlapped_, NULL); 410 addr, addr_len, &write_overlapped_, NULL);
363 if (rv == 0) { 411 if (rv == 0) {
364 if (ResetEventIfSignaled(write_overlapped_.hEvent)) { 412 if (ResetEventIfSignaled(write_overlapped_.hEvent))
365 ProcessSuccessfulWrite(num); 413 return LogWrite(num, buf->data(), address);
366 return static_cast<int>(num);
367 }
368 } else { 414 } else {
369 int os_error = WSAGetLastError(); 415 int os_error = WSAGetLastError();
370 if (os_error != WSA_IO_PENDING) 416 if (os_error != WSA_IO_PENDING)
371 return MapSystemError(os_error); 417 return LogWrite(MapSystemError(os_error), NULL, NULL);
372 } 418 }
373 419
374 write_watcher_.StartWatching(write_overlapped_.hEvent, &write_delegate_); 420 write_watcher_.StartWatching(write_overlapped_.hEvent, &write_delegate_);
375 return ERR_IO_PENDING; 421 return ERR_IO_PENDING;
376 } 422 }
377 423
378 int UDPSocketWin::DoBind(const IPEndPoint& address) { 424 int UDPSocketWin::DoBind(const IPEndPoint& address) {
379 struct sockaddr_storage addr_storage; 425 struct sockaddr_storage addr_storage;
380 size_t addr_len = sizeof(addr_storage); 426 size_t addr_len = sizeof(addr_storage);
381 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); 427 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage);
(...skipping 10 matching lines...) Expand all
392 IPAddressNumber ip(address.address().size()); 438 IPAddressNumber ip(address.address().size());
393 439
394 for (int i = 0; i < kBindRetries; ++i) { 440 for (int i = 0; i < kBindRetries; ++i) {
395 int rv = DoBind(IPEndPoint(ip, rand_int_cb_.Run(kPortStart, kPortEnd))); 441 int rv = DoBind(IPEndPoint(ip, rand_int_cb_.Run(kPortStart, kPortEnd)));
396 if (rv == OK || rv != ERR_ADDRESS_IN_USE) 442 if (rv == OK || rv != ERR_ADDRESS_IN_USE)
397 return rv; 443 return rv;
398 } 444 }
399 return DoBind(IPEndPoint(ip, 0)); 445 return DoBind(IPEndPoint(ip, 0));
400 } 446 }
401 447
448 bool UDPSocketWin::ReceiveAddressToIPEndpoint(IPEndPoint* address) const {
449 const struct sockaddr* addr =
450 reinterpret_cast<const struct sockaddr*>(&recv_addr_storage_);
451 return address->FromSockAddr(addr, recv_addr_len_);
452 }
453
402 } // namespace net 454 } // namespace net
OLDNEW
« net/udp/udp_socket_libevent.cc ('K') | « net/udp/udp_socket_win.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698