2015-04-28 11:18:12 +03: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/.
|
|
|
|
*/
|
|
|
|
|
2015-07-21 09:06:29 +03:00
|
|
|
#include "DataSocket.h"
|
2015-04-29 12:19:28 +03:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
#include "GeckoTaskTracer.h"
|
|
|
|
#endif
|
2015-07-21 09:06:29 +03:00
|
|
|
#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, MOZ_COUNT_DTOR
|
2015-04-29 12:19:28 +03:00
|
|
|
|
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
using namespace mozilla::tasktracer;
|
|
|
|
#endif
|
2015-04-28 11:18:12 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
|
|
|
//
|
|
|
|
// DataSocketIO
|
|
|
|
//
|
|
|
|
|
|
|
|
DataSocketIO::~DataSocketIO()
|
2015-07-21 09:06:29 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR_INHERITED(DataSocketIO, SocketIOBase);
|
|
|
|
}
|
2015-04-28 11:18:12 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
DataSocketIO::EnqueueData(UnixSocketIOBuffer* aBuffer)
|
|
|
|
{
|
|
|
|
if (!aBuffer->GetSize()) {
|
|
|
|
delete aBuffer; // delete empty data immediately
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mOutgoingQ.AppendElement(aBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DataSocketIO::HasPendingData() const
|
|
|
|
{
|
|
|
|
return !mOutgoingQ.IsEmpty();
|
|
|
|
}
|
|
|
|
|
2015-04-29 12:19:28 +03:00
|
|
|
ssize_t
|
|
|
|
DataSocketIO::ReceiveData(int aFd)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFd >= 0);
|
|
|
|
|
|
|
|
UnixSocketIOBuffer* incoming;
|
|
|
|
nsresult rv = QueryReceiveBuffer(&incoming);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
/* an error occured */
|
2015-06-09 10:50:10 +03:00
|
|
|
GetConsumerThread()->PostTask(FROM_HERE,
|
|
|
|
new SocketRequestClosingTask(this));
|
2015-04-29 12:19:28 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t res = incoming->Receive(aFd);
|
|
|
|
if (res < 0) {
|
|
|
|
/* an I/O error occured */
|
|
|
|
DiscardBuffer();
|
2015-06-09 10:50:10 +03:00
|
|
|
GetConsumerThread()->PostTask(FROM_HERE,
|
|
|
|
new SocketRequestClosingTask(this));
|
2015-04-29 12:19:28 +03:00
|
|
|
return -1;
|
|
|
|
} else if (!res) {
|
|
|
|
/* EOF or peer shut down sending */
|
|
|
|
DiscardBuffer();
|
2015-06-09 10:50:10 +03:00
|
|
|
GetConsumerThread()->PostTask(FROM_HERE,
|
|
|
|
new SocketRequestClosingTask(this));
|
2015-04-29 12:19:28 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
ConsumeBuffer();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DataSocketIO::SendPendingData(int aFd)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFd >= 0);
|
|
|
|
|
|
|
|
while (HasPendingData()) {
|
|
|
|
UnixSocketIOBuffer* outgoing = mOutgoingQ.ElementAt(0);
|
|
|
|
|
|
|
|
ssize_t res = outgoing->Send(aFd);
|
|
|
|
if (res < 0) {
|
|
|
|
/* an I/O error occured */
|
2015-06-09 10:50:10 +03:00
|
|
|
GetConsumerThread()->PostTask(FROM_HERE,
|
|
|
|
new SocketRequestClosingTask(this));
|
2015-04-29 12:19:28 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
} else if (!res && outgoing->GetSize()) {
|
|
|
|
/* I/O is currently blocked; try again later */
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (!outgoing->GetSize()) {
|
|
|
|
mOutgoingQ.RemoveElementAt(0);
|
|
|
|
delete outgoing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-06-09 10:50:10 +03:00
|
|
|
DataSocketIO::DataSocketIO(MessageLoop* aConsumerLoop)
|
|
|
|
: SocketIOBase(aConsumerLoop)
|
2015-07-21 09:06:29 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR_INHERITED(DataSocketIO, SocketIOBase);
|
|
|
|
}
|
2015-04-28 11:18:12 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// DataSocket
|
|
|
|
//
|
|
|
|
|
2015-07-21 09:06:29 +03:00
|
|
|
DataSocket::DataSocket()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR_INHERITED(DataSocket, SocketBase);
|
|
|
|
}
|
|
|
|
|
2015-04-28 11:18:12 +03:00
|
|
|
DataSocket::~DataSocket()
|
2015-07-21 09:06:29 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR_INHERITED(DataSocket, SocketBase);
|
|
|
|
}
|
2015-04-28 11:18:12 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|