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
103
104
105
106
107
108
|
// 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/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_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,
OldCompletionCallback* callback);
int Connect(const AddressList& server,
Packetizer::Listener* listener,
const CompletionCallback& callback);
// Packetizer implementation.
virtual int SendMessage(ConnectionKey key,
const char* data,
size_t length,
OldCompletionCallback* callback) OVERRIDE;
virtual void Close(ConnectionKey key) OVERRIDE;
virtual int GetPeerAddress(IPEndPoint* endpoint) const OVERRIDE;
virtual int max_message_payload() const OVERRIDE;
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_;
OldCompletionCallback* old_user_callback_;
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 Initiate Packet was sent.
scoped_refptr<IOBuffer> read_buffer_; // Buffer for internal reads.
uchar shortterm_public_key_[32];
OldCompletionCallbackImpl<ClientPacketizer> io_callback_;
base::WeakPtrFactory<ClientPacketizer> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ClientPacketizer);
};
} // namespace net
#endif // NET_CURVECP_CLIENT_PACKETIZER_H_
|