Skip to content

Commit

Permalink
Set send buffer size for P2P UDP sockets.
Browse files Browse the repository at this point in the history
On windows the default send buffer is too small. Set it explicitly to prevent packets from being lost when sending.

BUG=None
TEST=None


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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@105995 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
sergeyu@chromium.org committed Oct 18, 2011
1 parent 46b78da commit df31da4
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 19 deletions.
7 changes: 6 additions & 1 deletion content/browser/renderer_host/p2p/socket_host_udp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ namespace {
// UDP packets cannot be bigger than 64k.
const int kReadBufferSize = 65536;

// Send buffer size for the socket.
const int kSendBufferSize = 65536;

} // namespace

namespace content {
Expand Down Expand Up @@ -47,6 +50,9 @@ bool P2PSocketHostUdp::Init(const net::IPEndPoint& local_address,
return false;
}

if (!socket_->SetSendBufferSize(kSendBufferSize))
LOG(WARNING) << "Failed to set send buffer size for UDP socket.";

net::IPEndPoint address;
result = socket_->GetLocalAddress(&address);
if (result < 0) {
Expand All @@ -55,7 +61,6 @@ bool P2PSocketHostUdp::Init(const net::IPEndPoint& local_address,
OnError();
return false;
}

VLOG(1) << "Local address: " << address.ToString();

state_ = STATE_OPEN;
Expand Down
8 changes: 8 additions & 0 deletions content/browser/renderer_host/p2p/socket_host_udp_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ class FakeDatagramServerSocket : public net::DatagramServerSocket {
return buf_len;
}

virtual bool SetReceiveBufferSize(int32 size) OVERRIDE {
return true;
}

virtual bool SetSendBufferSize(int32 size) OVERRIDE {
return true;
}

void ReceivePacket(const net::IPEndPoint& address, std::vector<char> data) {
if (recv_callback_) {
int size = std::min(recv_size_, static_cast<int>(data.size()));
Expand Down
6 changes: 6 additions & 0 deletions net/udp/datagram_server_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class NET_EXPORT DatagramServerSocket : public DatagramSocket {
int buf_len,
const IPEndPoint& address,
OldCompletionCallback* callback) = 0;

// Set the receive buffer size (in bytes) for the socket.
virtual bool SetReceiveBufferSize(int32 size) = 0;

// Set the send buffer size (in bytes) for the socket.
virtual bool SetSendBufferSize(int32 size) = 0;
};

} // namespace net
Expand Down
4 changes: 2 additions & 2 deletions net/udp/udp_client_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ int UDPClientSocket::GetLocalAddress(IPEndPoint* address) const {
}

bool UDPClientSocket::SetReceiveBufferSize(int32 size) {
return true;
return socket_.SetReceiveBufferSize(size);
}

bool UDPClientSocket::SetSendBufferSize(int32 size) {
return true;
return socket_.SetSendBufferSize(size);
}

const BoundNetLog& UDPClientSocket::NetLog() const {
Expand Down
20 changes: 11 additions & 9 deletions net/udp/udp_client_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ class NET_EXPORT_PRIVATE UDPClientSocket : public DatagramClientSocket {
virtual ~UDPClientSocket();

// Implement DatagramClientSocket:
virtual int Connect(const IPEndPoint& address);
virtual int Read(IOBuffer* buf, int buf_len, OldCompletionCallback* callback);
virtual int Write(IOBuffer* buf, int buf_len, OldCompletionCallback* callback);
virtual void Close();
virtual int GetPeerAddress(IPEndPoint* address) const;
virtual int GetLocalAddress(IPEndPoint* address) const;
virtual bool SetReceiveBufferSize(int32 size);
virtual bool SetSendBufferSize(int32 size);
virtual const BoundNetLog& NetLog() const;
virtual int Connect(const IPEndPoint& address) OVERRIDE;
virtual int Read(IOBuffer* buf, int buf_len,
OldCompletionCallback* callback) OVERRIDE;
virtual int Write(IOBuffer* buf, int buf_len,
OldCompletionCallback* callback) OVERRIDE;
virtual void Close() OVERRIDE;
virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE;
virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE;
virtual bool SetReceiveBufferSize(int32 size) OVERRIDE;
virtual bool SetSendBufferSize(int32 size) OVERRIDE;
virtual const BoundNetLog& NetLog() const OVERRIDE;

private:
UDPSocket socket_;
Expand Down
8 changes: 8 additions & 0 deletions net/udp/udp_server_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ int UDPServerSocket::SendTo(IOBuffer* buf,
return socket_.SendTo(buf, buf_len, address, callback);
}

bool UDPServerSocket::SetReceiveBufferSize(int32 size) {
return socket_.SetReceiveBufferSize(size);
}

bool UDPServerSocket::SetSendBufferSize(int32 size) {
return socket_.SetSendBufferSize(size);
}

void UDPServerSocket::Close() {
socket_.Close();
}
Expand Down
16 changes: 9 additions & 7 deletions net/udp/udp_server_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@ class NET_EXPORT UDPServerSocket : public DatagramServerSocket {
virtual ~UDPServerSocket();

// Implement DatagramServerSocket:
virtual int Listen(const IPEndPoint& address);
virtual int Listen(const IPEndPoint& address) OVERRIDE;
virtual int RecvFrom(IOBuffer* buf,
int buf_len,
IPEndPoint* address,
OldCompletionCallback* callback);
OldCompletionCallback* callback) OVERRIDE;
virtual int SendTo(IOBuffer* buf,
int buf_len,
const IPEndPoint& address,
OldCompletionCallback* callback);
virtual void Close();
virtual int GetPeerAddress(IPEndPoint* address) const;
virtual int GetLocalAddress(IPEndPoint* address) const;
virtual const BoundNetLog& NetLog() const;
OldCompletionCallback* callback) OVERRIDE;
virtual bool SetReceiveBufferSize(int32 size) OVERRIDE;
virtual bool SetSendBufferSize(int32 size) OVERRIDE;
virtual void Close() OVERRIDE;
virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE;
virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE;
virtual const BoundNetLog& NetLog() const OVERRIDE;

private:
UDPSocket socket_;
Expand Down
18 changes: 18 additions & 0 deletions net/udp/udp_socket_libevent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ int UDPSocketLibevent::SendToOrWrite(IOBuffer* buf,
}

int UDPSocketLibevent::Connect(const IPEndPoint& address) {
DCHECK(CalledOnValidThread());
DCHECK(!is_connected());
DCHECK(!remote_address_.get());
int rv = CreateSocket(address);
Expand Down Expand Up @@ -246,6 +247,7 @@ int UDPSocketLibevent::Connect(const IPEndPoint& address) {
}

int UDPSocketLibevent::Bind(const IPEndPoint& address) {
DCHECK(CalledOnValidThread());
DCHECK(!is_connected());
int rv = CreateSocket(address);
if (rv < 0)
Expand All @@ -257,6 +259,22 @@ int UDPSocketLibevent::Bind(const IPEndPoint& address) {
return rv;
}

bool UDPSocketLibevent::SetReceiveBufferSize(int32 size) {
DCHECK(CalledOnValidThread());
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 UDPSocketLibevent::SetSendBufferSize(int32 size) {
DCHECK(CalledOnValidThread());
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;
}

void UDPSocketLibevent::DoReadCallback(int rv) {
DCHECK_NE(rv, ERR_IO_PENDING);
DCHECK(read_callback_);
Expand Down
6 changes: 6 additions & 0 deletions net/udp/udp_socket_libevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ class UDPSocketLibevent : public base::NonThreadSafe {
const IPEndPoint& address,
OldCompletionCallback* callback);

// Set the receive buffer size (in bytes) for the socket.
bool SetReceiveBufferSize(int32 size);

// Set the send buffer size (in bytes) for the socket.
bool SetSendBufferSize(int32 size);

// Returns true if the socket is already connected or bound.
bool is_connected() const { return socket_ != kInvalidSocket; }

Expand Down
16 changes: 16 additions & 0 deletions net/udp/udp_socket_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,22 @@ int UDPSocketWin::CreateSocket(const IPEndPoint& address) {
return OK;
}

bool UDPSocketWin::SetReceiveBufferSize(int32 size) {
DCHECK(CalledOnValidThread());
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 UDPSocketWin::SetSendBufferSize(int32 size) {
DCHECK(CalledOnValidThread());
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;
}

void UDPSocketWin::DoReadCallback(int rv) {
DCHECK_NE(rv, ERR_IO_PENDING);
DCHECK(read_callback_);
Expand Down
6 changes: 6 additions & 0 deletions net/udp/udp_socket_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ class UDPSocketWin : public base::NonThreadSafe {
const IPEndPoint& address,
OldCompletionCallback* callback);

// Set the receive buffer size (in bytes) for the socket.
bool SetReceiveBufferSize(int32 size);

// Set the send buffer size (in bytes) for the socket.
bool SetSendBufferSize(int32 size);

// Returns true if the socket is already connected or bound.
bool is_connected() const { return socket_ != INVALID_SOCKET; }

Expand Down

0 comments on commit df31da4

Please sign in to comment.