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.
5 #ifndef COMPONENTS_DEVTOOLS_BRIDGE_SOCKET_TUNNEL_CONNECTION_H_
6 #define COMPONENTS_DEVTOOLS_BRIDGE_SOCKET_TUNNEL_CONNECTION_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
15 class DrainableIOBuffer
;
16 class GrowableIOBuffer
;
17 class IOBufferWithSize
;
21 namespace devtools_bridge
{
24 * Abstract base class for SocketTunnelServer/Client connection.
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).
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.
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.
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).
46 class SocketTunnelConnection
{
58 static const int kMaxConnectionCount
= 64;
60 static const int kMaxPacketSizeBytes
= 1024 * 4;
61 static const int kControlPacketSizeBytes
= 3;
63 static const int kControlConnectionId
= 0;
65 static const int kMinConnectionId
= 1;
66 static const int kMaxConnectionId
=
67 kMinConnectionId
+ kMaxConnectionCount
- 1;
69 void Write(scoped_refptr
<net::IOBufferWithSize
> chunk
);
73 SocketTunnelConnection(int index
);
74 ~SocketTunnelConnection();
78 // |buffer| length must be kControlPacketSizeBytes.
79 void BuildControlPacket(char* buffer
, int op_code
);
81 virtual net::StreamSocket
* socket() = 0;
82 virtual void OnDataPacketRead(const void* data
, size_t length
) = 0;
83 virtual void OnReadError(int error
) = 0;
87 void OnWriteComplete(int result
);
88 void OnReadComplete(int result
);
90 std::deque
<scoped_refptr
<net::IOBufferWithSize
> > buffer_
;
91 scoped_refptr
<net::DrainableIOBuffer
> current_
;
92 scoped_refptr
<net::GrowableIOBuffer
> read_buffer_
;
94 DISALLOW_COPY_AND_ASSIGN(SocketTunnelConnection
);
97 } // namespace devtools_bridge
99 #endif // COMPONENTS_DEVTOOLS_BRIDGE_SOCKET_TUNNEL_CONNECTION_H_