Add support for setting the SO_REUSEADDR option for sockets.

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1557 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
sgjesse@chromium.org 2009-03-20 08:53:57 +00:00
parent 318c9eed21
commit 83111deb97
5 changed files with 40 additions and 0 deletions

View File

@ -667,6 +667,8 @@ class FreeBSDSocket : public Socket {
int Send(const char* data, int len) const;
int Receive(char* data, int len) const;
bool SetReuseAddress(bool reuse_address);
bool IsValid() const { return socket_ != -1; }
private:
@ -763,6 +765,13 @@ int FreeBSDSocket::Receive(char* data, int len) const {
}
bool FreeBSDSocket::SetReuseAddress(bool reuse_address) {
int on = reuse_address ? 1 : 0;
int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
return status == 0;
}
bool Socket::Setup() {
// Nothing to do on FreeBSD.
return true;

View File

@ -668,6 +668,8 @@ class LinuxSocket : public Socket {
int Send(const char* data, int len) const;
int Receive(char* data, int len) const;
bool SetReuseAddress(bool reuse_address);
bool IsValid() const { return socket_ != -1; }
private:
@ -764,6 +766,13 @@ int LinuxSocket::Receive(char* data, int len) const {
}
bool LinuxSocket::SetReuseAddress(bool reuse_address) {
int on = reuse_address ? 1 : 0;
int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
return status == 0;
}
bool Socket::Setup() {
// Nothing to do on Linux.
return true;

View File

@ -593,6 +593,8 @@ class MacOSSocket : public Socket {
int Send(const char* data, int len) const;
int Receive(char* data, int len) const;
bool SetReuseAddress(bool reuse_address);
bool IsValid() const { return socket_ != -1; }
private:
@ -695,6 +697,13 @@ int MacOSSocket::Receive(char* data, int len) const {
}
bool MacOSSocket::SetReuseAddress(bool reuse_address) {
int on = reuse_address ? 1 : 0;
int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
return status == 0;
}
bool Socket::Setup() {
// Nothing to do on MacOS.
return true;

View File

@ -1565,6 +1565,8 @@ class Win32Socket : public Socket {
int Send(const char* data, int len) const;
int Receive(char* data, int len) const;
bool SetReuseAddress(bool reuse_address);
bool IsValid() const { return socket_ != INVALID_SOCKET; }
private:
@ -1661,6 +1663,14 @@ int Win32Socket::Receive(char* data, int len) const {
}
bool Win32Socket::SetReuseAddress(bool reuse_address) {
BOOL on = reuse_address ? TRUE : FALSE;
int status = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<char*>(&on), sizeof(on));
return status == SOCKET_ERROR;
}
bool Socket::Setup() {
// Initialize Winsock32
int err;

View File

@ -448,6 +448,9 @@ class Socket {
virtual int Send(const char* data, int len) const = 0;
virtual int Receive(char* data, int len) const = 0;
// Set the value of the SO_REUSEADDR socket option.
virtual bool SetReuseAddress(bool reuse_address) = 0;
virtual bool IsValid() const = 0;
static bool Setup();