Bug 1162585: Set socket flags after socket has been created, r=kmachulis
authorThomas Zimmermann <tdz@users.sourceforge.net>
Fri, 08 May 2015 09:45:42 +0200
changeset 274368 01e6faa051bdc5c05df43646d2591f22d3e5f1ba
parent 274367 a099b6c773663a030777571ffa1e7ede5cd6649c
child 274369 a39d39ca26c7bc955b448ede212bd51197c1101f
push id863
push userraliiev@mozilla.com
push dateMon, 03 Aug 2015 13:22:43 +0000
treeherdermozilla-release@f6321b14228d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmachulis
bugs1162585
milestone40.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 1162585: Set socket flags after socket has been created, r=kmachulis This patch moves the code for setting socket flags in the socket I/O classes to the few locations were sockets are created. Any other socket setup is redundant and has been removed.
dom/bluetooth/bluez/BluetoothSocket.cpp
ipc/unixsocket/ListenSocket.cpp
ipc/unixsocket/StreamSocket.cpp
--- a/dom/bluetooth/bluez/BluetoothSocket.cpp
+++ b/dom/bluetooth/bluez/BluetoothSocket.cpp
@@ -215,36 +215,40 @@ BluetoothSocket::BluetoothSocketIO::Canc
 }
 
 void
 BluetoothSocket::BluetoothSocketIO::Listen()
 {
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
   MOZ_ASSERT(mConnector);
 
-  // This will set things we don't particularly care about, but it will hand
-  // back the correct structure size which is what we do care about.
-  if (!mConnector->CreateAddr(true, mAddrSize, mAddr, nullptr)) {
-    NS_WARNING("Cannot create socket address!");
-    FireSocketError();
-    return;
-  }
-
   if (!IsOpen()) {
     int fd = mConnector->Create();
     if (fd < 0) {
       NS_WARNING("Cannot create socket fd!");
       FireSocketError();
       return;
     }
     if (!SetSocketFlags(fd)) {
       NS_WARNING("Cannot set socket flags!");
       FireSocketError();
       return;
     }
+    if (!mConnector->SetUpListenSocket(fd)) {
+      NS_WARNING("Could not set up listen socket!");
+      FireSocketError();
+      return;
+    }
+    // This will set things we don't particularly care about, but it will hand
+    // back the correct structure size which is what we do care about.
+    if (!mConnector->CreateAddr(true, mAddrSize, mAddr, nullptr)) {
+      NS_WARNING("Cannot create socket address!");
+      FireSocketError();
+      return;
+    }
     SetFd(fd);
 
     // calls OnListening on success, or OnError otherwise
     nsresult rv = UnixSocketWatcher::Listen(
       reinterpret_cast<struct sockaddr*>(&mAddr), mAddrSize);
     NS_WARN_IF(NS_FAILED(rv));
   }
 }
@@ -262,25 +266,29 @@ BluetoothSocket::BluetoothSocketIO::Conn
       FireSocketError();
       return;
     }
     if (!SetSocketFlags(fd)) {
       NS_WARNING("Cannot set socket flags!");
       FireSocketError();
       return;
     }
+    if (!mConnector->SetUp(fd)) {
+      NS_WARNING("Could not set up socket!");
+      FireSocketError();
+      return;
+    }
+    if (!mConnector->CreateAddr(false, mAddrSize, mAddr, mAddress.get())) {
+      NS_WARNING("Cannot create socket address!");
+      FireSocketError();
+      return;
+    }
     SetFd(fd);
   }
 
-  if (!mConnector->CreateAddr(false, mAddrSize, mAddr, mAddress.get())) {
-    NS_WARNING("Cannot create socket address!");
-    FireSocketError();
-    return;
-  }
-
   // calls OnConnected() on success, or OnError() otherwise
   nsresult rv = UnixSocketWatcher::Connect(
     reinterpret_cast<struct sockaddr*>(&mAddr), mAddrSize);
   NS_WARN_IF(NS_FAILED(rv));
 }
 
 void
 BluetoothSocket::BluetoothSocketIO::Send(UnixSocketIOBuffer* aBuffer)
