Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / p2p / socket_host_tcp.h
1 // Copyright (c) 2011 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 CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_H_
7
8 #include <queue>
9 #include <vector>
10
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "content/browser/renderer_host/p2p/socket_host.h"
16 #include "content/common/p2p_socket_type.h"
17 #include "net/base/completion_callback.h"
18 #include "net/base/ip_endpoint.h"
19
20 namespace net {
21 class DrainableIOBuffer;
22 class GrowableIOBuffer;
23 class StreamSocket;
24 class URLRequestContextGetter;
25 }  // namespace net
26
27 namespace content {
28
29 class CONTENT_EXPORT P2PSocketHostTcpBase : public P2PSocketHost {
30  public:
31   P2PSocketHostTcpBase(IPC::Sender* message_sender,
32                        int socket_id,
33                        P2PSocketType type,
34                        net::URLRequestContextGetter* url_context);
35   virtual ~P2PSocketHostTcpBase();
36
37   bool InitAccepted(const net::IPEndPoint& remote_address,
38                     net::StreamSocket* socket);
39
40   // P2PSocketHost overrides.
41   virtual bool Init(const net::IPEndPoint& local_address,
42                     const P2PHostAndIPEndPoint& remote_address) OVERRIDE;
43   virtual void Send(const net::IPEndPoint& to,
44                     const std::vector<char>& data,
45                     const rtc::PacketOptions& options,
46                     uint64 packet_id) OVERRIDE;
47   virtual P2PSocketHost* AcceptIncomingTcpConnection(
48       const net::IPEndPoint& remote_address, int id) OVERRIDE;
49   virtual bool SetOption(P2PSocketOption option, int value) OVERRIDE;
50
51  protected:
52   // Derived classes will provide the implementation.
53   virtual int ProcessInput(char* input, int input_len) = 0;
54   virtual void DoSend(const net::IPEndPoint& to,
55                       const std::vector<char>& data,
56                       const rtc::PacketOptions& options) = 0;
57
58   void WriteOrQueue(scoped_refptr<net::DrainableIOBuffer>& buffer);
59   void OnPacket(const std::vector<char>& data);
60   void OnError();
61
62  private:
63   friend class P2PSocketHostTcpTestBase;
64   friend class P2PSocketHostTcpServerTest;
65
66   // SSL/TLS connection functions.
67   void StartTls();
68   void ProcessTlsSslConnectDone(int status);
69
70   void DidCompleteRead(int result);
71   void DoRead();
72
73   void DoWrite();
74   void HandleWriteResult(int result);
75
76   // Callbacks for Connect(), Read() and Write().
77   void OnConnected(int result);
78   void OnRead(int result);
79   void OnWritten(int result);
80
81   // Helper method to send socket create message and start read.
82   void OnOpen();
83   void DoSendSocketCreateMsg();
84
85   P2PHostAndIPEndPoint remote_address_;
86
87   scoped_ptr<net::StreamSocket> socket_;
88   scoped_refptr<net::GrowableIOBuffer> read_buffer_;
89   std::queue<scoped_refptr<net::DrainableIOBuffer> > write_queue_;
90   scoped_refptr<net::DrainableIOBuffer> write_buffer_;
91
92   bool write_pending_;
93
94   bool connected_;
95   P2PSocketType type_;
96   scoped_refptr<net::URLRequestContextGetter> url_context_;
97
98   DISALLOW_COPY_AND_ASSIGN(P2PSocketHostTcpBase);
99 };
100
101 class CONTENT_EXPORT P2PSocketHostTcp : public P2PSocketHostTcpBase {
102  public:
103   P2PSocketHostTcp(IPC::Sender* message_sender,
104                    int socket_id,
105                    P2PSocketType type,
106                    net::URLRequestContextGetter* url_context);
107   virtual ~P2PSocketHostTcp();
108
109  protected:
110   virtual int ProcessInput(char* input, int input_len) OVERRIDE;
111   virtual void DoSend(const net::IPEndPoint& to,
112                       const std::vector<char>& data,
113                       const rtc::PacketOptions& options) OVERRIDE;
114  private:
115   DISALLOW_COPY_AND_ASSIGN(P2PSocketHostTcp);
116 };
117
118 // P2PSocketHostStunTcp class provides the framing of STUN messages when used
119 // with TURN. These messages will not have length at front of the packet and
120 // are padded to multiple of 4 bytes.
121 // Formatting of messages is defined in RFC5766.
122 class CONTENT_EXPORT P2PSocketHostStunTcp : public P2PSocketHostTcpBase {
123  public:
124   P2PSocketHostStunTcp(IPC::Sender* message_sender,
125                        int socket_id,
126                        P2PSocketType type,
127                        net::URLRequestContextGetter* url_context);
128
129   virtual ~P2PSocketHostStunTcp();
130
131  protected:
132   virtual int ProcessInput(char* input, int input_len) OVERRIDE;
133   virtual void DoSend(const net::IPEndPoint& to,
134                       const std::vector<char>& data,
135                       const rtc::PacketOptions& options) OVERRIDE;
136  private:
137   int GetExpectedPacketSize(const char* data, int len, int* pad_bytes);
138
139   DISALLOW_COPY_AND_ASSIGN(P2PSocketHostStunTcp);
140 };
141
142
143 }  // namespace content
144
145 #endif  // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_H_