summaryrefslogtreecommitdiffstats
path: root/net/curvecp/client_packetizer.h
blob: ff2a4003b0c18d019278b0268dd196d65fd9a7a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_CURVECP_CLIENT_PACKETIZER_H_
#define NET_CURVECP_CLIENT_PACKETIZER_H_
#pragma once

#include "base/memory/scoped_ptr.h"
#include "base/task.h"
#include "net/base/address_list.h"
#include "net/base/completion_callback.h"
#include "net/curvecp/packetizer.h"
#include "net/curvecp/protocol.h"

namespace net {

class AddressList;
class IOBuffer;
class IPEndPoint;
class UDPClientSocket;

class ClientPacketizer : public Packetizer {
 public:
  ClientPacketizer();
  virtual ~ClientPacketizer();

  int Connect(const AddressList& server,
              Packetizer::Listener* listener,
              CompletionCallback* callback);

  // Packetizer methods
  virtual int SendMessage(ConnectionKey key,
                          const char* data,
                          size_t length,
                          CompletionCallback* callback);
  virtual void Close(ConnectionKey key);
  virtual int GetPeerAddress(IPEndPoint* endpoint) const;
  virtual int max_message_payload() const;

 private:
  enum StateType {
    NONE,                      // The initial state, before connect.
    LOOKUP_COOKIE,             // Looking up a cookie in the disk cache.
    LOOKUP_COOKIE_COMPLETE,    // The disk cache lookup is complete.
    SENDING_HELLO,             // Sending a Hello packet.
    SENDING_HELLO_COMPLETE,    // Hello packet has been sent.
    WAITING_COOKIE,            // Waiting for a Cookie packet.
    WAITING_COOKIE_COMPLETE,   // The Cookie packet has arrived.
    CONNECTED,                 // Connected
  };

  int DoLoop(int result);
  int DoLookupCookie();
  int DoLookupCookieComplete(int result);
  int DoSendingHello();
  int DoSendingHelloComplete(int result);
  int DoWaitingCookie();
  int DoWaitingCookieComplete(int result);
  int DoConnected(int result);

  void DoCallback(int result);

  // Connect to the next address in our list.
  int ConnectNextAddress();

  // We set a timeout for responses to the Hello message.
  void StartHelloTimer(int milliseconds);
  void RevokeHelloTimer();
  void OnHelloTimeout();   // Called when the Hello Timer fires.

  // Process the result of a Read operation.
  void ProcessRead(int bytes_read);

  // Read packets until an error occurs.
  int ReadPackets();

  // Callback when an internal IO is completed.
  void OnIOComplete(int result);

  StateType next_state_;
  scoped_ptr<UDPClientSocket> socket_;
  Packetizer::Listener* listener_;
  CompletionCallback* user_callback_;
  AddressList addresses_;
  const struct addrinfo* current_address_;
  int hello_attempts_;  // Number of attempts to send a Hello Packet.
  bool initiate_sent_;  // Indicates whether the Initialte Packet was sent.

  scoped_refptr<IOBuffer> read_buffer_;  // Buffer for interal reads.

  uchar shortterm_public_key_[32];

  CompletionCallbackImpl<ClientPacketizer> io_callback_;
  ScopedRunnableMethodFactory<ClientPacketizer> timers_factory_;

  DISALLOW_COPY_AND_ASSIGN(ClientPacketizer);
};

}  // namespace net

#endif  // NET_CURVECP_CLIENT_PACKETIZER_H_