OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef COMPONENTS_DEVTOOLS_BRIDGE_SOCKET_TUNNEL_CONNECTION_H_ | |
6 #define COMPONENTS_DEVTOOLS_BRIDGE_SOCKET_TUNNEL_CONNECTION_H_ | |
7 | |
8 #include <deque> | |
9 #include <string> | |
10 | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 | |
14 namespace net { | |
15 class DrainableIOBuffer; | |
16 class GrowableIOBuffer; | |
17 class IOBufferWithSize; | |
18 class StreamSocket; | |
19 } | |
20 | |
21 namespace devtools_bridge { | |
22 | |
23 /** | |
24 * Abstract base class for SocketTunnelServer/Client connection. | |
25 * | |
26 * Connection binds a pair of net::StreamSocket (or alike) through | |
27 * a data channel. SocketTunnel may handle up to kMaxConnectionCount | |
28 * simultaneous connection (DevTools can keep ~10 connection; | |
29 * other connections hang in unopened state; additional connections | |
30 * could help to deal with data channel latency). | |
31 * | |
32 * Client should create net::StreamListenSocket (or logical equivalent) | |
33 * and listen for incoming connection. When one comes it sends CLIENT_OPEN | |
34 * packet to the server. | |
35 * | |
36 * Server transforms client's packet to calls of net::StreamSocket. On | |
37 * CLIENT_OPEN it creates a socket and connects. If connection succeeds | |
38 * it sends back SERVER_OPEN_ACK. If it fails it sends SERVER_CLOSE. | |
39 * | |
40 * After SERVER_OPEN_ACK server may send SERVER_CLOSE any time (if the socket | |
41 * it connects to has closed on another side). If client closes the connection | |
42 * sending CLIENT_CLOSE server acknowledges it by sending SERVER_CLOSE. | |
43 * Client may reuse connection ID once it received SERVER_CLOSE (because | |
44 * data channel is ordered and reliable). | |
45 */ | |
46 class SocketTunnelConnection { | |
47 public: | |
48 enum ClientOpCode { | |
49 CLIENT_OPEN = 0, | |
50 CLIENT_CLOSE = 1 | |
51 }; | |
52 | |
53 enum ServerOpCode { | |
54 SERVER_OPEN_ACK = 0, | |
55 SERVER_CLOSE = 1 | |
56 }; | |
57 | |
58 static const int kMaxConnectionCount = 64; | |
59 | |
60 static const int kMaxPacketSizeBytes = 1024 * 4; | |
61 static const int kControlPacketSizeBytes = 3; | |
62 | |
63 static const int kControlConnectionId = 0; | |
64 | |
65 static const int kMinConnectionId = 1; | |
66 static const int kMaxConnectionId = | |
67 kMinConnectionId + kMaxConnectionCount - 1; | |
68 | |
69 void Write(scoped_refptr<net::IOBufferWithSize> chunk); | |
70 void ReadNextChunk(); | |
71 | |
72 protected: | |
73 SocketTunnelConnection(int index); | |
74 ~SocketTunnelConnection(); | |
75 | |
76 const int index_; | |
77 | |
78 // |buffer| length must be kControlPacketSizeBytes. | |
79 void BuildControlPacket(char* buffer, int op_code); | |
80 | |
81 virtual net::StreamSocket* socket() = 0; | |
82 virtual void OnDataPacketRead(const void* data, size_t length) = 0; | |
83 virtual void OnReadError(int error) = 0; | |
84 | |
85 private: | |
86 void WriteCurrent(); | |
87 void OnWriteComplete(int result); | |
88 void OnReadComplete(int result); | |
89 | |
90 std::deque<scoped_refptr<net::IOBufferWithSize> > buffer_; | |
91 scoped_refptr<net::DrainableIOBuffer> current_; | |
92 scoped_refptr<net::GrowableIOBuffer> read_buffer_; | |
93 | |
94 DISALLOW_COPY_AND_ASSIGN(SocketTunnelConnection); | |
95 }; | |
96 | |
97 } // namespace devtools_bridge | |
98 | |
99 #endif // COMPONENTS_DEVTOOLS_BRIDGE_SOCKET_TUNNEL_CONNECTION_H_ | |
OLD | NEW |