@@ -290,49 +298,31 @@ BluetoothSocket::BluetoothSocketIO::Send
 }
 
 void
 BluetoothSocket::BluetoothSocketIO::OnConnected()
 {
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
   MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED);
 
-  if (!SetSocketFlags(GetFd())) {
-    NS_WARNING("Cannot set socket flags!");
-    FireSocketError();
-    return;
-  }
-
-  if (!mConnector->SetUp(GetFd())) {
-    NS_WARNING("Could not set up socket!");
-    FireSocketError();
-    return;
-  }
-
   NS_DispatchToMainThread(
     new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));
 
   AddWatchers(READ_WATCHER, true);
   if (HasPendingData()) {
     AddWatchers(WRITE_WATCHER, false);
   }
 }
 
 void
 BluetoothSocket::BluetoothSocketIO::OnListening()
 {
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
   MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);
 
-  if (!mConnector->SetUpListenSocket(GetFd())) {
-    NS_WARNING("Could not set up listen socket!");
-    FireSocketError();
-    return;
-  }
-
   AddWatchers(READ_WATCHER, true);
 }
 
 void
 BluetoothSocket::BluetoothSocketIO::OnError(const char* aFunction, int aErrno)
 {
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
 
@@ -341,34 +331,34 @@ BluetoothSocket::BluetoothSocketIO::OnEr
 }
 
 void
 BluetoothSocket::BluetoothSocketIO::OnSocketCanAcceptWithoutBlocking()
 {
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
   MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);
 
+  RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
+
   socklen_t mAddrSize = sizeof(mAddr);
   int fd = TEMP_FAILURE_RETRY(accept(GetFd(),
     reinterpret_cast<struct sockaddr*>(&mAddr), &mAddrSize));
   if (fd < 0) {
     OnError("accept", errno);
     return;
   }
-
+  if (!SetSocketFlags(fd)) {
+    return;
+  }
   if (!mConnector->SetUp(fd)) {
     NS_WARNING("Could not set up socket!");
     return;
   }
 
-  RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
   Close();
-  if (!SetSocketFlags(fd)) {
-    return;
-  }
   SetSocket(fd, SOCKET_IS_CONNECTED);
 
   NS_DispatchToMainThread(
     new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));
 
   AddWatchers(READ_WATCHER, true);
   if (HasPendingData()) {
     AddWatchers(WRITE_WATCHER, false);
--- a/ipc/unixsocket/ListenSocket.cpp
+++ b/ipc/unixsocket/ListenSocket.cpp
@@ -149,30 +149,34 @@ ListenSocketIO::Listen(ConnectionOriente
       FireSocketError();
       return;
     }
     if (!SetSocketFlags(fd)) {
       NS_WARNING("Cannot set socket flags!");
       FireSocketError();
       return;
     }
+    if (!mConnector->SetUpListenSocket(GetFd())) {
+      NS_WARNING("Could not set up listen socket!");
+      FireSocketError();
+      return;
+    }
+    // This will set things we don't particularly care about, but
+    // it will hand back the correct structure size which is what
+    // we do care about.
+    if (!mConnector->CreateAddr(true, mAddrSize, mAddr, nullptr)) {
+      NS_WARNING("Cannot create socket address!");
+      FireSocketError();
+      return;
+    }
     SetFd(fd);
   }
 
   mCOSocketIO = aCOSocketIO;
 
-  // This will set things we don't particularly care about, but
-  // it will hand back the correct structure size which is what
-  // we do care about.
-  if (!mConnector->CreateAddr(true, mAddrSize, mAddr, nullptr)) {
-    NS_WARNING("Cannot create socket address!");
-    FireSocketError();
-    return;
-  }
-
   // calls OnListening on success, or OnError otherwise
   nsresult rv = UnixSocketWatcher::Listen(
     reinterpret_cast<struct sockaddr*>(&mAddr), mAddrSize);
   NS_WARN_IF(NS_FAILED(rv));
 }
 
 void
 ListenSocketIO::OnConnected()
