Commit b226f124 authored by sgjesse@chromium.org's avatar sgjesse@chromium.org

Add a close method to sockets.

Now the destructor is not the only way of closing a socket, which was a bit to limited.
Review URL: http://codereview.chromium.org/42330

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1535 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 74ebdf89
...@@ -651,13 +651,7 @@ class FreeBSDSocket : public Socket { ...@@ -651,13 +651,7 @@ class FreeBSDSocket : public Socket {
} }
explicit FreeBSDSocket(int socket): socket_(socket) { } explicit FreeBSDSocket(int socket): socket_(socket) { }
virtual ~FreeBSDSocket() { Close(); }
virtual ~FreeBSDSocket() {
if (IsValid()) {
// Close socket.
close(socket_);
}
}
// Server initialization. // Server initialization.
bool Bind(const int port); bool Bind(const int port);
...@@ -667,6 +661,9 @@ class FreeBSDSocket : public Socket { ...@@ -667,6 +661,9 @@ class FreeBSDSocket : public Socket {
// Client initialization. // Client initialization.
bool Connect(const char* host, const char* port); bool Connect(const char* host, const char* port);
// Close.
bool Close();
// Data Transimission // Data Transimission
int Send(const char* data, int len) const; int Send(const char* data, int len) const;
int Receive(char* data, int len) const; int Receive(char* data, int len) const;
...@@ -742,6 +739,16 @@ bool FreeBSDSocket::Connect(const char* host, const char* port) { ...@@ -742,6 +739,16 @@ bool FreeBSDSocket::Connect(const char* host, const char* port) {
} }
bool FreeBSDSocket::Close() {
if (IsValid()) {
// Close socket.
int status = close(socket_);
socket_ = -1;
return status == 0;
}
return true;
}
int FreeBSDSocket::Send(const char* data, int len) const { int FreeBSDSocket::Send(const char* data, int len) const {
int status = send(socket_, data, len, 0); int status = send(socket_, data, len, 0);
return status; return status;
......
...@@ -652,13 +652,7 @@ class LinuxSocket : public Socket { ...@@ -652,13 +652,7 @@ class LinuxSocket : public Socket {
} }
explicit LinuxSocket(int socket): socket_(socket) { } explicit LinuxSocket(int socket): socket_(socket) { }
virtual ~LinuxSocket() { Close(); }
virtual ~LinuxSocket() {
if (IsValid()) {
// Close socket.
close(socket_);
}
}
// Server initialization. // Server initialization.
bool Bind(const int port); bool Bind(const int port);
...@@ -668,6 +662,9 @@ class LinuxSocket : public Socket { ...@@ -668,6 +662,9 @@ class LinuxSocket : public Socket {
// Client initialization. // Client initialization.
bool Connect(const char* host, const char* port); bool Connect(const char* host, const char* port);
// Close.
bool Close();
// Data Transimission // Data Transimission
int Send(const char* data, int len) const; int Send(const char* data, int len) const;
int Receive(char* data, int len) const; int Receive(char* data, int len) const;
...@@ -743,6 +740,17 @@ bool LinuxSocket::Connect(const char* host, const char* port) { ...@@ -743,6 +740,17 @@ bool LinuxSocket::Connect(const char* host, const char* port) {
} }
bool LinuxSocket::Close() {
if (IsValid()) {
// Close socket.
int status = close(socket_);
socket_ = -1;
return status == 0;
}
return true;
}
int LinuxSocket::Send(const char* data, int len) const { int LinuxSocket::Send(const char* data, int len) const {
int status = send(socket_, data, len, 0); int status = send(socket_, data, len, 0);
return status; return status;
......
...@@ -577,13 +577,7 @@ class MacOSSocket : public Socket { ...@@ -577,13 +577,7 @@ class MacOSSocket : public Socket {
} }
explicit MacOSSocket(int socket): socket_(socket) { } explicit MacOSSocket(int socket): socket_(socket) { }
virtual ~MacOSSocket() { Close(); }
virtual ~MacOSSocket() {
if (IsValid()) {
// Close socket.
close(socket_);
}
}
// Server initialization. // Server initialization.
bool Bind(const int port); bool Bind(const int port);
...@@ -593,6 +587,9 @@ class MacOSSocket : public Socket { ...@@ -593,6 +587,9 @@ class MacOSSocket : public Socket {
// Client initialization. // Client initialization.
bool Connect(const char* host, const char* port); bool Connect(const char* host, const char* port);
// Close.
bool Close();
// Data Transimission // Data Transimission
int Send(const char* data, int len) const; int Send(const char* data, int len) const;
int Receive(char* data, int len) const; int Receive(char* data, int len) const;
...@@ -674,6 +671,17 @@ bool MacOSSocket::Connect(const char* host, const char* port) { ...@@ -674,6 +671,17 @@ bool MacOSSocket::Connect(const char* host, const char* port) {
} }
bool MacOSSocket::Close() {
if (IsValid()) {
// Close socket.
int status = close(socket_);
socket_ = -1;
return status == 0;
}
return true;
}
int MacOSSocket::Send(const char* data, int len) const { int MacOSSocket::Send(const char* data, int len) const {
int status = send(socket_, data, len, 0); int status = send(socket_, data, len, 0);
return status; return status;
......
...@@ -1550,12 +1550,7 @@ class Win32Socket : public Socket { ...@@ -1550,12 +1550,7 @@ class Win32Socket : public Socket {
explicit Win32Socket(SOCKET socket): socket_(socket) { } explicit Win32Socket(SOCKET socket): socket_(socket) { }
virtual ~Win32Socket() { virtual ~Win32Socket() { Close(); }
if (IsValid()) {
// Close socket.
closesocket(socket_);
}
}
// Server initialization. // Server initialization.
bool Bind(const int port); bool Bind(const int port);
...@@ -1565,6 +1560,9 @@ class Win32Socket : public Socket { ...@@ -1565,6 +1560,9 @@ class Win32Socket : public Socket {
// Client initialization. // Client initialization.
bool Connect(const char* host, const char* port); bool Connect(const char* host, const char* port);
// Close.
bool Close();
// Data Transimission // Data Transimission
int Send(const char* data, int len) const; int Send(const char* data, int len) const;
int Receive(char* data, int len) const; int Receive(char* data, int len) const;
...@@ -1640,6 +1638,17 @@ bool Win32Socket::Connect(const char* host, const char* port) { ...@@ -1640,6 +1638,17 @@ bool Win32Socket::Connect(const char* host, const char* port) {
} }
bool Win32Socket::Close() {
if (IsValid()) {
// Close socket.
int rc = closesocket(socket_);
socket_ = INVALID_SOCKET;
return rc != SOCKET_ERROR;
}
return true;
}
int Win32Socket::Send(const char* data, int len) const { int Win32Socket::Send(const char* data, int len) const {
int status = send(socket_, data, len, 0); int status = send(socket_, data, len, 0);
return status; return status;
......
...@@ -439,6 +439,9 @@ class Socket { ...@@ -439,6 +439,9 @@ class Socket {
// Client initialization. // Client initialization.
virtual bool Connect(const char* host, const char* port) = 0; virtual bool Connect(const char* host, const char* port) = 0;
// Close.
virtual bool Close() = 0;
// Data Transimission // Data Transimission
virtual int Send(const char* data, int len) const = 0; virtual int Send(const char* data, int len) const = 0;
virtual int Receive(char* data, int len) const = 0; virtual int Receive(char* data, int len) const = 0;
......
...@@ -103,6 +103,7 @@ static void SendAndReceive(char *data, int len) { ...@@ -103,6 +103,7 @@ static void SendAndReceive(char *data, int len) {
} }
// Close the client before the listener to avoid TIME_WAIT issues. // Close the client before the listener to avoid TIME_WAIT issues.
client->Close();
delete client; delete client;
delete listener; delete listener;
} }
......
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