| 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 JINGLE_GLUE_PSEUDOTCP_ADAPTER_H_ | 5 #ifndef JINGLE_GLUE_PSEUDOTCP_ADAPTER_H_ |
| 6 #define JINGLE_GLUE_PSEUDOTCP_ADAPTER_H_ | 6 #define JINGLE_GLUE_PSEUDOTCP_ADAPTER_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/timer.h" | 12 #include "base/timer.h" |
| 13 #include "base/threading/non_thread_safe.h" | 13 #include "base/threading/non_thread_safe.h" |
| 14 #include "net/base/net_log.h" | 14 #include "net/base/net_log.h" |
| 15 #include "net/socket/stream_socket.h" | 15 #include "net/socket/stream_socket.h" |
| 16 #include "third_party/libjingle/source/talk/p2p/base/pseudotcp.h" | 16 #include "third_party/libjingle/source/talk/p2p/base/pseudotcp.h" |
| 17 | 17 |
| 18 namespace jingle_glue { | 18 namespace jingle_glue { |
| 19 | 19 |
| 20 class PseudoTcpAdapter : public net::StreamSocket, | 20 // PseudoTcpAdapter adapts a connectionless net::Socket to a connection- |
| 21 public cricket::IPseudoTcpNotify, | 21 // oriented net::StreamSocket using PseudoTcp. Because net::StreamSockets |
| 22 public base::NonThreadSafe { | 22 // can be deleted during callbacks, while PseudoTcp cannot, the core of the |
| 23 // PseudoTcpAdapter is reference counted, with a reference held by the |
| 24 // adapter, and an additional reference held on the stack during callbacks. |
| 25 class PseudoTcpAdapter : public net::StreamSocket, base::NonThreadSafe { |
| 23 public: | 26 public: |
| 24 // Creates adapter for the specified |socket|. |socket| is assumed | 27 // Creates an adapter for the supplied Socket. |socket| should already |
| 25 // to be already connected. Takes ownership of |socket|. | 28 // be ready for use, and ownership of it will be assumed by the adapter. |
| 26 PseudoTcpAdapter(net::Socket* socket); | 29 PseudoTcpAdapter(net::Socket* socket); |
| 27 virtual ~PseudoTcpAdapter(); | 30 virtual ~PseudoTcpAdapter(); |
| 28 | 31 |
| 29 // net::Socket implementation. | 32 // net::Socket implementation. |
| 30 virtual int Read(net::IOBuffer* buffer, int buffer_size, | 33 virtual int Read(net::IOBuffer* buffer, int buffer_size, |
| 31 net::CompletionCallback* callback) OVERRIDE; | 34 net::CompletionCallback* callback) OVERRIDE; |
| 32 virtual int Write(net::IOBuffer* buffer, int buffer_size, | 35 virtual int Write(net::IOBuffer* buffer, int buffer_size, |
| 33 net::CompletionCallback* callback) OVERRIDE; | 36 net::CompletionCallback* callback) OVERRIDE; |
| 34 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; | 37 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; |
| 35 virtual bool SetSendBufferSize(int32 size) OVERRIDE; | 38 virtual bool SetSendBufferSize(int32 size) OVERRIDE; |
| 36 | 39 |
| 37 // net::StreamSocket implementation. | 40 // net::StreamSocket implementation. |
| 38 virtual int Connect(net::CompletionCallback* callback) OVERRIDE; | 41 virtual int Connect(net::CompletionCallback* callback) OVERRIDE; |
| 39 virtual void Disconnect() OVERRIDE; | 42 virtual void Disconnect() OVERRIDE; |
| 40 virtual bool IsConnected() const OVERRIDE; | 43 virtual bool IsConnected() const OVERRIDE; |
| 41 virtual bool IsConnectedAndIdle() const OVERRIDE; | 44 virtual bool IsConnectedAndIdle() const OVERRIDE; |
| 42 virtual int GetPeerAddress(net::AddressList* address) const OVERRIDE; | 45 virtual int GetPeerAddress(net::AddressList* address) const OVERRIDE; |
| 43 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE; | 46 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE; |
| 44 virtual const net::BoundNetLog& NetLog() const OVERRIDE; | 47 virtual const net::BoundNetLog& NetLog() const OVERRIDE; |
| 45 virtual void SetSubresourceSpeculation() OVERRIDE; | 48 virtual void SetSubresourceSpeculation() OVERRIDE; |
| 46 virtual void SetOmniboxSpeculation() OVERRIDE; | 49 virtual void SetOmniboxSpeculation() OVERRIDE; |
| 47 virtual bool WasEverUsed() const OVERRIDE; | 50 virtual bool WasEverUsed() const OVERRIDE; |
| 48 virtual bool UsingTCPFastOpen() const OVERRIDE; | 51 virtual bool UsingTCPFastOpen() const OVERRIDE; |
| 49 | 52 |
| 50 // cricket::IPseudoTcpNotify implementation. | 53 private: |
| 51 virtual void OnTcpOpen(cricket::PseudoTcp* tcp) OVERRIDE; | 54 class Core; |
| 52 virtual void OnTcpReadable(cricket::PseudoTcp* tcp) OVERRIDE; | |
| 53 virtual void OnTcpWriteable(cricket::PseudoTcp* tcp) OVERRIDE; | |
| 54 virtual void OnTcpClosed(cricket::PseudoTcp* tcp, uint32 error) OVERRIDE; | |
| 55 virtual WriteResult TcpWritePacket(cricket::PseudoTcp* tcp, | |
| 56 const char* buffer, size_t len) OVERRIDE; | |
| 57 | 55 |
| 58 private: | 56 scoped_refptr<Core> core_; |
| 59 void DoReadFromSocket(); | |
| 60 void HandleReadResults(int result); | |
| 61 | |
| 62 // Callback functions for Read() and Write() in |socket_| | |
| 63 void OnRead(int result); | |
| 64 void OnWritten(int result); | |
| 65 | |
| 66 void AdjustClock(); | |
| 67 void HandleTcpClock(); | |
| 68 | |
| 69 scoped_ptr<net::Socket> socket_; | |
| 70 cricket::PseudoTcp pseudotcp_; | |
| 71 | |
| 72 net::CompletionCallback* connect_callback_; | |
| 73 scoped_refptr<net::IOBuffer> read_buffer_; | |
| 74 int read_buffer_size_; | |
| 75 net::CompletionCallback* read_callback_; | |
| 76 scoped_refptr<net::IOBuffer> write_buffer_; | |
| 77 int write_buffer_size_; | |
| 78 net::CompletionCallback* write_callback_; | |
| 79 | |
| 80 bool socket_write_pending_; | |
| 81 scoped_refptr<net::IOBuffer> socket_read_buffer_; | |
| 82 | |
| 83 net::CompletionCallbackImpl<PseudoTcpAdapter> socket_read_callback_; | |
| 84 net::CompletionCallbackImpl<PseudoTcpAdapter> socket_write_callback_; | |
| 85 | |
| 86 base::OneShotTimer<PseudoTcpAdapter> timer_; | |
| 87 | 57 |
| 88 net::BoundNetLog net_log_; | 58 net::BoundNetLog net_log_; |
| 89 | 59 |
| 90 DISALLOW_COPY_AND_ASSIGN(PseudoTcpAdapter); | 60 DISALLOW_COPY_AND_ASSIGN(PseudoTcpAdapter); |
| 91 }; | 61 }; |
| 92 | 62 |
| 93 } // namespace jingle_glue | 63 } // namespace jingle_glue |
| 94 | 64 |
| 95 #endif // JINGLE_GLUE_STREAM_SOCKET_ADAPTER_H_ | 65 #endif // JINGLE_GLUE_STREAM_SOCKET_ADAPTER_H_ |
| OLD | NEW |