• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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_RENDERER_P2P_SOCKET_CLIENT_IMPL_H_
6 #define CONTENT_RENDERER_P2P_SOCKET_CLIENT_IMPL_H_
7 
8 #include <vector>
9 
10 #include "base/memory/ref_counted.h"
11 #include "content/public/common/p2p_socket_type.h"
12 #include "content/public/renderer/p2p_socket_client.h"
13 #include "net/base/ip_endpoint.h"
14 
15 namespace base {
16 class MessageLoopProxy;
17 }  // namespace base
18 
19 namespace content {
20 
21 class P2PSocketDispatcher;
22 
23 // P2P socket that routes all calls over IPC.
24 //
25 // The object runs on two threads: IPC thread and delegate thread. The
26 // IPC thread is used to interact with P2PSocketDispatcher. All
27 // callbacks to the user of this class are called on the delegate
28 // thread which is specified in Init().
29 class P2PSocketClientImpl : public P2PSocketClient {
30  public:
31   explicit P2PSocketClientImpl(P2PSocketDispatcher* dispatcher);
32 
33   // Initialize socket of the specified |type| and connected to the
34   // specified |address|. |address| matters only when |type| is set to
35   // P2P_SOCKET_TCP_CLIENT.
36   virtual void Init(P2PSocketType type,
37                     const net::IPEndPoint& local_address,
38                     const net::IPEndPoint& remote_address,
39                     P2PSocketClientDelegate* delegate);
40 
41   // Send the |data| to the |address|.
42   virtual void Send(const net::IPEndPoint& address,
43                     const std::vector<char>& data) OVERRIDE;
44 
45   // Send the |data| to the |address| using Differentiated Services Code Point
46   // |dscp|.
47   virtual void SendWithDscp(const net::IPEndPoint& address,
48                             const std::vector<char>& data,
49                             net::DiffServCodePoint dscp) OVERRIDE;
50 
51   // Must be called before the socket is destroyed. The delegate may
52   // not be called after |closed_task| is executed.
53   virtual void Close() OVERRIDE;
54 
55   virtual int GetSocketID() const OVERRIDE;
56 
57   virtual void SetDelegate(P2PSocketClientDelegate* delegate) OVERRIDE;
58 
59  private:
60   enum State {
61     STATE_UNINITIALIZED,
62     STATE_OPENING,
63     STATE_OPEN,
64     STATE_CLOSED,
65     STATE_ERROR,
66   };
67 
68   friend class P2PSocketDispatcher;
69 
70   virtual ~P2PSocketClientImpl();
71 
72   // Message handlers that run on IPC thread.
73   void OnSocketCreated(const net::IPEndPoint& address);
74   void OnIncomingTcpConnection(const net::IPEndPoint& address);
75   void OnSendComplete(int packet_id);
76   void OnSendComplete();
77   void OnError();
78   void OnDataReceived(const net::IPEndPoint& address,
79                       const std::vector<char>& data,
80                       const base::TimeTicks& timestamp);
81 
82   // Proxy methods that deliver messages to the delegate thread.
83   void DeliverOnSocketCreated(const net::IPEndPoint& address);
84   void DeliverOnIncomingTcpConnection(
85       const net::IPEndPoint& address,
86       scoped_refptr<P2PSocketClient> new_client);
87   void DeliverOnSendComplete();
88   void DeliverOnError();
89   void DeliverOnDataReceived(const net::IPEndPoint& address,
90                              const std::vector<char>& data,
91                              const base::TimeTicks& timestamp);
92 
93   // Scheduled on the IPC thread to finish initialization.
94   void DoInit(P2PSocketType type,
95               const net::IPEndPoint& local_address,
96               const net::IPEndPoint& remote_address);
97 
98   // Scheduled on the IPC thread to finish closing the connection.
99   void DoClose();
100 
101   // Called by the dispatcher when it is destroyed.
102   void Detach();
103 
104   P2PSocketDispatcher* dispatcher_;
105   scoped_refptr<base::MessageLoopProxy> ipc_message_loop_;
106   scoped_refptr<base::MessageLoopProxy> delegate_message_loop_;
107   int socket_id_;
108   P2PSocketClientDelegate* delegate_;
109   State state_;
110 
111   // These two fields are used to identify packets for tracing.
112   uint32 random_socket_id_;
113   uint32 next_packet_id_;
114 
115   DISALLOW_COPY_AND_ASSIGN(P2PSocketClientImpl);
116 };
117 
118 }  // namespace content
119 
120 #endif  // CONTENT_RENDERER_P2P_SOCKET_CLIENT_IMPL_H_
121