diff options
author | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-08 23:51:15 +0000 |
---|---|---|
committer | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-08 23:51:15 +0000 |
commit | 293f154b854a3507de8ed4116e8529f633060810 (patch) | |
tree | 63cc680a578707f0188e2615ebcd407906040166 /jingle | |
parent | 4c6df432834aa4541044a0890f774b25e15e7746 (diff) | |
download | chromium_src-293f154b854a3507de8ed4116e8529f633060810.zip chromium_src-293f154b854a3507de8ed4116e8529f633060810.tar.gz chromium_src-293f154b854a3507de8ed4116e8529f633060810.tar.bz2 |
base::Bind: Convert Socket::Write.
BUG=none
TEST=none
R=csilv
Review URL: http://codereview.chromium.org/8831001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@113699 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'jingle')
-rw-r--r-- | jingle/glue/channel_socket_adapter.cc | 70 | ||||
-rw-r--r-- | jingle/glue/channel_socket_adapter.h | 5 | ||||
-rw-r--r-- | jingle/glue/pseudotcp_adapter.cc | 67 | ||||
-rw-r--r-- | jingle/glue/pseudotcp_adapter.h | 2 | ||||
-rw-r--r-- | jingle/glue/pseudotcp_adapter_unittest.cc | 14 | ||||
-rw-r--r-- | jingle/notifier/base/fake_ssl_client_socket.cc | 8 | ||||
-rw-r--r-- | jingle/notifier/base/fake_ssl_client_socket.h | 2 | ||||
-rw-r--r-- | jingle/notifier/base/fake_ssl_client_socket_unittest.cc | 2 | ||||
-rw-r--r-- | jingle/notifier/base/proxy_resolving_client_socket.cc | 7 | ||||
-rw-r--r-- | jingle/notifier/base/proxy_resolving_client_socket.h | 2 |
10 files changed, 154 insertions, 25 deletions
diff --git a/jingle/glue/channel_socket_adapter.cc b/jingle/glue/channel_socket_adapter.cc index 3e22a35..39b3eb45c 100644 --- a/jingle/glue/channel_socket_adapter.cc +++ b/jingle/glue/channel_socket_adapter.cc @@ -19,7 +19,7 @@ TransportChannelSocketAdapter::TransportChannelSocketAdapter( : message_loop_(MessageLoop::current()), channel_(channel), old_read_callback_(NULL), - write_callback_(NULL), + old_write_callback_(NULL), closed_error_code_(net::OK) { DCHECK(channel_); @@ -73,11 +73,47 @@ int TransportChannelSocketAdapter::Read( } int TransportChannelSocketAdapter::Write( - net::IOBuffer* buffer, int buffer_size, net::OldCompletionCallback* callback) { + net::IOBuffer* buffer, int buffer_size, + net::OldCompletionCallback* callback) { DCHECK_EQ(MessageLoop::current(), message_loop_); DCHECK(buffer); DCHECK(callback); - CHECK(!write_callback_); + CHECK(!old_write_callback_ && write_callback_.is_null()); + + if (!channel_) { + DCHECK(closed_error_code_ != net::OK); + return closed_error_code_; + } + + int result; + if (channel_->writable()) { + result = channel_->SendPacket(buffer->data(), buffer_size); + if (result < 0) { + result = net::MapSystemError(channel_->GetError()); + + // If the underlying socket returns IO pending where it shouldn't we + // pretend the packet is dropped and return as succeeded because no + // writeable callback will happen. + if (result == net::ERR_IO_PENDING) + result = net::OK; + } + } else { + // Channel is not writable yet. + result = net::ERR_IO_PENDING; + old_write_callback_ = callback; + write_buffer_ = buffer; + write_buffer_size_ = buffer_size; + } + + return result; +} +int TransportChannelSocketAdapter::Write( + net::IOBuffer* buffer, int buffer_size, + const net::CompletionCallback& callback) { + DCHECK_EQ(MessageLoop::current(), message_loop_); + DCHECK(buffer); + DCHECK(!callback.is_null()); + CHECK(!old_write_callback_ && write_callback_.is_null()); if (!channel_) { DCHECK(closed_error_code_ != net::OK); @@ -141,11 +177,16 @@ void TransportChannelSocketAdapter::Close(int error_code) { callback.Run(error_code); } - if (write_callback_) { - net::OldCompletionCallback* callback = write_callback_; - write_callback_ = NULL; + if (old_write_callback_) { + net::OldCompletionCallback* callback = old_write_callback_; + old_write_callback_ = NULL; write_buffer_ = NULL; callback->Run(error_code); + } else if (!write_callback_.is_null()) { + net::CompletionCallback callback = write_callback_; + write_callback_.Reset(); + write_buffer_ = NULL; + callback.Run(error_code); } } @@ -186,17 +227,24 @@ void TransportChannelSocketAdapter::OnWritableState( cricket::TransportChannel* channel) { DCHECK_EQ(MessageLoop::current(), message_loop_); // Try to send the packet if there is a pending write. - if (write_callback_) { + if (old_write_callback_ || !write_callback_.is_null()) { int result = channel_->SendPacket(write_buffer_->data(), write_buffer_size_); if (result < 0) result = net::MapSystemError(channel_->GetError()); if (result != net::ERR_IO_PENDING) { - net::OldCompletionCallback* callback = write_callback_; - write_callback_ = NULL; - write_buffer_ = NULL; - callback->Run(result); + if (old_write_callback_) { + net::OldCompletionCallback* callback = old_write_callback_; + old_write_callback_ = NULL; + write_buffer_ = NULL; + callback->Run(result); + } else { + net::CompletionCallback callback = write_callback_; + write_callback_.Reset(); + write_buffer_ = NULL; + callback.Run(result); + } } } } diff --git a/jingle/glue/channel_socket_adapter.h b/jingle/glue/channel_socket_adapter.h index 1f367e8..75af022 100644 --- a/jingle/glue/channel_socket_adapter.h +++ b/jingle/glue/channel_socket_adapter.h @@ -41,6 +41,8 @@ class TransportChannelSocketAdapter : public net::Socket, const net::CompletionCallback& callback) OVERRIDE; virtual int Write(net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) OVERRIDE; + virtual int Write(net::IOBuffer* buf, int buf_len, + const net::CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; @@ -60,7 +62,8 @@ class TransportChannelSocketAdapter : public net::Socket, scoped_refptr<net::IOBuffer> read_buffer_; int read_buffer_size_; - net::OldCompletionCallback* write_callback_; // Not owned. + net::OldCompletionCallback* old_write_callback_; // Not owned. + net::CompletionCallback write_callback_; scoped_refptr<net::IOBuffer> write_buffer_; int write_buffer_size_; diff --git a/jingle/glue/pseudotcp_adapter.cc b/jingle/glue/pseudotcp_adapter.cc index 7807eed..84b0dcc 100644 --- a/jingle/glue/pseudotcp_adapter.cc +++ b/jingle/glue/pseudotcp_adapter.cc @@ -35,6 +35,8 @@ class PseudoTcpAdapter::Core : public cricket::IPseudoTcpNotify, const net::CompletionCallback& callback); int Write(net::IOBuffer* buffer, int buffer_size, net::OldCompletionCallback* callback); + int Write(net::IOBuffer* buffer, int buffer_size, + const net::CompletionCallback& callback); int Connect(net::OldCompletionCallback* callback); int Connect(const net::CompletionCallback& callback); void Disconnect(); @@ -75,7 +77,8 @@ class PseudoTcpAdapter::Core : public cricket::IPseudoTcpNotify, net::CompletionCallback connect_callback_; net::OldCompletionCallback* old_read_callback_; net::CompletionCallback read_callback_; - net::OldCompletionCallback* write_callback_; + net::OldCompletionCallback* old_write_callback_; + net::CompletionCallback write_callback_; cricket::PseudoTcp pseudo_tcp_; scoped_ptr<net::Socket> socket_; @@ -100,7 +103,7 @@ class PseudoTcpAdapter::Core : public cricket::IPseudoTcpNotify, PseudoTcpAdapter::Core::Core(net::Socket* socket) : old_connect_callback_(NULL), old_read_callback_(NULL), - write_callback_(NULL), + old_write_callback_(NULL), ALLOW_THIS_IN_INITIALIZER_LIST(pseudo_tcp_(this, 0)), socket_(socket), socket_write_pending_(false), @@ -164,7 +167,30 @@ int PseudoTcpAdapter::Core::Read(net::IOBuffer* buffer, int buffer_size, int PseudoTcpAdapter::Core::Write(net::IOBuffer* buffer, int buffer_size, net::OldCompletionCallback* callback) { - DCHECK(!write_callback_); + DCHECK(!old_write_callback_ && write_callback_.is_null()); + + // Reference the Core in case a callback deletes the adapter. + scoped_refptr<Core> core(this); + + int result = pseudo_tcp_.Send(buffer->data(), buffer_size); + if (result < 0) { + result = net::MapSystemError(pseudo_tcp_.GetError()); + DCHECK(result < 0); + } + + if (result == net::ERR_IO_PENDING) { + write_buffer_ = buffer; + write_buffer_size_ = buffer_size; + old_write_callback_ = callback; + } + + AdjustClock(); + + return result; +} +int PseudoTcpAdapter::Core::Write(net::IOBuffer* buffer, int buffer_size, + const net::CompletionCallback& callback) { + DCHECK(!old_write_callback_ && write_callback_.is_null()); // Reference the Core in case a callback deletes the adapter. scoped_refptr<Core> core(this); @@ -231,7 +257,8 @@ void PseudoTcpAdapter::Core::Disconnect() { old_read_callback_ = NULL; read_callback_.Reset(); read_buffer_ = NULL; - write_callback_ = NULL; + old_write_callback_ = NULL; + write_callback_.Reset(); write_buffer_ = NULL; old_connect_callback_ = NULL; connect_callback_.Reset(); @@ -297,7 +324,7 @@ void PseudoTcpAdapter::Core::OnTcpReadable(PseudoTcp* tcp) { void PseudoTcpAdapter::Core::OnTcpWriteable(PseudoTcp* tcp) { DCHECK_EQ(tcp, &pseudo_tcp_); - if (!write_callback_) + if (!old_write_callback_ && write_callback_.is_null()) return; int result = pseudo_tcp_.Send(write_buffer_->data(), write_buffer_size_); @@ -310,10 +337,17 @@ void PseudoTcpAdapter::Core::OnTcpWriteable(PseudoTcp* tcp) { AdjustClock(); - net::OldCompletionCallback* callback = write_callback_; - write_callback_ = NULL; - write_buffer_ = NULL; - callback->Run(result); + if (old_write_callback_) { + net::OldCompletionCallback* callback = old_write_callback_; + old_write_callback_ = NULL; + write_buffer_ = NULL; + callback->Run(result); + } else { + net::CompletionCallback callback = write_callback_; + write_callback_.Reset(); + write_buffer_ = NULL; + callback.Run(result); + } } void PseudoTcpAdapter::Core::OnTcpClosed(PseudoTcp* tcp, uint32 error) { @@ -339,10 +373,14 @@ void PseudoTcpAdapter::Core::OnTcpClosed(PseudoTcp* tcp, uint32 error) { callback.Run(net::MapSystemError(error)); } - if (write_callback_) { - net::OldCompletionCallback* callback = write_callback_; - write_callback_ = NULL; + if (old_write_callback_) { + net::OldCompletionCallback* callback = old_write_callback_; + old_write_callback_ = NULL; callback->Run(net::MapSystemError(error)); + } else if (!write_callback_.is_null()) { + net::CompletionCallback callback = write_callback_; + write_callback_.Reset(); + callback.Run(net::MapSystemError(error)); } } @@ -480,6 +518,11 @@ int PseudoTcpAdapter::Write(net::IOBuffer* buffer, int buffer_size, DCHECK(CalledOnValidThread()); return core_->Write(buffer, buffer_size, callback); } +int PseudoTcpAdapter::Write(net::IOBuffer* buffer, int buffer_size, + const net::CompletionCallback& callback) { + DCHECK(CalledOnValidThread()); + return core_->Write(buffer, buffer_size, callback); +} bool PseudoTcpAdapter::SetReceiveBufferSize(int32 size) { DCHECK(CalledOnValidThread()); diff --git a/jingle/glue/pseudotcp_adapter.h b/jingle/glue/pseudotcp_adapter.h index f0d27ee..6d3cf8c 100644 --- a/jingle/glue/pseudotcp_adapter.h +++ b/jingle/glue/pseudotcp_adapter.h @@ -36,6 +36,8 @@ class PseudoTcpAdapter : public net::StreamSocket, base::NonThreadSafe { const net::CompletionCallback& callback) OVERRIDE; virtual int Write(net::IOBuffer* buffer, int buffer_size, net::OldCompletionCallback* callback) OVERRIDE; + virtual int Write(net::IOBuffer* buffer, int buffer_size, + const net::CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; diff --git a/jingle/glue/pseudotcp_adapter_unittest.cc b/jingle/glue/pseudotcp_adapter_unittest.cc index 6d803a5..94b403f 100644 --- a/jingle/glue/pseudotcp_adapter_unittest.cc +++ b/jingle/glue/pseudotcp_adapter_unittest.cc @@ -174,6 +174,20 @@ class FakeSocket : public net::Socket { return buf_len; } + virtual int Write(net::IOBuffer* buf, int buf_len, + const net::CompletionCallback& callback) OVERRIDE { + DCHECK(buf); + if (peer_socket_) { + MessageLoop::current()->PostDelayedTask( + FROM_HERE, + base::Bind(&FakeSocket::AppendInputPacket, + base::Unretained(peer_socket_), + std::vector<char>(buf->data(), buf->data() + buf_len)), + latency_ms_); + } + + return buf_len; + } virtual bool SetReceiveBufferSize(int32 size) OVERRIDE { NOTIMPLEMENTED(); diff --git a/jingle/notifier/base/fake_ssl_client_socket.cc b/jingle/notifier/base/fake_ssl_client_socket.cc index bdad879..9c13f52 100644 --- a/jingle/notifier/base/fake_ssl_client_socket.cc +++ b/jingle/notifier/base/fake_ssl_client_socket.cc @@ -112,7 +112,13 @@ int FakeSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, } int FakeSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, - net::OldCompletionCallback* callback) { + net::OldCompletionCallback* callback) { + DCHECK_EQ(next_handshake_state_, STATE_NONE); + DCHECK(handshake_completed_); + return transport_socket_->Write(buf, buf_len, callback); +} +int FakeSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, + const net::CompletionCallback& callback) { DCHECK_EQ(next_handshake_state_, STATE_NONE); DCHECK(handshake_completed_); return transport_socket_->Write(buf, buf_len, callback); diff --git a/jingle/notifier/base/fake_ssl_client_socket.h b/jingle/notifier/base/fake_ssl_client_socket.h index 623e21b..047923d 100644 --- a/jingle/notifier/base/fake_ssl_client_socket.h +++ b/jingle/notifier/base/fake_ssl_client_socket.h @@ -52,6 +52,8 @@ class FakeSSLClientSocket : public net::StreamSocket { const net::CompletionCallback& callback) OVERRIDE; virtual int Write(net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) OVERRIDE; + virtual int Write(net::IOBuffer* buf, int buf_len, + const net::CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; diff --git a/jingle/notifier/base/fake_ssl_client_socket_unittest.cc b/jingle/notifier/base/fake_ssl_client_socket_unittest.cc index eb3ba5a..28ee5db 100644 --- a/jingle/notifier/base/fake_ssl_client_socket_unittest.cc +++ b/jingle/notifier/base/fake_ssl_client_socket_unittest.cc @@ -51,6 +51,8 @@ class MockClientSocket : public net::StreamSocket { MOCK_METHOD3(Read, int(net::IOBuffer*, int, const net::CompletionCallback&)); MOCK_METHOD3(Write, int(net::IOBuffer*, int, net::OldCompletionCallback*)); + MOCK_METHOD3(Write, int(net::IOBuffer*, int, + const net::CompletionCallback&)); MOCK_METHOD1(SetReceiveBufferSize, bool(int32)); MOCK_METHOD1(SetSendBufferSize, bool(int32)); MOCK_METHOD1(Connect, int(net::OldCompletionCallback*)); diff --git a/jingle/notifier/base/proxy_resolving_client_socket.cc b/jingle/notifier/base/proxy_resolving_client_socket.cc index 3d46ed6b..c7d54c2 100644 --- a/jingle/notifier/base/proxy_resolving_client_socket.cc +++ b/jingle/notifier/base/proxy_resolving_client_socket.cc @@ -89,6 +89,13 @@ int ProxyResolvingClientSocket::Write(net::IOBuffer* buf, int buf_len, NOTREACHED(); return net::ERR_SOCKET_NOT_CONNECTED; } +int ProxyResolvingClientSocket::Write(net::IOBuffer* buf, int buf_len, + const net::CompletionCallback& callback) { + if (transport_.get() && transport_->socket()) + return transport_->socket()->Write(buf, buf_len, callback); + NOTREACHED(); + return net::ERR_SOCKET_NOT_CONNECTED; +} bool ProxyResolvingClientSocket::SetReceiveBufferSize(int32 size) { if (transport_.get() && transport_->socket()) diff --git a/jingle/notifier/base/proxy_resolving_client_socket.h b/jingle/notifier/base/proxy_resolving_client_socket.h index 4463364..6eae9f4 100644 --- a/jingle/notifier/base/proxy_resolving_client_socket.h +++ b/jingle/notifier/base/proxy_resolving_client_socket.h @@ -52,6 +52,8 @@ class ProxyResolvingClientSocket : public net::StreamSocket { const net::CompletionCallback& callback) OVERRIDE; virtual int Write(net::IOBuffer* buf, int buf_len, net::OldCompletionCallback* callback) OVERRIDE; + virtual int Write(net::IOBuffer* buf, int buf_len, + const net::CompletionCallback& callback) OVERRIDE; virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; virtual bool SetSendBufferSize(int32 size) OVERRIDE; virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; |