Bug 1170466: Share socket I/O methods in |ConnectionOrientedSocketIO|, r=kmachulis
authorThomas Zimmermann <tdz@users.sourceforge.net>
Wed, 03 Jun 2015 11:53:50 +0200
changeset 246912 3bd34542b7c17a1c8361bd20a1e8dced96721e31
parent 246911 ef82cdf1079f3c2626d5a3dff0faab97acc5ec3f
child 246913 eecddaa2cc3c5348976886c87bc32b3b22eeee02
push id28846
push userryanvm@gmail.com
push dateWed, 03 Jun 2015 19:51:00 +0000
treeherdermozilla-central@e5ee2c56963c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmachulis
bugs1170466
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 1170466: Share socket I/O methods in |ConnectionOrientedSocketIO|, r=kmachulis With this patch, |ConnectionOrientedSocketIO| implements methods for sending and receiving data. All sub-classes have been changed accordingly.
ipc/bluetooth/BluetoothDaemonConnection.cpp
ipc/unixsocket/ConnectionOrientedSocket.cpp
ipc/unixsocket/ConnectionOrientedSocket.h
ipc/unixsocket/StreamSocket.cpp
--- a/ipc/bluetooth/BluetoothDaemonConnection.cpp
+++ b/ipc/bluetooth/BluetoothDaemonConnection.cpp
@@ -204,27 +204,16 @@ class BluetoothDaemonConnectionIO final 
 {
 public:
   BluetoothDaemonConnectionIO(nsIThread* aConsumerThread,
                               MessageLoop* aIOLoop,
                               int aFd, ConnectionStatus aConnectionStatus,
                               BluetoothDaemonConnection* aConnection,
                               BluetoothDaemonPDUConsumer* aConsumer);
 
-  // Task callback methods
-  //
-
-  void Send(UnixSocketIOBuffer* aBuffer);
-
-  void OnSocketCanReceiveWithoutBlocking() override;
-  void OnSocketCanSendWithoutBlocking() override;
-
-  void OnConnected() override;
-  void OnError(const char* aFunction, int aErrno) override;
-
   // Methods for |ConnectionOrientedSocketIO|
   //
 
   nsresult Accept(int aFd,
                   const struct sockaddr* aAddress,
                   socklen_t aAddressLength) override;
 
   // Methods for |DataSocketIO|
@@ -266,82 +255,16 @@ BluetoothDaemonConnectionIO::BluetoothDa
   , mConnection(aConnection)
   , mConsumer(aConsumer)
   , mShuttingDownOnIOThread(false)
 {
   MOZ_ASSERT(mConnection);
   MOZ_ASSERT(mConsumer);
 }
 
-void
-BluetoothDaemonConnectionIO::Send(UnixSocketIOBuffer* aBuffer)
-{
-  MOZ_ASSERT(aBuffer);
-
-  EnqueueData(aBuffer);
-  AddWatchers(WRITE_WATCHER, false);
-}
-
-void
-BluetoothDaemonConnectionIO::OnSocketCanReceiveWithoutBlocking()
-{
-  ssize_t res = ReceiveData(GetFd());
-  if (res < 0) {
-    /* I/O error */
-    RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
-  } else if (!res) {
-    /* EOF or peer shutdown */
-    RemoveWatchers(READ_WATCHER);
-  }
-}
-
-void
-BluetoothDaemonConnectionIO::OnSocketCanSendWithoutBlocking()
-{
-  MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
-  MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED);
-  MOZ_ASSERT(!IsShutdownOnIOThread());
-
-  if (NS_WARN_IF(NS_FAILED(SendPendingData(GetFd())))) {
-    RemoveWatchers(WRITE_WATCHER);
-  }
-}
-
-void
-BluetoothDaemonConnectionIO::OnConnected()
-{
-  MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
-  MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED);
-
-  GetConsumerThread()->Dispatch(
-    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS),
-    NS_DISPATCH_NORMAL);
-
-  AddWatchers(READ_WATCHER, true);
-  if (HasPendingData()) {
-    AddWatchers(WRITE_WATCHER, false);
-  }
-}
-
-void
-BluetoothDaemonConnectionIO::OnError(const char* aFunction, int aErrno)
-{
-  MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
-
-  UnixFdWatcher::OnError(aFunction, aErrno);
-
-  // Clean up watchers, status, fd
-  Close();
-
-  // Tell the consumer thread we've errored
-  GetConsumerThread()->Dispatch(
-    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_ERROR),
-    NS_DISPATCH_NORMAL);
-}
-
 // |ConnectionOrientedSocketIO|
 
 nsresult
 BluetoothDaemonConnectionIO::Accept(int aFd,
                                     const struct sockaddr* aAddress,
                                     socklen_t aAddressLength)
 {
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
--- a/ipc/unixsocket/ConnectionOrientedSocket.cpp
+++ b/ipc/unixsocket/ConnectionOrientedSocket.cpp
@@ -27,16 +27,100 @@ ConnectionOrientedSocketIO::ConnectionOr
   MessageLoop* aIOLoop)
   : DataSocketIO(aConsumerThread)
   , UnixSocketWatcher(aIOLoop)
 { }
 
 ConnectionOrientedSocketIO::~ConnectionOrientedSocketIO()
 { }
 
