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"
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
#include <fcntl.h>
|
2012-09-05 20:06:06 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
#include "base/eintr_wrapper.h"
|
|
|
|
#include "base/message_loop.h"
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
#include "mozilla/Monitor.h"
|
|
|
|
#include "mozilla/FileUtils.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsXULAppAPI.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;
|
|
|
|
|
2013-12-17 10:26:45 -08:00
|
|
|
#undef CHROMIUM_LOG
|
2012-10-01 00:03:16 -07:00
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
|
|
|
#include <android/log.h>
|
2013-12-17 10:26:45 -08:00
|
|
|
#define CHROMIUM_LOG(args...) __android_log_print(ANDROID_LOG_INFO, "GonkDBus", args);
|
2012-10-01 00:03:16 -07:00
|
|
|
#else
|
|
|
|
#define BTDEBUG true
|
2013-12-17 10:26:45 -08:00
|
|
|
#define CHROMIUM_LOG(args...) if (BTDEBUG) printf(args);
|
2012-10-01 00:03:16 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static const int SOCKET_RETRY_TIME_MS = 1000;
|
|
|
|
|
2012-09-05 20:06:06 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
class UnixSocketImpl : public MessageLoopForIO::Watcher
|
2012-09-05 20:06:06 -07:00
|
|
|
{
|
2012-09-25 13:13:15 -07:00
|
|
|
public:
|
2012-10-01 00:03:16 -07:00
|
|
|
UnixSocketImpl(UnixSocketConsumer* aConsumer, UnixSocketConnector* aConnector,
|
2013-12-09 05:21:18 -08:00
|
|
|
const nsACString& aAddress,
|
|
|
|
SocketConnectionStatus aConnectionStatus)
|
2012-09-25 13:13:15 -07:00
|
|
|
: mConsumer(aConsumer)
|
|
|
|
, mIOLoop(nullptr)
|
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)
|
2013-12-09 05:21:18 -08:00
|
|
|
, mConnectionStatus(aConnectionStatus)
|
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);
|
|
|
|
OnFileCanWriteWithoutBlocking(mFd);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isFdValid()
|
|
|
|
{
|
|
|
|
return mFd > 0;
|
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);
|
|
|
|
|
|
|
|
mReadWatcher.StopWatchingFileDescriptor();
|
|
|
|
mWriteWatcher.StopWatchingFileDescriptor();
|
|
|
|
|
|
|
|
mShuttingDownOnIOThread = true;
|
2012-10-01 00:03:16 -07:00
|
|
|
}
|
2013-03-07 23:03:03 -08:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
void SetUpIO()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIOLoop);
|
2013-02-21 15:21:34 -08:00
|
|
|
MOZ_ASSERT(mFd >= 0);
|
2012-09-25 13:13:15 -07:00
|
|
|
mIOLoop = MessageLoopForIO::current();
|
|
|
|
mIOLoop->WatchFileDescriptor(mFd,
|
|
|
|
true,
|
|
|
|
MessageLoopForIO::WATCH_READ,
|
|
|
|
&mReadWatcher,
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2013-09-06 01:17:55 -07:00
|
|
|
/**
|
2012-10-01 00:03:16 -07:00
|
|
|
* Accept an incoming connection
|
|
|
|
*/
|
|
|
|
void Accept();
|
|
|
|
|
2013-02-12 06:16:45 -08:00
|
|
|
/**
|
2013-03-08 14:40:26 -08:00
|
|
|
* Set up flags on whatever our current file descriptor is.
|
2012-10-01 00:03:16 -07:00
|
|
|
*
|
|
|
|
* @return true if successful, false otherwise
|
|
|
|
*/
|
2013-03-08 14:40:26 -08:00
|
|
|
bool SetSocketFlags();
|
2012-10-01 00:03:16 -07:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
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-09-30 22:54:27 -07:00
|
|
|
/**
|
2012-09-30 23:17:46 -07:00
|
|
|
* Read watcher for libevent. Only to be accessed on IO Thread.
|
2012-09-30 22:54:27 -07:00
|
|
|
*/
|
2012-09-30 23:17:46 -07:00
|
|
|
MessageLoopForIO::FileDescriptorWatcher mReadWatcher;
|
2012-09-30 22:54:27 -07:00
|
|
|
|
|
|
|
/**
|
2012-09-30 23:17:46 -07:00
|
|
|
* Write watcher for libevent. Only to be accessed on IO Thread.
|
2012-09-30 22:54:27 -07:00
|
|
|
*/
|
2012-09-30 23:17:46 -07:00
|
|
|
MessageLoopForIO::FileDescriptorWatcher mWriteWatcher;
|
2012-10-01 00:03:16 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* File descriptor to read from/write to. Connection happens on user provided
|
|
|
|
* thread. Read/write/close happens on IO thread.
|
|
|
|
*/
|
|
|
|
ScopedClose mFd;
|
|
|
|
|
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;
|
2013-12-09 05:21:18 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Socket connection status. Duplicate from UnixSocketConsumer. Should only
|
|
|
|
* be accessed on I/O thread.
|
|
|
|
*/
|
|
|
|
SocketConnectionStatus mConnectionStatus;
|
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
|
|
|
|
2012-10-10 22:48:40 -07: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);
|
2013-03-07 23:03:03 -08:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2012-10-10 22:48:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-03-07 23:03:03 -08:00
|
|
|
if (mImpl->IsShutdownOnMainThread()) {
|
|
|
|
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) {
|
|
|
|
mImpl->mConsumer->NotifySuccess();
|
|
|
|
} else if (mEvent == CONNECT_ERROR) {
|
|
|
|
mImpl->mConsumer->NotifyError();
|
2012-10-12 11:38:14 -07:00
|
|
|
} else if (mEvent == DISCONNECT) {
|
|
|
|
mImpl->mConsumer->NotifyDisconnect();
|
2012-10-10 22:48:40 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
SocketEvent mEvent;
|
|
|
|
};
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
class SocketReceiveTask : public nsRunnable
|
2012-09-05 20:06:06 -07:00
|
|
|
{
|
2012-09-25 13:13:15 -07:00
|
|
|
public:
|
|
|
|
SocketReceiveTask(UnixSocketImpl* aImpl, UnixSocketRawData* aData) :
|
|
|
|
mImpl(aImpl),
|
|
|
|
mRawData(aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImpl);
|
|
|
|
MOZ_ASSERT(aData);
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2013-03-07 23:03:03 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-04-01 17:26:25 -07:00
|
|
|
if (mImpl->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
|
|
|
|
|
|
|
MOZ_ASSERT(mImpl->mConsumer);
|
2012-09-25 13:13:15 -07:00
|
|
|
mImpl->mConsumer->ReceiveSocketData(mRawData);
|
|
|
|
return NS_OK;
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
2012-09-25 13:13:15 -07:00
|
|
|
private:
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
nsAutoPtr<UnixSocketRawData> mRawData;
|
|
|
|
};
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-09-25 13:13:15 -07: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-05 20:06:06 -07:00
|
|
|
}
|
|
|
|
|
2012-10-10 22:48:40 -07:00
|
|
|
void
|
|
|
|
Run()
|
2012-09-25 13:13:15 -07:00
|
|
|
{
|
2013-03-07 23:03:03 -08:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mImpl->IsShutdownOnIOThread());
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
mImpl->QueueWriteData(mData);
|
|
|
|
}
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
private:
|
|
|
|
nsRefPtr<UnixSocketConsumer> mConsumer;
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
UnixSocketRawData* mData;
|
|
|
|
};
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2013-02-21 07:32:17 -08:00
|
|
|
class RequestClosingSocketTask : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RequestClosingSocketTask(UnixSocketImpl* aImpl) : mImpl(aImpl)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-03-07 23:03:03 -08:00
|
|
|
if (mImpl->IsShutdownOnMainThread()) {
|
|
|
|
NS_WARNING("CloseSocket has already been called!");
|
2013-02-21 07:32:17 -08: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;
|
|
|
|
}
|
|
|
|
|
2013-03-07 23:03:03 -08:00
|
|
|
// Start from here, same handling flow as calling CloseSocket() from
|
|
|
|
// upper layer
|
2013-02-21 07:32:17 -08:00
|
|
|
mImpl->mConsumer->CloseSocket();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
};
|
|
|
|
|
2012-10-01 00:03:16 -07:00
|
|
|
class SocketAcceptTask : public CancelableTask {
|
|
|
|
virtual void Run();
|
|
|
|
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
public:
|
2013-03-07 23:03:03 -08:00
|
|
|
SocketAcceptTask(UnixSocketImpl* aImpl) : mImpl(aImpl) { }
|
|
|
|
|
|
|
|
virtual void Cancel()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
mImpl = nullptr;
|
|
|
|
}
|
2012-10-01 00:03:16 -07:00
|
|
|
};
|
|
|
|
|
2013-03-07 23:03:03 -08:00
|
|
|
void SocketAcceptTask::Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
if (mImpl) {
|
|
|
|
mImpl->Accept();
|
2012-10-01 00:03:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-07 23:03:03 -08:00
|
|
|
class SocketConnectTask : public Task {
|
2012-10-01 00:03:16 -07:00
|
|
|
virtual void Run();
|
|
|
|
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
public:
|
2013-03-07 23:03:03 -08:00
|
|
|
SocketConnectTask(UnixSocketImpl* aImpl) : mImpl(aImpl) { }
|
2012-10-01 00:03:16 -07:00
|
|
|
};
|
|
|
|
|
2013-03-07 23:03:03 -08:00
|
|
|
void SocketConnectTask::Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2012-10-01 00:03:16 -07:00
|
|
|
mImpl->Connect();
|
|
|
|
}
|
|
|
|
|
2013-04-01 17:26:25 -07:00
|
|
|
class SocketDelayedConnectTask : public CancelableTask {
|
|
|
|
virtual void Run();
|
|
|
|
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
public:
|
|
|
|
SocketDelayedConnectTask(UnixSocketImpl* aImpl) : mImpl(aImpl) { }
|
|
|
|
|
|
|
|
virtual void Cancel()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mImpl = nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void SocketDelayedConnectTask::Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!mImpl || mImpl->IsShutdownOnMainThread()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mImpl->ClearDelayedConnectTask();
|
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE, new SocketConnectTask(mImpl));
|
|
|
|
}
|
|
|
|
|
2013-03-07 23:03:03 -08:00
|
|
|
class ShutdownSocketTask : public Task {
|
|
|
|
virtual void Run();
|
|
|
|
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ShutdownSocketTask(UnixSocketImpl* aImpl) : mImpl(aImpl) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
void ShutdownSocketTask::Run()
|
2013-02-12 06:16:45 -08:00
|
|
|
{
|
2013-03-07 23:03:03 -08:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2013-02-12 06:16:45 -08:00
|
|
|
|
2013-03-07 23:03:03 -08:00
|
|
|
// At this point, there should be no new events on the IO thread after this
|
|
|
|
// one with the possible exception of a SocketAcceptTask 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 mImpl safely knowing
|
|
|
|
// that no more tasks reference it.
|
|
|
|
mImpl->ShutdownOnIOThread();
|
|
|
|
|
|
|
|
nsRefPtr<nsIRunnable> t(new DeleteInstanceRunnable<UnixSocketImpl>(mImpl));
|
2013-02-12 06:16:45 -08:00
|
|
|
nsresult rv = NS_DispatchToMainThread(t);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
}
|
|
|
|
|
2014-01-24 12:06:40 -08:00
|
|
|
void
|
|
|
|
UnixSocketImpl::Accept()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2014-01-24 14:19:52 -08:00
|
|
|
|
|
|
|
if (!mConnector) {
|
|
|
|
NS_WARNING("No connector object available!");
|
|
|
|
return;
|
|
|
|
}
|
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!");
|
|
|
|
return;
|
|
|
|
}
|
2012-10-01 00:03:16 -07:00
|
|
|
|
2013-04-01 17:26:25 -07:00
|
|
|
if (mFd.get() < 0) {
|
2012-10-01 00:03:16 -07:00
|
|
|
mFd = mConnector->Create();
|
|
|
|
if (mFd.get() < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-08 14:40:26 -08:00
|
|
|
if (!SetSocketFlags()) {
|
2012-10-01 00:03:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-19 10:23:47 -07:00
|
|
|
if (bind(mFd.get(), (struct sockaddr*)&mAddr, mAddrSize)) {
|
2012-10-01 00:03:16 -07:00
|
|
|
#ifdef DEBUG
|
2013-12-17 10:26:45 -08:00
|
|
|
CHROMIUM_LOG("...bind(%d) gave errno %d", mFd.get(), errno);
|
2012-10-01 00:03:16 -07:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(mFd.get(), 1)) {
|
|
|
|
#ifdef DEBUG
|
2013-12-17 10:26:45 -08:00
|
|
|
CHROMIUM_LOG("...listen(%d) gave errno %d", mFd.get(), errno);
|
2012-10-01 00:03:16 -07:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-24 19:00:22 -07:00
|
|
|
if (!mConnector->SetUpListenSocket(mFd)) {
|
|
|
|
NS_WARNING("Could not set up listen socket!");
|
2014-01-24 14:19:52 -08:00
|
|
|
nsRefPtr<OnSocketEventTask> t =
|
|
|
|
new OnSocketEventTask(this, OnSocketEventTask::CONNECT_ERROR);
|
|
|
|
NS_DispatchToMainThread(t);
|
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
2013-10-24 19:00:22 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-01 00:03:16 -07:00
|
|
|
}
|
|
|
|
|
2013-02-12 06:16:45 -08:00
|
|
|
SetUpIO();
|
2012-10-01 00:03:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnixSocketImpl::Connect()
|
|
|
|
{
|
2013-03-07 23:03:03 -08:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2014-01-24 14:19:52 -08:00
|
|
|
|
|
|
|
if (!mConnector) {
|
|
|
|
NS_WARNING("No connector object available!");
|
|
|
|
return;
|
|
|
|
}
|
2013-03-07 23:03:03 -08:00
|
|
|
|
2013-04-01 17:26:25 -07:00
|
|
|
if (mFd.get() < 0) {
|
2012-10-01 00:03:16 -07:00
|
|
|
mFd = mConnector->Create();
|
|
|
|
if (mFd.get() < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
2013-03-19 10:23:47 -07:00
|
|
|
if (!mConnector->CreateAddr(false, mAddrSize, mAddr, mAddress.get())) {
|
|
|
|
NS_WARNING("Cannot create socket address!");
|
|
|
|
return;
|
|
|
|
}
|
2012-10-01 00:03:16 -07:00
|
|
|
|
2013-05-13 20:51:21 -07:00
|
|
|
// Select non-blocking IO.
|
|
|
|
if (-1 == fcntl(mFd.get(), F_SETFL, O_NONBLOCK)) {
|
2014-01-24 14:19:52 -08:00
|
|
|
nsRefPtr<OnSocketEventTask> t =
|
|
|
|
new OnSocketEventTask(this, OnSocketEventTask::CONNECT_ERROR);
|
|
|
|
NS_DispatchToMainThread(t);
|
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
2013-05-13 20:51:21 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-19 10:23:47 -07:00
|
|
|
ret = connect(mFd.get(), (struct sockaddr*)&mAddr, mAddrSize);
|
2012-10-01 00:03:16 -07:00
|
|
|
|
|
|
|
if (ret) {
|
2013-05-13 20:51:21 -07:00
|
|
|
if (errno == EINPROGRESS) {
|
|
|
|
// Select blocking IO again, since we've now at least queue'd the connect
|
|
|
|
// as nonblock.
|
|
|
|
int current_opts = fcntl(mFd.get(), F_GETFL, 0);
|
|
|
|
if (-1 == current_opts) {
|
|
|
|
NS_WARNING("Cannot get socket opts!");
|
2014-01-24 14:19:52 -08:00
|
|
|
mFd.reset(-1);
|
|
|
|
nsRefPtr<OnSocketEventTask> t =
|
|
|
|
new OnSocketEventTask(this, OnSocketEventTask::CONNECT_ERROR);
|
|
|
|
NS_DispatchToMainThread(t);
|
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
2013-05-13 20:51:21 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (-1 == fcntl(mFd.get(), F_SETFL, current_opts & ~O_NONBLOCK)) {
|
|
|
|
NS_WARNING("Cannot set socket opts to blocking!");
|
2014-01-24 14:19:52 -08:00
|
|
|
mFd.reset(-1);
|
|
|
|
nsRefPtr<OnSocketEventTask> t =
|
|
|
|
new OnSocketEventTask(this, OnSocketEventTask::CONNECT_ERROR);
|
|
|
|
NS_DispatchToMainThread(t);
|
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
2013-05-13 20:51:21 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up a write watch to make sure we receive the connect signal
|
|
|
|
MessageLoopForIO::current()->WatchFileDescriptor(
|
|
|
|
mFd.get(),
|
|
|
|
false,
|
|
|
|
MessageLoopForIO::WATCH_WRITE,
|
|
|
|
&mWriteWatcher,
|
|
|
|
this);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2013-12-17 10:26:45 -08:00
|
|
|
CHROMIUM_LOG("UnixSocket Connection delayed!");
|
2013-05-13 20:51:21 -07:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2012-10-01 00:03:16 -07:00
|
|
|
#if DEBUG
|
2013-12-17 10:26:45 -08:00
|
|
|
CHROMIUM_LOG("Socket connect errno=%d\n", errno);
|
2012-10-01 00:03:16 -07:00
|
|
|
#endif
|
2014-01-24 14:19:52 -08:00
|
|
|
mFd.reset(-1);
|
|
|
|
nsRefPtr<OnSocketEventTask> t =
|
|
|
|
new OnSocketEventTask(this, OnSocketEventTask::CONNECT_ERROR);
|
|
|
|
NS_DispatchToMainThread(t);
|
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
2012-10-01 00:03:16 -07:00
|
|
|
return;
|
2012-09-30 22:54:27 -07:00
|
|
|
}
|
|
|
|
|
2013-03-08 14:40:26 -08:00
|
|
|
if (!SetSocketFlags()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-10 22:48:40 -07:00
|
|
|
if (!mConnector->SetUp(mFd)) {
|
2012-10-01 00:03:16 -07:00
|
|
|
NS_WARNING("Could not set up socket!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-10 22:48:40 -07:00
|
|
|
nsRefPtr<OnSocketEventTask> t =
|
|
|
|
new OnSocketEventTask(this, OnSocketEventTask::CONNECT_SUCCESS);
|
|
|
|
NS_DispatchToMainThread(t);
|
2013-12-09 05:21:18 -08:00
|
|
|
mConnectionStatus = SOCKET_CONNECTED;
|
2012-10-10 22:48:40 -07:00
|
|
|
|
2013-03-07 23:03:03 -08:00
|
|
|
SetUpIO();
|
2012-10-01 00:03:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-03-08 14:40:26 -08:00
|
|
|
UnixSocketImpl::SetSocketFlags()
|
2012-10-01 00:03:16 -07:00
|
|
|
{
|
|
|
|
// 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 13:13:15 -07:00
|
|
|
// Set close-on-exec bit.
|
2012-10-01 00:03:16 -07:00
|
|
|
int flags = fcntl(mFd, F_GETFD);
|
2012-09-25 13:13:15 -07:00
|
|
|
if (-1 == flags) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
flags |= FD_CLOEXEC;
|
2012-10-01 00:03:16 -07:00
|
|
|
if (-1 == fcntl(mFd, F_SETFD, flags)) {
|
2012-09-25 13:13:15 -07:00
|
|
|
return false;
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-10 22:48:40 -07:00
|
|
|
UnixSocketConsumer::UnixSocketConsumer() : mImpl(nullptr)
|
|
|
|
, mConnectionStatus(SOCKET_DISCONNECTED)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
UnixSocketConsumer::~UnixSocketConsumer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
new SocketSendTask(this, mImpl, aData));
|
|
|
|
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,
|
|
|
|
new SocketSendTask(this, mImpl, d));
|
|
|
|
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-09-25 13:13:15 -07:00
|
|
|
void
|
|
|
|
UnixSocketImpl::OnFileCanReadWithoutBlocking(int aFd)
|
|
|
|
{
|
2013-03-07 23:03:03 -08:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mShuttingDownOnIOThread);
|
2013-02-12 06:16:45 -08:00
|
|
|
|
2013-12-09 05:21:18 -08:00
|
|
|
if (mConnectionStatus == SOCKET_CONNECTED) {
|
2013-03-07 23:03:03 -08:00
|
|
|
// Read all of the incoming data.
|
2013-02-12 06:16:45 -08:00
|
|
|
while (true) {
|
2013-09-06 01:18:35 -07:00
|
|
|
nsAutoPtr<UnixSocketRawData> incoming(new UnixSocketRawData(MAX_READ_SIZE));
|
|
|
|
|
|
|
|
ssize_t ret = read(aFd, incoming->mData, incoming->mSize);
|
2013-03-07 23:03:03 -08:00
|
|
|
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
|
2012-09-25 13:13:15 -07:00
|
|
|
}
|
2013-03-07 23:03:03 -08:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
#ifdef DEBUG
|
2013-02-12 06:16:45 -08:00
|
|
|
NS_WARNING("Cannot read from network");
|
2012-09-25 13:13:15 -07:00
|
|
|
#endif
|
2013-03-07 23:03:03 -08:00
|
|
|
// else fall through to error handling on other errno's
|
2013-02-12 06:16:45 -08:00
|
|
|
}
|
2013-03-07 23:03:03 -08:00
|
|
|
|
|
|
|
// We're done with our descriptors. Ensure that spurious events don't
|
|
|
|
// cause us to end up back here.
|
|
|
|
mReadWatcher.StopWatchingFileDescriptor();
|
|
|
|
mWriteWatcher.StopWatchingFileDescriptor();
|
|
|
|
nsRefPtr<RequestClosingSocketTask> t = new RequestClosingSocketTask(this);
|
|
|
|
NS_DispatchToMainThread(t);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-06 01:18:35 -07:00
|
|
|
incoming->mSize = ret;
|
|
|
|
nsRefPtr<SocketReceiveTask> t =
|
|
|
|
new SocketReceiveTask(this, incoming.forget());
|
2013-03-07 23:03:03 -08:00
|
|
|
NS_DispatchToMainThread(t);
|
|
|
|
|
2013-09-06 01:18:35 -07:00
|
|
|
// If ret is less than MAX_READ_SIZE, there's no
|
|
|
|
// more data in the socket for us to read now.
|
2013-03-07 23:03:03 -08:00
|
|
|
if (ret < ssize_t(MAX_READ_SIZE)) {
|
|
|
|
return;
|
2012-09-25 13:13:15 -07:00
|
|
|
}
|
|
|
|
}
|
2013-02-12 06:16:45 -08:00
|
|
|
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("We returned early");
|
2013-12-09 05:21:18 -08:00
|
|
|
} else if (mConnectionStatus == SOCKET_LISTENING) {
|
2013-03-19 10:23:47 -07:00
|
|
|
int client_fd = accept(mFd.get(), (struct sockaddr*)&mAddr, &mAddrSize);
|
2013-02-12 06:16:45 -08:00
|
|
|
|
|
|
|
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);
|
2013-03-08 14:40:26 -08:00
|
|
|
if (!SetSocketFlags()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-01 20:11:01 -08:00
|
|
|
mIOLoop = nullptr;
|
2013-02-12 06:16:45 -08:00
|
|
|
|
|
|
|
nsRefPtr<OnSocketEventTask> t =
|
|
|
|
new OnSocketEventTask(this, OnSocketEventTask::CONNECT_SUCCESS);
|
|
|
|
NS_DispatchToMainThread(t);
|
2013-12-09 05:21:18 -08:00
|
|
|
mConnectionStatus = SOCKET_CONNECTED;
|
2013-02-12 06:16:45 -08:00
|
|
|
|
2013-03-07 23:03:03 -08:00
|
|
|
SetUpIO();
|
2012-09-25 13:13:15 -07:00
|
|
|
}
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
void
|
|
|
|
UnixSocketImpl::OnFileCanWriteWithoutBlocking(int aFd)
|
2012-09-05 20:06:06 -07:00
|
|
|
{
|
2013-03-07 23:03:03 -08:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mShuttingDownOnIOThread);
|
|
|
|
|
2013-02-21 15:21:34 -08:00
|
|
|
MOZ_ASSERT(aFd >= 0);
|
2013-12-09 05:21:18 -08:00
|
|
|
if (mConnectionStatus == SOCKET_CONNECTED) {
|
2013-05-13 20:51:21 -07: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;
|
2012-09-25 13:13:15 -07:00
|
|
|
}
|
2013-05-13 20:51:21 -07:00
|
|
|
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;
|
|
|
|
}
|
2012-09-25 13:13:15 -07:00
|
|
|
}
|
2013-05-13 20:51:21 -07:00
|
|
|
|
|
|
|
if (data->mCurrentWriteOffset != data->mSize) {
|
|
|
|
MessageLoopForIO::current()->WatchFileDescriptor(
|
|
|
|
aFd,
|
|
|
|
false,
|
|
|
|
MessageLoopForIO::WATCH_WRITE,
|
|
|
|
&mWriteWatcher,
|
|
|
|
this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mOutgoingQ.RemoveElementAt(0);
|
|
|
|
delete data;
|
|
|
|
}
|
2013-12-09 05:21:18 -08:00
|
|
|
} else if (mConnectionStatus == SOCKET_CONNECTING) {
|
2013-05-13 20:51:21 -07:00
|
|
|
int error, ret;
|
|
|
|
socklen_t len = sizeof(error);
|
|
|
|
ret = getsockopt(mFd.get(), SOL_SOCKET, SO_ERROR, &error, &len);
|
|
|
|
|
|
|
|
if (ret || error) {
|
|
|
|
NS_WARNING("getsockopt failure on async socket connect!");
|
2014-01-24 14:19:52 -08:00
|
|
|
mFd.reset(-1);
|
|
|
|
nsRefPtr<OnSocketEventTask> t =
|
|
|
|
new OnSocketEventTask(this, OnSocketEventTask::CONNECT_ERROR);
|
|
|
|
NS_DispatchToMainThread(t);
|
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
2013-05-13 20:51:21 -07:00
|
|
|
return;
|
2012-09-25 13:13:15 -07:00
|
|
|
}
|
|
|
|
|
2013-05-13 20:51:21 -07:00
|
|
|
if (!SetSocketFlags()) {
|
2014-01-24 14:19:52 -08:00
|
|
|
mFd.reset(-1);
|
|
|
|
nsRefPtr<OnSocketEventTask> t =
|
|
|
|
new OnSocketEventTask(this, OnSocketEventTask::CONNECT_ERROR);
|
|
|
|
NS_DispatchToMainThread(t);
|
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
2013-05-13 20:51:21 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mConnector->SetUp(mFd)) {
|
|
|
|
NS_WARNING("Could not set up socket!");
|
2014-01-24 14:19:52 -08:00
|
|
|
mFd.reset(-1);
|
|
|
|
nsRefPtr<OnSocketEventTask> t =
|
|
|
|
new OnSocketEventTask(this, OnSocketEventTask::CONNECT_ERROR);
|
|
|
|
NS_DispatchToMainThread(t);
|
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
2012-09-25 13:13:15 -07:00
|
|
|
return;
|
|
|
|
}
|
2013-05-13 20:51:21 -07:00
|
|
|
|
|
|
|
nsRefPtr<OnSocketEventTask> t =
|
|
|
|
new OnSocketEventTask(this, OnSocketEventTask::CONNECT_SUCCESS);
|
|
|
|
NS_DispatchToMainThread(t);
|
2013-12-09 05:21:18 -08:00
|
|
|
mConnectionStatus = SOCKET_CONNECTED;
|
2013-05-13 20:51:21 -07:00
|
|
|
|
|
|
|
SetUpIO();
|
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;
|
|
|
|
OnConnectSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnixSocketConsumer::NotifyError()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
|
|
|
OnConnectError();
|
|
|
|
}
|
2012-10-12 11:38:14 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
UnixSocketConsumer::NotifyDisconnect()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
|
|
|
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-12-09 05:21:18 -08:00
|
|
|
mImpl = new UnixSocketImpl(this, connector.forget(), addr, SOCKET_CONNECTING);
|
2013-02-01 04:28:18 -08:00
|
|
|
MessageLoop* ioLoop = XRE_GetIOMessageLoop();
|
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
|
|
|
|
2013-12-09 05:21:18 -08:00
|
|
|
mImpl = new UnixSocketImpl(this, connector.forget(), EmptyCString(),
|
|
|
|
SOCKET_LISTENING);
|
2013-02-12 06:16:45 -08:00
|
|
|
mConnectionStatus = SOCKET_LISTENING;
|
2012-09-25 13:13:15 -07:00
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE,
|
2012-10-01 00:03:16 -07:00
|
|
|
new SocketAcceptTask(mImpl));
|
2012-09-25 13:13:15 -07:00
|
|
|
return true;
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|