Bug 1119746: Support |ListenSocket| in |BluetoothDaemonConnection|, r=qdot

This patch adds support for |ListenSocket| to |BluetoothDaemonConnection|.
The class can now handle file descriptors that have been accepted by
|ListenSocket|.
This commit is contained in:
Thomas Zimmermann 2015-01-20 10:17:25 +08:00
parent 90406a14e0
commit 6be32cd438
2 changed files with 81 additions and 4 deletions

View File

@ -191,14 +191,22 @@ BluetoothDaemonPDUConsumer::~BluetoothDaemonPDUConsumer()
//
class BluetoothDaemonConnectionIO MOZ_FINAL : public UnixSocketWatcher
, public ConnectionOrientedSocketIO
{
public:
BluetoothDaemonConnectionIO(MessageLoop* aIOLoop,
BluetoothDaemonConnectionIO(MessageLoop* aIOLoop, int aFd,
ConnectionStatus aConnectionStatus,
BluetoothDaemonConnection* aConnection,
BluetoothDaemonPDUConsumer* aConsumer);
SocketBase* GetSocketBase();
// StreamSocketIOBase
//
nsresult Accept(int aFd,
const union sockaddr_any* aAddr, socklen_t aAddrLen);
// Shutdown state
//
@ -237,10 +245,11 @@ private:
};
BluetoothDaemonConnectionIO::BluetoothDaemonConnectionIO(
MessageLoop* aIOLoop,
MessageLoop* aIOLoop, int aFd,
ConnectionStatus aConnectionStatus,
BluetoothDaemonConnection* aConnection,
BluetoothDaemonPDUConsumer* aConsumer)
: UnixSocketWatcher(aIOLoop)
: UnixSocketWatcher(aIOLoop, aFd, aConnectionStatus)
, mConnection(aConnection)
, mConsumer(aConsumer)
, mShuttingDownOnIOThread(false)
@ -291,6 +300,30 @@ BluetoothDaemonConnectionIO::ShutdownOnIOThread()
mShuttingDownOnIOThread = true;
}
nsresult
BluetoothDaemonConnectionIO::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 (TEMP_FAILURE_RETRY(fcntl(aFd, F_SETFL, O_NONBLOCK)) < 0) {
OnError("fcntl", errno);
ScopedClose cleanupFd(aFd);
return NS_ERROR_FAILURE;
}
SetSocket(aFd, SOCKET_IS_CONNECTED);
// Signal success
OnConnected();
return NS_OK;
}
void
BluetoothDaemonConnectionIO::Connect(const char* aSocketName)
{
@ -519,7 +552,8 @@ BluetoothDaemonConnection::ConnectSocket(BluetoothDaemonPDUConsumer* aConsumer)
SetConnectionStatus(SOCKET_CONNECTING);
MessageLoop* ioLoop = XRE_GetIOMessageLoop();
mIO = new BluetoothDaemonConnectionIO(ioLoop, this, aConsumer);
mIO = new BluetoothDaemonConnectionIO(
ioLoop, -1, UnixSocketWatcher::SOCKET_IS_CONNECTING, this, aConsumer);
ioLoop->PostTask(FROM_HERE, new BluetoothDaemonConnectTask(mIO));
return NS_OK;
@ -560,5 +594,30 @@ BluetoothDaemonConnection::Send(BluetoothDaemonPDU* aPDU)
return NS_OK;
}
ConnectionOrientedSocketIO*
BluetoothDaemonConnection::GetIO()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mIO); // Call |PrepareAccept| before listening for connections
return mIO;
}
ConnectionOrientedSocketIO*
BluetoothDaemonConnection::PrepareAccept(BluetoothDaemonPDUConsumer* aConsumer)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!mIO);
MOZ_ASSERT(aConsumer);
SetConnectionStatus(SOCKET_CONNECTING);
mIO = new BluetoothDaemonConnectionIO(
XRE_GetIOMessageLoop(), -1, UnixSocketWatcher::SOCKET_IS_CONNECTING,
this, aConsumer);
return mIO;
}
}
}

View File

@ -9,6 +9,7 @@
#include "mozilla/Attributes.h"
#include "mozilla/FileUtils.h"
#include "mozilla/ipc/ConnectionOrientedSocket.h"
#include "mozilla/ipc/SocketBase.h"
#include "nsError.h"
#include "nsAutoPtr.h"
@ -105,16 +106,33 @@ protected:
* PDUs. PDU receiving is performed by |BluetoothDaemonPDUConsumer|.
*/
class BluetoothDaemonConnection : public SocketBase
, public ConnectionOrientedSocket
{
public:
BluetoothDaemonConnection();
virtual ~BluetoothDaemonConnection();
// SocketBase
//
nsresult ConnectSocket(BluetoothDaemonPDUConsumer* aConsumer);
void CloseSocket();
nsresult Send(BluetoothDaemonPDU* aPDU);
// ConnectionOrientedSocket
//
virtual ConnectionOrientedSocketIO* GetIO() MOZ_OVERRIDE;
protected:
// Prepares an instance of |BluetoothDaemonConnection| in DISCONNECTED
// state for accepting a connection. Subclasses implementing |GetIO|
// need to call this method.
ConnectionOrientedSocketIO*
PrepareAccept(BluetoothDaemonPDUConsumer* aConsumer);
private:
BluetoothDaemonConnectionIO* mIO;
};