• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CHANNEL_SOCKET_ADAPTER_H_
6 #define JINGLE_GLUE_CHANNEL_SOCKET_ADAPTER_H_
7 
8 #include "base/callback_forward.h"
9 #include "base/compiler_specific.h"
10 #include "net/socket/socket.h"
11 #include "third_party/libjingle/source/talk/base/asyncpacketsocket.h"
12 #include "third_party/libjingle/source/talk/base/socketaddress.h"
13 #include "third_party/libjingle/source/talk/base/sigslot.h"
14 
15 namespace base {
16 class MessageLoop;
17 }
18 
19 namespace cricket {
20 class TransportChannel;
21 }  // namespace cricket
22 
23 namespace jingle_glue {
24 
25 // TransportChannelSocketAdapter implements net::Socket interface on
26 // top of libjingle's TransportChannel. It is used by
27 // JingleChromotocolConnection to provide net::Socket interface for channels.
28 class TransportChannelSocketAdapter : public net::Socket,
29                                       public sigslot::has_slots<> {
30  public:
31   // TransportChannel object is always owned by the corresponding session.
32   explicit TransportChannelSocketAdapter(cricket::TransportChannel* channel);
33   virtual ~TransportChannelSocketAdapter();
34 
35   // Sets callback that should be called when the adapter is being
36   // destroyed. The callback is not allowed to touch the adapter, but
37   // can do anything else, e.g. destroy the TransportChannel.
38   void SetOnDestroyedCallback(const base::Closure& callback);
39 
40   // Closes the stream. |error_code| specifies error code that will
41   // be returned by Read() and Write() after the stream is closed.
42   // Must be called before the session and the channel are destroyed.
43   void Close(int error_code);
44 
45   // Socket implementation.
46   virtual int Read(net::IOBuffer* buf, int buf_len,
47                    const net::CompletionCallback& callback) OVERRIDE;
48   virtual int Write(net::IOBuffer* buf, int buf_len,
49                     const net::CompletionCallback& callback) OVERRIDE;
50 
51   virtual bool SetReceiveBufferSize(int32 size) OVERRIDE;
52   virtual bool SetSendBufferSize(int32 size) OVERRIDE;
53 
54  private:
55   void OnNewPacket(cricket::TransportChannel* channel,
56                    const char* data,
57                    size_t data_size,
58                    const talk_base::PacketTime& packet_time,
59                    int flags);
60   void OnWritableState(cricket::TransportChannel* channel);
61   void OnChannelDestroyed(cricket::TransportChannel* channel);
62 
63   base::MessageLoop* message_loop_;
64 
65   cricket::TransportChannel* channel_;
66 
67   base::Closure destruction_callback_;
68 
69   net::CompletionCallback read_callback_;
70   scoped_refptr<net::IOBuffer> read_buffer_;
71   int read_buffer_size_;
72 
73   net::CompletionCallback write_callback_;
74   scoped_refptr<net::IOBuffer> write_buffer_;
75   int write_buffer_size_;
76 
77   int closed_error_code_;
78 
79   DISALLOW_COPY_AND_ASSIGN(TransportChannelSocketAdapter);
80 };
81 
82 }  // namespace jingle_glue
83 
84 #endif  // JINGLE_GLUE_CHANNEL_SOCKET_ADAPTER_H_
85