blob: ad61ef7b2e48be3c8a0b92539efc585735fb2572 [file] [log] [blame]
[email protected]ad48b7f2012-02-21 21:20:021// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]2fc3b422010-09-28 20:59:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f6da0b252011-03-31 00:26:035#include "jingle/glue/channel_socket_adapter.h"
[email protected]2fc3b422010-09-28 20:59:566
7#include <limits>
8
[email protected]ad48b7f2012-02-21 21:20:029#include "base/callback.h"
[email protected]2fc3b422010-09-28 20:59:5610#include "base/logging.h"
11#include "base/message_loop.h"
12#include "net/base/io_buffer.h"
13#include "net/base/net_errors.h"
[email protected]2fc3b422010-09-28 20:59:5614#include "third_party/libjingle/source/talk/p2p/base/transportchannel.h"
15
[email protected]f6da0b252011-03-31 00:26:0316namespace jingle_glue {
[email protected]2fc3b422010-09-28 20:59:5617
18TransportChannelSocketAdapter::TransportChannelSocketAdapter(
19 cricket::TransportChannel* channel)
[email protected]b231184d2013-04-27 07:51:5520 : message_loop_(base::MessageLoop::current()),
[email protected]6c875752011-04-01 01:59:0721 channel_(channel),
[email protected]2fc3b422010-09-28 20:59:5622 closed_error_code_(net::OK) {
[email protected]2fc3b422010-09-28 20:59:5623 DCHECK(channel_);
24
25 channel_->SignalReadPacket.connect(
26 this, &TransportChannelSocketAdapter::OnNewPacket);
27 channel_->SignalWritableState.connect(
28 this, &TransportChannelSocketAdapter::OnWritableState);
29 channel_->SignalDestroyed.connect(
30 this, &TransportChannelSocketAdapter::OnChannelDestroyed);
31}
32
33TransportChannelSocketAdapter::~TransportChannelSocketAdapter() {
[email protected]ad48b7f2012-02-21 21:20:0234 if (!destruction_callback_.is_null())
35 destruction_callback_.Run();
36}
37
38void TransportChannelSocketAdapter::SetOnDestroyedCallback(
39 const base::Closure& callback) {
40 destruction_callback_ = callback;
[email protected]2fc3b422010-09-28 20:59:5641}
42
43int TransportChannelSocketAdapter::Read(
[email protected]83039bb2011-12-09 18:43:5544 net::IOBuffer* buf,
45 int buffer_size,
[email protected]3f55aa12011-12-07 02:03:3346 const net::CompletionCallback& callback) {
[email protected]b231184d2013-04-27 07:51:5547 DCHECK_EQ(base::MessageLoop::current(), message_loop_);
[email protected]3f55aa12011-12-07 02:03:3348 DCHECK(buf);
49 DCHECK(!callback.is_null());
[email protected]83039bb2011-12-09 18:43:5550 CHECK(read_callback_.is_null());
[email protected]2fc3b422010-09-28 20:59:5651
52 if (!channel_) {
53 DCHECK(closed_error_code_ != net::OK);
54 return closed_error_code_;
55 }
56
57 read_callback_ = callback;
58 read_buffer_ = buf;
59 read_buffer_size_ = buffer_size;
[email protected]2fc3b422010-09-28 20:59:5660
61 return net::ERR_IO_PENDING;
62}
63
64int TransportChannelSocketAdapter::Write(
[email protected]83039bb2011-12-09 18:43:5565 net::IOBuffer* buffer,
66 int buffer_size,
67 const net::CompletionCallback& callback) {
[email protected]b231184d2013-04-27 07:51:5568 DCHECK_EQ(base::MessageLoop::current(), message_loop_);
[email protected]2fc3b422010-09-28 20:59:5669 DCHECK(buffer);
[email protected]83039bb2011-12-09 18:43:5570 DCHECK(!callback.is_null());
71 CHECK(write_callback_.is_null());
[email protected]2fc3b422010-09-28 20:59:5672
73 if (!channel_) {
74 DCHECK(closed_error_code_ != net::OK);
75 return closed_error_code_;
76 }
77
[email protected]4d83fbc2011-04-22 19:35:4978 int result;
79 if (channel_->writable()) {
80 result = channel_->SendPacket(buffer->data(), buffer_size);
[email protected]87185ec2011-07-27 13:25:4081 if (result < 0) {
[email protected]4d83fbc2011-04-22 19:35:4982 result = net::MapSystemError(channel_->GetError());
[email protected]87185ec2011-07-27 13:25:4083
84 // If the underlying socket returns IO pending where it shouldn't we
85 // pretend the packet is dropped and return as succeeded because no
86 // writeable callback will happen.
87 if (result == net::ERR_IO_PENDING)
88 result = net::OK;
89 }
[email protected]4d83fbc2011-04-22 19:35:4990 } else {
91 // Channel is not writable yet.
92 result = net::ERR_IO_PENDING;
[email protected]4d83fbc2011-04-22 19:35:4993 write_callback_ = callback;
94 write_buffer_ = buffer;
95 write_buffer_size_ = buffer_size;
96 }
97
[email protected]2fc3b422010-09-28 20:59:5698 return result;
99}
100
101bool TransportChannelSocketAdapter::SetReceiveBufferSize(int32 size) {
[email protected]b231184d2013-04-27 07:51:55102 DCHECK_EQ(base::MessageLoop::current(), message_loop_);
[email protected]b8acc612011-08-04 20:01:42103 return channel_->SetOption(talk_base::Socket::OPT_RCVBUF, size) == 0;
[email protected]2fc3b422010-09-28 20:59:56104}
105
106bool TransportChannelSocketAdapter::SetSendBufferSize(int32 size) {
[email protected]b231184d2013-04-27 07:51:55107 DCHECK_EQ(base::MessageLoop::current(), message_loop_);
[email protected]b8acc612011-08-04 20:01:42108 return channel_->SetOption(talk_base::Socket::OPT_SNDBUF, size) == 0;
[email protected]2fc3b422010-09-28 20:59:56109}
110
111void TransportChannelSocketAdapter::Close(int error_code) {
[email protected]b231184d2013-04-27 07:51:55112 DCHECK_EQ(base::MessageLoop::current(), message_loop_);
[email protected]2fc3b422010-09-28 20:59:56113
114 if (!channel_) // Already closed.
115 return;
116
117 DCHECK(error_code != net::OK);
118 closed_error_code_ = error_code;
119 channel_->SignalReadPacket.disconnect(this);
120 channel_->SignalDestroyed.disconnect(this);
121 channel_ = NULL;
122
[email protected]83039bb2011-12-09 18:43:55123 if (!read_callback_.is_null()) {
[email protected]3f55aa12011-12-07 02:03:33124 net::CompletionCallback callback = read_callback_;
125 read_callback_.Reset();
126 read_buffer_ = NULL;
127 callback.Run(error_code);
[email protected]2fc3b422010-09-28 20:59:56128 }
129
[email protected]83039bb2011-12-09 18:43:55130 if (!write_callback_.is_null()) {
131 net::CompletionCallback callback = write_callback_;
132 write_callback_.Reset();
[email protected]2fc3b422010-09-28 20:59:56133 write_buffer_ = NULL;
[email protected]83039bb2011-12-09 18:43:55134 callback.Run(error_code);
[email protected]2fc3b422010-09-28 20:59:56135 }
136}
137
138void TransportChannelSocketAdapter::OnNewPacket(
[email protected]8bb746372012-04-26 04:20:12139 cricket::TransportChannel* channel,
140 const char* data,
141 size_t data_size,
142 int flags) {
[email protected]b231184d2013-04-27 07:51:55143 DCHECK_EQ(base::MessageLoop::current(), message_loop_);
[email protected]2fc3b422010-09-28 20:59:56144 DCHECK_EQ(channel, channel_);
[email protected]83039bb2011-12-09 18:43:55145 if (!read_callback_.is_null()) {
[email protected]f9d8a772013-06-01 04:33:17146 DCHECK(read_buffer_.get());
[email protected]2fc3b422010-09-28 20:59:56147 CHECK_LT(data_size, static_cast<size_t>(std::numeric_limits<int>::max()));
148
149 if (read_buffer_size_ < static_cast<int>(data_size)) {
150 LOG(WARNING) << "Data buffer is smaller than the received packet. "
151 << "Dropping the data that doesn't fit.";
152 data_size = read_buffer_size_;
153 }
154
155 memcpy(read_buffer_->data(), data, data_size);
156
[email protected]83039bb2011-12-09 18:43:55157 net::CompletionCallback callback = read_callback_;
158 read_callback_.Reset();
159 read_buffer_ = NULL;
160
161 callback.Run(data_size);
[email protected]2fc3b422010-09-28 20:59:56162 } else {
163 LOG(WARNING)
164 << "Data was received without a callback. Dropping the packet.";
165 }
166}
167
168void TransportChannelSocketAdapter::OnWritableState(
169 cricket::TransportChannel* channel) {
[email protected]b231184d2013-04-27 07:51:55170 DCHECK_EQ(base::MessageLoop::current(), message_loop_);
[email protected]2fc3b422010-09-28 20:59:56171 // Try to send the packet if there is a pending write.
[email protected]83039bb2011-12-09 18:43:55172 if (!write_callback_.is_null()) {
[email protected]2fc3b422010-09-28 20:59:56173 int result = channel_->SendPacket(write_buffer_->data(),
174 write_buffer_size_);
175 if (result < 0)
[email protected]f6da0b252011-03-31 00:26:03176 result = net::MapSystemError(channel_->GetError());
[email protected]2fc3b422010-09-28 20:59:56177
178 if (result != net::ERR_IO_PENDING) {
[email protected]83039bb2011-12-09 18:43:55179 net::CompletionCallback callback = write_callback_;
180 write_callback_.Reset();
[email protected]f749f9c2011-12-09 01:06:19181 write_buffer_ = NULL;
[email protected]83039bb2011-12-09 18:43:55182 callback.Run(result);
[email protected]2fc3b422010-09-28 20:59:56183 }
184 }
185}
186
187void TransportChannelSocketAdapter::OnChannelDestroyed(
188 cricket::TransportChannel* channel) {
[email protected]b231184d2013-04-27 07:51:55189 DCHECK_EQ(base::MessageLoop::current(), message_loop_);
[email protected]2fc3b422010-09-28 20:59:56190 DCHECK_EQ(channel, channel_);
191 Close(net::ERR_CONNECTION_ABORTED);
192}
193
[email protected]f6da0b252011-03-31 00:26:03194} // namespace jingle_glue