Skip to content

Commit

Permalink
Bug 1162524: Move |accept| out of |UnixSocketWatcher|, r=kmachulis
Browse files Browse the repository at this point in the history
Calls to |accept| are now handled by the corresponding socket I/O
class. This makes the code more flexible. All callers have been
adapted.
  • Loading branch information
tdz committed May 8, 2015
1 parent 7f93be6 commit e5ff47d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 100 deletions.
71 changes: 36 additions & 35 deletions dom/bluetooth/bluez/BluetoothSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ class BluetoothSocket::BluetoothSocketIO final
// I/O callback methods
//

void OnAccepted(int aFd, const sockaddr_any* aAddr,
socklen_t aAddrLen) override;
void OnConnected() override;
void OnError(const char* aFunction, int aErrno) override;
void OnListening() override;
void OnSocketCanAcceptWithoutBlocking() override;
void OnSocketCanReceiveWithoutBlocking() override;
void OnSocketCanSendWithoutBlocking() override;

Expand Down Expand Up @@ -290,39 +289,6 @@ BluetoothSocket::BluetoothSocketIO::Send(UnixSocketIOBuffer* aBuffer)
AddWatchers(WRITE_WATCHER, false);
}

void
BluetoothSocket::BluetoothSocketIO::OnAccepted(
int aFd, const sockaddr_any* aAddr, socklen_t aAddrLen)
{
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);
MOZ_ASSERT(aAddr);
MOZ_ASSERT(aAddrLen > 0 && (size_t)aAddrLen <= sizeof(mAddr));

memcpy (&mAddr, aAddr, aAddrLen);
mAddrSize = aAddrLen;

if (!mConnector->SetUp(aFd)) {
NS_WARNING("Could not set up socket!");
return;
}

RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
Close();
if (!SetSocketFlags(aFd)) {
return;
}
SetSocket(aFd, SOCKET_IS_CONNECTED);

NS_DispatchToMainThread(
new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));

AddWatchers(READ_WATCHER, true);
if (HasPendingData()) {
AddWatchers(WRITE_WATCHER, false);
}
}

void
BluetoothSocket::BluetoothSocketIO::OnConnected()
{
Expand Down Expand Up @@ -374,6 +340,41 @@ BluetoothSocket::BluetoothSocketIO::OnError(const char* aFunction, int aErrno)
FireSocketError();
}

void
BluetoothSocket::BluetoothSocketIO::OnSocketCanAcceptWithoutBlocking()
{
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);

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 (!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);
}
}

void
BluetoothSocket::BluetoothSocketIO::OnSocketCanReceiveWithoutBlocking()
{
Expand Down
10 changes: 1 addition & 9 deletions ipc/unixfd/UnixSocketWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,7 @@ UnixSocketWatcher::OnFileCanReadWithoutBlocking(int aFd)
if (mConnectionStatus == SOCKET_IS_CONNECTED) {
OnSocketCanReceiveWithoutBlocking();
} else if (mConnectionStatus == SOCKET_IS_LISTENING) {
sockaddr_any addr;
socklen_t addrLen = sizeof(addr);
int fd = TEMP_FAILURE_RETRY(accept(GetFd(),
reinterpret_cast<struct sockaddr*>(&addr), &addrLen));
if (fd < 0) {
OnError("accept", errno);
} else {
OnAccepted(fd, &addr, addrLen);
}
OnSocketCanAcceptWithoutBlocking();
} else {
NS_NOTREACHED("invalid connection state for reading");
}
Expand Down
7 changes: 3 additions & 4 deletions ipc/unixfd/UnixSocketWatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,15 @@ class UnixSocketWatcher : public UnixFdWatcher
// Listen on socket for incoming connection requests
nsresult Listen(const struct sockaddr* aAddr, socklen_t aAddrLen);

// Callback method for accepted connections
virtual void OnAccepted(int aFd, const sockaddr_any* aAddr,
socklen_t aAddrLen) {};

// Callback method for successful connection requests
virtual void OnConnected() {};

// Callback method for successful listen requests
virtual void OnListening() {};

// Callback method for accepting from a listening socket
virtual void OnSocketCanAcceptWithoutBlocking() {};

// Callback method for receiving from socket
virtual void OnSocketCanReceiveWithoutBlocking() {};

Expand Down
40 changes: 24 additions & 16 deletions ipc/unixsocket/ListenSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ class ListenSocketIO final
// I/O callback methods
//

void OnAccepted(int aFd, const sockaddr_any* aAddr,
socklen_t aAddrLen) override;
void OnConnected() override;
void OnError(const char* aFunction, int aErrno) override;
void OnListening() override;
void OnSocketCanAcceptWithoutBlocking() override;

// Methods for |SocketIOBase|
//
Expand Down Expand Up @@ -175,20 +174,6 @@ ListenSocketIO::Listen(ConnectionOrientedSocketIO* aCOSocketIO)
NS_WARN_IF(NS_FAILED(rv));
}

void
ListenSocketIO::OnAccepted(int aFd,
const sockaddr_any* aAddr,
socklen_t aAddrLen)
{
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);
MOZ_ASSERT(mCOSocketIO);

RemoveWatchers(READ_WATCHER|WRITE_WATCHER);

mCOSocketIO->Accept(aFd, aAddr, aAddrLen);
}

void
ListenSocketIO::OnConnected()
{
Expand Down Expand Up @@ -273,6 +258,29 @@ ListenSocketIO::SetSocketFlags(int aFd)
return true;
}

void
ListenSocketIO::OnSocketCanAcceptWithoutBlocking()
{
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);
MOZ_ASSERT(mCOSocketIO);

struct sockaddr_storage addr;
socklen_t addrLen = sizeof(addr);
int fd = TEMP_FAILURE_RETRY(accept(GetFd(),
reinterpret_cast<struct sockaddr*>(&addr), &addrLen));
if (fd < 0) {
OnError("accept", errno);
return;
}

RemoveWatchers(READ_WATCHER|WRITE_WATCHER);

mCOSocketIO->Accept(fd,
reinterpret_cast<union sockaddr_any*>(&addr),
addrLen);
}

// |SocketIOBase|

SocketBase*
Expand Down
36 changes: 0 additions & 36 deletions ipc/unixsocket/StreamSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ class StreamSocketIO final
// I/O callback methods
//

void OnAccepted(int aFd, const sockaddr_any* aAddr,
socklen_t aAddrLen) override;
void OnConnected() override;
void OnError(const char* aFunction, int aErrno) override;
void OnListening() override;
Expand Down Expand Up @@ -275,40 +273,6 @@ StreamSocketIO::Send(UnixSocketIOBuffer* aData)
AddWatchers(WRITE_WATCHER, false);
}

void
StreamSocketIO::OnAccepted(int aFd,
const sockaddr_any* aAddr,
socklen_t aAddrLen)
{
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);
MOZ_ASSERT(aAddr);
MOZ_ASSERT(aAddrLen <= static_cast<socklen_t>(sizeof(mAddr)));

memcpy (&mAddr, aAddr, aAddrLen);
mAddrSize = aAddrLen;

if (!mConnector->SetUp(aFd)) {
NS_WARNING("Could not set up socket!");
return;
}

RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
Close();
if (!SetSocketFlags(aFd)) {
return;
}
SetSocket(aFd, SOCKET_IS_CONNECTED);

NS_DispatchToMainThread(
new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));

AddWatchers(READ_WATCHER, true);
if (HasPendingData()) {
AddWatchers(WRITE_WATCHER, false);
}
}

void
StreamSocketIO::OnConnected()
{
Expand Down

0 comments on commit e5ff47d

Please sign in to comment.