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.
5 #ifndef JINGLE_GLUE_CHANNEL_SOCKET_ADAPTER_H_
6 #define JINGLE_GLUE_CHANNEL_SOCKET_ADAPTER_H_
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/socketaddress.h"
12 #include "third_party/libjingle/source/talk/base/sigslot.h"
19 class TransportChannel
;
20 } // namespace cricket
22 namespace jingle_glue
{
24 // TransportChannelSocketAdapter implements net::Socket interface on
25 // top of libjingle's TransportChannel. It is used by
26 // JingleChromotocolConnection to provide net::Socket interface for channels.
27 class TransportChannelSocketAdapter
: public net::Socket
,
28 public sigslot::has_slots
<> {
30 // TransportChannel object is always owned by the corresponding session.
31 explicit TransportChannelSocketAdapter(cricket::TransportChannel
* channel
);
32 virtual ~TransportChannelSocketAdapter();
34 // Sets callback that should be called when the adapter is being
35 // destroyed. The callback is not allowed to touch the adapter, but
36 // can do anything else, e.g. destroy the TransportChannel.
37 void SetOnDestroyedCallback(const base::Closure
& callback
);
39 // Closes the stream. |error_code| specifies error code that will
40 // be returned by Read() and Write() after the stream is closed.
41 // Must be called before the session and the channel are destroyed.
42 void Close(int error_code
);
44 // Socket implementation.
45 virtual int Read(net::IOBuffer
* buf
, int buf_len
,
46 const net::CompletionCallback
& callback
) OVERRIDE
;
47 virtual int Write(net::IOBuffer
* buf
, int buf_len
,
48 const net::CompletionCallback
& callback
) OVERRIDE
;
50 virtual bool SetReceiveBufferSize(int32 size
) OVERRIDE
;
51 virtual bool SetSendBufferSize(int32 size
) OVERRIDE
;
54 void OnNewPacket(cricket::TransportChannel
* channel
,
58 void OnWritableState(cricket::TransportChannel
* channel
);
59 void OnChannelDestroyed(cricket::TransportChannel
* channel
);
61 base::MessageLoop
* message_loop_
;
63 cricket::TransportChannel
* channel_
;
65 base::Closure destruction_callback_
;
67 net::CompletionCallback read_callback_
;
68 scoped_refptr
<net::IOBuffer
> read_buffer_
;
69 int read_buffer_size_
;
71 net::CompletionCallback write_callback_
;
72 scoped_refptr
<net::IOBuffer
> write_buffer_
;
73 int write_buffer_size_
;
75 int closed_error_code_
;
77 DISALLOW_COPY_AND_ASSIGN(TransportChannelSocketAdapter
);
80 } // namespace jingle_glue
82 #endif // JINGLE_GLUE_CHANNEL_SOCKET_ADAPTER_H_