2014-12-03 02:00:36 +03:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "ListenSocket.h"
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include "ConnectionOrientedSocket.h"
|
2015-04-28 11:18:12 +03:00
|
|
|
#include "DataSocket.h"
|
2015-05-26 14:24:20 +03:00
|
|
|
#include "ListenSocketConsumer.h"
|
2014-12-03 02:00:36 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#include "UnixSocketConnector.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
|
|
|
//
|
|
|
|
// ListenSocketIO
|
|
|
|
//
|
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
class ListenSocketIO final
|
|
|
|
: public UnixSocketWatcher
|
|
|
|
, public SocketIOBase
|
2014-12-03 02:00:36 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
class ListenTask;
|
|
|
|
|
2015-06-09 10:50:10 +03:00
|
|
|
ListenSocketIO(MessageLoop* aConsumerLoop,
|
2015-06-02 11:01:57 +03:00
|
|
|
MessageLoop* aIOLoop,
|
2014-12-03 02:00:36 +03:00
|
|
|
ListenSocket* aListenSocket,
|
2015-05-19 14:28:46 +03:00
|
|
|
UnixSocketConnector* aConnector);
|
2014-12-03 02:00:36 +03:00
|
|
|
~ListenSocketIO();
|
|
|
|
|
2015-05-21 14:34:37 +03:00
|
|
|
UnixSocketConnector* GetConnector() const;
|
|
|
|
|
2014-12-03 02:00:36 +03:00
|
|
|
// Task callback methods
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run bind/listen to prepare for further runs of accept()
|
|
|
|
*/
|
|
|
|
void Listen(ConnectionOrientedSocketIO* aCOSocketIO);
|
|
|
|
|
|
|
|
// I/O callback methods
|
|
|
|
//
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
void OnConnected() override;
|
|
|
|
void OnError(const char* aFunction, int aErrno) override;
|
|
|
|
void OnListening() override;
|
2015-05-08 10:43:01 +03:00
|
|
|
void OnSocketCanAcceptWithoutBlocking() override;
|
2014-12-03 02:00:36 +03:00
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
// Methods for |SocketIOBase|
|
|
|
|
//
|
|
|
|
|
|
|
|
SocketBase* GetSocketBase() override;
|
|
|
|
|
2015-06-02 11:01:58 +03:00
|
|
|
bool IsShutdownOnConsumerThread() const override;
|
2015-04-30 13:55:37 +03:00
|
|
|
bool IsShutdownOnIOThread() const override;
|
|
|
|
|
2015-06-02 11:01:58 +03:00
|
|
|
void ShutdownOnConsumerThread() override;
|
2015-04-30 13:55:37 +03:00
|
|
|
void ShutdownOnIOThread() override;
|
|
|
|
|
2014-12-03 02:00:36 +03:00
|
|
|
private:
|
|
|
|
void FireSocketError();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Consumer pointer. Non-thread safe RefPtr, so should only be manipulated
|
2015-06-02 11:01:58 +03:00
|
|
|
* directly from consumer thread. All non-consumer-thread accesses should
|
|
|
|
* happen with mIO as container.
|
2014-12-03 02:00:36 +03:00
|
|
|
*/
|
|
|
|
RefPtr<ListenSocket> mListenSocket;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connector object used to create the connection we are currently using.
|
|
|
|
*/
|
|
|
|
nsAutoPtr<UnixSocketConnector> mConnector;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If true, do not requeue whatever task we're running
|
|
|
|
*/
|
|
|
|
bool mShuttingDownOnIOThread;
|
|
|
|
|
|
|
|
/**
|
2015-05-19 14:28:46 +03:00
|
|
|
* Number of valid bytes in |mAddress|
|
2014-12-03 02:00:36 +03:00
|
|
|
*/
|
2015-05-19 14:28:46 +03:00
|
|
|
socklen_t mAddressLength;
|
2014-12-03 02:00:36 +03:00
|
|
|
|
|
|
|
/**
|
2015-05-19 14:28:46 +03:00
|
|
|
* Address structure of the socket currently in use
|
2014-12-03 02:00:36 +03:00
|
|
|
*/
|
2015-05-19 14:28:46 +03:00
|
|
|
struct sockaddr_storage mAddress;
|
2014-12-03 02:00:36 +03:00
|
|
|
|
|
|
|
ConnectionOrientedSocketIO* mCOSocketIO;
|
|
|
|
};
|
|
|
|
|
2015-06-09 10:50:10 +03:00
|
|
|
ListenSocketIO::ListenSocketIO(MessageLoop* aConsumerLoop,
|
2015-06-02 11:01:57 +03:00
|
|
|
MessageLoop* aIOLoop,
|
2014-12-03 02:00:36 +03:00
|
|
|
ListenSocket* aListenSocket,
|
2015-05-19 14:28:46 +03:00
|
|
|
UnixSocketConnector* aConnector)
|
2015-06-02 11:01:57 +03:00
|
|
|
: UnixSocketWatcher(aIOLoop)
|
2015-06-09 10:50:10 +03:00
|
|
|
, SocketIOBase(aConsumerLoop)
|
2015-04-28 11:18:12 +03:00
|
|
|
, mListenSocket(aListenSocket)
|
|
|
|
, mConnector(aConnector)
|
|
|
|
, mShuttingDownOnIOThread(false)
|
2015-05-19 14:28:46 +03:00
|
|
|
, mAddressLength(0)
|
2015-04-28 11:18:12 +03:00
|
|
|
, mCOSocketIO(nullptr)
|
2014-12-03 02:00:36 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mListenSocket);
|
|
|
|
MOZ_ASSERT(mConnector);
|
|
|
|
}
|
|
|
|
|
|
|
|
ListenSocketIO::~ListenSocketIO()
|
|
|
|
{
|
2015-06-02 11:01:57 +03:00
|
|
|
MOZ_ASSERT(IsConsumerThread());
|
2015-06-02 11:01:58 +03:00
|
|
|
MOZ_ASSERT(IsShutdownOnConsumerThread());
|
2014-12-03 02:00:36 +03:00
|
|
|
}
|
|
|
|
|
2015-05-21 14:34:37 +03:00
|
|
|
UnixSocketConnector*
|
|
|
|
ListenSocketIO::GetConnector() const
|
|
|
|
{
|
|
|
|
return mConnector;
|
|
|
|
}
|
|
|
|
|
2014-12-03 02:00:36 +03:00
|
|
|
void
|
|
|
|
ListenSocketIO::Listen(ConnectionOrientedSocketIO* aCOSocketIO)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
MOZ_ASSERT(mConnector);
|
|
|
|
MOZ_ASSERT(aCOSocketIO);
|
|
|
|
|
2015-05-19 14:28:46 +03:00
|
|
|
struct sockaddr* address = reinterpret_cast<struct sockaddr*>(&mAddress);
|
|
|
|
mAddressLength = sizeof(mAddress);
|
|
|
|
|
2014-12-03 02:00:36 +03:00
|
|
|
if (!IsOpen()) {
|
2015-05-19 14:28:46 +03:00
|
|
|
int fd;
|
|
|
|
nsresult rv = mConnector->CreateListenSocket(address, &mAddressLength,
|
|
|
|
fd);
|
|
|
|
if (NS_FAILED(rv)) {
|
2015-05-08 10:45:42 +03:00
|
|
|
FireSocketError();
|
|
|
|
return;
|
|
|
|
}
|
2014-12-03 02:00:36 +03:00
|
|
|
SetFd(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
mCOSocketIO = aCOSocketIO;
|
|
|
|
|
|
|
|
// calls OnListening on success, or OnError otherwise
|
2015-05-19 14:28:46 +03:00
|
|
|
nsresult rv = UnixSocketWatcher::Listen(address, mAddressLength);
|
2014-12-03 02:00:36 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocketIO::OnConnected()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
|
|
|
|
NS_NOTREACHED("Invalid call to |ListenSocketIO::OnConnected|");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocketIO::OnListening()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);
|
|
|
|
|
|
|
|
AddWatchers(READ_WATCHER, true);
|
2015-01-20 05:17:19 +03:00
|
|
|
|
|
|
|
/* We signal a successful 'connection' to a local address for listening. */
|
2015-06-09 10:50:10 +03:00
|
|
|
GetConsumerThread()->PostTask(
|
|
|
|
FROM_HERE, new SocketEventTask(this, SocketEventTask::CONNECT_SUCCESS));
|
2014-12-03 02:00:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocketIO::OnError(const char* aFunction, int aErrno)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
|
|
|
|
UnixFdWatcher::OnError(aFunction, aErrno);
|
|
|
|
FireSocketError();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocketIO::FireSocketError()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
|
|
|
|
// Clean up watchers, statuses, fds
|
|
|
|
Close();
|
|
|
|
|
2015-06-02 11:01:58 +03:00
|
|
|
// Tell the consumer thread we've errored
|
2015-06-09 10:50:10 +03:00
|
|
|
GetConsumerThread()->PostTask(
|
|
|
|
FROM_HERE, new SocketEventTask(this, SocketEventTask::CONNECT_ERROR));
|
2014-12-03 02:00:36 +03:00
|
|
|
}
|
|
|
|
|
2015-05-08 10:43:01 +03:00
|
|
|
void
|
|
|
|
ListenSocketIO::OnSocketCanAcceptWithoutBlocking()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);
|
|
|
|
MOZ_ASSERT(mCOSocketIO);
|
|
|
|
|
2015-05-19 14:28:46 +03:00
|
|
|
RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
|
|
|
|
|
|
|
|
struct sockaddr_storage storage;
|
|
|
|
socklen_t addressLength = sizeof(storage);
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
nsresult rv = mConnector->AcceptStreamSocket(
|
|
|
|
GetFd(),
|
|
|
|
reinterpret_cast<struct sockaddr*>(&storage), &addressLength,
|
|
|
|
fd);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
FireSocketError();
|
2015-05-08 10:43:01 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCOSocketIO->Accept(fd,
|
2015-05-27 14:49:07 +03:00
|
|
|
reinterpret_cast<struct sockaddr*>(&storage),
|
2015-05-19 14:28:46 +03:00
|
|
|
addressLength);
|
2015-05-08 10:43:01 +03:00
|
|
|
}
|
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
// |SocketIOBase|
|
|
|
|
|
|
|
|
SocketBase*
|
|
|
|
ListenSocketIO::GetSocketBase()
|
|
|
|
{
|
|
|
|
return mListenSocket.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-06-02 11:01:58 +03:00
|
|
|
ListenSocketIO::IsShutdownOnConsumerThread() const
|
2015-04-30 13:55:37 +03:00
|
|
|
{
|
2015-06-02 11:01:57 +03:00
|
|
|
MOZ_ASSERT(IsConsumerThread());
|
2015-04-30 13:55:37 +03:00
|
|
|
|
|
|
|
return mListenSocket == nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ListenSocketIO::IsShutdownOnIOThread() const
|
|
|
|
{
|
|
|
|
return mShuttingDownOnIOThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-06-02 11:01:58 +03:00
|
|
|
ListenSocketIO::ShutdownOnConsumerThread()
|
2015-04-30 13:55:37 +03:00
|
|
|
{
|
2015-06-02 11:01:57 +03:00
|
|
|
MOZ_ASSERT(IsConsumerThread());
|
2015-06-02 11:01:58 +03:00
|
|
|
MOZ_ASSERT(!IsShutdownOnConsumerThread());
|
2015-04-30 13:55:37 +03:00
|
|
|
|
|
|
|
mListenSocket = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocketIO::ShutdownOnIOThread()
|
|
|
|
{
|
2015-06-02 11:01:57 +03:00
|
|
|
MOZ_ASSERT(!IsConsumerThread());
|
2015-04-30 13:55:37 +03:00
|
|
|
MOZ_ASSERT(!mShuttingDownOnIOThread);
|
|
|
|
|
|
|
|
Close(); // will also remove fd from I/O loop
|
|
|
|
mShuttingDownOnIOThread = true;
|
|
|
|
}
|
|
|
|
|
2014-12-03 02:00:36 +03:00
|
|
|
//
|
|
|
|
// Socket tasks
|
|
|
|
//
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class ListenSocketIO::ListenTask final
|
2014-12-03 02:00:36 +03:00
|
|
|
: public SocketIOTask<ListenSocketIO>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ListenTask(ListenSocketIO* aIO, ConnectionOrientedSocketIO* aCOSocketIO)
|
|
|
|
: SocketIOTask<ListenSocketIO>(aIO)
|
|
|
|
, mCOSocketIO(aCOSocketIO)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mCOSocketIO);
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
void Run() override
|
2014-12-03 02:00:36 +03:00
|
|
|
{
|
2015-06-02 11:01:57 +03:00
|
|
|
MOZ_ASSERT(!GetIO()->IsConsumerThread());
|
2014-12-03 02:00:36 +03:00
|
|
|
|
|
|
|
if (!IsCanceled()) {
|
|
|
|
GetIO()->Listen(mCOSocketIO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ConnectionOrientedSocketIO* mCOSocketIO;
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// UnixSocketConsumer
|
|
|
|
//
|
|
|
|
|
2015-05-26 14:24:20 +03:00
|
|
|
ListenSocket::ListenSocket(ListenSocketConsumer* aConsumer, int aIndex)
|
2015-06-02 11:01:57 +03:00
|
|
|
: mIO(nullptr)
|
|
|
|
, mConsumer(aConsumer)
|
2015-05-26 14:24:20 +03:00
|
|
|
, mIndex(aIndex)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mConsumer);
|
|
|
|
}
|
2014-12-03 02:00:36 +03:00
|
|
|
|
|
|
|
ListenSocket::~ListenSocket()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIO);
|
|
|
|
}
|
|
|
|
|
2015-05-20 11:50:43 +03:00
|
|
|
nsresult
|
2014-12-03 02:00:36 +03:00
|
|
|
ListenSocket::Listen(UnixSocketConnector* aConnector,
|
2015-06-09 10:50:10 +03:00
|
|
|
MessageLoop* aConsumerLoop,
|
2015-06-02 11:01:57 +03:00
|
|
|
MessageLoop* aIOLoop,
|
2014-12-03 02:00:36 +03:00
|
|
|
ConnectionOrientedSocket* aCOSocket)
|
|
|
|
{
|
2015-05-20 11:50:43 +03:00
|
|
|
MOZ_ASSERT(!mIO);
|
2014-12-03 02:00:36 +03:00
|
|
|
|
2015-06-09 10:50:10 +03:00
|
|
|
mIO = new ListenSocketIO(aConsumerLoop, aIOLoop, this, aConnector);
|
2014-12-03 02:00:36 +03:00
|
|
|
|
|
|
|
// Prepared I/O object, now start listening.
|
2015-05-21 14:34:37 +03:00
|
|
|
nsresult rv = Listen(aCOSocket);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete mIO;
|
|
|
|
mIO = nullptr;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2014-12-03 02:00:36 +03:00
|
|
|
}
|
|
|
|
|
2015-06-02 11:01:57 +03:00
|
|
|
nsresult
|
|
|
|
ListenSocket::Listen(UnixSocketConnector* aConnector,
|
|
|
|
ConnectionOrientedSocket* aCOSocket)
|
|
|
|
{
|
2015-06-09 10:50:10 +03:00
|
|
|
return Listen(aConnector, MessageLoop::current(), XRE_GetIOMessageLoop(),
|
|
|
|
aCOSocket);
|
2015-06-02 11:01:57 +03:00
|
|
|
}
|
|
|
|
|
2015-05-20 11:50:43 +03:00
|
|
|
nsresult
|
2014-12-03 02:00:36 +03:00
|
|
|
ListenSocket::Listen(ConnectionOrientedSocket* aCOSocket)
|
|
|
|
{
|
2015-05-20 11:30:55 +03:00
|
|
|
MOZ_ASSERT(aCOSocket);
|
2015-05-20 11:50:43 +03:00
|
|
|
MOZ_ASSERT(mIO);
|
2014-12-03 02:00:36 +03:00
|
|
|
|
2015-05-21 14:34:37 +03:00
|
|
|
// We first prepare the connection-oriented socket with a
|
|
|
|
// socket connector and a socket I/O class.
|
|
|
|
|
|
|
|
nsAutoPtr<UnixSocketConnector> connector;
|
|
|
|
nsresult rv = mIO->GetConnector()->Duplicate(*connector.StartAssignment());
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoPtr<ConnectionOrientedSocketIO> io;
|
2015-06-02 11:01:57 +03:00
|
|
|
rv = aCOSocket->PrepareAccept(connector,
|
|
|
|
mIO->GetConsumerThread(), mIO->GetIOLoop(),
|
2015-06-02 11:01:57 +03:00
|
|
|
*io.StartAssignment());
|
2015-05-21 14:34:37 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
connector.forget(); // now owned by |io|
|
|
|
|
|
|
|
|
// Then we start listening for connection requests.
|
|
|
|
|
2014-12-03 02:00:36 +03:00
|
|
|
SetConnectionStatus(SOCKET_LISTENING);
|
|
|
|
|
2015-06-02 11:01:57 +03:00
|
|
|
mIO->GetIOLoop()->PostTask(
|
2015-05-21 14:34:37 +03:00
|
|
|
FROM_HERE, new ListenSocketIO::ListenTask(mIO, io.forget()));
|
2014-12-03 02:00:36 +03:00
|
|
|
|
2015-05-20 11:50:43 +03:00
|
|
|
return NS_OK;
|
2014-12-03 02:00:36 +03:00
|
|
|
}
|
|
|
|
|
2015-04-30 13:55:37 +03:00
|
|
|
// |SocketBase|
|
|
|
|
|
|
|
|
void
|
2015-05-20 11:50:43 +03:00
|
|
|
ListenSocket::Close()
|
2015-04-30 13:55:37 +03:00
|
|
|
{
|
2015-05-20 11:50:43 +03:00
|
|
|
if (!mIO) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-02 11:01:57 +03:00
|
|
|
MOZ_ASSERT(mIO->IsConsumerThread());
|
|
|
|
|
2015-05-20 11:50:43 +03:00
|
|
|
// From this point on, we consider mIO as being deleted. We sever
|
|
|
|
// the relationship here so any future calls to listen or connect
|
|
|
|
// will create a new implementation.
|
2015-06-02 11:01:58 +03:00
|
|
|
mIO->ShutdownOnConsumerThread();
|
2015-06-02 11:01:57 +03:00
|
|
|
mIO->GetIOLoop()->PostTask(FROM_HERE, new SocketIOShutdownTask(mIO));
|
2015-05-20 11:50:43 +03:00
|
|
|
mIO = nullptr;
|
|
|
|
|
|
|
|
NotifyDisconnect();
|
2015-04-30 13:55:37 +03:00
|
|
|
}
|
|
|
|
|
2015-05-26 14:24:20 +03:00
|
|
|
void
|
|
|
|
ListenSocket::OnConnectSuccess()
|
|
|
|
{
|
|
|
|
mConsumer->OnConnectSuccess(mIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocket::OnConnectError()
|
|
|
|
{
|
|
|
|
mConsumer->OnConnectError(mIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ListenSocket::OnDisconnect()
|
|
|
|
{
|
|
|
|
mConsumer->OnDisconnect(mIndex);
|
|
|
|
}
|
|
|
|
|
2014-12-03 02:00:36 +03:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|