channel_socket_adapter.h 3.34 KB
Newer Older
1
// Copyright 2015 The Chromium Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef REMOTING_PROTOCOL_CHANNEL_SOCKET_ADAPTER_H_
#define REMOTING_PROTOCOL_CHANNEL_SOCKET_ADAPTER_H_
7

8 9
#include <stddef.h>

10
#include "base/callback_forward.h"
11
#include "base/compiler_specific.h"
12
#include "base/macros.h"
13
#include "base/threading/thread_checker.h"
14
#include "remoting/protocol/p2p_datagram_socket.h"
15 16 17 18
// TODO(zhihuang):Replace #include by forward declaration once proper
// inheritance is defined for cricket::IceTransportInternal and
// cricket::P2PTransportChannel.
#include "third_party/webrtc/p2p/base/icetransportinternal.h"
19 20 21 22
// TODO(johan): Replace #include by forward declaration once proper
// inheritance is defined for rtc::PacketTransportInterface and
// cricket::TransportChannel.
#include "third_party/webrtc/p2p/base/packettransportinterface.h"
23 24 25
#include "third_party/webrtc/rtc_base/asyncpacketsocket.h"
#include "third_party/webrtc/rtc_base/sigslot.h"
#include "third_party/webrtc/rtc_base/socketaddress.h"
26

27 28
namespace remoting {
namespace protocol {
29

30
// TransportChannelSocketAdapter implements P2PDatagramSocket interface on
31
// top of cricket::IceTransportInternal. It is used by IceTransport to provide
32
// P2PDatagramSocket interface for channels.
33
class TransportChannelSocketAdapter : public P2PDatagramSocket,
34 35
                                      public sigslot::has_slots<> {
 public:
36 37
  // Doesn't take ownership of the |channel|. The |channel| must outlive
  // this adapter.
38 39
  explicit TransportChannelSocketAdapter(
      cricket::IceTransportInternal* ice_transport);
40
  ~TransportChannelSocketAdapter() override;
41

42 43 44
  // Sets callback that should be called when the adapter is being
  // destroyed. The callback is not allowed to touch the adapter, but
  // can do anything else, e.g. destroy the TransportChannel.
45 46
  void SetOnDestroyedCallback(const base::Closure& callback);

47
  // Closes the stream. |error_code| specifies error code that will
48
  // be returned by Recv() and Send() after the stream is closed.
49 50 51
  // Must be called before the session and the channel are destroyed.
  void Close(int error_code);

52 53 54 55
  // P2PDatagramSocket interface.
  int Recv(const scoped_refptr<net::IOBuffer>& buf, int buf_len,
           const net::CompletionCallback& callback) override;
  int Send(const scoped_refptr<net::IOBuffer>& buf, int buf_len,
56
           const net::CompletionCallback& callback) override;
57 58

 private:
59
  void OnNewPacket(rtc::PacketTransportInterface* transport,
60 61
                   const char* data,
                   size_t data_size,
62
                   const rtc::PacketTime& packet_time,
63
                   int flags);
64
  void OnWritableState(rtc::PacketTransportInterface* transport);
65
  void OnChannelDestroyed(cricket::IceTransportInternal* ice_transport);
66

67
  base::ThreadChecker thread_checker_;
68

69
  cricket::IceTransportInternal* channel_;
70

71 72
  base::Closure destruction_callback_;

73
  net::CompletionCallback read_callback_;
74 75 76
  scoped_refptr<net::IOBuffer> read_buffer_;
  int read_buffer_size_;

77
  net::CompletionCallback write_callback_;
78 79 80 81 82 83 84 85
  scoped_refptr<net::IOBuffer> write_buffer_;
  int write_buffer_size_;

  int closed_error_code_;

  DISALLOW_COPY_AND_ASSIGN(TransportChannelSocketAdapter);
};

86 87
}  // namespace protocol
}  // namespace remoting
88

89
#endif  // REMOTING_PROTOCOL_CHANNEL_SOCKET_ADAPTER_H_