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 rtc::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
& local_address
,
78 const net::IPEndPoint
& remote_address
);
79 void OnIncomingTcpConnection(const net::IPEndPoint
& address
);
80 void OnSendComplete(int packet_id
);
81 void OnSendComplete();
83 void OnDataReceived(const net::IPEndPoint
& address
,
84 const std::vector
<char>& data
,
85 const base::TimeTicks
& timestamp
);
87 // Proxy methods that deliver messages to the delegate thread.
88 void DeliverOnSocketCreated(const net::IPEndPoint
& local_address
,
89 const net::IPEndPoint
& remote_address
);
90 void DeliverOnIncomingTcpConnection(
91 const net::IPEndPoint
& address
,
92 scoped_refptr
<P2PSocketClient
> new_client
);
93 void DeliverOnSendComplete();
94 void DeliverOnError();
95 void DeliverOnDataReceived(const net::IPEndPoint
& address
,
96 const std::vector
<char>& data
,
97 const base::TimeTicks
& timestamp
);
99 // Scheduled on the IPC thread to finish initialization.
100 void DoInit(P2PSocketType type
,
101 const net::IPEndPoint
& local_address
,
102 const P2PHostAndIPEndPoint
& remote_address
);
104 // Scheduled on the IPC thread to finish closing the connection.
107 // Called by the dispatcher when it is destroyed.
110 P2PSocketDispatcher
* dispatcher_
;
111 scoped_refptr
<base::MessageLoopProxy
> ipc_message_loop_
;
112 scoped_refptr
<base::MessageLoopProxy
> delegate_message_loop_
;
114 P2PSocketClientDelegate
* delegate_
;
117 // These two fields are used to identify packets for tracing.
118 uint32 random_socket_id_
;
119 uint32 next_packet_id_
;
121 DISALLOW_COPY_AND_ASSIGN(P2PSocketClientImpl
);
124 } // namespace content
126 #endif // CONTENT_RENDERER_P2P_SOCKET_CLIENT_IMPL_H_