2012-09-05 20:06:06 -07: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/. */
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
#include "UnixSocket.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
2014-02-28 01:16:53 -08:00
|
|
|
#include <fcntl.h>
|
2014-07-31 04:55:30 -07:00
|
|
|
#include "mozilla/unused.h"
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-12-20 02:36:55 -08:00
|
|
|
static const size_t MAX_READ_SIZE = 1 << 16;
|
|
|
|
|
2012-09-05 20:06:06 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2014-07-31 04:55:27 -07:00
|
|
|
//
|
2014-07-31 04:55:30 -07:00
|
|
|
// UnixSocketConsumerIO
|
2014-07-31 04:55:27 -07:00
|
|
|
//
|
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
class UnixSocketConsumerIO MOZ_FINAL : public UnixSocketWatcher
|
|
|
|
, protected SocketIOBase
|
2012-09-05 20:06:06 -07:00
|
|
|
{
|
2012-09-25 13:13:15 -07:00
|
|
|
public:
|
2014-07-31 04:55:30 -07:00
|
|
|
UnixSocketConsumerIO(MessageLoop* mIOLoop,
|
|
|
|
UnixSocketConsumer* aConsumer,
|
|
|
|
UnixSocketConnector* aConnector,
|
|
|
|
const nsACString& aAddress);
|
|
|
|
~UnixSocketConsumerIO();
|
2012-09-25 13:13:15 -07:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
void GetSocketAddr(nsAString& aAddrStr) const;
|
|
|
|
SocketConsumerBase* GetConsumer();
|
2014-10-21 01:52:11 -07:00
|
|
|
SocketBase* GetSocketBase();
|
2012-09-25 13:13:15 -07:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
// Shutdown state
|
|
|
|
//
|
2012-09-25 13:13:15 -07:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
bool IsShutdownOnMainThread() const;
|
|
|
|
void ShutdownOnMainThread();
|
2013-09-06 01:17:55 -07:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
bool IsShutdownOnIOThread() const;
|
|
|
|
void ShutdownOnIOThread();
|
2013-03-07 23:03:03 -08:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
// Delayed-task handling
|
|
|
|
//
|
2013-02-12 06:16:45 -08:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
void SetDelayedConnectTask(CancelableTask* aTask);
|
|
|
|
void ClearDelayedConnectTask();
|
|
|
|
void CancelDelayedConnectTask();
|
2014-07-31 01:14:18 -07:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
// Task callback methods
|
|
|
|
//
|
2014-07-31 00:29:20 -07:00
|
|
|
|
2014-07-31 01:14:18 -07:00
|
|
|
/**
|
|
|
|
* Run bind/listen to prepare for further runs of accept()
|
|
|
|
*/
|
|
|
|
void Listen();
|
|
|
|
|
|
|
|
/**
|
2014-07-31 04:55:30 -07:00
|
|
|
* Connect to a socket
|
2014-07-31 01:14:18 -07:00
|
|
|
*/
|
2014-07-31 04:55:30 -07:00
|
|
|
void Connect();
|
|
|
|
|
|
|
|
void Send(UnixSocketRawData* aData);
|
|
|
|
|
|
|
|
// I/O callback methods
|
|
|
|
//
|
2012-09-25 13:13:15 -07:00
|
|
|
|
2014-03-09 19:11:27 -07:00
|
|
|
void OnAccepted(int aFd, const sockaddr_any* aAddr,
|
|
|
|
socklen_t aAddrLen) MOZ_OVERRIDE;
|
2014-02-26 08:52:09 -08:00
|
|
|
void OnConnected() MOZ_OVERRIDE;
|
|
|
|
void OnError(const char* aFunction, int aErrno) MOZ_OVERRIDE;
|
|
|
|
void OnListening() MOZ_OVERRIDE;
|
|
|
|
void OnSocketCanReceiveWithoutBlocking() MOZ_OVERRIDE;
|
|
|
|
void OnSocketCanSendWithoutBlocking() MOZ_OVERRIDE;
|
|
|
|
|
2014-07-31 01:14:18 -07:00
|
|
|
private:
|
2014-07-31 04:55:30 -07:00
|
|
|
void FireSocketError();
|
|
|
|
|
|
|
|
// Set up flags on file descriptor.
|
2014-02-28 01:16:52 -08:00
|
|
|
static bool SetSocketFlags(int aFd);
|
2013-12-09 16:46:20 -08:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
/**
|
|
|
|
* Consumer pointer. Non-thread safe RefPtr, so should only be manipulated
|
|
|
|
* directly from main thread. All non-main-thread accesses should happen with
|
|
|
|
* mIO as container.
|
|
|
|
*/
|
|
|
|
RefPtr<UnixSocketConsumer> mConsumer;
|
2013-12-09 16:46:20 -08:00
|
|
|
|
2013-09-06 01:17:55 -07:00
|
|
|
/**
|
2012-10-01 00:03:16 -07:00
|
|
|
* Connector object used to create the connection we are currently using.
|
|
|
|
*/
|
|
|
|
nsAutoPtr<UnixSocketConnector> mConnector;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If true, do not requeue whatever task we're running
|
|
|
|
*/
|
2013-03-07 23:03:03 -08:00
|
|
|
bool mShuttingDownOnIOThread;
|
2012-10-01 00:03:16 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Address we are connecting to, assuming we are creating a client connection.
|
|
|
|
*/
|
|
|
|
nsCString mAddress;
|
2012-10-17 17:10:27 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Size of the socket address struct
|
|
|
|
*/
|
|
|
|
socklen_t mAddrSize;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Address struct of the socket currently in use
|
|
|
|
*/
|
2013-03-19 10:23:47 -07:00
|
|
|
sockaddr_any mAddr;
|
2013-04-01 17:26:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Task member for delayed connect task. Should only be access on main thread.
|
|
|
|
*/
|
|
|
|
CancelableTask* mDelayedConnectTask;
|
2012-09-25 13:13:15 -07:00
|
|
|
};
|
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
UnixSocketConsumerIO::UnixSocketConsumerIO(MessageLoop* mIOLoop,
|
|
|
|
UnixSocketConsumer* aConsumer,
|
|
|
|
UnixSocketConnector* aConnector,
|
|
|
|
const nsACString& aAddress)
|
|
|
|
: UnixSocketWatcher(mIOLoop)
|
|
|
|
, SocketIOBase(MAX_READ_SIZE)
|
|
|
|
, mConsumer(aConsumer)
|
|
|
|
, mConnector(aConnector)
|
|
|
|
, mShuttingDownOnIOThread(false)
|
|
|
|
, mAddress(aAddress)
|
|
|
|
, mDelayedConnectTask(nullptr)
|
2014-07-31 00:29:20 -07:00
|
|
|
{
|
2014-07-31 04:55:30 -07:00
|
|
|
MOZ_ASSERT(mConsumer);
|
|
|
|
MOZ_ASSERT(mConnector);
|
|
|
|
}
|
2014-07-31 00:29:20 -07:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
UnixSocketConsumerIO::~UnixSocketConsumerIO()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(IsShutdownOnMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnixSocketConsumerIO::GetSocketAddr(nsAString& aAddrStr) const
|
|
|
|
{
|
|
|
|
if (!mConnector) {
|
|
|
|
NS_WARNING("No connector to get socket address from!");
|
|
|
|
aAddrStr.Truncate();
|
|
|
|
return;
|
2013-03-07 23:03:03 -08:00
|
|
|
}
|
2014-07-31 04:55:30 -07:00
|
|
|
mConnector->GetSocketAddr(mAddr, aAddrStr);
|
|
|
|
}
|
2013-04-01 17:26:25 -07:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
SocketConsumerBase*
|
|
|
|
UnixSocketConsumerIO::GetConsumer()
|
2014-01-24 12:06:40 -08:00
|
|
|
{
|
2014-07-31 04:55:30 -07:00
|
|
|
return mConsumer.get();
|
|
|
|
}
|
2014-01-24 14:19:52 -08:00
|
|
|
|
2014-10-21 01:52:11 -07:00
|
|
|
SocketBase*
|
|
|
|
UnixSocketConsumerIO::GetSocketBase()
|
|
|
|
{
|
|
|
|
return GetConsumer();
|
|
|
|
}
|
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
bool
|
|
|
|
UnixSocketConsumerIO::IsShutdownOnMainThread() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
return mConsumer == nullptr;
|
|
|
|
}
|
2013-12-09 16:46:20 -08:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
void
|
|
|
|
UnixSocketConsumerIO::ShutdownOnMainThread()
|
2014-07-31 00:29:20 -07:00
|
|
|
{
|
2014-07-31 04:55:30 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!IsShutdownOnMainThread());
|
2014-07-31 00:29:18 -07:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
mConsumer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UnixSocketConsumerIO::IsShutdownOnIOThread() const
|
|
|
|
{
|
|
|
|
return mShuttingDownOnIOThread;
|
|
|
|
}
|
2014-07-31 00:29:20 -07:00
|
|
|
|
|
|
|
void
|
2014-07-31 04:55:30 -07:00
|
|
|
UnixSocketConsumerIO::ShutdownOnIOThread()
|
2014-07-31 00:29:20 -07:00
|
|
|
{
|
2014-07-31 04:55:30 -07:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mShuttingDownOnIOThread);
|
2013-12-09 16:46:20 -08:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
Close(); // will also remove fd from I/O loop
|
|
|
|
mShuttingDownOnIOThread = true;
|
|
|
|
}
|
2014-07-31 00:29:20 -07:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
void
|
|
|
|
UnixSocketConsumerIO::SetDelayedConnectTask(CancelableTask* aTask)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-07-31 04:55:28 -07:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
mDelayedConnectTask = aTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnixSocketConsumerIO::ClearDelayedConnectTask()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mDelayedConnectTask = nullptr;
|
2014-07-31 00:29:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-31 04:55:30 -07:00
|
|
|
UnixSocketConsumerIO::CancelDelayedConnectTask()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!mDelayedConnectTask) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDelayedConnectTask->Cancel();
|
|
|
|
ClearDelayedConnectTask();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnixSocketConsumerIO::Listen()
|
2013-12-09 16:46:20 -08:00
|
|
|
{
|
2014-02-26 08:52:09 -08:00
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
2013-12-09 16:46:20 -08:00
|
|
|
MOZ_ASSERT(mConnector);
|
2012-10-10 22:48:40 -07:00
|
|
|
|
2012-10-01 00:03:16 -07:00
|
|
|
// 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.
|
2013-03-19 10:23:47 -07:00
|
|
|
if (!mConnector->CreateAddr(true, mAddrSize, mAddr, nullptr)) {
|
|
|
|
NS_WARNING("Cannot create socket address!");
|
2013-12-09 16:46:20 -08:00
|
|
|
FireSocketError();
|
2013-03-19 10:23:47 -07:00
|
|
|
return;
|
|
|
|
}
|
2012-10-01 00:03:16 -07:00
|
|
|
|
2014-02-26 08:52:00 -08:00
|
|
|
if (!IsOpen()) {
|
|
|
|
int fd = mConnector->Create();
|
|
|
|
if (fd < 0) {
|
2013-12-09 16:46:20 -08:00
|
|
|
NS_WARNING("Cannot create socket fd!");
|
|
|
|
FireSocketError();
|
2012-10-01 00:03:16 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-02-28 01:16:52 -08:00
|
|
|
if (!SetSocketFlags(fd)) {
|
2013-12-09 16:46:20 -08:00
|
|
|
NS_WARNING("Cannot set socket flags!");
|
|
|
|
FireSocketError();
|
2012-10-01 00:03:16 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-02-28 01:16:52 -08:00
|
|
|
SetFd(fd);
|
2012-10-01 00:03:16 -07:00
|
|
|
|
2014-02-26 08:52:09 -08:00
|
|
|
// calls OnListening on success, or OnError otherwise
|
|
|
|
nsresult rv = UnixSocketWatcher::Listen(
|
|
|
|
reinterpret_cast<struct sockaddr*>(&mAddr), mAddrSize);
|
|
|
|
NS_WARN_IF(NS_FAILED(rv));
|
2012-10-01 00:03:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-31 04:55:30 -07:00
|
|
|
UnixSocketConsumerIO::Connect()
|
2012-10-01 00:03:16 -07:00
|
|
|
{
|
2014-02-26 08:52:09 -08:00
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
2013-12-09 16:46:20 -08:00
|
|
|
MOZ_ASSERT(mConnector);
|
2013-03-07 23:03:03 -08:00
|
|
|
|
2014-02-26 08:52:00 -08:00
|
|
|
if (!IsOpen()) {
|
|
|
|
int fd = mConnector->Create();
|
|
|
|
if (fd < 0) {
|
2013-12-09 16:46:20 -08:00
|
|
|
NS_WARNING("Cannot create socket fd!");
|
|
|
|
FireSocketError();
|
2012-10-01 00:03:16 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-02-28 01:16:52 -08:00
|
|
|
if (!SetSocketFlags(fd)) {
|
|
|
|
NS_WARNING("Cannot set socket flags!");
|
|
|
|
FireSocketError();
|
|
|
|
return;
|
|
|
|
}
|
2014-02-26 08:52:00 -08:00
|
|
|
SetFd(fd);
|
2012-10-01 00:03:16 -07:00
|
|
|
}
|
|
|
|
|
2013-03-19 10:23:47 -07:00
|
|
|
if (!mConnector->CreateAddr(false, mAddrSize, mAddr, mAddress.get())) {
|
|
|
|
NS_WARNING("Cannot create socket address!");
|
2013-12-09 16:46:20 -08:00
|
|
|
FireSocketError();
|
2013-03-19 10:23:47 -07:00
|
|
|
return;
|
|
|
|
}
|
2012-10-01 00:03:16 -07:00
|
|
|
|
2014-02-26 08:52:09 -08:00
|
|
|
// calls OnConnected() on success, or OnError() otherwise
|
|
|
|
nsresult rv = UnixSocketWatcher::Connect(
|
|
|
|
reinterpret_cast<struct sockaddr*>(&mAddr), mAddrSize);
|
|
|
|
NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
}
|
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
void
|
|
|
|
UnixSocketConsumerIO::Send(UnixSocketRawData* aData)
|
2014-02-26 08:52:09 -08:00
|
|
|
{
|
2014-07-31 04:55:30 -07:00
|
|
|
EnqueueData(aData);
|
|
|
|
AddWatchers(WRITE_WATCHER, false);
|
2014-02-26 08:52:09 -08:00
|
|
|
}
|
2013-05-13 20:51:21 -07:00
|
|
|
|
2014-02-26 08:52:09 -08:00
|
|
|
void
|
2014-07-31 04:55:30 -07:00
|
|
|
UnixSocketConsumerIO::OnAccepted(int aFd,
|
|
|
|
const sockaddr_any* aAddr,
|
|
|
|
socklen_t aAddrLen)
|
2014-02-26 08:52:09 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);
|
2014-03-09 19:11:27 -07:00
|
|
|
MOZ_ASSERT(aAddr);
|
2014-11-24 16:54:33 -08:00
|
|
|
MOZ_ASSERT(aAddrLen > 0 && (size_t)aAddrLen <= sizeof(mAddr));
|
2014-03-09 19:11:27 -07:00
|
|
|
|
|
|
|
memcpy (&mAddr, aAddr, aAddrLen);
|
|
|
|
mAddrSize = aAddrLen;
|
2013-05-13 20:51:21 -07:00
|
|
|
|
2014-02-26 08:52:09 -08:00
|
|
|
if (!mConnector->SetUp(aFd)) {
|
|
|
|
NS_WARNING("Could not set up socket!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
|
|
|
|
Close();
|
2014-02-28 01:16:52 -08:00
|
|
|
if (!SetSocketFlags(aFd)) {
|
2012-10-01 00:03:16 -07:00
|
|
|
return;
|
2012-09-30 22:54:27 -07:00
|
|
|
}
|
2014-02-28 01:16:52 -08:00
|
|
|
SetSocket(aFd, SOCKET_IS_CONNECTED);
|
2012-09-30 22:54:27 -07:00
|
|
|
|
2014-07-31 04:55:28 -07:00
|
|
|
nsRefPtr<nsRunnable> r =
|
2014-07-31 04:55:30 -07:00
|
|
|
new SocketIOEventRunnable<UnixSocketConsumerIO>(
|
|
|
|
this, SocketIOEventRunnable<UnixSocketConsumerIO>::CONNECT_SUCCESS);
|
2014-02-28 01:16:53 -08:00
|
|
|
NS_DispatchToMainThread(r);
|
2014-02-26 08:52:09 -08:00
|
|
|
|
2014-02-26 08:52:18 -08:00
|
|
|
AddWatchers(READ_WATCHER, true);
|
2014-07-31 04:55:29 -07:00
|
|
|
if (HasPendingData()) {
|
2014-02-26 08:52:18 -08:00
|
|
|
AddWatchers(WRITE_WATCHER, false);
|
|
|
|
}
|
2014-02-26 08:52:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-31 04:55:30 -07:00
|
|
|
UnixSocketConsumerIO::OnConnected()
|
2014-02-26 08:52:09 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED);
|
|
|
|
|
|
|
|
if (!SetSocketFlags(GetFd())) {
|
2013-12-09 16:46:20 -08:00
|
|
|
NS_WARNING("Cannot set socket flags!");
|
|
|
|
FireSocketError();
|
2013-03-08 14:40:26 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-26 08:52:00 -08:00
|
|
|
if (!mConnector->SetUp(GetFd())) {
|
2012-10-01 00:03:16 -07:00
|
|
|
NS_WARNING("Could not set up socket!");
|
2013-12-09 16:46:20 -08:00
|
|
|
FireSocketError();
|
2012-10-01 00:03:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-31 04:55:28 -07:00
|
|
|
nsRefPtr<nsRunnable> r =
|
2014-07-31 04:55:30 -07:00
|
|
|
new SocketIOEventRunnable<UnixSocketConsumerIO>(
|
|
|
|
this, SocketIOEventRunnable<UnixSocketConsumerIO>::CONNECT_SUCCESS);
|
2014-02-28 01:16:53 -08:00
|
|
|
NS_DispatchToMainThread(r);
|
2012-10-10 22:48:40 -07:00
|
|
|
|
2014-02-26 08:52:18 -08:00
|
|
|
AddWatchers(READ_WATCHER, true);
|
2014-07-31 04:55:29 -07:00
|
|
|
if (HasPendingData()) {
|
2014-02-26 08:52:18 -08:00
|
|
|
AddWatchers(WRITE_WATCHER, false);
|
|
|
|
}
|
2012-10-01 00:03:16 -07:00
|
|
|
}
|
|
|
|
|
2014-02-26 08:52:09 -08:00
|
|
|
void
|
2014-07-31 04:55:30 -07:00
|
|
|
UnixSocketConsumerIO::OnListening()
|
2012-10-01 00:03:16 -07:00
|
|
|
{
|
2014-02-26 08:52:09 -08:00
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);
|
2012-10-01 00:03:16 -07:00
|
|
|
|
2014-02-26 08:52:09 -08:00
|
|
|
if (!mConnector->SetUpListenSocket(GetFd())) {
|
|
|
|
NS_WARNING("Could not set up listen socket!");
|
|
|
|
FireSocketError();
|
|
|
|
return;
|
2012-09-25 13:13:15 -07:00
|
|
|
}
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2014-02-26 08:52:18 -08:00
|
|
|
AddWatchers(READ_WATCHER, true);
|
2014-02-26 08:52:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-31 04:55:30 -07:00
|
|
|
UnixSocketConsumerIO::OnError(const char* aFunction, int aErrno)
|
2014-02-26 08:52:09 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
|
|
|
|
UnixFdWatcher::OnError(aFunction, aErrno);
|
|
|
|
FireSocketError();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-31 04:55:30 -07:00
|
|
|
UnixSocketConsumerIO::OnSocketCanReceiveWithoutBlocking()
|
2014-02-26 08:52:09 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
2014-04-08 00:30:58 -07:00
|
|
|
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); // see bug 990984
|
2014-02-26 08:52:09 -08:00
|
|
|
|
2014-09-05 01:16:24 -07:00
|
|
|
ssize_t res = ReceiveData(GetFd(), this);
|
|
|
|
if (res < 0) {
|
|
|
|
/* I/O error */
|
2014-07-31 04:55:29 -07:00
|
|
|
RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
|
2014-09-05 01:16:24 -07:00
|
|
|
} else if (!res) {
|
|
|
|
/* EOF or peer shutdown */
|
|
|
|
RemoveWatchers(READ_WATCHER);
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
2014-02-26 08:52:09 -08:00
|
|
|
}
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2014-02-26 08:52:09 -08:00
|
|
|
void
|
2014-07-31 04:55:30 -07:00
|
|
|
UnixSocketConsumerIO::OnSocketCanSendWithoutBlocking()
|
2014-02-26 08:52:09 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
2014-04-08 00:30:58 -07:00
|
|
|
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); // see bug 990984
|
2014-02-26 08:52:09 -08:00
|
|
|
|
2014-07-31 04:55:29 -07:00
|
|
|
nsresult rv = SendPendingData(GetFd(), this);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-26 08:52:09 -08:00
|
|
|
|
2014-07-31 04:55:29 -07:00
|
|
|
if (HasPendingData()) {
|
|
|
|
AddWatchers(WRITE_WATCHER, false);
|
2014-02-26 08:52:09 -08:00
|
|
|
}
|
2012-09-25 13:13:15 -07:00
|
|
|
}
|
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
void
|
|
|
|
UnixSocketConsumerIO::FireSocketError()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
|
|
|
|
// Clean up watchers, statuses, fds
|
|
|
|
Close();
|
|
|
|
|
|
|
|
// Tell the main thread we've errored
|
|
|
|
nsRefPtr<nsRunnable> r =
|
|
|
|
new SocketIOEventRunnable<UnixSocketConsumerIO>(
|
|
|
|
this, SocketIOEventRunnable<UnixSocketConsumerIO>::CONNECT_ERROR);
|
|
|
|
|
|
|
|
NS_DispatchToMainThread(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UnixSocketConsumerIO::SetSocketFlags(int aFd)
|
|
|
|
{
|
|
|
|
// Set socket addr to be reused even if kernel is still waiting to close
|
|
|
|
int n = 1;
|
|
|
|
if (setsockopt(aFd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set close-on-exec bit.
|
|
|
|
int flags = TEMP_FAILURE_RETRY(fcntl(aFd, F_GETFD));
|
|
|
|
if (-1 == flags) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
flags |= FD_CLOEXEC;
|
|
|
|
if (-1 == TEMP_FAILURE_RETRY(fcntl(aFd, F_SETFD, flags))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set non-blocking status flag.
|
|
|
|
flags = TEMP_FAILURE_RETRY(fcntl(aFd, F_GETFL));
|
|
|
|
if (-1 == flags) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
if (-1 == TEMP_FAILURE_RETRY(fcntl(aFd, F_SETFL, flags))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Socket tasks
|
|
|
|
//
|
|
|
|
|
|
|
|
class ListenTask MOZ_FINAL : public SocketIOTask<UnixSocketConsumerIO>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ListenTask(UnixSocketConsumerIO* aIO)
|
|
|
|
: SocketIOTask<UnixSocketConsumerIO>(aIO)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void Run() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!IsCanceled()) {
|
|
|
|
GetIO()->Listen();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ConnectTask MOZ_FINAL : public SocketIOTask<UnixSocketConsumerIO>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ConnectTask(UnixSocketConsumerIO* aIO)
|
|
|
|
: SocketIOTask<UnixSocketConsumerIO>(aIO)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void Run() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!IsCanceled());
|
|
|
|
|
|
|
|
GetIO()->Connect();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DelayedConnectTask MOZ_FINAL : public SocketIOTask<UnixSocketConsumerIO>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DelayedConnectTask(UnixSocketConsumerIO* aIO)
|
|
|
|
: SocketIOTask<UnixSocketConsumerIO>(aIO)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void Run() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (IsCanceled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnixSocketConsumerIO* io = GetIO();
|
|
|
|
if (io->IsShutdownOnMainThread()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
io->ClearDelayedConnectTask();
|
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE, new ConnectTask(io));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-31 04:55:27 -07:00
|
|
|
//
|
|
|
|
// UnixSocketConsumer
|
|
|
|
//
|
|
|
|
|
|
|
|
UnixSocketConsumer::UnixSocketConsumer()
|
2014-07-31 04:55:30 -07:00
|
|
|
: mIO(nullptr)
|
2014-07-31 04:55:27 -07:00
|
|
|
{ }
|
2012-10-10 22:48:40 -07:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
UnixSocketConsumer::~UnixSocketConsumer()
|
|
|
|
{
|
2014-07-31 04:55:30 -07:00
|
|
|
MOZ_ASSERT(!mIO);
|
2012-09-25 13:13:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UnixSocketConsumer::SendSocketData(UnixSocketRawData* aData)
|
|
|
|
{
|
2012-10-10 22:48:40 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-07-31 04:55:30 -07:00
|
|
|
if (!mIO) {
|
2012-09-25 13:13:15 -07:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-07 23:03:03 -08:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
MOZ_ASSERT(!mIO->IsShutdownOnMainThread());
|
2014-07-31 04:55:30 -07:00
|
|
|
XRE_GetIOMessageLoop()->PostTask(
|
2014-10-21 01:52:12 -07:00
|
|
|
FROM_HERE,
|
|
|
|
new SocketIOSendTask<UnixSocketConsumerIO, UnixSocketRawData>(mIO, aData));
|
2014-07-31 04:55:30 -07:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UnixSocketConsumer::SendSocketData(const nsACString& aStr)
|
|
|
|
{
|
2014-07-31 04:55:30 -07:00
|
|
|
if (aStr.Length() > MAX_READ_SIZE) {
|
2012-09-25 13:13:15 -07:00
|
|
|
return false;
|
|
|
|
}
|
2014-07-31 04:55:30 -07:00
|
|
|
|
|
|
|
nsAutoPtr<UnixSocketRawData> data(
|
|
|
|
new UnixSocketRawData(aStr.BeginReading(), aStr.Length()));
|
|
|
|
|
|
|
|
if (!SendSocketData(data)) {
|
2012-09-25 13:13:15 -07:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-07 23:03:03 -08:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
unused << data.forget();
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnixSocketConsumer::CloseSocket()
|
|
|
|
{
|
2012-10-12 11:38:14 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-07-31 04:55:30 -07:00
|
|
|
if (!mIO) {
|
2012-09-25 13:13:15 -07:00
|
|
|
return;
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
2013-02-21 07:32:17 -08:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
mIO->CancelDelayedConnectTask();
|
2013-04-01 17:26:25 -07:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
// From this point on, we consider mIO as being deleted.
|
2013-03-07 23:03:03 -08:00
|
|
|
// We sever the relationship here so any future calls to listen or connect
|
|
|
|
// will create a new implementation.
|
2014-07-31 04:55:30 -07:00
|
|
|
mIO->ShutdownOnMainThread();
|
2013-03-07 23:03:03 -08:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
XRE_GetIOMessageLoop()->PostTask(
|
2014-07-31 04:55:30 -07:00
|
|
|
FROM_HERE, new SocketIOShutdownTask<UnixSocketConsumerIO>(mIO));
|
2013-03-07 23:03:03 -08:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
mIO = nullptr;
|
2013-01-15 18:21:49 -08:00
|
|
|
|
2012-10-12 11:38:14 -07:00
|
|
|
NotifyDisconnect();
|
2012-09-25 13:13:15 -07:00
|
|
|
}
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-10-17 17:10:27 -07:00
|
|
|
void
|
2012-10-17 17:11:05 -07:00
|
|
|
UnixSocketConsumer::GetSocketAddr(nsAString& aAddrStr)
|
2012-10-17 17:10:27 -07:00
|
|
|
{
|
2013-03-07 23:03:03 -08:00
|
|
|
aAddrStr.Truncate();
|
2014-07-31 04:55:30 -07:00
|
|
|
if (!mIO || GetConnectionStatus() != SOCKET_CONNECTED) {
|
2012-10-17 17:10:27 -07:00
|
|
|
NS_WARNING("No socket currently open!");
|
|
|
|
return;
|
|
|
|
}
|
2014-07-31 04:55:30 -07:00
|
|
|
mIO->GetSocketAddr(aAddrStr);
|
2012-10-17 17:10:27 -07:00
|
|
|
}
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
bool
|
2012-10-01 00:03:16 -07:00
|
|
|
UnixSocketConsumer::ConnectSocket(UnixSocketConnector* aConnector,
|
2013-02-01 04:28:18 -08:00
|
|
|
const char* aAddress,
|
|
|
|
int aDelayMs)
|
2012-09-05 20:06:06 -07:00
|
|
|
{
|
2012-10-01 00:03:16 -07:00
|
|
|
MOZ_ASSERT(aConnector);
|
2012-10-10 22:48:40 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-03-17 23:39:08 -07:00
|
|
|
|
|
|
|
nsAutoPtr<UnixSocketConnector> connector(aConnector);
|
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
if (mIO) {
|
2012-10-01 00:03:16 -07:00
|
|
|
NS_WARNING("Socket already connecting/connected!");
|
2012-09-25 13:13:15 -07:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-17 23:39:08 -07:00
|
|
|
|
2013-03-07 23:03:03 -08:00
|
|
|
nsCString addr(aAddress);
|
2013-02-01 04:28:18 -08:00
|
|
|
MessageLoop* ioLoop = XRE_GetIOMessageLoop();
|
2014-07-31 04:55:30 -07:00
|
|
|
mIO = new UnixSocketConsumerIO(ioLoop, this, connector.forget(), addr);
|
2014-07-31 04:55:27 -07:00
|
|
|
SetConnectionStatus(SOCKET_CONNECTING);
|
2013-02-01 04:28:18 -08:00
|
|
|
if (aDelayMs > 0) {
|
2014-07-31 04:55:30 -07:00
|
|
|
DelayedConnectTask* connectTask = new DelayedConnectTask(mIO);
|
|
|
|
mIO->SetDelayedConnectTask(connectTask);
|
2013-04-01 17:26:25 -07:00
|
|
|
MessageLoop::current()->PostDelayedTask(FROM_HERE, connectTask, aDelayMs);
|
2013-02-01 04:28:18 -08:00
|
|
|
} else {
|
2014-07-31 04:55:30 -07:00
|
|
|
ioLoop->PostTask(FROM_HERE, new ConnectTask(mIO));
|
2013-02-01 04:28:18 -08:00
|
|
|
}
|
2012-10-01 00:03:16 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UnixSocketConsumer::ListenSocket(UnixSocketConnector* aConnector)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aConnector);
|
2012-10-10 22:48:40 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-03-17 23:39:08 -07:00
|
|
|
|
|
|
|
nsAutoPtr<UnixSocketConnector> connector(aConnector);
|
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
if (mIO) {
|
2012-10-01 00:03:16 -07:00
|
|
|
NS_WARNING("Socket already connecting/connected!");
|
2012-09-25 13:13:15 -07:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-17 23:39:08 -07:00
|
|
|
|
2014-07-31 04:55:30 -07:00
|
|
|
mIO = new UnixSocketConsumerIO(
|
|
|
|
XRE_GetIOMessageLoop(), this, connector.forget(), EmptyCString());
|
2014-07-31 04:55:27 -07:00
|
|
|
SetConnectionStatus(SOCKET_LISTENING);
|
2014-07-31 04:55:30 -07:00
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE, new ListenTask(mIO));
|
2012-09-25 13:13:15 -07:00
|
|
|
return true;
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|