@@ -183,22 +187,16 @@ ListenSocketIO::OnConnected()
 }
 
 void
 ListenSocketIO::OnListening()
 {
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
   MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);
 
-  if (!mConnector->SetUpListenSocket(GetFd())) {
-    NS_WARNING("Could not set up listen socket!");
-    FireSocketError();
-    return;
-  }
-
   AddWatchers(READ_WATCHER, true);
 
   /* We signal a successful 'connection' to a local address for listening. */
   NS_DispatchToMainThread(
     new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));
 }
 
 void
--- a/ipc/unixsocket/StreamSocket.cpp
+++ b/ipc/unixsocket/StreamSocket.cpp
@@ -246,25 +246,29 @@ StreamSocketIO::Connect()
       FireSocketError();
       return;
     }
     if (!SetSocketFlags(fd)) {
       NS_WARNING("Cannot set socket flags!");
       FireSocketError();
       return;
     }
+    if (!mConnector->SetUp(GetFd())) {
+      NS_WARNING("Could not set up socket!");
+      FireSocketError();
+      return;
+    }
+    if (!mConnector->CreateAddr(false, mAddrSize, mAddr, mAddress.get())) {
+      NS_WARNING("Cannot create socket address!");
+      FireSocketError();
+      return;
+    }
     SetFd(fd);
   }
 
-  if (!mConnector->CreateAddr(false, mAddrSize, mAddr, mAddress.get())) {
-    NS_WARNING("Cannot create socket address!");
-    FireSocketError();
-    return;
-  }
-
   // calls OnConnected() on success, or OnError() otherwise
   nsresult rv = UnixSocketWatcher::Connect(
     reinterpret_cast<struct sockaddr*>(&mAddr), mAddrSize);
   NS_WARN_IF(NS_FAILED(rv));
 }
 
 void
 StreamSocketIO::Send(UnixSocketIOBuffer* aData)
@@ -274,28 +278,16 @@ StreamSocketIO::Send(UnixSocketIOBuffer*
 }
 
 void
 StreamSocketIO::OnConnected()
 {
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
   MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED);
 
-  if (!SetSocketFlags(GetFd())) {
-    NS_WARNING("Cannot set socket flags!");
-    FireSocketError();
-    return;
-  }
-
-  if (!mConnector->SetUp(GetFd())) {
-    NS_WARNING("Could not set up socket!");
-    FireSocketError();
-    return;
-  }
-
   NS_DispatchToMainThread(
     new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));
 
   AddWatchers(READ_WATCHER, true);
   if (HasPendingData()) {
     AddWatchers(WRITE_WATCHER, false);
   }
 }
@@ -403,40 +395,39 @@ nsresult
 StreamSocketIO::Accept(int aFd,
                        const union sockaddr_any* aAddr, socklen_t aAddrLen)
 {
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
   MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTING);
 
   // File-descriptor setup
 
+  if (!SetSocketFlags(aFd)) {
+    return NS_ERROR_FAILURE;
+  }
   if (!mConnector->SetUp(aFd)) {
     NS_WARNING("Could not set up socket!");
     return NS_ERROR_FAILURE;
   }
 
-  if (!SetSocketFlags(aFd)) {
-    return NS_ERROR_FAILURE;
-  }
   SetSocket(aFd, SOCKET_IS_CONNECTED);
 
-  AddWatchers(READ_WATCHER, true);
-  if (HasPendingData()) {
-    AddWatchers(WRITE_WATCHER, false);
-  }
-
   // Address setup
-
   memcpy(&mAddr, aAddr, aAddrLen);
   mAddrSize = aAddrLen;
 
   // Signal success
   NS_DispatchToMainThread(
     new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));
 
+  AddWatchers(READ_WATCHER, true);
+  if (HasPendingData()) {
+    AddWatchers(WRITE_WATCHER, false);
+  }
+
   return NS_OK;
 }
 
 // |DataSocketIO|
 
 nsresult
 StreamSocketIO::QueryReceiveBuffer(UnixSocketIOBuffer** aBuffer)
 {