+void
+ConnectionOrientedSocketIO::Send(UnixSocketIOBuffer* aBuffer)
+{
+  MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
+
+  EnqueueData(aBuffer);
+  AddWatchers(WRITE_WATCHER, false);
+}
+
+// |UnixSocketWatcher|
+
+void
+ConnectionOrientedSocketIO::OnSocketCanReceiveWithoutBlocking()
+{
+  MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
+  MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); // see bug 990984
+
+  ssize_t res = ReceiveData(GetFd());
+  if (res < 0) {
+    /* I/O error */
+    RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
+  } else if (!res) {
+    /* EOF or peer shutdown */
+    RemoveWatchers(READ_WATCHER);
+  }
+}
+
+void
+ConnectionOrientedSocketIO::OnSocketCanSendWithoutBlocking()
+{
+  MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
+  MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); // see bug 990984
+  MOZ_ASSERT(!IsShutdownOnIOThread());
+
+  nsresult rv = SendPendingData(GetFd());
+  if (NS_FAILED(rv)) {
+    return;
+  }
+
+  if (HasPendingData()) {
+    AddWatchers(WRITE_WATCHER, false);
+  }
+}
+
+void
+ConnectionOrientedSocketIO::OnConnected()
+{
+  MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
+  MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED);
+
+  GetConsumerThread()->Dispatch(
+    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS),
+    NS_DISPATCH_NORMAL);
+
+  AddWatchers(READ_WATCHER, true);
+  if (HasPendingData()) {
+    AddWatchers(WRITE_WATCHER, false);
+  }
+}
+
+void
+ConnectionOrientedSocketIO::OnListening()
+{
+  MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
+
+  NS_NOTREACHED("Invalid call to |ConnectionOrientedSocketIO::OnListening|");
+}
+
+void
+ConnectionOrientedSocketIO::OnError(const char* aFunction, int aErrno)
+{
+  MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
+
+  UnixFdWatcher::OnError(aFunction, aErrno);
+
+  // Clean up watchers, status, fd
+  Close();
+
+  // Tell the consumer thread we've errored
+  GetConsumerThread()->Dispatch(
+    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_ERROR),
+    NS_DISPATCH_NORMAL);
+}
+
 //
 // ConnectionOrientedSocket
 //
 
 ConnectionOrientedSocket::~ConnectionOrientedSocket()
 { }
 
 }
