Bug 1161020: Added clean interface to |UnixSocketConnector|, r=kmachulis
☠☠ backed out by b0784c6cf439 ☠ ☠
authorThomas Zimmermann <tdz@users.sourceforge.net>
Mon, 18 May 2015 11:28:30 +0200
changeset 244340 f90b8d3d6b709896b1a8b106ba84743610876ad3
parent 244339 a57b47c23eddc63be79ee921180bf572616b532e
child 244341 13753f9043f72975ad483ae1564dc955c19f95e7
push id28774
push userryanvm@gmail.com
push dateMon, 18 May 2015 20:24:02 +0000
treeherdermozilla-central@3c292fab543d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmachulis
bugs1161020
milestone41.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1161020: Added clean interface to |UnixSocketConnector|, r=kmachulis The current interface of |UnixSocketConnector| doesn't follow any design and is not easily understandable. This patch adds a new interface to the class. The new interface provides a method for each of the following operations: * converting an address to a human-readable string, * creating a listening socket, * creating a stream socket, and * accepting a stream socket from a listening socket. All arguments are stored in the connector class, so that connect and listen methods of the socket classes don't require protocol-specific arguments. All socket parameters are set within the connector class, so each connector class can now select parameters individually.
ipc/unixsocket/UnixSocketConnector.cpp
ipc/unixsocket/UnixSocketConnector.h
--- a/ipc/unixsocket/UnixSocketConnector.cpp
+++ b/ipc/unixsocket/UnixSocketConnector.cpp
@@ -1,16 +1,58 @@
 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
 /* vim: set ts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "UnixSocketConnector.h"
+#include "nsISupportsImpl.h" // For MOZ_COUNT_CTOR, MOZ_COUNT_DTOR
 
 namespace mozilla {
 namespace ipc {
 
+UnixSocketConnector::UnixSocketConnector()
+{
+  MOZ_COUNT_CTOR(UnixSocketConnector);
+}
+
 UnixSocketConnector::~UnixSocketConnector()
-{ }
+{
+  MOZ_COUNT_DTOR(UnixSocketConnector);
+}
+
+nsresult
+UnixSocketConnector::ConvertAddressToString(const struct sockaddr& aAddress,
+                                            socklen_t aAddressLength,
+                                            nsACString& aAddressString)
+{
+  MOZ_CRASH("|UnixSocketConnector| does not convert addresses to strings.");
+  return NS_ERROR_ABORT;
+}
+
+nsresult
+UnixSocketConnector::CreateListenSocket(struct sockaddr* aAddress,
+                                        socklen_t* aAddressLength,
+                                        int& aListenFd)
+{
+  MOZ_CRASH("|UnixSocketConnector| does not support listening sockets.");
+}
+
+nsresult
+UnixSocketConnector::AcceptStreamSocket(int aListenFd,
+                                        struct sockaddr* aAddress,
+                                        socklen_t* aAddressLen,
+                                        int& aStreamFd)
+{
+  MOZ_CRASH("|UnixSocketConnector| does not support accepting stream sockets.");
+}
+
+nsresult
+UnixSocketConnector::CreateStreamSocket(struct sockaddr* aAddress,
+                                        socklen_t* aAddressLength,
+                                        int& aStreamFd)
+{
+  MOZ_CRASH("|UnixSocketConnector| does not support creating stream sockets.");
+}
 
 }
 }
--- a/ipc/unixsocket/UnixSocketConnector.h
+++ b/ipc/unixsocket/UnixSocketConnector.h
@@ -2,46 +2,101 @@
 /* vim: set ts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_ipc_unixsocketconnector_h
 #define mozilla_ipc_unixsocketconnector_h
 
+#include <sys/socket.h>
 #include "mozilla/ipc/UnixSocketWatcher.h"
 #include "nsString.h"
 
 namespace mozilla {
 namespace ipc {
 
 /**
  * |UnixSocketConnector| defines the socket creation and connection/listening
  * functions for |UnixSocketConsumer|, et al. Due to the fact that socket setup
- * can vary between protocols (unix sockets, tcp sockets, bluetooth sockets, etc),
+ * can vary between protocols (Unix sockets, TCP sockets, Bluetooth sockets, etc),
  * this allows the user to create whatever connection mechanism they need while
  * still depending on libevent for non-blocking communication handling.
  */
 class UnixSocketConnector
 {
 public:
   virtual ~UnixSocketConnector();
 
   /**
+   * Converts an address to a human-readable string.
+   *
+   * @param aAddress A socket address
+   * @param aAddressLength The number of valid bytes in |aAddress|
+   * @param[out] aAddressString The resulting string
+   * @return NS_OK on success, or an XPCOM error code otherwise.
+   */
+  virtual nsresult ConvertAddressToString(const struct sockaddr& aAddress,
+                                          socklen_t aAddressLength,
+                                          nsACString& aAddressString);
+
+  /**
+   * Creates a listening socket. I/O thread only.
+   *
+   * @param[out] aAddress The listening socket's address
+   * @param[out] aAddressLength The number of valid bytes in |aAddress|
+   * @param[out] aListenFd The socket's file descriptor
+   * @return NS_OK on success, or an XPCOM error code otherwise.
+   */
+  virtual nsresult CreateListenSocket(struct sockaddr* aAddress,
+                                      socklen_t* aAddressLength,
+                                      int& aListenFd);
+
+  /**
+   * Accepts a stream socket from a listening socket. I/O thread only.
+   *
+   * @param aListenFd The listening socket
+   * @param[out] aAddress Returns the stream socket's address
+   * @param[out] aAddressLength Returns the number of valid bytes in |aAddress|
+   * @param[out] aStreamFd The stream socket's file descriptor
+   * @return NS_OK on success, or an XPCOM error code otherwise.
+   */
+  virtual nsresult AcceptStreamSocket(int aListenFd,
+                                      struct sockaddr* aAddress,
+                                      socklen_t* aAddressLen,
+                                      int& aStreamFd);
+
+  /**
+   * Creates a stream socket. I/O thread only.
+   *
+   * @param[in|out] aAddress The stream socket's address
+   * @param[in|out] aAddressLength The number of valid bytes in |aAddress|
+   * @param[out] aStreamFd The socket's file descriptor
+   * @return NS_OK on success, or an XPCOM error code otherwise.
+   */
+  virtual nsresult CreateStreamSocket(struct sockaddr* aAddress,
+                                      socklen_t* aAddressLength,
+                                      int& aStreamFd);
+
+  /**
    * Establishs a file descriptor for a socket.
    *
+   * @deprecated
+   *
    * @return File descriptor for socket
    */
   virtual int Create() = 0;
 
   /**
    * Since most socket specifics are related to address formation into a
    * sockaddr struct, this function is defined by subclasses and fills in the
    * structure as needed for whatever connection it is trying to build
    *
+   * @deprecated
+   *
    * @param aIsServer True is we are acting as a server socket
    * @param aAddrSize Size of the struct
    * @param aAddr Struct to fill
    * @param aAddress If aIsServer is false, Address to connect to. nullptr otherwise.
    *
    * @return True if address is filled correctly, false otherwise
    */
   virtual bool CreateAddr(bool aIsServer,
@@ -57,29 +112,36 @@ public:
    *
    * @return true is successful, false otherwise
    */
   virtual bool SetUp(int aFd) = 0;
 
   /**
    * Perform socket setup for socket created by ListenSocket(), after listen().
    *
+   * @deprecated
+   *
    * @param aFd File descriptor for opened socket
    *
    * @return true is successful, false otherwise
    */
   virtual bool SetUpListenSocket(int aFd) = 0;
 
   /**
    * Get address of socket we're currently connected to. Return null string if
    * not connected.
    *
+   * @deprecated
+   *
    * @param aAddr Address struct
    * @param aAddrStr String to store address to
    */
   virtual void GetSocketAddr(const sockaddr_any& aAddr,
                              nsAString& aAddrStr) = 0;
+
+protected:
+  UnixSocketConnector();
 };
 
 }
 }
 
 #endif