Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / p2p / socket_client_impl.h
blob868f6121f83e51a1fcdbfdb51cd21fe87336304e
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_
8 #include <vector>
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"
15 namespace base {
16 class MessageLoopProxy;
17 class TimeTicks;
18 } // namespace base
20 namespace content {
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 {
31 public:
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| using Differentiated Services Code Point
43 // |dscp|. Return value is the unique packet_id for this packet.
44 uint64_t Send(const net::IPEndPoint& address,
45 const std::vector<char>& data,
46 const rtc::PacketOptions& options) override;
48 // Setting socket options.
49 void SetOption(P2PSocketOption option, int value) override;
51 // Must be called before the socket is destroyed. The delegate may
52 // not be called after |closed_task| is executed.
53 void Close() override;
55 int GetSocketID() const override;
57 void SetDelegate(P2PSocketClientDelegate* delegate) override;
59 private:
60 enum State {
61 STATE_UNINITIALIZED,
62 STATE_OPENING,
63 STATE_OPEN,
64 STATE_CLOSED,
65 STATE_ERROR,
68 friend class P2PSocketDispatcher;
70 ~P2PSocketClientImpl() override;
72 // Message handlers that run on IPC thread.
73 void OnSocketCreated(const net::IPEndPoint& local_address,
74 const net::IPEndPoint& remote_address);
75 void OnIncomingTcpConnection(const net::IPEndPoint& address);
76 void OnSendComplete(const P2PSendPacketMetrics& send_metrics);
77 void OnError();
78 void OnDataReceived(const net::IPEndPoint& address,
79 const std::vector<char>& data,
80 const base::TimeTicks& timestamp);
82 // Proxy methods that deliver messages to the delegate thread.
83 void DeliverOnSocketCreated(const net::IPEndPoint& local_address,
84 const net::IPEndPoint& remote_address);
85 void DeliverOnIncomingTcpConnection(
86 const net::IPEndPoint& address,
87 scoped_refptr<P2PSocketClient> new_client);
88 void DeliverOnSendComplete(const P2PSendPacketMetrics& send_metrics);
89 void DeliverOnError();
90 void DeliverOnDataReceived(const net::IPEndPoint& address,
91 const std::vector<char>& data,
92 const base::TimeTicks& timestamp);
94 // Helper function to be called by Send to handle different threading
95 // condition.
96 void SendWithPacketId(const net::IPEndPoint& address,
97 const std::vector<char>& data,
98 const rtc::PacketOptions& options,
99 uint64_t packet_id);
101 // Scheduled on the IPC thread to finish initialization.
102 void DoInit(P2PSocketType type,
103 const net::IPEndPoint& local_address,
104 const P2PHostAndIPEndPoint& remote_address);
106 // Scheduled on the IPC thread to finish closing the connection.
107 void DoClose();
109 // Called by the dispatcher when it is destroyed.
110 void Detach();
112 P2PSocketDispatcher* dispatcher_;
113 scoped_refptr<base::MessageLoopProxy> ipc_message_loop_;
114 scoped_refptr<base::MessageLoopProxy> delegate_message_loop_;
115 int socket_id_;
116 P2PSocketClientDelegate* delegate_;
117 State state_;
119 // These two fields are used to identify packets for tracing.
120 uint32 random_socket_id_;
121 uint32 next_packet_id_;
123 DISALLOW_COPY_AND_ASSIGN(P2PSocketClientImpl);
126 } // namespace content
128 #endif // CONTENT_RENDERER_P2P_SOCKET_CLIENT_IMPL_H_