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.
5 #ifndef CONTENT_RENDERER_P2P_SOCKET_CLIENT_IMPL_H_
6 #define CONTENT_RENDERER_P2P_SOCKET_CLIENT_IMPL_H_
10 #include "base/memory/ref_counted.h"
11 #include "content/common/p2p_socket_type.h"
12 #include "content/renderer/p2p/socket_client.h"
13 #include "net/base/ip_endpoint.h"
16 class MessageLoopProxy
;
22 class P2PSocketDispatcher
;
24 // P2P socket that routes all calls over IPC.
26 // The object runs on two threads: IPC thread and delegate thread. The
27 // IPC thread is used to interact with P2PSocketDispatcher. All
28 // callbacks to the user of this class are called on the delegate
29 // thread which is specified in Init().
30 class P2PSocketClientImpl
: public P2PSocketClient
{
32 explicit P2PSocketClientImpl(P2PSocketDispatcher
* dispatcher
);
34 // Initialize socket of the specified |type| and connected to the
35 // specified |address|. |address| matters only when |type| is set to
36 // P2P_SOCKET_TCP_CLIENT.
37 virtual void Init(P2PSocketType type
,
38 const net::IPEndPoint
& local_address
,
39 const P2PHostAndIPEndPoint
& remote_address
,
40 P2PSocketClientDelegate
* delegate
);
42 // Send the |data| to the |address|.
43 virtual void Send(const net::IPEndPoint
& address
,
44 const std::vector
<char>& data
) OVERRIDE
;
46 // Send the |data| to the |address| using Differentiated Services Code Point
48 virtual void SendWithDscp(const net::IPEndPoint
& address
,
49 const std::vector
<char>& data
,
50 const talk_base::PacketOptions
& options
) OVERRIDE
;
52 // Setting socket options.
53 virtual void SetOption(P2PSocketOption option
, int value
) OVERRIDE
;
55 // Must be called before the socket is destroyed. The delegate may
56 // not be called after |closed_task| is executed.
57 virtual void Close() OVERRIDE
;
59 virtual int GetSocketID() const OVERRIDE
;
61 virtual void SetDelegate(P2PSocketClientDelegate
* delegate
) OVERRIDE
;
72 friend class P2PSocketDispatcher
;
74 virtual ~P2PSocketClientImpl();
76 // Message handlers that run on IPC thread.
77 void OnSocketCreated(const net::IPEndPoint
& address
);
78 void OnIncomingTcpConnection(const net::IPEndPoint
& address
);
79 void OnSendComplete(int packet_id
);
80 void OnSendComplete();
82 void OnDataReceived(const net::IPEndPoint
& address
,
83 const std::vector
<char>& data
,
84 const base::TimeTicks
& timestamp
);
86 // Proxy methods that deliver messages to the delegate thread.
87 void DeliverOnSocketCreated(const net::IPEndPoint
& address
);
88 void DeliverOnIncomingTcpConnection(
89 const net::IPEndPoint
& address
,
90 scoped_refptr
<P2PSocketClient
> new_client
);
91 void DeliverOnSendComplete();
92 void DeliverOnError();
93 void DeliverOnDataReceived(const net::IPEndPoint
& address
,
94 const std::vector
<char>& data
,
95 const base::TimeTicks
& timestamp
);
97 // Scheduled on the IPC thread to finish initialization.
98 void DoInit(P2PSocketType type
,
99 const net::IPEndPoint
& local_address
,
100 const P2PHostAndIPEndPoint
& remote_address
);
102 // Scheduled on the IPC thread to finish closing the connection.
105 // Called by the dispatcher when it is destroyed.
108 P2PSocketDispatcher
* dispatcher_
;
109 scoped_refptr
<base::MessageLoopProxy
> ipc_message_loop_
;
110 scoped_refptr
<base::MessageLoopProxy
> delegate_message_loop_
;
112 P2PSocketClientDelegate
* delegate_
;
115 // These two fields are used to identify packets for tracing.
116 uint32 random_socket_id_
;
117 uint32 next_packet_id_
;
119 DISALLOW_COPY_AND_ASSIGN(P2PSocketClientImpl
);
122 } // namespace content
124 #endif // CONTENT_RENDERER_P2P_SOCKET_CLIENT_IMPL_H_