Commit d3f66578 authored by mbelshe@google.com's avatar mbelshe@google.com

Add methods for setting socket buffers to the Socket

class.

Also add a few stats counters for TCP read/write stats.
BUG=none
TEST=none

Review URL: http://codereview.chromium.org/199048

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@25803 0039d316-1c4b-4281-b951-d872f2087c98
parent 87fce2b9
......@@ -42,6 +42,8 @@ class MockClientSocket : public ClientSocket {
IOBuffer* /* buf */, int /* len */, CompletionCallback* /* callback */) {
return ERR_UNEXPECTED;
}
virtual bool SetReceiveBufferSize(int32 size) { return true; };
virtual bool SetSendBufferSize(int32 size) { return true; };
// ClientSocket methods:
......@@ -1816,7 +1818,7 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, CleanupTimedOutIdleSockets) {
MessageLoop::current()->RunAllPending();
ASSERT_EQ(2, pool_->IdleSocketCount());
// Invoke the idle socket cleanup check. Only one socket should be left, the
// used socket. Request it to make sure that it's used.
......
......@@ -37,6 +37,16 @@ class Socket {
// buffer that is written to the socket.
virtual int Write(IOBuffer* buf, int buf_len,
CompletionCallback* callback) = 0;
// Set the receive buffer size (in bytes) for the socket.
// Note: changing this value can effect the TCP window size on some platforms.
// Returns true on success, or false on failure.
virtual bool SetReceiveBufferSize(int32 size) = 0;
// Set the send buffer size (in bytes) for the socket.
// Note: changing this value can effect the TCP window size on some platforms.
// Returns true on success, or false on failure.
virtual bool SetSendBufferSize(int32 size) = 0;
};
} // namespace net
......
......@@ -247,6 +247,8 @@ class MockClientSocket : public net::SSLClientSocket {
net::CompletionCallback* callback) = 0;
virtual int Write(net::IOBuffer* buf, int buf_len,
net::CompletionCallback* callback) = 0;
virtual bool SetReceiveBufferSize(int32 size) { return true; };
virtual bool SetSendBufferSize(int32 size) { return true; };
#if defined(OS_LINUX)
virtual int GetPeerName(struct sockaddr *name, socklen_t *namelen);
......
......@@ -102,6 +102,14 @@ int SOCKS5ClientSocket::Write(IOBuffer* buf, int buf_len,
return transport_->Write(buf, buf_len, callback);
}
bool SOCKS5ClientSocket::SetReceiveBufferSize(int32 size) {
return transport_->SetReceiveBufferSize(size);
}
bool SOCKS5ClientSocket::SetSendBufferSize(int32 size) {
return transport_->SetSendBufferSize(size);
}
void SOCKS5ClientSocket::DoCallback(int result) {
DCHECK_NE(ERR_IO_PENDING, result);
DCHECK(user_callback_);
......
......@@ -48,6 +48,9 @@ class SOCKS5ClientSocket : public ClientSocket {
virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback);
virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback);
virtual bool SetReceiveBufferSize(int32 size);
virtual bool SetSendBufferSize(int32 size);
#if defined(OS_LINUX)
virtual int GetPeerName(struct sockaddr* name, socklen_t* namelen);
#endif
......
......@@ -136,6 +136,14 @@ int SOCKSClientSocket::Write(IOBuffer* buf, int buf_len,
return transport_->Write(buf, buf_len, callback);
}
bool SOCKSClientSocket::SetReceiveBufferSize(int32 size) {
return transport_->SetReceiveBufferSize(size);
}
bool SOCKSClientSocket::SetSendBufferSize(int32 size) {
return transport_->SetSendBufferSize(size);
}
void SOCKSClientSocket::DoCallback(int result) {
DCHECK_NE(ERR_IO_PENDING, result);
DCHECK(user_callback_);
......
......@@ -47,6 +47,9 @@ class SOCKSClientSocket : public ClientSocket {
virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback);
virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback);
virtual bool SetReceiveBufferSize(int32 size);
virtual bool SetSendBufferSize(int32 size);
#if defined(OS_LINUX)
// Needed by ssl_client_socket_nss.
virtual int GetPeerName(struct sockaddr* name, socklen_t* namelen);
......
......@@ -495,6 +495,14 @@ int SSLClientSocketMac::Write(IOBuffer* buf, int buf_len,
return rv;
}
bool SSLClientSocketMac::SetReceiveBufferSize(int32 size) {
return transport_->SetReceiveBufferSize(size);
}
bool SSLClientSocketMac::SetSendBufferSize(int32 size) {
return transport_->SetSendBufferSize(size);
}
void SSLClientSocketMac::GetSSLInfo(SSLInfo* ssl_info) {
ssl_info->Reset();
......
......@@ -45,6 +45,8 @@ class SSLClientSocketMac : public SSLClientSocket {
// Socket methods:
virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback);
virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback);
virtual bool SetReceiveBufferSize(int32 size);
virtual bool SetSendBufferSize(int32 size);
private:
void DoCallback(int result);
......
......@@ -434,6 +434,14 @@ int SSLClientSocketNSS::Write(IOBuffer* buf, int buf_len,
return rv;
}
bool SSLClientSocketNSS::SetReceiveBufferSize(int32 size) {
return transport_->SetReceiveBufferSize(size);
}
bool SSLClientSocketNSS::SetSendBufferSize(int32 size) {
return transport_->SetSendBufferSize(size);
}
X509Certificate *SSLClientSocketNSS::UpdateServerCert() {
// We set the server_cert_ from OwnAuthCertHandler(), but this handler
// does not necessarily get called if we are continuing a cached SSL
......
......@@ -51,6 +51,8 @@ class SSLClientSocketNSS : public SSLClientSocket {
// Socket methods:
virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback);
virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback);
virtual bool SetReceiveBufferSize(int32 size);
virtual bool SetSendBufferSize(int32 size);
private:
void InvalidateSessionIfBadCertificate();
......
......@@ -580,6 +580,14 @@ int SSLClientSocketWin::Write(IOBuffer* buf, int buf_len,
return rv;
}
bool SSLClientSocketWin::SetReceiveBufferSize(int32 size) {
return transport_->SetReceiveBufferSize(size);
}
bool SSLClientSocketWin::SetSendBufferSize(int32 size) {
return transport_->SetSendBufferSize(size);
}
void SSLClientSocketWin::DoCallback(int rv) {
DCHECK(rv != ERR_IO_PENDING);
DCHECK(user_callback_);
......
......@@ -49,6 +49,9 @@ class SSLClientSocketWin : public SSLClientSocket {
virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback);
virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback);
virtual bool SetReceiveBufferSize(int32 size);
virtual bool SetSendBufferSize(int32 size);
private:
void DoCallback(int result);
void OnIOComplete(int result);
......
......@@ -262,6 +262,23 @@ int TCPClientSocketLibevent::Write(IOBuffer* buf,
return ERR_IO_PENDING;
}
bool TCPClientSocketLibevent::SetReceiveBufferSize(int32 size) {
int rv = setsockopt(socket_, SOL_SOCKET, SO_RCVBUF,
reinterpret_cast<const char*>(&size),
sizeof(size));
DCHECK(!rv) << "Could not set socket receive buffer size: " << errno;
return rv == 0;
}
bool TCPClientSocketLibevent::SetSendBufferSize(int32 size) {
int rv = setsockopt(socket_, SOL_SOCKET, SO_SNDBUF,
reinterpret_cast<const char*>(&size),
sizeof(size));
DCHECK(!rv) << "Could not set socket send buffer size: " << errno;
return rv == 0;
}
int TCPClientSocketLibevent::CreateSocket(const addrinfo* ai) {
socket_ = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (socket_ == kInvalidSocket)
......
......@@ -40,6 +40,8 @@ class TCPClientSocketLibevent : public ClientSocket {
// Full duplex mode (reading and writing at the same time) is supported
virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback);
virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback);
virtual bool SetReceiveBufferSize(int32 size);
virtual bool SetSendBufferSize(int32 size);
private:
class ReadWatcher : public MessageLoopForIO::Watcher {
......
......@@ -51,6 +51,8 @@ class MockClientSocket : public ClientSocket {
CompletionCallback* callback) {
return ERR_FAILED;
}
virtual bool SetReceiveBufferSize(int32 size) { return true; }
virtual bool SetSendBufferSize(int32 size) { return true; }
private:
bool connected_;
......@@ -84,6 +86,8 @@ class MockFailingClientSocket : public ClientSocket {
CompletionCallback* callback) {
return ERR_FAILED;
}
virtual bool SetReceiveBufferSize(int32 size) { return true; }
virtual bool SetSendBufferSize(int32 size) { return true; }
};
class MockPendingClientSocket : public ClientSocket {
......@@ -121,6 +125,8 @@ class MockPendingClientSocket : public ClientSocket {
CompletionCallback* callback) {
return ERR_FAILED;
}
virtual bool SetReceiveBufferSize(int32 size) { return true; }
virtual bool SetSendBufferSize(int32 size) { return true; }
private:
void DoCallback(CompletionCallback* callback) {
......
......@@ -7,6 +7,7 @@
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory_debug.h"
#include "base/stats_counters.h"
#include "base/string_util.h"
#include "base/sys_info.h"
#include "base/trace_event.h"
......@@ -239,6 +240,9 @@ int TCPClientSocketWin::Connect(CompletionCallback* callback) {
if (socket_ != INVALID_SOCKET)
return OK;
static StatsCounter connects("tcp.connect");
connects.Increment();
TRACE_EVENT_BEGIN("socket.connect", this, "");
const struct addrinfo* ai = current_ai_;
DCHECK(ai);
......@@ -386,6 +390,8 @@ int TCPClientSocketWin::Read(IOBuffer* buf,
// false error reports.
// See bug 5297.
base::MemoryDebug::MarkAsInitialized(core_->read_buffer_.buf, num);
static StatsCounter read_bytes("tcp.read_bytes");
read_bytes.Add(num);
return static_cast<int>(num);
}
} else {
......@@ -409,6 +415,9 @@ int TCPClientSocketWin::Write(IOBuffer* buf,
DCHECK_GT(buf_len, 0);
DCHECK(!core_->write_iobuffer_);
static StatsCounter reads("tcp.writes");
reads.Increment();
core_->write_buffer_.len = buf_len;
core_->write_buffer_.buf = buf->data();
......@@ -422,6 +431,8 @@ int TCPClientSocketWin::Write(IOBuffer* buf,
if (rv == 0) {
if (ResetEventIfSignaled(core_->write_overlapped_.hEvent)) {
TRACE_EVENT_END("socket.write", this, StringPrintf("%d bytes", num));
static StatsCounter write_bytes("tcp.write_bytes");
write_bytes.Add(num);
return static_cast<int>(num);
}
} else {
......@@ -436,6 +447,20 @@ int TCPClientSocketWin::Write(IOBuffer* buf,
return ERR_IO_PENDING;
}
bool TCPClientSocketWin::SetReceiveBufferSize(int32 size) {
int rv = setsockopt(socket_, SOL_SOCKET, SO_RCVBUF,
reinterpret_cast<const char*>(&size), sizeof(size));
DCHECK(!rv) << "Could not set socket receive buffer size: " << GetLastError();
return rv == 0;
}
bool TCPClientSocketWin::SetSendBufferSize(int32 size) {
int rv = setsockopt(socket_, SOL_SOCKET, SO_SNDBUF,
reinterpret_cast<const char*>(&size), sizeof(size));
DCHECK(!rv) << "Could not set socket send buffer size: " << GetLastError();
return rv == 0;
}
int TCPClientSocketWin::CreateSocket(const struct addrinfo* ai) {
socket_ = WSASocket(ai->ai_family, ai->ai_socktype, ai->ai_protocol, NULL, 0,
WSA_FLAG_OVERLAPPED);
......@@ -459,15 +484,9 @@ int TCPClientSocketWin::CreateSocket(const struct addrinfo* ai) {
base::SysInfo::OperatingSystemVersionNumbers(&major_version, &minor_version,
&fix_version);
if (major_version < 6) {
const int kSocketBufferSize = 64 * 1024;
int rv = setsockopt(socket_, SOL_SOCKET, SO_SNDBUF,
reinterpret_cast<const char*>(&kSocketBufferSize),
sizeof(kSocketBufferSize));
DCHECK(!rv) << "Could not set socket send buffer size";
rv = setsockopt(socket_, SOL_SOCKET, SO_RCVBUF,
reinterpret_cast<const char*>(&kSocketBufferSize),
sizeof(kSocketBufferSize));
DCHECK(!rv) << "Could not set socket receive buffer size";
const int32 kSocketBufferSize = 64 * 1024;
SetReceiveBufferSize(kSocketBufferSize);
SetSendBufferSize(kSocketBufferSize);
}
// Disable Nagle.
......@@ -504,6 +523,9 @@ void TCPClientSocketWin::DoReadCallback(int rv) {
DCHECK_NE(rv, ERR_IO_PENDING);
DCHECK(read_callback_);
static StatsCounter read_bytes("tcp.read_bytes");
read_bytes.Add(rv);
// since Run may result in Read being called, clear read_callback_ up front.
CompletionCallback* c = read_callback_;
read_callback_ = NULL;
......@@ -514,6 +536,9 @@ void TCPClientSocketWin::DoWriteCallback(int rv) {
DCHECK_NE(rv, ERR_IO_PENDING);
DCHECK(write_callback_);
static StatsCounter write_bytes("tcp.write_bytes");
write_bytes.Add(rv);
// since Run may result in Write being called, clear write_callback_ up front.
CompletionCallback* c = write_callback_;
write_callback_ = NULL;
......
......@@ -35,6 +35,9 @@ class TCPClientSocketWin : public ClientSocket {
virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback);
virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback);
virtual bool SetReceiveBufferSize(int32 size);
virtual bool SetSendBufferSize(int32 size);
private:
class Core;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment