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.
5 #ifndef CONTENT_RENDERER_P2P_SOCKET_CLIENT_H_
6 #define CONTENT_RENDERER_P2P_SOCKET_CLIENT_H_
10 #include "base/memory/ref_counted.h"
11 #include "content/common/p2p_sockets.h"
12 #include "net/base/ip_endpoint.h"
15 class MessageLoopProxy;
20 class P2PSocketDispatcher;
22 // P2P socket that rountes all calls over IPC.
24 // The object runs on two threads: IPC thread and delegate thread. The
25 // IPC thread is used to interact with P2PSocketDispatcher. All
26 // callbacks to the user of this class are called on the delegate
27 // thread which is specified in Init().
28 class P2PSocketClient : public base::RefCountedThreadSafe<P2PSocketClient> {
30 // Delegate is called on the the same thread on which P2PSocketCLient is
34 virtual ~Delegate() { }
36 virtual void OnOpen(const net::IPEndPoint& address) = 0;
37 virtual void OnIncomingTcpConnection(const net::IPEndPoint& address,
38 P2PSocketClient* client) = 0;
39 virtual void OnSendComplete() = 0;
40 virtual void OnError() = 0;
41 virtual void OnDataReceived(const net::IPEndPoint& address,
42 const std::vector<char>& data) = 0;
45 explicit P2PSocketClient(P2PSocketDispatcher* dispatcher);
47 // Initialize socket of the specified |type| and connected to the
48 // specified |address|. |address| matters only when |type| is set to
49 // P2P_SOCKET_TCP_CLIENT.
50 void Init(P2PSocketType type,
51 const net::IPEndPoint& local_address,
52 const net::IPEndPoint& remote_address,
55 // Send the |data| to the |address|.
56 void Send(const net::IPEndPoint& address, const std::vector<char>& data);
58 // Send the |data| to the |address| using Differentiated Services Code Point
60 void SendWithDscp(const net::IPEndPoint& address,
61 const std::vector<char>& data,
62 net::DiffServCodePoint dscp);
64 // Must be called before the socket is destroyed. The delegate may
65 // not be called after |closed_task| is executed.
68 int socket_id() const { return socket_id_; }
70 void set_delegate(Delegate* delegate);
81 friend class P2PSocketDispatcher;
84 friend class base::RefCountedThreadSafe<P2PSocketClient>;
86 virtual ~P2PSocketClient();
88 // Message handlers that run on IPC thread.
89 void OnSocketCreated(const net::IPEndPoint& address);
90 void OnIncomingTcpConnection(const net::IPEndPoint& address);
91 void OnSendComplete(int packet_id);
92 void OnSendComplete();
94 void OnDataReceived(const net::IPEndPoint& address,
95 const std::vector<char>& data);
97 // Proxy methods that deliver messages to the delegate thread.
98 void DeliverOnSocketCreated(const net::IPEndPoint& address);
99 void DeliverOnIncomingTcpConnection(
100 const net::IPEndPoint& address,
101 scoped_refptr<P2PSocketClient> new_client);
102 void DeliverOnSendComplete();
103 void DeliverOnError();
104 void DeliverOnDataReceived(const net::IPEndPoint& address,
105 const std::vector<char>& data);
107 // Scheduled on the IPC thread to finish initialization.
108 void DoInit(P2PSocketType type,
109 const net::IPEndPoint& local_address,
110 const net::IPEndPoint& remote_address);
112 // Scheduled on the IPC thread to finish closing the connection.
115 // Called by the dispatcher when it is destroyed.
118 P2PSocketDispatcher* dispatcher_;
119 scoped_refptr<base::MessageLoopProxy> ipc_message_loop_;
120 scoped_refptr<base::MessageLoopProxy> delegate_message_loop_;
125 // These two fields are used to identify packets for tracing.
126 uint32 random_socket_id_;
127 uint32 next_packet_id_;
129 DISALLOW_COPY_AND_ASSIGN(P2PSocketClient);
132 } // namespace content
134 #endif // CONTENT_RENDERER_P2P_SOCKET_CLIENT_H_