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.
This commit is contained in:
Thomas Zimmermann 2015-05-08 09:45:42 +02:00
Родитель e5ff47d14f
Коммит 63a3cb989e
3 изменённых файлов: 58 добавлений и 79 удалений

Просмотреть файл

@ -220,14 +220,6 @@ 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) {
@ -240,6 +232,18 @@ BluetoothSocket::BluetoothSocketIO::Listen()
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
@ -267,15 +271,19 @@ BluetoothSocket::BluetoothSocketIO::Connect()
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);
@ -295,18 +303,6 @@ 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));
@ -322,12 +318,6 @@ 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);
}
@ -346,6 +336,8 @@ 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));
@ -353,17 +345,15 @@ BluetoothSocket::BluetoothSocketIO::OnSocketCanAcceptWithoutBlocking()
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(

Просмотреть файл

@ -154,20 +154,24 @@ ListenSocketIO::Listen(ConnectionOrientedSocketIO* aCOSocketIO)
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);
@ -188,12 +192,6 @@ 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. */

Просмотреть файл

@ -251,15 +251,19 @@ StreamSocketIO::Connect()
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);
@ -279,18 +283,6 @@ 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));
@ -408,23 +400,17 @@ StreamSocketIO::Accept(int aFd,
// 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;
@ -432,6 +418,11 @@ StreamSocketIO::Accept(int aFd,
NS_DispatchToMainThread(
new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));
AddWatchers(READ_WATCHER, true);
if (HasPendingData()) {
AddWatchers(WRITE_WATCHER, false);
}
return NS_OK;
}