| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_RENDERER_P2P_SOCKET_CLIENT_H_ | 5 #ifndef CONTENT_RENDERER_P2P_SOCKET_CLIENT_H_ |
| 6 #define CONTENT_RENDERER_P2P_SOCKET_CLIENT_H_ | 6 #define CONTENT_RENDERER_P2P_SOCKET_CLIENT_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "content/common/p2p_sockets.h" | 11 #include "content/public/common/p2p_sockets.h" |
| 12 #include "content/public/renderer/socket_client.h" |
| 12 #include "net/base/ip_endpoint.h" | 13 #include "net/base/ip_endpoint.h" |
| 13 | 14 |
| 14 namespace base { | 15 namespace base { |
| 15 class MessageLoopProxy; | 16 class MessageLoopProxy; |
| 16 } // namespace base | 17 } // namespace base |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 | 20 |
| 20 class P2PSocketDispatcher; | 21 class P2PSocketDispatcher; |
| 21 | 22 |
| 22 // P2P socket that rountes all calls over IPC. | 23 // P2P socket that rountes all calls over IPC. |
| 23 // | 24 // |
| 24 // The object runs on two threads: IPC thread and delegate thread. The | 25 // The object runs on two threads: IPC thread and delegate thread. The |
| 25 // IPC thread is used to interact with P2PSocketDispatcher. All | 26 // IPC thread is used to interact with P2PSocketDispatcher. All |
| 26 // callbacks to the user of this class are called on the delegate | 27 // callbacks to the user of this class are called on the delegate |
| 27 // thread which is specified in Init(). | 28 // thread which is specified in Init(). |
| 28 class P2PSocketClient : public base::RefCountedThreadSafe<P2PSocketClient> { | 29 class P2PSocketClientImpl : public P2PSocketClient { |
| 29 public: | 30 public: |
| 30 // Delegate is called on the the same thread on which P2PSocketCLient is | 31 explicit P2PSocketClientImpl(P2PSocketDispatcher* dispatcher); |
| 31 // created. | |
| 32 class Delegate { | |
| 33 public: | |
| 34 virtual ~Delegate() { } | |
| 35 | |
| 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; | |
| 43 }; | |
| 44 | |
| 45 explicit P2PSocketClient(P2PSocketDispatcher* dispatcher); | |
| 46 | 32 |
| 47 // Initialize socket of the specified |type| and connected to the | 33 // Initialize socket of the specified |type| and connected to the |
| 48 // specified |address|. |address| matters only when |type| is set to | 34 // specified |address|. |address| matters only when |type| is set to |
| 49 // P2P_SOCKET_TCP_CLIENT. | 35 // P2P_SOCKET_TCP_CLIENT. |
| 50 void Init(P2PSocketType type, | 36 virtual void Init(P2PSocketType type, |
| 51 const net::IPEndPoint& local_address, | 37 const net::IPEndPoint& local_address, |
| 52 const net::IPEndPoint& remote_address, | 38 const net::IPEndPoint& remote_address, |
| 53 Delegate* delegate); | 39 Delegate* delegate) OVERRIDE; |
| 54 | 40 |
| 55 // Send the |data| to the |address|. | 41 // Send the |data| to the |address|. |
| 56 void Send(const net::IPEndPoint& address, const std::vector<char>& data); | 42 virtual void Send(const net::IPEndPoint& address, |
| 43 const std::vector<char>& data) OVERRIDE; |
| 57 | 44 |
| 58 // Send the |data| to the |address| using Differentiated Services Code Point | 45 // Send the |data| to the |address| using Differentiated Services Code Point |
| 59 // |dscp|. | 46 // |dscp|. |
| 60 void SendWithDscp(const net::IPEndPoint& address, | 47 virtual void SendWithDscp(const net::IPEndPoint& address, |
| 61 const std::vector<char>& data, | 48 const std::vector<char>& data, |
| 62 net::DiffServCodePoint dscp); | 49 net::DiffServCodePoint dscp) OVERRIDE; |
| 63 | 50 |
| 64 // Must be called before the socket is destroyed. The delegate may | 51 // Must be called before the socket is destroyed. The delegate may |
| 65 // not be called after |closed_task| is executed. | 52 // not be called after |closed_task| is executed. |
| 66 void Close(); | 53 virtual void Close() OVERRIDE; |
| 67 | 54 |
| 68 int socket_id() const { return socket_id_; } | 55 virtual int socket_id() const OVERRIDE; |
| 69 | 56 |
| 70 void set_delegate(Delegate* delegate); | 57 virtual void set_delegate(Delegate* delegate) OVERRIDE; |
| 71 | 58 |
| 72 private: | 59 private: |
| 73 enum State { | 60 enum State { |
| 74 STATE_UNINITIALIZED, | 61 STATE_UNINITIALIZED, |
| 75 STATE_OPENING, | 62 STATE_OPENING, |
| 76 STATE_OPEN, | 63 STATE_OPEN, |
| 77 STATE_CLOSED, | 64 STATE_CLOSED, |
| 78 STATE_ERROR, | 65 STATE_ERROR, |
| 79 }; | 66 }; |
| 80 | 67 |
| 81 friend class P2PSocketDispatcher; | 68 friend class P2PSocketDispatcher; |
| 82 | 69 |
| 83 // Calls destructor. | 70 virtual ~P2PSocketClientImpl(); |
| 84 friend class base::RefCountedThreadSafe<P2PSocketClient>; | |
| 85 | |
| 86 virtual ~P2PSocketClient(); | |
| 87 | 71 |
| 88 // Message handlers that run on IPC thread. | 72 // Message handlers that run on IPC thread. |
| 89 void OnSocketCreated(const net::IPEndPoint& address); | 73 void OnSocketCreated(const net::IPEndPoint& address); |
| 90 void OnIncomingTcpConnection(const net::IPEndPoint& address); | 74 void OnIncomingTcpConnection(const net::IPEndPoint& address); |
| 91 void OnSendComplete(int packet_id); | 75 void OnSendComplete(int packet_id); |
| 92 void OnSendComplete(); | 76 void OnSendComplete(); |
| 93 void OnError(); | 77 void OnError(); |
| 94 void OnDataReceived(const net::IPEndPoint& address, | 78 void OnDataReceived(const net::IPEndPoint& address, |
| 95 const std::vector<char>& data); | 79 const std::vector<char>& data); |
| 96 | 80 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 119 scoped_refptr<base::MessageLoopProxy> ipc_message_loop_; | 103 scoped_refptr<base::MessageLoopProxy> ipc_message_loop_; |
| 120 scoped_refptr<base::MessageLoopProxy> delegate_message_loop_; | 104 scoped_refptr<base::MessageLoopProxy> delegate_message_loop_; |
| 121 int socket_id_; | 105 int socket_id_; |
| 122 Delegate* delegate_; | 106 Delegate* delegate_; |
| 123 State state_; | 107 State state_; |
| 124 | 108 |
| 125 // These two fields are used to identify packets for tracing. | 109 // These two fields are used to identify packets for tracing. |
| 126 uint32 random_socket_id_; | 110 uint32 random_socket_id_; |
| 127 uint32 next_packet_id_; | 111 uint32 next_packet_id_; |
| 128 | 112 |
| 129 DISALLOW_COPY_AND_ASSIGN(P2PSocketClient); | 113 DISALLOW_COPY_AND_ASSIGN(P2PSocketClientImpl); |
| 130 }; | 114 }; |
| 131 | 115 |
| 132 } // namespace content | 116 } // namespace content |
| 133 | 117 |
| 134 #endif // CONTENT_RENDERER_P2P_SOCKET_CLIENT_H_ | 118 #endif // CONTENT_RENDERER_P2P_SOCKET_CLIENT_H_ |
| OLD | NEW |