Author: [email protected]
Date: Wed Mar 18 06:11:43 2009
New Revision: 1535

Modified:
    branches/bleeding_edge/src/platform-freebsd.cc
    branches/bleeding_edge/src/platform-linux.cc
    branches/bleeding_edge/src/platform-macos.cc
    branches/bleeding_edge/src/platform-win32.cc
    branches/bleeding_edge/src/platform.h
    branches/bleeding_edge/test/cctest/test-sockets.cc

Log:
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

Modified: branches/bleeding_edge/src/platform-freebsd.cc
==============================================================================
--- branches/bleeding_edge/src/platform-freebsd.cc      (original)
+++ branches/bleeding_edge/src/platform-freebsd.cc      Wed Mar 18 06:11:43 2009
@@ -651,13 +651,7 @@
    }
    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 @@
    // 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;
@@ -741,6 +738,16 @@
    return status == 0;
  }

+
+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);

Modified: branches/bleeding_edge/src/platform-linux.cc
==============================================================================
--- branches/bleeding_edge/src/platform-linux.cc        (original)
+++ branches/bleeding_edge/src/platform-linux.cc        Wed Mar 18 06:11:43 2009
@@ -652,13 +652,7 @@
    }
    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 @@
    // 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;
@@ -740,6 +737,17 @@
    // Connect.
    status = connect(socket_, result->ai_addr, result->ai_addrlen);
    return status == 0;
+}
+
+
+bool LinuxSocket::Close() {
+  if (IsValid()) {
+    // Close socket.
+    int status = close(socket_);
+    socket_ = -1;
+    return status == 0;
+  }
+  return true;
  }



Modified: branches/bleeding_edge/src/platform-macos.cc
==============================================================================
--- branches/bleeding_edge/src/platform-macos.cc        (original)
+++ branches/bleeding_edge/src/platform-macos.cc        Wed Mar 18 06:11:43 2009
@@ -577,13 +577,7 @@
    }
    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 @@
    // 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;
@@ -671,6 +668,17 @@
    // Connect.
    status = connect(socket_, result->ai_addr, result->ai_addrlen);
    return status == 0;
+}
+
+
+bool MacOSSocket::Close() {
+  if (IsValid()) {
+    // Close socket.
+    int status = close(socket_);
+    socket_ = -1;
+    return status == 0;
+  }
+  return true;
  }



Modified: branches/bleeding_edge/src/platform-win32.cc
==============================================================================
--- branches/bleeding_edge/src/platform-win32.cc        (original)
+++ branches/bleeding_edge/src/platform-win32.cc        Wed Mar 18 06:11:43 2009
@@ -1550,12 +1550,7 @@
    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 @@
    // 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;
@@ -1637,6 +1635,17 @@
    // Connect.
    status = connect(socket_, result->ai_addr, result->ai_addrlen);
    return status == 0;
+}
+
+
+bool Win32Socket::Close() {
+  if (IsValid()) {
+    // Close socket.
+    int rc = closesocket(socket_);
+    socket_ = INVALID_SOCKET;
+    return rc != SOCKET_ERROR;
+  }
+  return true;
  }



Modified: branches/bleeding_edge/src/platform.h
==============================================================================
--- branches/bleeding_edge/src/platform.h       (original)
+++ branches/bleeding_edge/src/platform.h       Wed Mar 18 06:11:43 2009
@@ -439,6 +439,9 @@
    // 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;

Modified: branches/bleeding_edge/test/cctest/test-sockets.cc
==============================================================================
--- branches/bleeding_edge/test/cctest/test-sockets.cc  (original)
+++ branches/bleeding_edge/test/cctest/test-sockets.cc  Wed Mar 18 06:11:43  
2009
@@ -103,6 +103,7 @@
    }

    // Close the client before the listener to avoid TIME_WAIT issues.
+  client->Close();
    delete client;
    delete listener;
  }

--~--~---------~--~----~------------~-------~--~----~
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to