1// Copyright (c) 2012 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.
4
5#ifndef JINGLE_GLUE_PSEUDOTCP_ADAPTER_H_
6#define JINGLE_GLUE_PSEUDOTCP_ADAPTER_H_
7
8#include "base/basictypes.h"
9#include "base/compiler_specific.h"
10#include "base/memory/ref_counted.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/threading/non_thread_safe.h"
13#include "base/timer/timer.h"
14#include "net/base/net_log.h"
15#include "net/socket/stream_socket.h"
16#include "third_party/libjingle/source/talk/p2p/base/pseudotcp.h"
17
18namespace jingle_glue {
19
20// PseudoTcpAdapter adapts a connectionless net::Socket to a connection-
21// oriented net::StreamSocket using PseudoTcp.  Because net::StreamSockets
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.
25class PseudoTcpAdapter : public net::StreamSocket, base::NonThreadSafe {
26 public:
27  // Creates an adapter for the supplied Socket.  |socket| should already
28  // be ready for use, and ownership of it will be assumed by the adapter.
29  PseudoTcpAdapter(net::Socket* socket);
30  virtual ~PseudoTcpAdapter();
31
32  // net::Socket implementation.
33  virtual int Read(net::IOBuffer* buffer, int buffer_size,
34                   const net::CompletionCallback& callback) OVERRIDE;
35  virtual int Write(net::IOBuffer* buffer, int buffer_size,
36                    const net::CompletionCallback& callback) OVERRIDE;
37  virtual int SetReceiveBufferSize(int32 size) OVERRIDE;
38  virtual int SetSendBufferSize(int32 size) OVERRIDE;
39
40  // net::StreamSocket implementation.
41  virtual int Connect(const net::CompletionCallback& callback) OVERRIDE;
42  virtual void Disconnect() OVERRIDE;
43  virtual bool IsConnected() const OVERRIDE;
44  virtual bool IsConnectedAndIdle() const OVERRIDE;
45  virtual int GetPeerAddress(net::IPEndPoint* address) const OVERRIDE;
46  virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE;
47  virtual const net::BoundNetLog& NetLog() const OVERRIDE;
48  virtual void SetSubresourceSpeculation() OVERRIDE;
49  virtual void SetOmniboxSpeculation() OVERRIDE;
50  virtual bool WasEverUsed() const OVERRIDE;
51  virtual bool UsingTCPFastOpen() const OVERRIDE;
52  virtual bool WasNpnNegotiated() const OVERRIDE;
53  virtual net::NextProto GetNegotiatedProtocol() const OVERRIDE;
54  virtual bool GetSSLInfo(net::SSLInfo* ssl_info) OVERRIDE;
55
56  // Set the delay for sending ACK.
57  void SetAckDelay(int delay_ms);
58
59  // Set whether Nagle's algorithm is enabled.
60  void SetNoDelay(bool no_delay);
61
62  // When write_waits_for_send flag is set to true the Write() method
63  // will wait until the data is sent to the remote end before the
64  // write completes (it still doesn't wait until the data is received
65  // and acknowledged by the remote end). Otherwise write completes
66  // after the data has been copied to the send buffer.
67  //
68  // This flag is useful in cases when the sender needs to get
69  // feedback from the connection when it is congested. E.g. remoting
70  // host uses this feature to adjust screen capturing rate according
71  // to the available bandwidth. In the same time it may negatively
72  // impact performance in some cases. E.g. when the sender writes one
73  // byte at a time then each byte will always be sent in a separate
74  // packet.
75  //
76  // TODO(sergeyu): Remove this flag once remoting has a better
77  // flow-control solution.
78  void SetWriteWaitsForSend(bool write_waits_for_send);
79
80 private:
81  class Core;
82
83  scoped_refptr<Core> core_;
84
85  net::BoundNetLog net_log_;
86
87  DISALLOW_COPY_AND_ASSIGN(PseudoTcpAdapter);
88};
89
90}  // namespace jingle_glue
91
92#endif  // JINGLE_GLUE_STREAM_SOCKET_ADAPTER_H_
93