Add a close method to sockets.
authorsgjesse@chromium.org <sgjesse@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 18 Mar 2009 13:11:43 +0000 (13:11 +0000)
committersgjesse@chromium.org <sgjesse@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 18 Mar 2009 13:11:43 +0000 (13:11 +0000)
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

src/platform-freebsd.cc
src/platform-linux.cc
src/platform-macos.cc
src/platform-win32.cc
src/platform.h
test/cctest/test-sockets.cc

index d68ccae..6e2046c 100644 (file)
@@ -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;
index 406ff78..3270976 100644 (file)
@@ -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;
index 2c85065..972883e 100644 (file)
@@ -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;
index 00e5a7c..26997ab 100644 (file)
@@ -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;
index aca91ee..9d1b373 100644 (file)
@@ -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;
index 9316a26..ae3031f 100644 (file)
@@ -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;
 }