--- a/ipc/unixsocket/ConnectionOrientedSocket.h
+++ b/ipc/unixsocket/ConnectionOrientedSocket.h
@@ -30,16 +30,28 @@ class ConnectionOrientedSocketIO
 {
 public:
   virtual ~ConnectionOrientedSocketIO();
 
   virtual nsresult Accept(int aFd,
                           const struct sockaddr* aAddress,
                           socklen_t aAddressLength) = 0;
 
+  void Send(UnixSocketIOBuffer* aBuffer);
+
+  // Methods for |UnixSocketWatcher|
+  //
+
+  void OnSocketCanReceiveWithoutBlocking() final;
+  void OnSocketCanSendWithoutBlocking() final;
+
+  void OnListening() final;
+  void OnConnected() final;
+  void OnError(const char* aFunction, int aErrno) final;
+
 protected:
   /**
    * Constructs an instance of |ConnectionOrientedSocketIO|
    *
    * @param aConsumerThread The socket's consumer thread.
    * @param aIOLoop The socket's I/O loop.
    * @param aFd The socket file descriptor.
    * @param aConnectionStatus The connection status for |aFd|.
--- a/ipc/unixsocket/StreamSocket.cpp
+++ b/ipc/unixsocket/StreamSocket.cpp
@@ -51,27 +51,16 @@ public:
   // Task callback methods
   //
 
   /**
    * Connect to a socket
    */
   void Connect();
 
-  void Send(UnixSocketIOBuffer* aBuffer);
-
-  // I/O callback methods
-  //
-
-  void OnConnected() override;
-  void OnError(const char* aFunction, int aErrno) override;
-  void OnListening() override;
-  void OnSocketCanReceiveWithoutBlocking() override;
-  void OnSocketCanSendWithoutBlocking() override;
-
   // Methods for |ConnectionOrientedSocketIO|
   //
 
   nsresult Accept(int aFd,
                   const struct sockaddr* aAddress,
                   socklen_t aAddressLength) override;
 
   // Methods for |DataSocket|
@@ -235,88 +224,16 @@ StreamSocketIO::Connect()
   SetFd(fd);
 
   // calls OnConnected() on success, or OnError() otherwise
   rv = UnixSocketWatcher::Connect(address, mAddressLength);
   NS_WARN_IF(NS_FAILED(rv));
 }
 
 void
-StreamSocketIO::Send(UnixSocketIOBuffer* aData)
-{
-  EnqueueData(aData);
-  AddWatchers(WRITE_WATCHER, false);
-}
-
-void
-StreamSocketIO::OnConnected()
-{
-  MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
-  MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED);
-
-  GetConsumerThread()->Dispatch(
-    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS),
-    NS_DISPATCH_NORMAL);
-
-  AddWatchers(READ_WATCHER, true);
-  if (HasPendingData()) {
-    AddWatchers(WRITE_WATCHER, false);
-  }
-}
-
-void
-StreamSocketIO::OnListening()
-{
-  MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
-
-  NS_NOTREACHED("Invalid call to |StreamSocketIO::OnListening|");
-}
-
-void
-StreamSocketIO::OnError(const char* aFunction, int aErrno)
-{
-  MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
-
-  UnixFdWatcher::OnError(aFunction, aErrno);
-  FireSocketError();
-}
-
-void
-StreamSocketIO::OnSocketCanReceiveWithoutBlocking()
-{
-  MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
-  MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); // see bug 990984
-
-  ssize_t res = ReceiveData(GetFd());
-  if (res < 0) {
-    /* I/O error */
-    RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
-  } else if (!res) {
-    /* EOF or peer shutdown */
-    RemoveWatchers(READ_WATCHER);
-  }
-}
-
-void
-StreamSocketIO::OnSocketCanSendWithoutBlocking()
-{
-  MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
-  MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); // see bug 990984
-
-  nsresult rv = SendPendingData(GetFd());
-  if (NS_FAILED(rv)) {
-    return;
-  }
-
-  if (HasPendingData()) {
-    AddWatchers(WRITE_WATCHER, false);
-  }
-}
-
-void
 StreamSocketIO::FireSocketError()
 {
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
 
   // Clean up watchers, statuses, fds
   Close();
 
   // Tell the consumer thread we've errored