2014-07-31 15:55:27 +04:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SocketBase.h"
|
2014-09-08 13:44:00 +04:00
|
|
|
#include <errno.h>
|
2014-07-31 15:55:27 +04:00
|
|
|
#include <string.h>
|
2014-09-08 13:44:00 +04:00
|
|
|
#include <unistd.h>
|
2015-07-21 09:06:29 +03:00
|
|
|
#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, MOZ_COUNT_DTOR
|
2014-07-31 15:55:27 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
|
|
|
//
|
2014-10-21 12:52:12 +04:00
|
|
|
// UnixSocketIOBuffer
|
2014-07-31 15:55:27 +04:00
|
|
|
//
|
|
|
|
|
2015-06-08 11:20:17 +03:00
|
|
|
UnixSocketBuffer::UnixSocketBuffer()
|
2015-04-23 14:48:47 +03:00
|
|
|
: mSize(0)
|
|
|
|
, mOffset(0)
|
2015-06-08 11:20:17 +03:00
|
|
|
, mAvailableSpace(0)
|
|
|
|
, mData(nullptr)
|
2015-07-21 09:06:29 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(UnixSocketBuffer);
|
|
|
|
}
|
2014-09-08 13:44:02 +04:00
|
|
|
|
2015-04-23 14:48:47 +03:00
|
|
|
UnixSocketBuffer::~UnixSocketBuffer()
|
2015-06-08 11:20:17 +03:00
|
|
|
{
|
2015-07-21 09:06:29 +03:00
|
|
|
MOZ_COUNT_DTOR(UnixSocketBuffer);
|
|
|
|
|
2015-06-08 11:20:17 +03:00
|
|
|
// Make sure that the caller released the buffer's memory.
|
|
|
|
MOZ_ASSERT(!GetBuffer());
|
|
|
|
}
|
2014-10-21 12:52:12 +04:00
|
|
|
|
|
|
|
const uint8_t*
|
2015-04-23 14:48:47 +03:00
|
|
|
UnixSocketBuffer::Consume(size_t aLen)
|
2014-10-21 12:52:12 +04:00
|
|
|
{
|
|
|
|
if (NS_WARN_IF(GetSize() < aLen)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
uint8_t* data = mData + mOffset;
|
|
|
|
mOffset += aLen;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-04-23 14:48:47 +03:00
|
|
|
UnixSocketBuffer::Read(void* aValue, size_t aLen)
|
2014-10-21 12:52:12 +04:00
|
|
|
{
|
|
|
|
const uint8_t* data = Consume(aLen);
|
|
|
|
if (!data) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
memcpy(aValue, data, aLen);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t*
|
2015-04-23 14:48:47 +03:00
|
|
|
UnixSocketBuffer::Append(size_t aLen)
|
2014-10-21 12:52:12 +04:00
|
|
|
{
|
|
|
|
if (((mAvailableSpace - mSize) < aLen)) {
|
|
|
|
size_t availableSpace = mAvailableSpace + std::max(mAvailableSpace, aLen);
|
|
|
|
uint8_t* data = new uint8_t[availableSpace];
|
|
|
|
memcpy(data, mData, mSize);
|
|
|
|
mData = data;
|
|
|
|
mAvailableSpace = availableSpace;
|
|
|
|
}
|
|
|
|
uint8_t* data = mData + mSize;
|
|
|
|
mSize += aLen;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-04-23 14:48:47 +03:00
|
|
|
UnixSocketBuffer::Write(const void* aValue, size_t aLen)
|
2014-10-21 12:52:12 +04:00
|
|
|
{
|
|
|
|
uint8_t* data = Append(aLen);
|
|
|
|
if (!data) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
memcpy(data, aValue, aLen);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-23 14:48:47 +03:00
|
|
|
UnixSocketBuffer::CleanupLeadingSpace()
|
2014-10-21 12:52:12 +04:00
|
|
|
{
|
|
|
|
if (GetLeadingSpace()) {
|
|
|
|
if (GetSize() <= GetLeadingSpace()) {
|
|
|
|
memcpy(mData, GetData(), GetSize());
|
|
|
|
} else {
|
|
|
|
memmove(mData, GetData(), GetSize());
|
|
|
|
}
|
|
|
|
mOffset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 14:48:47 +03:00
|
|
|
//
|
|
|
|
// UnixSocketIOBuffer
|
|
|
|
//
|
|
|
|
|
2015-07-21 09:06:29 +03:00
|
|
|
UnixSocketIOBuffer::UnixSocketIOBuffer()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR_INHERITED(UnixSocketIOBuffer, UnixSocketBuffer);
|
|
|
|
}
|
|
|
|
|
2015-04-23 14:48:47 +03:00
|
|
|
UnixSocketIOBuffer::~UnixSocketIOBuffer()
|
2015-07-21 09:06:29 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR_INHERITED(UnixSocketIOBuffer, UnixSocketBuffer);
|
|
|
|
}
|
2015-04-23 14:48:47 +03:00
|
|
|
|
2014-10-21 12:52:12 +04:00
|
|
|
//
|
|
|
|
// UnixSocketRawData
|
|
|
|
//
|
|
|
|
|
|
|
|
UnixSocketRawData::UnixSocketRawData(const void* aData, size_t aSize)
|
2015-06-08 11:20:17 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aData || !aSize);
|
|
|
|
|
2015-07-21 09:06:29 +03:00
|
|
|
MOZ_COUNT_CTOR_INHERITED(UnixSocketRawData, UnixSocketIOBuffer);
|
|
|
|
|
2015-06-08 11:20:17 +03:00
|
|
|
ResetBuffer(static_cast<uint8_t*>(memcpy(new uint8_t[aSize], aData, aSize)),
|
|
|
|
0, aSize, aSize);
|
|
|
|
}
|
2014-10-21 12:52:12 +04:00
|
|
|
|
2015-11-03 00:13:10 +03:00
|
|
|
UnixSocketRawData::UnixSocketRawData(UniquePtr<uint8_t[]> aData, size_t aSize)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aData || !aSize);
|
|
|
|
|
|
|
|
MOZ_COUNT_CTOR_INHERITED(UnixSocketRawData, UnixSocketIOBuffer);
|
|
|
|
|
|
|
|
ResetBuffer(aData.release(), 0, aSize, aSize);
|
|
|
|
}
|
|
|
|
|
2014-10-21 12:52:12 +04:00
|
|
|
UnixSocketRawData::UnixSocketRawData(size_t aSize)
|
2015-06-08 11:20:17 +03:00
|
|
|
{
|
2015-07-21 09:06:29 +03:00
|
|
|
MOZ_COUNT_CTOR_INHERITED(UnixSocketRawData, UnixSocketIOBuffer);
|
|
|
|
|
2015-06-08 11:20:17 +03:00
|
|
|
ResetBuffer(new uint8_t[aSize], 0, 0, aSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnixSocketRawData::~UnixSocketRawData()
|
|
|
|
{
|
2015-07-21 09:06:29 +03:00
|
|
|
MOZ_COUNT_DTOR_INHERITED(UnixSocketRawData, UnixSocketIOBuffer);
|
|
|
|
|
2015-12-06 18:53:37 +03:00
|
|
|
UniquePtr<uint8_t[]> data(GetBuffer());
|
2015-06-08 11:20:17 +03:00
|
|
|
ResetBuffer(nullptr, 0, 0, 0);
|
|
|
|
}
|
2014-10-21 12:52:12 +04:00
|
|
|
|
2014-09-08 13:44:00 +04:00
|
|
|
ssize_t
|
|
|
|
UnixSocketRawData::Receive(int aFd)
|
|
|
|
{
|
|
|
|
if (!GetTrailingSpace()) {
|
|
|
|
if (!GetLeadingSpace()) {
|
|
|
|
return -1; /* buffer is full */
|
|
|
|
}
|
|
|
|
/* free up space at the end of data buffer */
|
2014-10-21 12:52:12 +04:00
|
|
|
CleanupLeadingSpace();
|
2014-09-08 13:44:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t res =
|
|
|
|
TEMP_FAILURE_RETRY(read(aFd, GetTrailingBytes(), GetTrailingSpace()));
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
/* I/O error */
|
|
|
|
return -1;
|
|
|
|
} else if (!res) {
|
|
|
|
/* EOF or peer shutdown sending */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-21 12:52:12 +04:00
|
|
|
Append(res); /* mark read data as 'valid' */
|
2014-09-08 13:44:00 +04:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
UnixSocketRawData::Send(int aFd)
|
|
|
|
{
|
|
|
|
if (!GetSize()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t res = TEMP_FAILURE_RETRY(write(aFd, GetData(), GetSize()));
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
return 0; /* socket is blocked; try again later */
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
} else if (!res) {
|
|
|
|
/* nothing written */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Consume(res);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-07-31 15:55:27 +04:00
|
|
|
//
|
2014-10-21 12:52:11 +04:00
|
|
|
// SocketBase
|
2014-07-31 15:55:27 +04:00
|
|
|
//
|
|
|
|
|
|
|
|
SocketConnectionStatus
|
2014-10-21 12:52:11 +04:00
|
|
|
SocketBase::GetConnectionStatus() const
|
2014-07-31 15:55:27 +04:00
|
|
|
{
|
|
|
|
return mConnectionStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-10-21 12:52:11 +04:00
|
|
|
SocketBase::GetSuggestedConnectDelayMs() const
|
2014-07-31 15:55:27 +04:00
|
|
|
{
|
|
|
|
return mConnectDelayMs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-10-21 12:52:11 +04:00
|
|
|
SocketBase::NotifySuccess()
|
2014-07-31 15:55:27 +04:00
|
|
|
{
|
|
|
|
mConnectionStatus = SOCKET_CONNECTED;
|
|
|
|
mConnectTimestamp = PR_IntervalNow();
|
|
|
|
OnConnectSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-10-21 12:52:11 +04:00
|
|
|
SocketBase::NotifyError()
|
2014-07-31 15:55:27 +04:00
|
|
|
{
|
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
|
|
|
mConnectDelayMs = CalculateConnectDelayMs();
|
2014-08-18 23:53:00 +04:00
|
|
|
mConnectTimestamp = 0;
|
2014-07-31 15:55:27 +04:00
|
|
|
OnConnectError();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-10-21 12:52:11 +04:00
|
|
|
SocketBase::NotifyDisconnect()
|
2014-07-31 15:55:27 +04:00
|
|
|
{
|
|
|
|
mConnectionStatus = SOCKET_DISCONNECTED;
|
|
|
|
mConnectDelayMs = CalculateConnectDelayMs();
|
2014-08-18 23:53:00 +04:00
|
|
|
mConnectTimestamp = 0;
|
2014-07-31 15:55:27 +04:00
|
|
|
OnDisconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2014-10-21 12:52:11 +04:00
|
|
|
SocketBase::CalculateConnectDelayMs() const
|
2014-07-31 15:55:27 +04:00
|
|
|
{
|
|
|
|
uint32_t connectDelayMs = mConnectDelayMs;
|
|
|
|
|
2014-08-18 23:53:00 +04:00
|
|
|
if (mConnectTimestamp && (PR_IntervalNow()-mConnectTimestamp) > connectDelayMs) {
|
2014-07-31 15:55:27 +04:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-10-21 12:52:11 +04:00
|
|
|
SocketBase::SocketBase()
|
2014-07-31 15:55:27 +04:00
|
|
|
: mConnectionStatus(SOCKET_DISCONNECTED)
|
|
|
|
, mConnectTimestamp(0)
|
|
|
|
, mConnectDelayMs(0)
|
2015-07-21 09:06:29 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(SocketBase);
|
|
|
|
}
|
2014-07-31 15:55:27 +04:00
|
|
|
|
2015-03-02 12:23:52 +03:00
|
|
|
SocketBase::~SocketBase()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mConnectionStatus == SOCKET_DISCONNECTED);
|
2015-07-21 09:06:29 +03:00
|
|
|
|
|
|
|
MOZ_COUNT_DTOR(SocketBase);
|
2015-03-02 12:23:52 +03:00
|
|
|
}
|
|
|
|
|
2014-07-31 15:55:27 +04:00
|
|
|
void
|
2014-10-21 12:52:11 +04:00
|
|
|
SocketBase::SetConnectionStatus(SocketConnectionStatus aConnectionStatus)
|
2014-07-31 15:55:27 +04:00
|
|
|
{
|
|
|
|
mConnectionStatus = aConnectionStatus;
|
|
|
|
}
|
|
|
|
|
2014-10-21 12:52:11 +04:00
|
|
|
//
|
2015-04-28 11:18:12 +03:00
|
|
|
// SocketIOBase
|
2014-10-21 12:52:11 +04:00
|
|
|
//
|
|
|
|
|
2015-06-09 10:50:10 +03:00
|
|
|
SocketIOBase::SocketIOBase(MessageLoop* aConsumerLoop)
|
|
|
|
: mConsumerLoop(aConsumerLoop)
|
2015-06-02 11:01:57 +03:00
|
|
|
{
|
2015-06-09 10:50:10 +03:00
|
|
|
MOZ_ASSERT(mConsumerLoop);
|
2015-07-21 09:06:29 +03:00
|
|
|
|
|
|
|
MOZ_COUNT_CTOR(SocketIOBase);
|
2015-06-02 11:01:57 +03:00
|
|
|
}
|
2014-10-21 12:52:11 +04:00
|
|
|
|
2014-07-31 15:55:29 +04:00
|
|
|
SocketIOBase::~SocketIOBase()
|
2015-07-21 09:06:29 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(SocketIOBase);
|
|
|
|
}
|
2014-07-31 15:55:29 +04:00
|
|
|
|
2015-06-09 10:50:10 +03:00
|
|
|
MessageLoop*
|
2015-06-02 11:01:57 +03:00
|
|
|
SocketIOBase::GetConsumerThread() const
|
|
|
|
{
|
2015-06-09 10:50:10 +03:00
|
|
|
return mConsumerLoop;
|
2015-06-02 11:01:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SocketIOBase::IsConsumerThread() const
|
|
|
|
{
|
2015-06-09 10:50:10 +03:00
|
|
|
return GetConsumerThread() == MessageLoop::current();
|
2015-06-02 11:01:57 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 12:19:28 +03:00
|
|
|
//
|
2015-06-09 10:50:10 +03:00
|
|
|
// SocketEventTask
|
2015-04-29 12:19:28 +03:00
|
|
|
//
|
|
|
|
|
2015-06-09 10:50:10 +03:00
|
|
|
SocketEventTask::SocketEventTask(SocketIOBase* aIO, SocketEvent aEvent)
|
|
|
|
: SocketTask<SocketIOBase>(aIO)
|
2015-04-29 12:19:28 +03:00
|
|
|
, mEvent(aEvent)
|
2015-07-21 09:06:29 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(SocketEventTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
SocketEventTask::~SocketEventTask()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(SocketEventTask);
|
|
|
|
}
|
2015-04-29 12:19:28 +03:00
|
|
|
|
2016-05-02 20:27:15 +03:00
|
|
|
NS_IMETHODIMP
|
2015-06-09 10:50:10 +03:00
|
|
|
SocketEventTask::Run()
|
2015-04-29 12:19:28 +03:00
|
|
|
{
|
2015-06-09 10:50:10 +03:00
|
|
|
SocketIOBase* io = SocketTask<SocketIOBase>::GetIO();
|
2015-04-29 12:19:28 +03:00
|
|
|
|
2015-06-02 11:01:57 +03:00
|
|
|
MOZ_ASSERT(io->IsConsumerThread());
|
|
|
|
|
2015-06-02 11:01:58 +03:00
|
|
|
if (NS_WARN_IF(io->IsShutdownOnConsumerThread())) {
|
2015-04-29 12:19:28 +03:00
|
|
|
// Since we've already explicitly closed and the close
|
|
|
|
// happened before this, this isn't really an error.
|
2016-05-02 20:27:15 +03:00
|
|
|
return NS_OK;
|
2015-04-29 12:19:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SocketBase* socketBase = io->GetSocketBase();
|
|
|
|
MOZ_ASSERT(socketBase);
|
|
|
|
|
|
|
|
if (mEvent == CONNECT_SUCCESS) {
|
|
|
|
socketBase->NotifySuccess();
|
|
|
|
} else if (mEvent == CONNECT_ERROR) {
|
|
|
|
socketBase->NotifyError();
|
|
|
|
} else if (mEvent == DISCONNECT) {
|
|
|
|
socketBase->NotifyDisconnect();
|
|
|
|
}
|
2016-05-02 20:27:15 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2015-04-29 12:19:28 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 12:19:28 +03:00
|
|
|
//
|
2015-06-09 10:50:10 +03:00
|
|
|
// SocketRequestClosingTask
|
2015-04-29 12:19:28 +03:00
|
|
|
//
|
|
|
|
|
2015-07-21 09:06:29 +03:00
|
|
|
SocketRequestClosingTask::SocketRequestClosingTask(SocketIOBase* aIO)
|
2015-06-09 10:50:10 +03:00
|
|
|
: SocketTask<SocketIOBase>(aIO)
|
2015-07-21 09:06:29 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(SocketRequestClosingTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
SocketRequestClosingTask::~SocketRequestClosingTask()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(SocketRequestClosingTask);
|
|
|
|
}
|
2015-04-29 12:19:28 +03:00
|
|
|
|
2016-05-02 20:27:15 +03:00
|
|
|
NS_IMETHODIMP
|
2015-06-09 10:50:10 +03:00
|
|
|
SocketRequestClosingTask::Run()
|
2015-04-29 12:19:28 +03:00
|
|
|
{
|
2015-06-09 10:50:10 +03:00
|
|
|
SocketIOBase* io = SocketTask<SocketIOBase>::GetIO();
|
2015-04-29 12:19:28 +03:00
|
|
|
|
2015-06-02 11:01:57 +03:00
|
|
|
MOZ_ASSERT(io->IsConsumerThread());
|
|
|
|
|
2015-06-02 11:01:58 +03:00
|
|
|
if (NS_WARN_IF(io->IsShutdownOnConsumerThread())) {
|
2015-04-29 12:19:28 +03:00
|
|
|
// Since we've already explicitly closed and the close
|
|
|
|
// happened before this, this isn't really an error.
|
2016-05-02 20:27:15 +03:00
|
|
|
return NS_OK;
|
2015-04-29 12:19:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SocketBase* socketBase = io->GetSocketBase();
|
|
|
|
MOZ_ASSERT(socketBase);
|
|
|
|
|
2015-05-20 11:50:43 +03:00
|
|
|
socketBase->Close();
|
2016-05-02 20:27:15 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2015-04-29 12:19:28 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 12:19:28 +03:00
|
|
|
//
|
2015-06-09 10:50:10 +03:00
|
|
|
// SocketDeleteInstanceTask
|
2015-04-29 12:19:28 +03:00
|
|
|
//
|
|
|
|
|
2015-07-21 09:06:29 +03:00
|
|
|
SocketDeleteInstanceTask::SocketDeleteInstanceTask(SocketIOBase* aIO)
|
2015-04-29 12:19:28 +03:00
|
|
|
: mIO(aIO)
|
2015-07-21 09:06:29 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(SocketDeleteInstanceTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
SocketDeleteInstanceTask::~SocketDeleteInstanceTask()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(SocketDeleteInstanceTask);
|
|
|
|
}
|
2015-04-29 12:19:28 +03:00
|
|
|
|
2016-05-02 20:27:15 +03:00
|
|
|
NS_IMETHODIMP
|
2015-06-09 10:50:10 +03:00
|
|
|
SocketDeleteInstanceTask::Run()
|
2015-04-29 12:19:28 +03:00
|
|
|
{
|
2016-04-05 14:40:11 +03:00
|
|
|
mIO.reset(); // delete instance
|
2016-05-02 20:27:15 +03:00
|
|
|
return NS_OK;
|
2015-04-29 12:19:28 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 12:19:28 +03:00
|
|
|
//
|
|
|
|
// SocketIOShutdownTask
|
|
|
|
//
|
|
|
|
|
|
|
|
SocketIOShutdownTask::SocketIOShutdownTask(SocketIOBase* aIO)
|
|
|
|
: SocketIOTask<SocketIOBase>(aIO)
|
2015-07-21 09:06:29 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(SocketIOShutdownTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
SocketIOShutdownTask::~SocketIOShutdownTask()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(SocketIOShutdownTask);
|
|
|
|
}
|
2015-04-29 12:19:28 +03:00
|
|
|
|
2016-05-02 20:27:15 +03:00
|
|
|
NS_IMETHODIMP
|
2015-04-29 12:19:28 +03:00
|
|
|
SocketIOShutdownTask::Run()
|
|
|
|
{
|
|
|
|
SocketIOBase* io = SocketIOTask<SocketIOBase>::GetIO();
|
|
|
|
|
2015-06-02 11:01:57 +03:00
|
|
|
MOZ_ASSERT(!io->IsConsumerThread());
|
|
|
|
MOZ_ASSERT(!io->IsShutdownOnIOThread());
|
|
|
|
|
2015-04-29 12:19:28 +03:00
|
|
|
// At this point, there should be no new events on the I/O thread
|
|
|
|
// after this one with the possible exception of an accept task,
|
|
|
|
// which ShutdownOnIOThread will cancel for us. We are now fully
|
2015-06-02 11:01:58 +03:00
|
|
|
// shut down, so we can send a message to the consumer thread to
|
|
|
|
// delete |io| safely knowing that it's not reference any longer.
|
2015-04-29 12:19:28 +03:00
|
|
|
io->ShutdownOnIOThread();
|
2016-05-02 20:27:15 +03:00
|
|
|
io->GetConsumerThread()->PostTask(
|
|
|
|
MakeAndAddRef<SocketDeleteInstanceTask>(io));
|
|
|
|
return NS_OK;
|
2015-04-29 12:19:28 +03:00
|
|
|
}
|
|
|
|
|
2014-07-31 15:55:27 +04:00
|
|
|
}
|
|
|
|
}
|