| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_H_ |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_H_ |
| 7 | 7 |
| 8 #include <queue> | 8 #include <queue> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "content/browser/renderer_host/p2p/socket_host.h" | 15 #include "content/browser/renderer_host/p2p/socket_host.h" |
| 16 #include "content/common/p2p_sockets.h" | 16 #include "content/common/p2p_sockets.h" |
| 17 #include "net/base/completion_callback.h" | 17 #include "net/base/completion_callback.h" |
| 18 #include "net/base/ip_endpoint.h" | 18 #include "net/base/ip_endpoint.h" |
| 19 | 19 |
| 20 namespace net { | 20 namespace net { |
| 21 class DrainableIOBuffer; | 21 class DrainableIOBuffer; |
| 22 class GrowableIOBuffer; | 22 class GrowableIOBuffer; |
| 23 class StreamSocket; | 23 class StreamSocket; |
| 24 class URLRequestContextGetter; |
| 24 } // namespace net | 25 } // namespace net |
| 25 | 26 |
| 26 namespace content { | 27 namespace content { |
| 27 | 28 |
| 28 class CONTENT_EXPORT P2PSocketHostTcpBase : public P2PSocketHost { | 29 class CONTENT_EXPORT P2PSocketHostTcpBase : public P2PSocketHost { |
| 29 public: | 30 public: |
| 30 P2PSocketHostTcpBase(IPC::Sender* message_sender, int id); | 31 P2PSocketHostTcpBase(IPC::Sender* message_sender, int id, |
| 32 net::URLRequestContextGetter* url_context); |
| 31 virtual ~P2PSocketHostTcpBase(); | 33 virtual ~P2PSocketHostTcpBase(); |
| 32 | 34 |
| 33 bool InitAccepted(const net::IPEndPoint& remote_address, | 35 bool InitAccepted(const net::IPEndPoint& remote_address, |
| 34 net::StreamSocket* socket); | 36 net::StreamSocket* socket); |
| 35 | 37 |
| 36 // P2PSocketHost overrides. | 38 // P2PSocketHost overrides. |
| 37 virtual bool Init(const net::IPEndPoint& local_address, | 39 virtual bool Init(const net::IPEndPoint& local_address, |
| 38 const net::IPEndPoint& remote_address) OVERRIDE; | 40 const net::IPEndPoint& remote_address) OVERRIDE; |
| 39 virtual void Send(const net::IPEndPoint& to, | 41 virtual void Send(const net::IPEndPoint& to, |
| 40 const std::vector<char>& data) OVERRIDE; | 42 const std::vector<char>& data) OVERRIDE; |
| 41 virtual P2PSocketHost* AcceptIncomingTcpConnection( | 43 virtual P2PSocketHost* AcceptIncomingTcpConnection( |
| 42 const net::IPEndPoint& remote_address, int id) OVERRIDE; | 44 const net::IPEndPoint& remote_address, int id) OVERRIDE; |
| 43 | 45 |
| 44 protected: | 46 protected: |
| 45 // Derived classes will provide the implementation. | 47 // Derived classes will provide the implementation. |
| 46 virtual int ProcessInput(char* input, int input_len) = 0; | 48 virtual int ProcessInput(char* input, int input_len) = 0; |
| 47 virtual void DoSend(const net::IPEndPoint& to, | 49 virtual void DoSend(const net::IPEndPoint& to, |
| 48 const std::vector<char>& data) = 0; | 50 const std::vector<char>& data) = 0; |
| 49 | 51 |
| 50 void WriteOrQueue(scoped_refptr<net::DrainableIOBuffer>& buffer); | 52 void WriteOrQueue(scoped_refptr<net::DrainableIOBuffer>& buffer); |
| 51 void OnPacket(const std::vector<char>& data); | 53 void OnPacket(const std::vector<char>& data); |
| 52 void OnError(); | 54 void OnError(); |
| 53 | 55 |
| 54 private: | 56 private: |
| 55 friend class P2PSocketHostTcpTest; | 57 friend class P2PSocketHostTcpTestBase; |
| 56 friend class P2PSocketHostTcpServerTest; | 58 friend class P2PSocketHostTcpServerTest; |
| 57 friend class P2PSocketHostStunTcpTest; | |
| 58 | 59 |
| 59 void DidCompleteRead(int result); | 60 void DidCompleteRead(int result); |
| 60 void DoRead(); | 61 void DoRead(); |
| 61 | 62 |
| 62 void DoWrite(); | 63 void DoWrite(); |
| 63 void HandleWriteResult(int result); | 64 void HandleWriteResult(int result); |
| 64 | 65 |
| 65 // Callbacks for Connect(), Read() and Write(). | 66 // Callbacks for Connect(), Read() and Write(). |
| 66 void OnConnected(int result); | 67 void OnConnected(int result); |
| 67 void OnRead(int result); | 68 void OnRead(int result); |
| 68 void OnWritten(int result); | 69 void OnWritten(int result); |
| 69 | 70 |
| 70 net::IPEndPoint remote_address_; | 71 net::IPEndPoint remote_address_; |
| 71 | 72 |
| 72 scoped_ptr<net::StreamSocket> socket_; | 73 scoped_ptr<net::StreamSocket> socket_; |
| 73 scoped_refptr<net::GrowableIOBuffer> read_buffer_; | 74 scoped_refptr<net::GrowableIOBuffer> read_buffer_; |
| 74 std::queue<scoped_refptr<net::DrainableIOBuffer> > write_queue_; | 75 std::queue<scoped_refptr<net::DrainableIOBuffer> > write_queue_; |
| 75 scoped_refptr<net::DrainableIOBuffer> write_buffer_; | 76 scoped_refptr<net::DrainableIOBuffer> write_buffer_; |
| 76 | 77 |
| 77 bool write_pending_; | 78 bool write_pending_; |
| 78 | 79 |
| 79 bool connected_; | 80 bool connected_; |
| 80 | 81 |
| 82 scoped_refptr<net::URLRequestContextGetter> url_context_; |
| 83 |
| 81 DISALLOW_COPY_AND_ASSIGN(P2PSocketHostTcpBase); | 84 DISALLOW_COPY_AND_ASSIGN(P2PSocketHostTcpBase); |
| 82 }; | 85 }; |
| 83 | 86 |
| 84 class CONTENT_EXPORT P2PSocketHostTcp : public P2PSocketHostTcpBase { | 87 class CONTENT_EXPORT P2PSocketHostTcp : public P2PSocketHostTcpBase { |
| 85 public: | 88 public: |
| 86 P2PSocketHostTcp(IPC::Sender* message_sender, int id); | 89 P2PSocketHostTcp(IPC::Sender* message_sender, int id, |
| 90 net::URLRequestContextGetter* url_context); |
| 87 virtual ~P2PSocketHostTcp(); | 91 virtual ~P2PSocketHostTcp(); |
| 88 | 92 |
| 89 protected: | 93 protected: |
| 90 virtual int ProcessInput(char* input, int input_len) OVERRIDE; | 94 virtual int ProcessInput(char* input, int input_len) OVERRIDE; |
| 91 virtual void DoSend(const net::IPEndPoint& to, | 95 virtual void DoSend(const net::IPEndPoint& to, |
| 92 const std::vector<char>& data) OVERRIDE; | 96 const std::vector<char>& data) OVERRIDE; |
| 93 private: | 97 private: |
| 94 DISALLOW_COPY_AND_ASSIGN(P2PSocketHostTcp); | 98 DISALLOW_COPY_AND_ASSIGN(P2PSocketHostTcp); |
| 95 }; | 99 }; |
| 96 | 100 |
| 97 // P2PSocketHostStunTcp class provides the framing of STUN messages when used | 101 // P2PSocketHostStunTcp class provides the framing of STUN messages when used |
| 98 // with TURN. These messages will not have length at front of the packet and | 102 // with TURN. These messages will not have length at front of the packet and |
| 99 // are padded to multiple of 4 bytes. | 103 // are padded to multiple of 4 bytes. |
| 100 // Formatting of messages is defined in RFC5766. | 104 // Formatting of messages is defined in RFC5766. |
| 101 class CONTENT_EXPORT P2PSocketHostStunTcp : public P2PSocketHostTcpBase { | 105 class CONTENT_EXPORT P2PSocketHostStunTcp : public P2PSocketHostTcpBase { |
| 102 public: | 106 public: |
| 103 P2PSocketHostStunTcp(IPC::Sender* message_sender, int id); | 107 P2PSocketHostStunTcp(IPC::Sender* message_sender, int id, |
| 108 net::URLRequestContextGetter* url_context); |
| 104 virtual ~P2PSocketHostStunTcp(); | 109 virtual ~P2PSocketHostStunTcp(); |
| 105 | 110 |
| 106 protected: | 111 protected: |
| 107 virtual int ProcessInput(char* input, int input_len) OVERRIDE; | 112 virtual int ProcessInput(char* input, int input_len) OVERRIDE; |
| 108 virtual void DoSend(const net::IPEndPoint& to, | 113 virtual void DoSend(const net::IPEndPoint& to, |
| 109 const std::vector<char>& data) OVERRIDE; | 114 const std::vector<char>& data) OVERRIDE; |
| 110 private: | 115 private: |
| 111 int GetExpectedPacketSize(const char* data, int len, int* pad_bytes); | 116 int GetExpectedPacketSize(const char* data, int len, int* pad_bytes); |
| 112 | 117 |
| 113 DISALLOW_COPY_AND_ASSIGN(P2PSocketHostStunTcp); | 118 DISALLOW_COPY_AND_ASSIGN(P2PSocketHostStunTcp); |
| 114 }; | 119 }; |
| 115 | 120 |
| 116 | 121 |
| 117 } // namespace content | 122 } // namespace content |
| 118 | 123 |
| 119 #endif // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_H_ | 124 #endif // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_H_ |
| OLD | NEW |