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
This commit is contained in:
parent
74ebdf896c
commit
b226f1242e
@ -651,13 +651,7 @@ class FreeBSDSocket : public Socket {
|
||||
}
|
||||
explicit FreeBSDSocket(int socket): socket_(socket) { }
|
||||
|
||||
|
||||
virtual ~FreeBSDSocket() {
|
||||
if (IsValid()) {
|
||||
// Close socket.
|
||||
close(socket_);
|
||||
}
|
||||
}
|
||||
virtual ~FreeBSDSocket() { Close(); }
|
||||
|
||||
// Server initialization.
|
||||
bool Bind(const int port);
|
||||
@ -667,6 +661,9 @@ class FreeBSDSocket : public Socket {
|
||||
// Client initialization.
|
||||
bool Connect(const char* host, const char* port);
|
||||
|
||||
// Close.
|
||||
bool Close();
|
||||
|
||||
// Data Transimission
|
||||
int Send(const 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) {
|
||||
}
|
||||
|
||||
|
||||
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 status = send(socket_, data, len, 0);
|
||||
return status;
|
||||
|
@ -652,13 +652,7 @@ class LinuxSocket : public Socket {
|
||||
}
|
||||
explicit LinuxSocket(int socket): socket_(socket) { }
|
||||
|
||||
|
||||
virtual ~LinuxSocket() {
|
||||
if (IsValid()) {
|
||||
// Close socket.
|
||||
close(socket_);
|
||||
}
|
||||
}
|
||||
virtual ~LinuxSocket() { Close(); }
|
||||
|
||||
// Server initialization.
|
||||
bool Bind(const int port);
|
||||
@ -668,6 +662,9 @@ class LinuxSocket : public Socket {
|
||||
// Client initialization.
|
||||
bool Connect(const char* host, const char* port);
|
||||
|
||||
// Close.
|
||||
bool Close();
|
||||
|
||||
// Data Transimission
|
||||
int Send(const 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) {
|
||||
}
|
||||
|
||||
|
||||
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 status = send(socket_, data, len, 0);
|
||||
return status;
|
||||
|
@ -577,13 +577,7 @@ class MacOSSocket : public Socket {
|
||||
}
|
||||
explicit MacOSSocket(int socket): socket_(socket) { }
|
||||
|
||||
|
||||
virtual ~MacOSSocket() {
|
||||
if (IsValid()) {
|
||||
// Close socket.
|
||||
close(socket_);
|
||||
}
|
||||
}
|
||||
virtual ~MacOSSocket() { Close(); }
|
||||
|
||||
// Server initialization.
|
||||
bool Bind(const int port);
|
||||
@ -593,6 +587,9 @@ class MacOSSocket : public Socket {
|
||||
// Client initialization.
|
||||
bool Connect(const char* host, const char* port);
|
||||
|
||||
// Close.
|
||||
bool Close();
|
||||
|
||||
// Data Transimission
|
||||
int Send(const 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) {
|
||||
}
|
||||
|
||||
|
||||
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 status = send(socket_, data, len, 0);
|
||||
return status;
|
||||
|
@ -1550,12 +1550,7 @@ class Win32Socket : public Socket {
|
||||
explicit Win32Socket(SOCKET socket): socket_(socket) { }
|
||||
|
||||
|
||||
virtual ~Win32Socket() {
|
||||
if (IsValid()) {
|
||||
// Close socket.
|
||||
closesocket(socket_);
|
||||
}
|
||||
}
|
||||
virtual ~Win32Socket() { Close(); }
|
||||
|
||||
// Server initialization.
|
||||
bool Bind(const int port);
|
||||
@ -1565,6 +1560,9 @@ class Win32Socket : public Socket {
|
||||
// Client initialization.
|
||||
bool Connect(const char* host, const char* port);
|
||||
|
||||
// Close.
|
||||
bool Close();
|
||||
|
||||
// Data Transimission
|
||||
int Send(const 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) {
|
||||
}
|
||||
|
||||
|
||||
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 status = send(socket_, data, len, 0);
|
||||
return status;
|
||||
|
@ -439,6 +439,9 @@ class Socket {
|
||||
// Client initialization.
|
||||
virtual bool Connect(const char* host, const char* port) = 0;
|
||||
|
||||
// Close.
|
||||
virtual bool Close() = 0;
|
||||
|
||||
// Data Transimission
|
||||
virtual int Send(const 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) {
|
||||
}
|
||||
|
||||
// Close the client before the listener to avoid TIME_WAIT issues.
|
||||
client->Close();
|
||||
delete client;
|
||||
delete listener;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user