| 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 "net/curvecp/client_packetizer.h" | 5 #include "net/curvecp/client_packetizer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "net/base/io_buffer.h" | 8 #include "net/base/io_buffer.h" |
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 #include "net/base/sys_addrinfo.h" | |
| 11 #include "net/curvecp/protocol.h" | 10 #include "net/curvecp/protocol.h" |
| 12 #include "net/udp/udp_client_socket.h" | 11 #include "net/udp/udp_client_socket.h" |
| 13 | 12 |
| 14 namespace { | 13 namespace { |
| 15 | 14 |
| 16 const int kMaxHelloAttempts = 8; | 15 const int kMaxHelloAttempts = 8; |
| 17 const int kHelloTimeoutMs[kMaxHelloAttempts] = { | 16 const int kHelloTimeoutMs[kMaxHelloAttempts] = { |
| 18 1000, // 1 second, with 1.5x increase for each retry. | 17 1000, // 1 second, with 1.5x increase for each retry. |
| 19 1500, | 18 1500, |
| 20 2250, | 19 2250, |
| 21 3375, | 20 3375, |
| 22 5063, | 21 5063, |
| 23 7594, | 22 7594, |
| 24 11391, | 23 11391, |
| 25 17086, | 24 17086, |
| 26 }; | 25 }; |
| 27 | 26 |
| 28 } // namespace | 27 } // namespace |
| 29 | 28 |
| 30 namespace net { | 29 namespace net { |
| 31 | 30 |
| 32 ClientPacketizer::ClientPacketizer() | 31 ClientPacketizer::ClientPacketizer() |
| 33 : Packetizer(), | 32 : Packetizer(), |
| 34 next_state_(NONE), | 33 next_state_(NONE), |
| 35 listener_(NULL), | 34 listener_(NULL), |
| 36 current_address_(NULL), | 35 current_address_index_(-1), |
| 37 hello_attempts_(0), | 36 hello_attempts_(0), |
| 38 initiate_sent_(false), | 37 initiate_sent_(false), |
| 39 ALLOW_THIS_IN_INITIALIZER_LIST( | 38 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 40 io_callback_(base::Bind(&ClientPacketizer::OnIOComplete, | 39 io_callback_(base::Bind(&ClientPacketizer::OnIOComplete, |
| 41 base::Unretained(this)))), | 40 base::Unretained(this)))), |
| 42 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 41 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 43 // TODO(mbelshe): Initialize our keys and such properly. | 42 // TODO(mbelshe): Initialize our keys and such properly. |
| 44 // for now we use random values to keep them unique. | 43 // for now we use random values to keep them unique. |
| 45 for (int i = 0; i < 32; ++i) | 44 for (int i = 0; i < 32; ++i) |
| 46 shortterm_public_key_[i] = rand() % 26 + 'a'; | 45 shortterm_public_key_[i] = rand() % 26 + 'a'; |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 DCHECK(!user_callback_.is_null()); | 282 DCHECK(!user_callback_.is_null()); |
| 284 | 283 |
| 285 CompletionCallback callback = user_callback_; | 284 CompletionCallback callback = user_callback_; |
| 286 user_callback_.Reset(); | 285 user_callback_.Reset(); |
| 287 callback.Run(result); | 286 callback.Run(result); |
| 288 } | 287 } |
| 289 | 288 |
| 290 int ClientPacketizer::ConnectNextAddress() { | 289 int ClientPacketizer::ConnectNextAddress() { |
| 291 // TODO(mbelshe): plumb Netlog information | 290 // TODO(mbelshe): plumb Netlog information |
| 292 | 291 |
| 293 DCHECK(addresses_.head()); | 292 DCHECK(!addresses_.empty()); |
| 294 | 293 |
| 295 socket_.reset(new UDPClientSocket(DatagramSocket::DEFAULT_BIND, | 294 socket_.reset(new UDPClientSocket(DatagramSocket::DEFAULT_BIND, |
| 296 RandIntCallback(), | 295 RandIntCallback(), |
| 297 NULL, | 296 NULL, |
| 298 NetLog::Source())); | 297 NetLog::Source())); |
| 299 | 298 |
| 300 // Rotate to next address in the list. | 299 // Rotate to next address in the list. Note, this sets it to 0 on first call. |
| 301 if (current_address_) | 300 current_address_index_ = (current_address_index_ + 1) % addresses_.size(); |
| 302 current_address_ = current_address_->ai_next; | |
| 303 if (!current_address_) | |
| 304 current_address_ = addresses_.head(); | |
| 305 | 301 |
| 306 IPEndPoint endpoint; | 302 int rv = socket_->Connect(addresses_[current_address_index_]); |
| 307 if (!endpoint.FromSockAddr(current_address_->ai_addr, | |
| 308 current_address_->ai_addrlen)) | |
| 309 return ERR_FAILED; | |
| 310 | |
| 311 int rv = socket_->Connect(endpoint); | |
| 312 DCHECK_NE(ERR_IO_PENDING, rv); | 303 DCHECK_NE(ERR_IO_PENDING, rv); |
| 313 | 304 |
| 314 return rv; | 305 return rv; |
| 315 } | 306 } |
| 316 | 307 |
| 317 void ClientPacketizer::StartHelloTimer(base::TimeDelta delay) { | 308 void ClientPacketizer::StartHelloTimer(base::TimeDelta delay) { |
| 318 MessageLoop::current()->PostDelayedTask( | 309 MessageLoop::current()->PostDelayedTask( |
| 319 FROM_HERE, | 310 FROM_HERE, |
| 320 base::Bind(&ClientPacketizer::OnHelloTimeout, weak_factory_.GetWeakPtr()), | 311 base::Bind(&ClientPacketizer::OnHelloTimeout, weak_factory_.GetWeakPtr()), |
| 321 delay); | 312 delay); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 return rv; | 372 return rv; |
| 382 } | 373 } |
| 383 | 374 |
| 384 void ClientPacketizer::OnIOComplete(int result) { | 375 void ClientPacketizer::OnIOComplete(int result) { |
| 385 int rv = DoLoop(result); | 376 int rv = DoLoop(result); |
| 386 if (rv != ERR_IO_PENDING) | 377 if (rv != ERR_IO_PENDING) |
| 387 DoCallback(rv); | 378 DoCallback(rv); |
| 388 } | 379 } |
| 389 | 380 |
| 390 } // namespace net | 381 } // namespace net |
| OLD | NEW |