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>
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2014-04-23 00:32:31 -07:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
#include "GeckoTaskTracer.h"
|
|
|
|
using namespace mozilla::tasktracer;
|
|
|
|
#endif
|
|
|
|
|
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-02-26 08:52:09 -08:00
|
|
|
class UnixSocketImpl : public UnixSocketWatcher
|
2012-09-05 20:06:06 -07:00
|
|
|
{
|
2012-09-25 13:13:15 -07:00
|
|
|
public:
|
2014-02-26 08:52:00 -08:00
|
|
|
UnixSocketImpl(MessageLoop* mIOLoop,
|
|
|
|
UnixSocketConsumer* aConsumer, UnixSocketConnector* aConnector,
|
2014-02-26 08:52:09 -08:00
|
|
|
const nsACString& aAddress)
|
|
|
|
: UnixSocketWatcher(mIOLoop)
|
2014-02-26 08:52:00 -08:00
|
|
|
, mConsumer(aConsumer)
|
2012-10-01 00:03:16 -07:00
|
|
|
, mConnector(aConnector)
|
2013-03-07 23:03:03 -08:00
|
|
|
, mShuttingDownOnIOThread(false)
|
2012-10-01 00:03:16 -07:00
|
|
|
, mAddress(aAddress)
|
2013-04-01 17:26:25 -07:00
|
|
|
, mDelayedConnectTask(nullptr)
|
2012-09-25 13:13:15 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~UnixSocketImpl()
|
|
|
|
{
|
2013-03-07 23:03:03 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(IsShutdownOnMainThread());
|
2012-09-25 13:13:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void QueueWriteData(UnixSocketRawData* aData)
|
|
|
|
{
|
|
|
|
mOutgoingQ.AppendElement(aData);
|
2014-02-26 08:52:09 -08:00
|
|
|
AddWatchers(WRITE_WATCHER, false);
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
2012-09-25 13:13:15 -07:00
|
|
|
|
2013-03-07 23:03:03 -08:00
|
|
|
bool IsShutdownOnMainThread()
|
2012-10-01 00:03:16 -07:00
|
|
|
{
|
2013-03-07 23:03:03 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mConsumer == nullptr;
|
2012-10-01 00:03:16 -07:00
|
|
|
}
|
2013-09-06 01:17:55 -07:00
|
|
|
|
2013-03-07 23:03:03 -08:00
|
|
|
void ShutdownOnMainThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!IsShutdownOnMainThread());
|
|
|
|
mConsumer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsShutdownOnIOThread()
|
2013-02-12 06:16:45 -08:00
|
|
|
{
|
2013-03-07 23:03:03 -08:00
|
|
|
return mShuttingDownOnIOThread;
|
2013-02-12 06:16:45 -08:00
|
|
|
}
|
|
|
|
|
2013-03-07 23:03:03 -08:00
|
|
|
void ShutdownOnIOThread()
|
2012-10-01 00:03:16 -07:00
|
|
|
{
|
2013-03-07 23:03:03 -08:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mShuttingDownOnIOThread);
|
|
|
|
|
2014-04-08 00:30:58 -07:00
|
|
|
Close(); // will also remove fd from I/O loop
|
2013-03-07 23:03:03 -08:00
|
|
|
mShuttingDownOnIOThread = true;
|
2012-10-01 00:03:16 -07:00
|
|
|
}
|
2013-03-07 23:03:03 -08:00
|
|
|
|
2013-04-01 17:26:25 -07:00
|
|
|
void SetDelayedConnectTask(CancelableTask* aTask)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mDelayedConnectTask = aTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearDelayedConnectTask()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mDelayedConnectTask = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CancelDelayedConnectTask()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!mDelayedConnectTask) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mDelayedConnectTask->Cancel();
|
|
|
|
ClearDelayedConnectTask();
|
|
|
|
}
|
|
|
|
|
2013-09-06 01:17:55 -07:00
|
|
|
/**
|
2012-10-01 00:03:16 -07:00
|
|
|
* Connect to a socket
|
|
|
|
*/
|
|
|
|
void Connect();
|
|
|
|
|
2013-09-06 01:17:55 -07:00
|
|
|
/**
|
2012-10-01 00:03:16 -07:00
|
|
|
* Run bind/listen to prepare for further runs of accept()
|
|
|
|
*/
|
|
|
|
void Listen();
|
|
|
|
|
2012-10-17 17:11:05 -07:00
|
|
|
void GetSocketAddr(nsAString& aAddrStr)
|
2012-10-17 17:10:27 -07:00
|
|
|
{
|
2013-04-01 17:26:25 -07:00
|
|
|
if (!mConnector) {
|
2012-10-17 17:11:05 -07:00
|
|
|
NS_WARNING("No connector to get socket address from!");
|
2013-03-07 23:03:03 -08:00
|
|
|
aAddrStr.Truncate();
|
2012-10-17 17:11:05 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mConnector->GetSocketAddr(mAddr, aAddrStr);
|
2012-10-17 17:10:27 -07:00
|
|
|
}
|
|
|
|
|
2012-09-25 13:13:15 -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
|
|
|
|
* mImpl as container.
|
|
|
|
*/
|
|
|
|
RefPtr<UnixSocketConsumer> mConsumer;
|
|
|
|
|
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;
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
private:
|
2014-02-28 01:16:52 -08:00
|
|
|
// Set up flags on whatever our current file descriptor is.
|
|
|
|
static bool SetSocketFlags(int aFd);
|
2013-12-09 16:46:20 -08:00
|
|
|
|
|
|
|
void FireSocketError();
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
/**
|
|
|
|
* Raw data queue. Must be pushed/popped from IO thread only.
|
|
|
|
*/
|
|
|
|
typedef nsTArray<UnixSocketRawData* > UnixSocketRawDataQueue;
|
|
|
|
UnixSocketRawDataQueue mOutgoingQ;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2013-01-15 18:21:49 -08:00
|
|
|
template<class T>
|
|
|
|
class DeleteInstanceRunnable : public nsRunnable
|
2012-09-25 13:13:15 -07:00
|
|
|
{
|
2013-01-15 18:21:49 -08:00
|
|
|
public:
|
|
|
|
DeleteInstanceRunnable(T* aInstance)
|
|
|
|
: mInstance(aInstance)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
delete mInstance;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* mInstance;
|
|
|
|
};
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
class UnixSocketImplRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UnixSocketImpl* GetImpl() const
|
|
|
|
{
|
|
|
|
return mImpl;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
UnixSocketImplRunnable(UnixSocketImpl* aImpl)
|
|
|
|
: mImpl(aImpl)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImpl);
|
|
|
|
}
|
|
|
|
virtual ~UnixSocketImplRunnable()
|
|
|
|
{ }
|
|
|
|
private:
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
};
|
|
|
|
|
|
|
|
class OnSocketEventRunnable : public UnixSocketImplRunnable
|
2012-10-10 22:48:40 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum SocketEvent {
|
|
|
|
CONNECT_SUCCESS,
|
|
|
|
CONNECT_ERROR,
|
|
|
|
DISCONNECT
|
|
|
|
};
|
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
OnSocketEventRunnable(UnixSocketImpl* aImpl, SocketEvent e)
|
|
|
|
: UnixSocketImplRunnable(aImpl)
|
|
|
|
, mEvent(e)
|
2012-10-10 22:48:40 -07:00
|
|
|
{
|
2013-03-07 23:03:03 -08:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2012-10-10 22:48:40 -07:00
|
|
|
}
|
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE
|
2012-10-10 22:48:40 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-02-28 01:16:53 -08:00
|
|
|
|
|
|
|
UnixSocketImpl* impl = GetImpl();
|
|
|
|
|
|
|
|
if (impl->IsShutdownOnMainThread()) {
|
2013-03-07 23:03:03 -08:00
|
|
|
NS_WARNING("CloseSocket has already been called!");
|
2012-10-10 22:48:40 -07:00
|
|
|
// Since we've already explicitly closed and the close happened before
|
|
|
|
// this, this isn't really an error. Since we've warned, return OK.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (mEvent == CONNECT_SUCCESS) {
|
2014-02-28 01:16:53 -08:00
|
|
|
impl->mConsumer->NotifySuccess();
|
2012-10-10 22:48:40 -07:00
|
|
|
} else if (mEvent == CONNECT_ERROR) {
|
2014-02-28 01:16:53 -08:00
|
|
|
impl->mConsumer->NotifyError();
|
2012-10-12 11:38:14 -07:00
|
|
|
} else if (mEvent == DISCONNECT) {
|
2014-02-28 01:16:53 -08:00
|
|
|
impl->mConsumer->NotifyDisconnect();
|
2012-10-10 22:48:40 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
SocketEvent mEvent;
|
|
|
|
};
|
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
class SocketReceiveRunnable : public UnixSocketImplRunnable
|
2012-09-05 20:06:06 -07:00
|
|
|
{
|
2012-09-25 13:13:15 -07:00
|
|
|
public:
|
2014-02-28 01:16:53 -08:00
|
|
|
SocketReceiveRunnable(UnixSocketImpl* aImpl, UnixSocketRawData* aData)
|
|
|
|
: UnixSocketImplRunnable(aImpl)
|
|
|
|
, mRawData(aData)
|
2012-09-25 13:13:15 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aData);
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE
|
2012-09-25 13:13:15 -07:00
|
|
|
{
|
2013-03-07 23:03:03 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-02-28 01:16:53 -08:00
|
|
|
|
|
|
|
UnixSocketImpl* impl = GetImpl();
|
|
|
|
|
|
|
|
if (impl->IsShutdownOnMainThread()) {
|
2012-09-25 13:13:15 -07:00
|
|
|
NS_WARNING("mConsumer is null, aborting receive!");
|
|
|
|
// Since we've already explicitly closed and the close happened before
|
|
|
|
// this, this isn't really an error. Since we've warned, return OK.
|
|
|
|
return NS_OK;
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
2013-03-07 23:03:03 -08:00
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
MOZ_ASSERT(impl->mConsumer);
|
|
|
|
impl->mConsumer->ReceiveSocketData(mRawData);
|
2012-09-25 13:13:15 -07:00
|
|
|
return NS_OK;
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
2012-09-25 13:13:15 -07:00
|
|
|
private:
|
|
|
|
nsAutoPtr<UnixSocketRawData> mRawData;
|
|
|
|
};
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
class RequestClosingSocketRunnable : public UnixSocketImplRunnable
|
2012-09-25 13:13:15 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-02-28 01:16:53 -08:00
|
|
|
RequestClosingSocketRunnable(UnixSocketImpl* aImpl)
|
|
|
|
: UnixSocketImplRunnable(aImpl)
|
|
|
|
{ }
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE
|
2012-09-25 13:13:15 -07:00
|
|
|
{
|
2014-02-28 01:16:53 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
UnixSocketImpl* impl = GetImpl();
|
|
|
|
if (impl->IsShutdownOnMainThread()) {
|
|
|
|
NS_WARNING("CloseSocket has already been called!");
|
|
|
|
// Since we've already explicitly closed and the close happened before
|
|
|
|
// this, this isn't really an error. Since we've warned, return OK.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-03-07 23:03:03 -08:00
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
// Start from here, same handling flow as calling CloseSocket() from
|
|
|
|
// upper layer
|
|
|
|
impl->mConsumer->CloseSocket();
|
|
|
|
return NS_OK;
|
2012-09-25 13:13:15 -07:00
|
|
|
}
|
2014-02-28 01:16:53 -08:00
|
|
|
};
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
class UnixSocketImplTask : public CancelableTask
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UnixSocketImpl* GetImpl() const
|
|
|
|
{
|
|
|
|
return mImpl;
|
|
|
|
}
|
|
|
|
void Cancel() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
mImpl = nullptr;
|
|
|
|
}
|
|
|
|
bool IsCanceled() const
|
|
|
|
{
|
|
|
|
return !mImpl;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
UnixSocketImplTask(UnixSocketImpl* aImpl)
|
|
|
|
: mImpl(aImpl)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mImpl);
|
|
|
|
}
|
2012-09-25 13:13:15 -07:00
|
|
|
private:
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
};
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
class SocketSendTask : public UnixSocketImplTask
|
2013-02-21 07:32:17 -08:00
|
|
|
{
|
|
|
|
public:
|
2014-02-28 01:16:53 -08:00
|
|
|
SocketSendTask(UnixSocketImpl* aImpl,
|
|
|
|
UnixSocketConsumer* aConsumer,
|
|
|
|
UnixSocketRawData* aData)
|
|
|
|
: UnixSocketImplTask(aImpl)
|
|
|
|
, mConsumer(aConsumer)
|
|
|
|
, mData(aData)
|
2013-02-21 07:32:17 -08:00
|
|
|
{
|
2014-02-28 01:16:53 -08:00
|
|
|
MOZ_ASSERT(aConsumer);
|
|
|
|
MOZ_ASSERT(aData);
|
2013-02-21 07:32:17 -08:00
|
|
|
}
|
2014-02-28 01:16:53 -08:00
|
|
|
void Run() MOZ_OVERRIDE
|
2013-02-21 07:32:17 -08:00
|
|
|
{
|
2014-02-28 01:16:53 -08:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!IsCanceled());
|
2013-02-21 07:32:17 -08:00
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
UnixSocketImpl* impl = GetImpl();
|
|
|
|
MOZ_ASSERT(!impl->IsShutdownOnIOThread());
|
2013-02-21 07:32:17 -08:00
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
impl->QueueWriteData(mData);
|
2013-02-21 07:32:17 -08:00
|
|
|
}
|
|
|
|
private:
|
2014-02-28 01:16:53 -08:00
|
|
|
nsRefPtr<UnixSocketConsumer> mConsumer;
|
|
|
|
UnixSocketRawData* mData;
|
2013-02-21 07:32:17 -08:00
|
|
|
};
|
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
class SocketListenTask : public UnixSocketImplTask
|
2014-02-26 08:52:09 -08:00
|
|
|
{
|
2012-10-01 00:03:16 -07:00
|
|
|
public:
|
2014-02-28 01:16:53 -08:00
|
|
|
SocketListenTask(UnixSocketImpl* aImpl)
|
|
|
|
: UnixSocketImplTask(aImpl)
|
|
|
|
{ }
|
2013-03-07 23:03:03 -08:00
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
void Run() MOZ_OVERRIDE
|
2013-03-07 23:03:03 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2014-02-28 01:16:53 -08:00
|
|
|
if (!IsCanceled()) {
|
|
|
|
GetImpl()->Listen();
|
|
|
|
}
|
2013-03-07 23:03:03 -08:00
|
|
|
}
|
2012-10-01 00:03:16 -07:00
|
|
|
};
|
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
class SocketConnectTask : public UnixSocketImplTask
|
2013-03-07 23:03:03 -08:00
|
|
|
{
|
2014-02-28 01:16:53 -08:00
|
|
|
public:
|
|
|
|
SocketConnectTask(UnixSocketImpl* aImpl)
|
|
|
|
: UnixSocketImplTask(aImpl)
|
|
|
|
{ }
|
2013-03-07 23:03:03 -08:00
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
void Run() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!IsCanceled());
|
|
|
|
GetImpl()->Connect();
|
2012-10-01 00:03:16 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
class SocketDelayedConnectTask : public UnixSocketImplTask
|
2013-03-07 23:03:03 -08:00
|
|
|
{
|
2013-04-01 17:26:25 -07:00
|
|
|
public:
|
2014-02-28 01:16:53 -08:00
|
|
|
SocketDelayedConnectTask(UnixSocketImpl* aImpl)
|
|
|
|
: UnixSocketImplTask(aImpl)
|
|
|
|
{ }
|
2013-04-01 17:26:25 -07:00
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
void Run() MOZ_OVERRIDE
|
2013-04-01 17:26:25 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-02-28 01:16:53 -08:00
|
|
|
if (IsCanceled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UnixSocketImpl* impl = GetImpl();
|
|
|
|
if (impl->IsShutdownOnMainThread()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
impl->ClearDelayedConnectTask();
|
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE, new SocketConnectTask(impl));
|
2013-04-01 17:26:25 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
class ShutdownSocketTask : public UnixSocketImplTask
|
2013-04-01 17:26:25 -07:00
|
|
|
{
|
2014-02-28 01:16:53 -08:00
|
|
|
public:
|
|
|
|
ShutdownSocketTask(UnixSocketImpl* aImpl)
|
|
|
|
: UnixSocketImplTask(aImpl)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void Run() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!IsCanceled());
|
2013-04-01 17:26:25 -07:00
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
UnixSocketImpl* impl = GetImpl();
|
2013-03-07 23:03:03 -08:00
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
// At this point, there should be no new events on the IO thread after this
|
|
|
|
// one with the possible exception of a SocketListenTask that
|
|
|
|
// ShutdownOnIOThread will cancel for us. We are now fully shut down, so we
|
|
|
|
// can send a message to the main thread that will delete impl safely knowing
|
|
|
|
// that no more tasks reference it.
|
|
|
|
impl->ShutdownOnIOThread();
|
2013-03-07 23:03:03 -08:00
|
|
|
|
2014-02-28 01:16:53 -08:00
|
|
|
nsRefPtr<nsIRunnable> r(new DeleteInstanceRunnable<UnixSocketImpl>(impl));
|
|
|
|
nsresult rv = NS_DispatchToMainThread(r);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
}
|
2013-03-07 23:03:03 -08:00
|
|
|
};
|
|
|
|
|
2014-01-24 12:06:40 -08:00
|
|
|
void
|
2013-12-09 16:46:20 -08:00
|
|
|
UnixSocketImpl::FireSocketError()
|
2014-01-24 12:06:40 -08:00
|
|
|
{
|
2014-02-26 08:52:09 -08:00
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
2014-01-24 14:19:52 -08:00
|
|
|
|
2013-12-09 16:46:20 -08:00
|
|
|
// Clean up watchers, statuses, fds
|
2014-02-26 08:52:00 -08:00
|
|
|
Close();
|
2013-12-09 16:46:20 -08:00
|
|
|
|
|
|
|
// Tell the main thread we've errored
|
2014-02-28 01:16:53 -08:00
|
|
|
nsRefPtr<OnSocketEventRunnable> r =
|
|
|
|
new OnSocketEventRunnable(this, OnSocketEventRunnable::CONNECT_ERROR);
|
|
|
|
NS_DispatchToMainThread(r);
|
2013-12-09 16:46:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-02-26 08:52:09 -08:00
|
|
|
UnixSocketImpl::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
|
|
|
|
UnixSocketImpl::Connect()
|
|
|
|
{
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UnixSocketImpl::SetSocketFlags(int aFd)
|
|
|
|
{
|
|
|
|
// Set socket addr to be reused even if kernel is still waiting to close
|
|
|
|
int n = 1;
|
2014-02-28 01:16:52 -08:00
|
|
|
if (setsockopt(aFd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-26 08:52:09 -08:00
|
|
|
|
|
|
|
// Set close-on-exec bit.
|
2014-02-28 01:16:52 -08:00
|
|
|
int flags = TEMP_FAILURE_RETRY(fcntl(aFd, F_GETFD));
|
2014-02-26 08:52:09 -08:00
|
|
|
if (-1 == flags) {
|
|
|
|
return false;
|
2013-05-13 20:51:21 -07:00
|
|
|
}
|
2014-02-26 08:52:09 -08:00
|
|
|
flags |= FD_CLOEXEC;
|
2014-02-28 01:16:52 -08:00
|
|
|
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))) {
|
2014-02-26 08:52:09 -08:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-01 00:03:16 -07:00
|
|
|
|
2014-02-26 08:52:09 -08:00
|
|
|
return true;
|
|
|
|
}
|
2013-05-13 20:51:21 -07:00
|
|
|
|
2014-02-26 08:52:09 -08:00
|
|
|
void
|
2014-03-09 19:11:27 -07:00
|
|
|
UnixSocketImpl::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);
|
|
|
|
MOZ_ASSERT(aAddrLen <= sizeof(mAddr));
|
|
|
|
|
|
|
|
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-02-28 01:16:53 -08:00
|
|
|
nsRefPtr<OnSocketEventRunnable> r =
|
|
|
|
new OnSocketEventRunnable(this, OnSocketEventRunnable::CONNECT_SUCCESS);
|
|
|
|
NS_DispatchToMainThread(r);
|
2014-02-26 08:52:09 -08:00
|
|
|
|
2014-02-26 08:52:18 -08:00
|
|
|
AddWatchers(READ_WATCHER, true);
|
|
|
|
if (!mOutgoingQ.IsEmpty()) {
|
|
|
|
AddWatchers(WRITE_WATCHER, false);
|
|
|
|
}
|
2014-02-26 08:52:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnixSocketImpl::OnConnected()
|
|
|
|
{
|
|
|
|
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-02-28 01:16:53 -08:00
|
|
|
nsRefPtr<OnSocketEventRunnable> r =
|
|
|
|
new OnSocketEventRunnable(this, OnSocketEventRunnable::CONNECT_SUCCESS);
|
|
|
|
NS_DispatchToMainThread(r);
|
2012-10-10 22:48:40 -07:00
|
|
|
|
2014-02-26 08:52:18 -08:00
|
|
|
AddWatchers(READ_WATCHER, true);
|
|
|
|
if (!mOutgoingQ.IsEmpty()) {
|
|
|
|
AddWatchers(WRITE_WATCHER, false);
|
|
|
|
}
|
2012-10-01 00:03:16 -07:00
|
|
|
}
|
|
|
|
|
2014-02-26 08:52:09 -08:00
|
|
|
void
|
|
|
|
UnixSocketImpl::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
|
|
|
|
UnixSocketImpl::OnError(const char* aFunction, int aErrno)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
|
|
|
|
|
|
|
|
UnixFdWatcher::OnError(aFunction, aErrno);
|
|
|
|
FireSocketError();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnixSocketImpl::OnSocketCanReceiveWithoutBlocking()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
// Read all of the incoming data.
|
|
|
|
while (true) {
|
|
|
|
nsAutoPtr<UnixSocketRawData> incoming(new UnixSocketRawData(MAX_READ_SIZE));
|
|
|
|
|
|
|
|
ssize_t ret = read(GetFd(), incoming->mData, incoming->mSize);
|
|
|
|
if (ret <= 0) {
|
|
|
|
if (ret == -1) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
continue; // retry system call when interrupted
|
|
|
|
}
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
return; // no data available: return and re-poll
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_WARNING("Cannot read from network");
|
|
|
|
#endif
|
|
|
|
// else fall through to error handling on other errno's
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're done with our descriptors. Ensure that spurious events don't
|
|
|
|
// cause us to end up back here.
|
|
|
|
RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
|
2014-02-28 01:16:53 -08:00
|
|
|
nsRefPtr<RequestClosingSocketRunnable> r =
|
|
|
|
new RequestClosingSocketRunnable(this);
|
|
|
|
NS_DispatchToMainThread(r);
|
2014-02-26 08:52:09 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-23 00:32:31 -07:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
// Make unix socket creation events to be the source events of TaskTracer,
|
|
|
|
// and originate the rest correlation tasks from here.
|
|
|
|
AutoSourceEvent taskTracerEvent(SourceEventType::UNIXSOCKET);
|
|
|
|
#endif
|
|
|
|
|
2014-02-26 08:52:09 -08:00
|
|
|
incoming->mSize = ret;
|
2014-02-28 01:16:53 -08:00
|
|
|
nsRefPtr<SocketReceiveRunnable> r =
|
|
|
|
new SocketReceiveRunnable(this, incoming.forget());
|
|
|
|
NS_DispatchToMainThread(r);
|
2014-02-26 08:52:09 -08:00
|
|
|
|
|
|
|
// If ret is less than MAX_READ_SIZE, there's no
|
|
|
|
// more data in the socket for us to read now.
|
|
|
|
if (ret < ssize_t(MAX_READ_SIZE)) {
|
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
|
UnixSocketImpl::OnSocketCanSendWithoutBlocking()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
// Try to write the bytes of mCurrentRilRawData. If all were written, continue.
|
|
|
|
//
|
|
|
|
// Otherwise, save the byte position of the next byte to write
|
|
|
|
// within mCurrentWriteOffset, and request another write when the
|
|
|
|
// system won't block.
|
|
|
|
//
|
|
|
|
while (true) {
|
|
|
|
UnixSocketRawData* data;
|
|
|
|
if (mOutgoingQ.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data = mOutgoingQ.ElementAt(0);
|
|
|
|
const uint8_t *toWrite;
|
|
|
|
toWrite = data->mData;
|
|
|
|
|
|
|
|
while (data->mCurrentWriteOffset < data->mSize) {
|
|
|
|
ssize_t write_amount = data->mSize - data->mCurrentWriteOffset;
|
|
|
|
ssize_t written;
|
|
|
|
written = write (GetFd(), toWrite + data->mCurrentWriteOffset,
|
|
|
|
write_amount);
|
|
|
|
if (written > 0) {
|
|
|
|
data->mCurrentWriteOffset += written;
|
|
|
|
}
|
|
|
|
if (written != write_amount) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->mCurrentWriteOffset != data->mSize) {
|
|
|
|
AddWatchers(WRITE_WATCHER, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mOutgoingQ.RemoveElementAt(0);
|
|
|
|
delete data;
|
|
|
|
}
|
2012-09-25 13:13:15 -07:00
|
|
|
}
|
|
|
|
|
2012-10-10 22:48:40 -07:00
|
|
|
UnixSocketConsumer::UnixSocketConsumer() : mImpl(nullptr)
|
|
|
|
, mConnectionStatus(SOCKET_DISCONNECTED)
|
2014-02-13 23:03:05 -08:00
|
|
|
, mConnectTimestamp(0)
|
|
|
|
, mConnectDelayMs(0)
|
2012-10-10 22:48:40 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
UnixSocketConsumer::~UnixSocketConsumer()
|
|
|
|
{
|
2014-04-08 00:27:47 -07:00
|
|
|
MOZ_ASSERT(mConnectionStatus == SOCKET_DISCONNECTED);
|
|
|
|
MOZ_ASSERT(!mImpl);
|
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());
|
2012-09-25 13:13:15 -07:00
|
|
|
if (!mImpl) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-07 23:03:03 -08:00
|
|
|
|
|
|
|
MOZ_ASSERT(!mImpl->IsShutdownOnMainThread());
|
2012-09-25 13:13:15 -07:00
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE,
|
2014-02-28 01:16:53 -08:00
|
|
|
new SocketSendTask(mImpl, this, aData));
|
2012-09-25 13:13:15 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UnixSocketConsumer::SendSocketData(const nsACString& aStr)
|
|
|
|
{
|
2012-10-10 22:48:40 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-25 13:13:15 -07:00
|
|
|
if (!mImpl) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-20 02:36:55 -08:00
|
|
|
if (aStr.Length() > MAX_READ_SIZE) {
|
2012-09-25 13:13:15 -07:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-07 23:03:03 -08:00
|
|
|
|
|
|
|
MOZ_ASSERT(!mImpl->IsShutdownOnMainThread());
|
2013-09-06 01:18:35 -07:00
|
|
|
UnixSocketRawData* d = new UnixSocketRawData(aStr.BeginReading(),
|
|
|
|
aStr.Length());
|
2012-09-25 13:13:15 -07:00
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE,
|
2014-02-28 01:16:53 -08:00
|
|
|
new SocketSendTask(mImpl, this, d));
|
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());
|
2012-09-25 13:13:15 -07:00
|
|
|
if (!mImpl) {
|
|
|
|
return;
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
2013-02-21 07:32:17 -08:00
|
|
|
|
2013-04-01 17:26:25 -07:00
|
|
|
mImpl->CancelDelayedConnectTask();
|
|
|
|
|
2013-03-07 23:03:03 -08:00
|
|
|
// From this point on, we consider mImpl as being deleted.
|
|
|
|
// We sever the relationship here so any future calls to listen or connect
|
|
|
|
// will create a new implementation.
|
|
|
|
mImpl->ShutdownOnMainThread();
|
|
|
|
|
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE,
|
|
|
|
new ShutdownSocketTask(mImpl));
|
|
|
|
|
|
|
|
mImpl = 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();
|
2012-10-17 17:11:05 -07:00
|
|
|
if (!mImpl || mConnectionStatus != SOCKET_CONNECTED) {
|
2012-10-17 17:10:27 -07:00
|
|
|
NS_WARNING("No socket currently open!");
|
|
|
|
return;
|
|
|
|
}
|
2012-10-17 17:11:05 -07:00
|
|
|
mImpl->GetSocketAddr(aAddrStr);
|
2012-10-17 17:10:27 -07:00
|
|
|
}
|
|
|
|
|
2012-10-10 22:48:40 -07:00
|
|
|
void
|
|
|
|
UnixSocketConsumer::NotifySuccess()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mConnectionStatus = SOCKET_CONNECTED;
|
2014-02-13 23:03:05 -08:00
|
|
|
mConnectTimestamp = PR_IntervalNow();
|
2012-10-10 22:48:40 -07:00
|
|
|
OnConnectSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnixSocketConsumer::NotifyError()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
2014-02-13 23:03:05 -08:00
|
|
|
mConnectDelayMs = CalculateConnectDelayMs();
|
2012-10-10 22:48:40 -07:00
|
|
|
OnConnectError();
|
|
|
|
}
|
2012-10-12 11:38:14 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
UnixSocketConsumer::NotifyDisconnect()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
2014-02-13 23:03:05 -08:00
|
|
|
mConnectDelayMs = CalculateConnectDelayMs();
|
2012-10-12 11:38:14 -07:00
|
|
|
OnDisconnect();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2012-10-01 00:03:16 -07:00
|
|
|
if (mImpl) {
|
|
|
|
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-02-26 08:52:09 -08:00
|
|
|
mImpl = new UnixSocketImpl(ioLoop, this, connector.forget(), addr);
|
2013-02-12 06:16:45 -08:00
|
|
|
mConnectionStatus = SOCKET_CONNECTING;
|
2013-02-01 04:28:18 -08:00
|
|
|
if (aDelayMs > 0) {
|
2013-04-01 17:26:25 -07:00
|
|
|
SocketDelayedConnectTask* connectTask = new SocketDelayedConnectTask(mImpl);
|
|
|
|
mImpl->SetDelayedConnectTask(connectTask);
|
|
|
|
MessageLoop::current()->PostDelayedTask(FROM_HERE, connectTask, aDelayMs);
|
2013-02-01 04:28:18 -08:00
|
|
|
} else {
|
|
|
|
ioLoop->PostTask(FROM_HERE, new SocketConnectTask(mImpl));
|
|
|
|
}
|
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);
|
|
|
|
|
2012-10-01 00:03:16 -07:00
|
|
|
if (mImpl) {
|
|
|
|
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-02-26 08:52:00 -08:00
|
|
|
mImpl = new UnixSocketImpl(XRE_GetIOMessageLoop(), this, connector.forget(),
|
2014-02-26 08:52:09 -08:00
|
|
|
EmptyCString());
|
2013-02-12 06:16:45 -08:00
|
|
|
mConnectionStatus = SOCKET_LISTENING;
|
2012-09-25 13:13:15 -07:00
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE,
|
2014-02-26 08:52:09 -08:00
|
|
|
new SocketListenTask(mImpl));
|
2012-09-25 13:13:15 -07:00
|
|
|
return true;
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
|
|
|
|
2014-02-13 23:03:05 -08:00
|
|
|
uint32_t
|
|
|
|
UnixSocketConsumer::CalculateConnectDelayMs() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
uint32_t connectDelayMs = mConnectDelayMs;
|
|
|
|
|
|
|
|
if ((PR_IntervalNow()-mConnectTimestamp) > connectDelayMs) {
|
|
|
|
// reset delay if connection has been opened for a while, or...
|
|
|
|
connectDelayMs = 0;
|
|
|
|
} else if (!connectDelayMs) {
|
|
|
|
// ...start with a delay of ~1 sec, or...
|
|
|
|
connectDelayMs = 1<<10;
|
|
|
|
} else if (connectDelayMs < (1<<16)) {
|
|
|
|
// ...otherwise increase delay by a factor of 2
|
|
|
|
connectDelayMs <<= 1;
|
|
|
|
}
|
|
|
|
return connectDelayMs;
|
|
|
|
}
|
|
|
|
|
2012-09-05 20:06:06 -07:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|