2012-09-06 03:06:06 +00: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 20:13:15 +00:00
|
|
|
#include "UnixSocket.h"
|
2012-09-06 03:06:06 +00:00
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
#include <fcntl.h>
|
2012-09-06 03:06:06 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
#include "base/eintr_wrapper.h"
|
|
|
|
#include "base/message_loop.h"
|
2012-09-06 03:06:06 +00:00
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
#include "mozilla/Monitor.h"
|
|
|
|
#include "mozilla/Util.h"
|
|
|
|
#include "mozilla/FileUtils.h"
|
|
|
|
#include "nsString.h"
|
2012-09-06 03:06:06 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2012-09-25 20:13:15 +00:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
2012-09-06 03:06:06 +00:00
|
|
|
|
2012-12-20 10:36:55 +00:00
|
|
|
static const size_t MAX_READ_SIZE = 1 << 16;
|
|
|
|
|
2012-10-01 07:03:16 +00:00
|
|
|
#undef LOG
|
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
|
|
|
#include <android/log.h>
|
|
|
|
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "GonkDBus", args);
|
|
|
|
#else
|
|
|
|
#define BTDEBUG true
|
|
|
|
#define LOG(args...) if (BTDEBUG) printf(args);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const int SOCKET_RETRY_TIME_MS = 1000;
|
|
|
|
|
2012-09-06 03:06:06 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
class UnixSocketImpl : public MessageLoopForIO::Watcher
|
2012-09-06 03:06:06 +00:00
|
|
|
{
|
2012-09-25 20:13:15 +00:00
|
|
|
public:
|
2012-10-01 07:03:16 +00:00
|
|
|
UnixSocketImpl(UnixSocketConsumer* aConsumer, UnixSocketConnector* aConnector,
|
|
|
|
const nsACString& aAddress)
|
2012-09-25 20:13:15 +00:00
|
|
|
: mConsumer(aConsumer)
|
|
|
|
, mIOLoop(nullptr)
|
2012-10-01 07:03:16 +00:00
|
|
|
, mFd(-1)
|
|
|
|
, mConnector(aConnector)
|
|
|
|
, mCurrentTaskIsCanceled(false)
|
2013-02-06 20:44:04 +00:00
|
|
|
, mTask(nullptr)
|
2012-10-01 07:03:16 +00:00
|
|
|
, mAddress(aAddress)
|
2013-02-12 14:16:45 +00:00
|
|
|
, mLock("UnixSocketImpl.mLock")
|
2012-09-25 20:13:15 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~UnixSocketImpl()
|
|
|
|
{
|
2012-10-11 05:48:40 +00:00
|
|
|
StopTask();
|
2012-09-25 20:13:15 +00:00
|
|
|
mReadWatcher.StopWatchingFileDescriptor();
|
|
|
|
mWriteWatcher.StopWatchingFileDescriptor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QueueWriteData(UnixSocketRawData* aData)
|
|
|
|
{
|
|
|
|
mOutgoingQ.AppendElement(aData);
|
|
|
|
OnFileCanWriteWithoutBlocking(mFd);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isFdValid()
|
|
|
|
{
|
|
|
|
return mFd > 0;
|
2012-09-06 03:06:06 +00:00
|
|
|
}
|
2012-09-25 20:13:15 +00:00
|
|
|
|
2012-10-01 07:03:16 +00:00
|
|
|
void CancelTask()
|
|
|
|
{
|
2013-02-12 14:16:45 +00:00
|
|
|
MutexAutoLock lock(mLock);
|
2012-10-01 07:03:16 +00:00
|
|
|
mCurrentTaskIsCanceled = true;
|
|
|
|
}
|
|
|
|
|
2013-02-12 14:16:45 +00:00
|
|
|
bool IsCanceled()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
return mCurrentTaskIsCanceled;
|
|
|
|
}
|
|
|
|
|
2012-10-01 07:03:16 +00:00
|
|
|
void UnsetTask()
|
|
|
|
{
|
|
|
|
mTask = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnqueueTask(int aDelayMs, CancelableTask* aTask)
|
|
|
|
{
|
|
|
|
MessageLoopForIO* ioLoop = MessageLoopForIO::current();
|
|
|
|
if (!ioLoop) {
|
|
|
|
NS_WARNING("No IOLoop to attach to, cancelling self!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mTask) {
|
|
|
|
return;
|
|
|
|
}
|
2013-02-12 14:16:45 +00:00
|
|
|
if (IsCanceled()) {
|
2012-10-01 07:03:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mTask = aTask;
|
|
|
|
if (aDelayMs) {
|
|
|
|
ioLoop->PostDelayedTask(FROM_HERE, mTask, aDelayMs);
|
|
|
|
} else {
|
|
|
|
ioLoop->PostTask(FROM_HERE, mTask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
void SetUpIO()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIOLoop);
|
2013-02-21 23:21:34 +00:00
|
|
|
MOZ_ASSERT(mFd >= 0);
|
2012-09-25 20:13:15 +00:00
|
|
|
mIOLoop = MessageLoopForIO::current();
|
|
|
|
mIOLoop->WatchFileDescriptor(mFd,
|
|
|
|
true,
|
|
|
|
MessageLoopForIO::WATCH_READ,
|
|
|
|
&mReadWatcher,
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
2012-10-01 07:03:16 +00:00
|
|
|
/**
|
|
|
|
* Connect to a socket
|
|
|
|
*/
|
|
|
|
void Connect();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run bind/listen to prepare for further runs of accept()
|
|
|
|
*/
|
|
|
|
void Listen();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accept an incoming connection
|
|
|
|
*/
|
|
|
|
void Accept();
|
|
|
|
|
|
|
|
/**
|
2013-02-12 14:16:45 +00:00
|
|
|
* Close an open connection
|
|
|
|
*/
|
|
|
|
void Close();
|
|
|
|
|
|
|
|
/**
|
2012-10-01 07:03:16 +00:00
|
|
|
* Stop whatever connect/accept task is running
|
|
|
|
*/
|
2012-10-11 05:48:40 +00:00
|
|
|
void StopTask()
|
|
|
|
{
|
|
|
|
if (mTask) {
|
|
|
|
mTask->Cancel();
|
|
|
|
mTask = nullptr;
|
|
|
|
}
|
2013-02-12 14:16:45 +00:00
|
|
|
MutexAutoLock lock(mLock);
|
2012-10-11 05:48:40 +00:00
|
|
|
mCurrentTaskIsCanceled = true;
|
|
|
|
}
|
2012-10-01 07:03:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up nonblocking flags on whatever our current file descriptor is.
|
|
|
|
*
|
|
|
|
* @return true if successful, false otherwise
|
|
|
|
*/
|
|
|
|
bool SetNonblockFlags();
|
|
|
|
|
2012-10-18 00:11:05 +00:00
|
|
|
void GetSocketAddr(nsAString& aAddrStr)
|
2012-10-18 00:10:27 +00:00
|
|
|
{
|
2012-10-18 00:11:05 +00:00
|
|
|
if (!mConnector)
|
|
|
|
{
|
|
|
|
NS_WARNING("No connector to get socket address from!");
|
|
|
|
aAddrStr = nsString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mConnector->GetSocketAddr(mAddr, aAddrStr);
|
2012-10-18 00:10:27 +00:00
|
|
|
}
|
|
|
|
|
2012-09-25 20:13:15 +00: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;
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* libevent triggered functions that reads data from socket when available and
|
|
|
|
* guarenteed non-blocking. Only to be called on IO thread.
|
|
|
|
*
|
|
|
|
* @param aFd File descriptor to read from
|
|
|
|
*/
|
|
|
|
virtual void OnFileCanReadWithoutBlocking(int aFd);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* libevent or developer triggered functions that writes data to socket when
|
|
|
|
* available and guarenteed non-blocking. Only to be called on IO thread.
|
|
|
|
*
|
|
|
|
* @param aFd File descriptor to read from
|
|
|
|
*/
|
|
|
|
virtual void OnFileCanWriteWithoutBlocking(int aFd);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IO Loop pointer. Must be initalized and called from IO thread only.
|
|
|
|
*/
|
|
|
|
MessageLoopForIO* mIOLoop;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Raw data queue. Must be pushed/popped from IO thread only.
|
|
|
|
*/
|
|
|
|
typedef nsTArray<UnixSocketRawData* > UnixSocketRawDataQueue;
|
|
|
|
UnixSocketRawDataQueue mOutgoingQ;
|
|
|
|
|
2012-10-01 05:54:27 +00:00
|
|
|
/**
|
2012-10-01 06:17:46 +00:00
|
|
|
* Incoming packet. Only to be accessed on IO Thread.
|
2012-10-01 05:54:27 +00:00
|
|
|
*/
|
2012-10-01 06:17:46 +00:00
|
|
|
nsAutoPtr<UnixSocketRawData> mIncoming;
|
2012-10-01 05:54:27 +00:00
|
|
|
|
|
|
|
/**
|
2012-10-01 06:17:46 +00:00
|
|
|
* Read watcher for libevent. Only to be accessed on IO Thread.
|
2012-10-01 05:54:27 +00:00
|
|
|
*/
|
2012-10-01 06:17:46 +00:00
|
|
|
MessageLoopForIO::FileDescriptorWatcher mReadWatcher;
|
2012-10-01 05:54:27 +00:00
|
|
|
|
|
|
|
/**
|
2012-10-01 06:17:46 +00:00
|
|
|
* Write watcher for libevent. Only to be accessed on IO Thread.
|
2012-10-01 05:54:27 +00:00
|
|
|
*/
|
2012-10-01 06:17:46 +00:00
|
|
|
MessageLoopForIO::FileDescriptorWatcher mWriteWatcher;
|
2012-10-01 07:03:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* File descriptor to read from/write to. Connection happens on user provided
|
|
|
|
* thread. Read/write/close happens on IO thread.
|
|
|
|
*/
|
|
|
|
ScopedClose mFd;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 mCurrentTaskIsCanceled;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pointer to the task we're currently running. DO NOT DELETE MANUALLY. This
|
|
|
|
* will be taken care of by the IO loop. Just set to nullptr.
|
|
|
|
*/
|
|
|
|
CancelableTask* mTask;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Address we are connecting to, assuming we are creating a client connection.
|
|
|
|
*/
|
|
|
|
nsCString mAddress;
|
2012-10-18 00:10:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Size of the socket address struct
|
|
|
|
*/
|
|
|
|
socklen_t mAddrSize;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Address struct of the socket currently in use
|
|
|
|
*/
|
2012-10-18 00:11:05 +00:00
|
|
|
sockaddr mAddr;
|
2012-10-18 00:10:27 +00:00
|
|
|
|
2013-02-12 14:16:45 +00:00
|
|
|
/**
|
|
|
|
* Protects mCurrentTaskIsCanceled
|
|
|
|
*/
|
|
|
|
mozilla::Mutex mLock;
|
2012-09-25 20:13:15 +00:00
|
|
|
};
|
|
|
|
|
2013-01-16 02:21:49 +00:00
|
|
|
template<class T>
|
|
|
|
class DeleteInstanceRunnable : public nsRunnable
|
2012-09-25 20:13:15 +00:00
|
|
|
{
|
2013-01-16 02:21:49 +00:00
|
|
|
public:
|
|
|
|
DeleteInstanceRunnable(T* aInstance)
|
|
|
|
: mInstance(aInstance)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
delete mInstance;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* mInstance;
|
|
|
|
};
|
2012-09-06 03:06:06 +00:00
|
|
|
|
2012-10-11 05:48:40 +00:00
|
|
|
class OnSocketEventTask : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum SocketEvent {
|
|
|
|
CONNECT_SUCCESS,
|
|
|
|
CONNECT_ERROR,
|
|
|
|
DISCONNECT
|
|
|
|
};
|
|
|
|
|
|
|
|
OnSocketEventTask(UnixSocketImpl* aImpl, SocketEvent e) :
|
|
|
|
mImpl(aImpl),
|
|
|
|
mEvent(e)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!mImpl->mConsumer) {
|
|
|
|
NS_WARNING("CloseSocket has already been called! (mConsumer is null)");
|
|
|
|
// 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) {
|
|
|
|
mImpl->mConsumer->NotifySuccess();
|
|
|
|
} else if (mEvent == CONNECT_ERROR) {
|
|
|
|
mImpl->mConsumer->NotifyError();
|
2012-10-12 18:38:14 +00:00
|
|
|
} else if (mEvent == DISCONNECT) {
|
|
|
|
mImpl->mConsumer->NotifyDisconnect();
|
2012-10-11 05:48:40 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
SocketEvent mEvent;
|
|
|
|
};
|
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
class SocketReceiveTask : public nsRunnable
|
2012-09-06 03:06:06 +00:00
|
|
|
{
|
2012-09-25 20:13:15 +00:00
|
|
|
public:
|
|
|
|
SocketReceiveTask(UnixSocketImpl* aImpl, UnixSocketRawData* aData) :
|
|
|
|
mImpl(aImpl),
|
|
|
|
mRawData(aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImpl);
|
|
|
|
MOZ_ASSERT(aData);
|
2012-09-06 03:06:06 +00:00
|
|
|
}
|
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
if(!mImpl->mConsumer) {
|
|
|
|
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-06 03:06:06 +00:00
|
|
|
}
|
2012-09-25 20:13:15 +00:00
|
|
|
mImpl->mConsumer->ReceiveSocketData(mRawData);
|
|
|
|
return NS_OK;
|
2012-09-06 03:06:06 +00:00
|
|
|
}
|
2012-09-25 20:13:15 +00:00
|
|
|
private:
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
nsAutoPtr<UnixSocketRawData> mRawData;
|
|
|
|
};
|
2012-09-06 03:06:06 +00:00
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
class SocketSendTask : public Task
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SocketSendTask(UnixSocketConsumer* aConsumer, UnixSocketImpl* aImpl,
|
|
|
|
UnixSocketRawData* aData)
|
|
|
|
: mConsumer(aConsumer),
|
|
|
|
mImpl(aImpl),
|
|
|
|
mData(aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aConsumer);
|
|
|
|
MOZ_ASSERT(aImpl);
|
|
|
|
MOZ_ASSERT(aData);
|
2012-09-06 03:06:06 +00:00
|
|
|
}
|
|
|
|
|
2012-10-11 05:48:40 +00:00
|
|
|
void
|
|
|
|
Run()
|
2012-09-25 20:13:15 +00:00
|
|
|
{
|
|
|
|
mImpl->QueueWriteData(mData);
|
|
|
|
}
|
2012-09-06 03:06:06 +00:00
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
private:
|
|
|
|
nsRefPtr<UnixSocketConsumer> mConsumer;
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
UnixSocketRawData* mData;
|
|
|
|
};
|
2012-09-06 03:06:06 +00:00
|
|
|
|
2013-02-12 14:16:45 +00:00
|
|
|
class SocketCloseTask : public Task
|
2012-10-12 18:38:14 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
SocketCloseTask(UnixSocketImpl* aImpl)
|
|
|
|
: mImpl(aImpl)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImpl);
|
|
|
|
}
|
|
|
|
|
2013-02-12 14:16:45 +00:00
|
|
|
void Run()
|
2012-10-12 18:38:14 +00:00
|
|
|
{
|
2013-02-12 14:16:45 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(mImpl);
|
|
|
|
|
|
|
|
mImpl->UnsetTask();
|
|
|
|
mImpl->Close();
|
2012-10-12 18:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
};
|
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
class StartImplReadingTask : public Task
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StartImplReadingTask(UnixSocketImpl* aImpl)
|
|
|
|
: mImpl(aImpl)
|
|
|
|
{
|
|
|
|
}
|
2012-09-06 03:06:06 +00:00
|
|
|
|
2012-10-11 05:48:40 +00:00
|
|
|
void
|
|
|
|
Run()
|
2012-09-25 20:13:15 +00:00
|
|
|
{
|
|
|
|
mImpl->SetUpIO();
|
2012-09-06 03:06:06 +00:00
|
|
|
}
|
2012-09-25 20:13:15 +00:00
|
|
|
private:
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
};
|
2012-09-06 03:06:06 +00:00
|
|
|
|
2013-02-21 15:32:17 +00:00
|
|
|
class RequestClosingSocketTask : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RequestClosingSocketTask(UnixSocketImpl* aImpl) : mImpl(aImpl)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if(!mImpl->mConsumer) {
|
|
|
|
NS_WARNING("CloseSocket has already been called! (mConsumer is null)");
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start from here, same handling flow as calling CloseSocket() from upper layer
|
|
|
|
mImpl->mConsumer->CloseSocket();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
};
|
|
|
|
|
2012-10-01 07:03:16 +00:00
|
|
|
class SocketAcceptTask : public CancelableTask {
|
|
|
|
virtual void Run();
|
|
|
|
|
|
|
|
bool mCanceled;
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
public:
|
|
|
|
virtual void Cancel() { mCanceled = true; }
|
|
|
|
SocketAcceptTask(UnixSocketImpl* aImpl) : mCanceled(false), mImpl(aImpl) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
void SocketAcceptTask::Run() {
|
|
|
|
mImpl->UnsetTask();
|
|
|
|
if (mCanceled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mImpl->Accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
class SocketConnectTask : public CancelableTask {
|
|
|
|
virtual void Run();
|
|
|
|
|
|
|
|
bool mCanceled;
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
public:
|
|
|
|
SocketConnectTask(UnixSocketImpl* aImpl) : mCanceled(false), mImpl(aImpl) { }
|
|
|
|
virtual void Cancel() { mCanceled = true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
void SocketConnectTask::Run() {
|
|
|
|
mImpl->UnsetTask();
|
|
|
|
if (mCanceled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mImpl->Connect();
|
|
|
|
}
|
|
|
|
|
2013-02-12 14:16:45 +00:00
|
|
|
void
|
|
|
|
UnixSocketImpl::Close()
|
|
|
|
{
|
|
|
|
mReadWatcher.StopWatchingFileDescriptor();
|
|
|
|
mWriteWatcher.StopWatchingFileDescriptor();
|
|
|
|
|
|
|
|
nsRefPtr<nsIRunnable> t(new DeleteInstanceRunnable<UnixSocketImpl>(this));
|
|
|
|
NS_ENSURE_TRUE_VOID(t);
|
|
|
|
nsresult rv = NS_DispatchToMainThread(t);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
}
|
|
|
|
|
2012-10-01 07:03:16 +00:00
|
|
|
void
|
|
|
|
UnixSocketImpl::Accept()
|
2012-10-01 05:54:27 +00:00
|
|
|
{
|
2012-10-01 07:03:16 +00:00
|
|
|
|
2012-10-11 05:48:40 +00:00
|
|
|
if (!mConnector) {
|
|
|
|
NS_WARNING("No connector object available!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-01 07:03:16 +00: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.
|
2012-10-18 00:10:27 +00:00
|
|
|
mConnector->CreateAddr(true, mAddrSize, &mAddr, nullptr);
|
2012-10-01 07:03:16 +00:00
|
|
|
|
|
|
|
if(mFd.get() < 0)
|
2012-10-01 05:54:27 +00:00
|
|
|
{
|
2012-10-01 07:03:16 +00:00
|
|
|
mFd = mConnector->Create();
|
|
|
|
if (mFd.get() < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetNonblockFlags()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-18 00:10:27 +00:00
|
|
|
if (bind(mFd.get(), &mAddr, mAddrSize)) {
|
2012-10-01 07:03:16 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
LOG("...bind(%d) gave errno %d", mFd.get(), errno);
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(mFd.get(), 1)) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
LOG("...listen(%d) gave errno %d", mFd.get(), errno);
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-02-12 14:16:45 +00:00
|
|
|
SetUpIO();
|
2012-10-01 07:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnixSocketImpl::Connect()
|
|
|
|
{
|
|
|
|
if(mFd.get() < 0)
|
|
|
|
{
|
|
|
|
mFd = mConnector->Create();
|
|
|
|
if (mFd.get() < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
2012-10-18 00:10:27 +00:00
|
|
|
mConnector->CreateAddr(false, mAddrSize, &mAddr, mAddress.get());
|
2012-10-01 07:03:16 +00:00
|
|
|
|
2012-10-18 00:10:27 +00:00
|
|
|
ret = connect(mFd.get(), &mAddr, mAddrSize);
|
2012-10-01 07:03:16 +00:00
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
#if DEBUG
|
|
|
|
LOG("Socket connect errno=%d\n", errno);
|
|
|
|
#endif
|
|
|
|
mFd.reset(-1);
|
2012-10-11 05:48:40 +00:00
|
|
|
nsRefPtr<OnSocketEventTask> t =
|
|
|
|
new OnSocketEventTask(this, OnSocketEventTask::CONNECT_ERROR);
|
|
|
|
NS_DispatchToMainThread(t);
|
2012-10-01 07:03:16 +00:00
|
|
|
return;
|
2012-10-01 05:54:27 +00:00
|
|
|
}
|
|
|
|
|
2012-10-11 05:48:40 +00:00
|
|
|
if (!mConnector->SetUp(mFd)) {
|
2012-10-01 07:03:16 +00:00
|
|
|
NS_WARNING("Could not set up socket!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-11 05:48:40 +00:00
|
|
|
nsRefPtr<OnSocketEventTask> t =
|
|
|
|
new OnSocketEventTask(this, OnSocketEventTask::CONNECT_SUCCESS);
|
|
|
|
NS_DispatchToMainThread(t);
|
|
|
|
|
|
|
|
// Due to the fact that we've dispatched our OnConnectSuccess message before
|
|
|
|
// starting reading, we're guaranteed that any subsequent read tasks will
|
|
|
|
// happen after the object has been notified of a successful connect.
|
2012-10-01 07:03:16 +00:00
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE,
|
|
|
|
new StartImplReadingTask(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UnixSocketImpl::SetNonblockFlags()
|
|
|
|
{
|
|
|
|
// Set socket addr to be reused even if kernel is still waiting to close
|
|
|
|
int n = 1;
|
|
|
|
setsockopt(mFd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n));
|
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
// Set close-on-exec bit.
|
2012-10-01 07:03:16 +00:00
|
|
|
int flags = fcntl(mFd, F_GETFD);
|
2012-09-25 20:13:15 +00:00
|
|
|
if (-1 == flags) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-06 03:06:06 +00:00
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
flags |= FD_CLOEXEC;
|
2012-10-01 07:03:16 +00:00
|
|
|
if (-1 == fcntl(mFd, F_SETFD, flags)) {
|
2012-09-25 20:13:15 +00:00
|
|
|
return false;
|
2012-09-06 03:06:06 +00:00
|
|
|
}
|
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-11 05:48:40 +00:00
|
|
|
UnixSocketConsumer::UnixSocketConsumer() : mImpl(nullptr)
|
|
|
|
, mConnectionStatus(SOCKET_DISCONNECTED)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
UnixSocketConsumer::~UnixSocketConsumer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UnixSocketConsumer::SendSocketData(UnixSocketRawData* aData)
|
|
|
|
{
|
2012-10-11 05:48:40 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-25 20:13:15 +00:00
|
|
|
if (!mImpl) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE,
|
|
|
|
new SocketSendTask(this, mImpl, aData));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UnixSocketConsumer::SendSocketData(const nsACString& aStr)
|
|
|
|
{
|
2012-10-11 05:48:40 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-25 20:13:15 +00:00
|
|
|
if (!mImpl) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-20 10:36:55 +00:00
|
|
|
if (aStr.Length() > MAX_READ_SIZE) {
|
2012-09-25 20:13:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
nsCString str(aStr);
|
|
|
|
UnixSocketRawData* d = new UnixSocketRawData(aStr.Length());
|
|
|
|
memcpy(d->mData, str.get(), aStr.Length());
|
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE,
|
|
|
|
new SocketSendTask(this, mImpl, d));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnixSocketConsumer::CloseSocket()
|
|
|
|
{
|
2012-10-12 18:38:14 +00:00
|
|
|
// Needed due to refcount change
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-25 20:13:15 +00:00
|
|
|
if (!mImpl) {
|
|
|
|
return;
|
2012-09-06 03:06:06 +00:00
|
|
|
}
|
2012-10-05 23:05:35 +00:00
|
|
|
UnixSocketImpl* impl = mImpl;
|
2012-10-12 18:38:14 +00:00
|
|
|
// To make sure the owner doesn't die on the IOThread, remove pointer here
|
2012-10-05 23:05:35 +00:00
|
|
|
mImpl = nullptr;
|
2012-10-12 18:38:14 +00:00
|
|
|
// Line it up to be destructed on the IO Thread
|
2012-10-11 05:48:40 +00:00
|
|
|
impl->mConsumer.forget();
|
2013-02-12 14:16:45 +00:00
|
|
|
impl->CancelTask();
|
2013-02-21 15:32:17 +00:00
|
|
|
|
2013-02-12 14:16:45 +00:00
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE, new SocketCloseTask(impl));
|
2013-01-16 02:21:49 +00:00
|
|
|
|
2012-10-12 18:38:14 +00:00
|
|
|
NotifyDisconnect();
|
2012-09-25 20:13:15 +00:00
|
|
|
}
|
2012-09-06 03:06:06 +00:00
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
void
|
|
|
|
UnixSocketImpl::OnFileCanReadWithoutBlocking(int aFd)
|
|
|
|
{
|
2013-02-12 14:16:45 +00:00
|
|
|
enum SocketConnectionStatus status = mConsumer->GetConnectionStatus();
|
|
|
|
|
|
|
|
if (status == SOCKET_CONNECTED) {
|
|
|
|
|
|
|
|
// Keep reading data until either
|
|
|
|
//
|
|
|
|
// - mIncoming is completely read
|
|
|
|
// If so, sConsumer->MessageReceived(mIncoming.forget())
|
|
|
|
//
|
|
|
|
// - mIncoming isn't completely read, but there's no more
|
|
|
|
// data available on the socket
|
|
|
|
// If so, break;
|
|
|
|
while (true) {
|
|
|
|
if (!mIncoming) {
|
|
|
|
uint8_t data[MAX_READ_SIZE];
|
|
|
|
ssize_t ret = read(aFd, data, MAX_READ_SIZE);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (ret == -1) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
continue; // retry system call when interrupted
|
|
|
|
}
|
|
|
|
else if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
return; // no data available: return and re-poll
|
|
|
|
}
|
|
|
|
// else fall through to error handling on other errno's
|
2012-09-25 20:13:15 +00:00
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
2013-02-12 14:16:45 +00:00
|
|
|
NS_WARNING("Cannot read from network");
|
2012-09-25 20:13:15 +00:00
|
|
|
#endif
|
2013-02-12 14:16:45 +00:00
|
|
|
// At this point, assume that we can't actually access
|
|
|
|
// the socket anymore
|
|
|
|
mReadWatcher.StopWatchingFileDescriptor();
|
|
|
|
mWriteWatcher.StopWatchingFileDescriptor();
|
2013-02-21 15:32:17 +00:00
|
|
|
nsRefPtr<RequestClosingSocketTask> t = new RequestClosingSocketTask(this);
|
|
|
|
NS_DispatchToMainThread(t);
|
2013-02-12 14:16:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ret) {
|
|
|
|
mIncoming = new UnixSocketRawData(ret);
|
|
|
|
memcpy(mIncoming->mData, data, ret);
|
|
|
|
nsRefPtr<SocketReceiveTask> t =
|
|
|
|
new SocketReceiveTask(this, mIncoming.forget());
|
|
|
|
NS_DispatchToMainThread(t);
|
|
|
|
}
|
|
|
|
if (ret < ssize_t(MAX_READ_SIZE)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-25 20:13:15 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-12 14:16:45 +00:00
|
|
|
} else if (status == SOCKET_LISTENING) {
|
|
|
|
|
|
|
|
int client_fd = accept(mFd.get(), &mAddr, &mAddrSize);
|
|
|
|
|
|
|
|
if (client_fd < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mConnector->SetUp(client_fd)) {
|
|
|
|
NS_WARNING("Could not set up socket!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mReadWatcher.StopWatchingFileDescriptor();
|
|
|
|
mWriteWatcher.StopWatchingFileDescriptor();
|
|
|
|
|
|
|
|
mFd.reset(client_fd);
|
|
|
|
|
|
|
|
nsRefPtr<OnSocketEventTask> t =
|
|
|
|
new OnSocketEventTask(this, OnSocketEventTask::CONNECT_SUCCESS);
|
|
|
|
NS_DispatchToMainThread(t);
|
|
|
|
|
|
|
|
// Due to the fact that we've dispatched our OnConnectSuccess message before
|
|
|
|
// starting reading, we're guaranteed that any subsequent read tasks will
|
|
|
|
// happen after the object has been notified of a successful connect.
|
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE,
|
|
|
|
new StartImplReadingTask(this));
|
2012-09-25 20:13:15 +00:00
|
|
|
}
|
2012-09-06 03:06:06 +00:00
|
|
|
}
|
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
void
|
|
|
|
UnixSocketImpl::OnFileCanWriteWithoutBlocking(int aFd)
|
2012-09-06 03:06:06 +00:00
|
|
|
{
|
2012-09-25 20:13:15 +00: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 mCurrentRilRawData, and request another write when the
|
|
|
|
// system won't block.
|
|
|
|
//
|
2013-02-21 23:21:34 +00:00
|
|
|
MOZ_ASSERT(aFd >= 0);
|
2012-09-25 20:13:15 +00:00
|
|
|
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 (aFd, toWrite + data->mCurrentWriteOffset,
|
|
|
|
write_amount);
|
|
|
|
if (written > 0) {
|
|
|
|
data->mCurrentWriteOffset += written;
|
|
|
|
}
|
|
|
|
if (written != write_amount) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->mCurrentWriteOffset != data->mSize) {
|
|
|
|
MessageLoopForIO::current()->WatchFileDescriptor(
|
|
|
|
aFd,
|
|
|
|
false,
|
|
|
|
MessageLoopForIO::WATCH_WRITE,
|
|
|
|
&mWriteWatcher,
|
|
|
|
this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mOutgoingQ.RemoveElementAt(0);
|
|
|
|
delete data;
|
|
|
|
}
|
2012-09-06 03:06:06 +00:00
|
|
|
}
|
|
|
|
|
2012-10-18 00:10:27 +00:00
|
|
|
void
|
2012-10-18 00:11:05 +00:00
|
|
|
UnixSocketConsumer::GetSocketAddr(nsAString& aAddrStr)
|
2012-10-18 00:10:27 +00:00
|
|
|
{
|
2012-10-18 00:11:05 +00:00
|
|
|
if (!mImpl || mConnectionStatus != SOCKET_CONNECTED) {
|
2012-10-18 00:10:27 +00:00
|
|
|
NS_WARNING("No socket currently open!");
|
2012-10-18 00:11:05 +00:00
|
|
|
aAddrStr = nsString();
|
2012-10-18 00:10:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-10-18 00:11:05 +00:00
|
|
|
mImpl->GetSocketAddr(aAddrStr);
|
2012-10-18 00:10:27 +00:00
|
|
|
}
|
|
|
|
|
2012-10-11 05:48:40 +00:00
|
|
|
void
|
|
|
|
UnixSocketConsumer::NotifySuccess()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mConnectionStatus = SOCKET_CONNECTED;
|
|
|
|
OnConnectSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnixSocketConsumer::NotifyError()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
|
|
|
OnConnectError();
|
|
|
|
}
|
2012-10-12 18:38:14 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
UnixSocketConsumer::NotifyDisconnect()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
|
|
|
OnDisconnect();
|
|
|
|
}
|
|
|
|
|
2012-09-25 20:13:15 +00:00
|
|
|
bool
|
2012-10-01 07:03:16 +00:00
|
|
|
UnixSocketConsumer::ConnectSocket(UnixSocketConnector* aConnector,
|
2013-02-01 12:28:18 +00:00
|
|
|
const char* aAddress,
|
|
|
|
int aDelayMs)
|
2012-09-06 03:06:06 +00:00
|
|
|
{
|
2012-10-01 07:03:16 +00:00
|
|
|
MOZ_ASSERT(aConnector);
|
2012-10-11 05:48:40 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-10-01 07:03:16 +00:00
|
|
|
if (mImpl) {
|
|
|
|
NS_WARNING("Socket already connecting/connected!");
|
2012-09-25 20:13:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-01 07:03:16 +00:00
|
|
|
nsCString addr;
|
|
|
|
addr.Assign(aAddress);
|
|
|
|
mImpl = new UnixSocketImpl(this, aConnector, addr);
|
2013-02-01 12:28:18 +00:00
|
|
|
MessageLoop* ioLoop = XRE_GetIOMessageLoop();
|
2013-02-12 14:16:45 +00:00
|
|
|
mConnectionStatus = SOCKET_CONNECTING;
|
2013-02-01 12:28:18 +00:00
|
|
|
if (aDelayMs > 0) {
|
|
|
|
ioLoop->PostDelayedTask(FROM_HERE, new SocketConnectTask(mImpl), aDelayMs);
|
|
|
|
} else {
|
|
|
|
ioLoop->PostTask(FROM_HERE, new SocketConnectTask(mImpl));
|
|
|
|
}
|
2012-10-01 07:03:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UnixSocketConsumer::ListenSocket(UnixSocketConnector* aConnector)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aConnector);
|
2012-10-11 05:48:40 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-10-01 07:03:16 +00:00
|
|
|
if (mImpl) {
|
|
|
|
NS_WARNING("Socket already connecting/connected!");
|
2012-09-25 20:13:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-01 07:03:16 +00:00
|
|
|
nsCString addr;
|
|
|
|
mImpl = new UnixSocketImpl(this, aConnector, addr);
|
2013-02-12 14:16:45 +00:00
|
|
|
mConnectionStatus = SOCKET_LISTENING;
|
2012-09-25 20:13:15 +00:00
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE,
|
2012-10-01 07:03:16 +00:00
|
|
|
new SocketAcceptTask(mImpl));
|
2012-09-25 20:13:15 +00:00
|
|
|
return true;
|
2012-09-06 03:06:06 +00:00
|
|
|
}
|
|
|
|
|
2012-10-01 07:03:16 +00:00
|
|
|
void
|
|
|
|
UnixSocketConsumer::CancelSocketTask()
|
|
|
|
{
|
2012-10-11 05:48:40 +00:00
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
2012-10-01 07:03:16 +00:00
|
|
|
if(!mImpl) {
|
|
|
|
NS_WARNING("No socket implementation to cancel task on!");
|
|
|
|
return;
|
|
|
|
}
|
2013-02-12 14:16:45 +00:00
|
|
|
mImpl->StopTask();
|
2012-10-01 07:03:16 +00:00
|
|
|
}
|
|
|
|
|
2012-09-06 03:06:06 +00:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|