mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 1158876: Rename |SocketConsumerBase| to |DataSocket|, r=kmachulis
This patch renames |SocketConsumerBase| to |DataSocket|, and for the I/O classes |SocketConsumerIO| to |DataSocketIO|. |DataSocketIO| also contains send and receive functionality from |SocketBaseIO|. |DataSocket| is a virtual base class that represents a socket that transfers data, without a particular constraints to what the data represents. |DataSocketIO| is the corresponding helper class on the I/O thread.
This commit is contained in:
parent
e60f688b75
commit
f1427ebbb7
@ -43,7 +43,7 @@ EnsureBluetoothSocketHalLoad()
|
||||
}
|
||||
|
||||
class mozilla::dom::bluetooth::DroidSocketImpl : public ipc::UnixFdWatcher
|
||||
, protected SocketIOBase
|
||||
, protected DataSocketIO
|
||||
{
|
||||
public:
|
||||
/* The connection status in DroidSocketImpl indicates the current
|
||||
@ -75,7 +75,7 @@ public:
|
||||
|
||||
DroidSocketImpl(MessageLoop* aIOLoop, BluetoothSocket* aConsumer)
|
||||
: ipc::UnixFdWatcher(aIOLoop)
|
||||
, SocketIOBase(MAX_READ_SIZE)
|
||||
, DataSocketIO(MAX_READ_SIZE)
|
||||
, mConsumer(aConsumer)
|
||||
, mShuttingDownOnIOThread(false)
|
||||
, mConnectionStatus(SOCKET_IS_DISCONNECTED)
|
||||
@ -135,14 +135,14 @@ public:
|
||||
AddWatchers(WRITE_WATCHER, false);
|
||||
}
|
||||
|
||||
SocketConsumerBase* GetConsumer()
|
||||
DataSocket* GetDataSocket()
|
||||
{
|
||||
return mConsumer.get();
|
||||
}
|
||||
|
||||
SocketBase* GetSocketBase()
|
||||
{
|
||||
return GetConsumer();
|
||||
return GetDataSocket();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -8,7 +8,7 @@
|
||||
#define mozilla_dom_bluetooth_BluetoothSocket_h
|
||||
|
||||
#include "BluetoothCommon.h"
|
||||
#include "mozilla/ipc/SocketBase.h"
|
||||
#include "mozilla/ipc/DataSocket.h"
|
||||
|
||||
BEGIN_BLUETOOTH_NAMESPACE
|
||||
|
||||
@ -16,7 +16,7 @@ class BluetoothSocketObserver;
|
||||
class BluetoothSocketResultHandler;
|
||||
class DroidSocketImpl;
|
||||
|
||||
class BluetoothSocket : public mozilla::ipc::SocketConsumerBase
|
||||
class BluetoothSocket : public mozilla::ipc::DataSocket
|
||||
{
|
||||
public:
|
||||
BluetoothSocket(BluetoothSocketObserver* aObserver,
|
||||
|
@ -25,7 +25,7 @@ static const size_t MAX_READ_SIZE = 1 << 16;
|
||||
|
||||
class BluetoothSocket::BluetoothSocketIO final
|
||||
: public UnixSocketWatcher
|
||||
, protected SocketIOBase
|
||||
, protected DataSocketIO
|
||||
{
|
||||
public:
|
||||
BluetoothSocketIO(MessageLoop* mIOLoop,
|
||||
@ -34,9 +34,9 @@ public:
|
||||
const nsACString& aAddress);
|
||||
~BluetoothSocketIO();
|
||||
|
||||
void GetSocketAddr(nsAString& aAddrStr) const;
|
||||
SocketConsumerBase* GetConsumer();
|
||||
SocketBase* GetSocketBase();
|
||||
void GetSocketAddr(nsAString& aAddrStr) const;
|
||||
DataSocket* GetDataSocket();
|
||||
SocketBase* GetSocketBase();
|
||||
|
||||
// Shutdown state
|
||||
//
|
||||
@ -130,7 +130,7 @@ BluetoothSocket::BluetoothSocketIO::BluetoothSocketIO(
|
||||
UnixSocketConnector* aConnector,
|
||||
const nsACString& aAddress)
|
||||
: UnixSocketWatcher(mIOLoop)
|
||||
, SocketIOBase(MAX_READ_SIZE)
|
||||
, DataSocketIO(MAX_READ_SIZE)
|
||||
, mConsumer(aConsumer)
|
||||
, mConnector(aConnector)
|
||||
, mShuttingDownOnIOThread(false)
|
||||
@ -158,8 +158,8 @@ BluetoothSocket::BluetoothSocketIO::GetSocketAddr(nsAString& aAddrStr) const
|
||||
mConnector->GetSocketAddr(mAddr, aAddrStr);
|
||||
}
|
||||
|
||||
SocketConsumerBase*
|
||||
BluetoothSocket::BluetoothSocketIO::GetConsumer()
|
||||
DataSocket*
|
||||
BluetoothSocket::BluetoothSocketIO::GetDataSocket()
|
||||
{
|
||||
return mConsumer.get();
|
||||
}
|
||||
@ -167,7 +167,7 @@ BluetoothSocket::BluetoothSocketIO::GetConsumer()
|
||||
SocketBase*
|
||||
BluetoothSocket::BluetoothSocketIO::GetSocketBase()
|
||||
{
|
||||
return GetConsumer();
|
||||
return GetDataSocket();
|
||||
}
|
||||
|
||||
bool
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
#include "BluetoothCommon.h"
|
||||
#include <stdlib.h>
|
||||
#include "mozilla/ipc/SocketBase.h"
|
||||
#include "mozilla/ipc/DataSocket.h"
|
||||
#include "mozilla/ipc/UnixSocketWatcher.h"
|
||||
#include "mozilla/RefPtr.h"
|
||||
#include "nsAutoPtr.h"
|
||||
@ -21,7 +21,7 @@ BEGIN_BLUETOOTH_NAMESPACE
|
||||
class BluetoothSocketObserver;
|
||||
class BluetoothUnixSocketConnector;
|
||||
|
||||
class BluetoothSocket final : public mozilla::ipc::SocketConsumerBase
|
||||
class BluetoothSocket final : public mozilla::ipc::DataSocket
|
||||
{
|
||||
public:
|
||||
BluetoothSocket(BluetoothSocketObserver* aObserver,
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
#include "mozilla/ipc/DataSocket.h"
|
||||
#include "mozilla/ipc/UnixSocketWatcher.h"
|
||||
#include "nsTArray.h"
|
||||
#include "nsXULAppAPI.h"
|
||||
|
49
ipc/unixsocket/DataSocket.cpp
Normal file
49
ipc/unixsocket/DataSocket.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
/* -*- 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 "mozilla/ipc/DataSocket.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace ipc {
|
||||
|
||||
//
|
||||
// DataSocketIO
|
||||
//
|
||||
|
||||
DataSocketIO::~DataSocketIO()
|
||||
{ }
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
DataSocketIO::DataSocketIO(size_t aMaxReadSize)
|
||||
: mMaxReadSize(aMaxReadSize)
|
||||
{ }
|
||||
|
||||
//
|
||||
// DataSocket
|
||||
//
|
||||
|
||||
DataSocket::~DataSocket()
|
||||
{ }
|
||||
|
||||
}
|
||||
}
|
219
ipc/unixsocket/DataSocket.h
Normal file
219
ipc/unixsocket/DataSocket.h
Normal file
@ -0,0 +1,219 @@
|
||||
/* -*- 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/.
|
||||
*/
|
||||
|
||||
#ifndef mozilla_ipc_datasocket_h
|
||||
#define mozilla_ipc_datasocket_h
|
||||
|
||||
#include "mozilla/ipc/SocketBase.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace ipc {
|
||||
|
||||
//
|
||||
// DataSocket
|
||||
//
|
||||
|
||||
/**
|
||||
* |DataSocket| represents a socket that can send or receive data. This
|
||||
* can be a stream-based socket, a datagram-based socket, or any other
|
||||
* socket that transfers data.
|
||||
*/
|
||||
class DataSocket : public SocketBase
|
||||
{
|
||||
public:
|
||||
virtual ~DataSocket();
|
||||
|
||||
/**
|
||||
* Function to be called whenever data is received. This is only called on the
|
||||
* main thread.
|
||||
*
|
||||
* @param aBuffer Data received from the socket.
|
||||
*/
|
||||
virtual void ReceiveSocketData(nsAutoPtr<UnixSocketBuffer>& aBuffer) = 0;
|
||||
|
||||
/**
|
||||
* Queue data to be sent to the socket on the IO thread. Can only be called on
|
||||
* originating thread.
|
||||
*
|
||||
* @param aBuffer Data to be sent to socket
|
||||
*/
|
||||
virtual void SendSocketData(UnixSocketIOBuffer* aBuffer) = 0;
|
||||
};
|
||||
|
||||
//
|
||||
// Runnables
|
||||
//
|
||||
|
||||
/**
|
||||
* |SocketReceiveRunnable| transfers data received on the I/O thread
|
||||
* to an instance of |DataSocket| on the main thread.
|
||||
*/
|
||||
template <typename T>
|
||||
class SocketIOReceiveRunnable final : public SocketIORunnable<T>
|
||||
{
|
||||
public:
|
||||
SocketIOReceiveRunnable(T* aIO, UnixSocketBuffer* aBuffer)
|
||||
: SocketIORunnable<T>(aIO)
|
||||
, mBuffer(aBuffer)
|
||||
{ }
|
||||
|
||||
NS_IMETHOD Run() override
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
|
||||
T* io = SocketIORunnable<T>::GetIO();
|
||||
|
||||
if (io->IsShutdownOnMainThread()) {
|
||||
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;
|
||||
}
|
||||
|
||||
DataSocket* dataSocket = io->GetDataSocket();
|
||||
MOZ_ASSERT(dataSocket);
|
||||
|
||||
dataSocket->ReceiveSocketData(mBuffer);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
private:
|
||||
nsAutoPtr<UnixSocketBuffer> mBuffer;
|
||||
};
|
||||
|
||||
//
|
||||
// DataSocketIO
|
||||
//
|
||||
|
||||
/**
|
||||
* |DataSocketIO| is a base class for Socket I/O classes that
|
||||
* transfer data on the I/O thread. It provides methods for the
|
||||
* most common read and write scenarios.
|
||||
*/
|
||||
class DataSocketIO : public SocketIOBase
|
||||
{
|
||||
public:
|
||||
virtual ~DataSocketIO();
|
||||
|
||||
void EnqueueData(UnixSocketIOBuffer* aBuffer);
|
||||
bool HasPendingData() const;
|
||||
|
||||
template <typename T>
|
||||
ssize_t ReceiveData(int aFd, T* aIO)
|
||||
{
|
||||
MOZ_ASSERT(aFd >= 0);
|
||||
MOZ_ASSERT(aIO);
|
||||
|
||||
nsAutoPtr<UnixSocketRawData> incoming(
|
||||
new UnixSocketRawData(mMaxReadSize));
|
||||
|
||||
ssize_t res = incoming->Receive(aFd);
|
||||
if (res < 0) {
|
||||
/* an I/O error occured */
|
||||
nsRefPtr<nsRunnable> r = new SocketIORequestClosingRunnable<T>(aIO);
|
||||
NS_DispatchToMainThread(r);
|
||||
return -1;
|
||||
} else if (!res) {
|
||||
/* EOF or peer shut down sending */
|
||||
nsRefPtr<nsRunnable> r = new SocketIORequestClosingRunnable<T>(aIO);
|
||||
NS_DispatchToMainThread(r);
|
||||
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
|
||||
|
||||
nsRefPtr<nsRunnable> r =
|
||||
new SocketIOReceiveRunnable<T>(aIO, incoming.forget());
|
||||
NS_DispatchToMainThread(r);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
nsresult SendPendingData(int aFd, T* aIO)
|
||||
{
|
||||
MOZ_ASSERT(aFd >= 0);
|
||||
MOZ_ASSERT(aIO);
|
||||
|
||||
while (HasPendingData()) {
|
||||
UnixSocketIOBuffer* outgoing = mOutgoingQ.ElementAt(0);
|
||||
|
||||
ssize_t res = outgoing->Send(aFd);
|
||||
if (res < 0) {
|
||||
/* an I/O error occured */
|
||||
nsRefPtr<nsRunnable> r = new SocketIORequestClosingRunnable<T>(aIO);
|
||||
NS_DispatchToMainThread(r);
|
||||
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;
|
||||
}
|
||||
|
||||
protected:
|
||||
DataSocketIO(size_t aMaxReadSize);
|
||||
|
||||
private:
|
||||
const size_t mMaxReadSize;
|
||||
|
||||
/**
|
||||
* Raw data queue. Must be pushed/popped from I/O thread only.
|
||||
*/
|
||||
nsTArray<UnixSocketIOBuffer*> mOutgoingQ;
|
||||
};
|
||||
|
||||
//
|
||||
// Tasks
|
||||
//
|
||||
|
||||
/* |SocketIOSendTask| transfers an instance of |Tdata|, such as
|
||||
* |UnixSocketRawData|, to the I/O thread and queues it up for
|
||||
* sending the contained data.
|
||||
*/
|
||||
template<typename Tio, typename Tdata>
|
||||
class SocketIOSendTask final : public SocketIOTask<Tio>
|
||||
{
|
||||
public:
|
||||
SocketIOSendTask(Tio* aIO, Tdata* aData)
|
||||
: SocketIOTask<Tio>(aIO)
|
||||
, mData(aData)
|
||||
{
|
||||
MOZ_ASSERT(aData);
|
||||
}
|
||||
|
||||
void Run() override
|
||||
{
|
||||
MOZ_ASSERT(!NS_IsMainThread());
|
||||
MOZ_ASSERT(!SocketIOTask<Tio>::IsCanceled());
|
||||
|
||||
Tio* io = SocketIOTask<Tio>::GetIO();
|
||||
MOZ_ASSERT(!io->IsShutdownOnIOThread());
|
||||
|
||||
io->Send(mData);
|
||||
}
|
||||
|
||||
private:
|
||||
Tdata* mData;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -7,12 +7,11 @@
|
||||
#include "ListenSocket.h"
|
||||
#include <fcntl.h>
|
||||
#include "ConnectionOrientedSocket.h"
|
||||
#include "DataSocket.h"
|
||||
#include "mozilla/RefPtr.h"
|
||||
#include "nsXULAppAPI.h"
|
||||
#include "UnixSocketConnector.h"
|
||||
|
||||
static const size_t MAX_READ_SIZE = 1; /* any small constant */
|
||||
|
||||
namespace mozilla {
|
||||
namespace ipc {
|
||||
|
||||
@ -32,9 +31,9 @@ public:
|
||||
const nsACString& aAddress);
|
||||
~ListenSocketIO();
|
||||
|
||||
void GetSocketAddr(nsAString& aAddrStr) const;
|
||||
SocketConsumerBase* GetConsumer();
|
||||
SocketBase* GetSocketBase();
|
||||
void GetSocketAddr(nsAString& aAddrStr) const;
|
||||
DataSocket* GetDataSocket();
|
||||
SocketBase* GetSocketBase();
|
||||
|
||||
// Shutdown state
|
||||
//
|
||||
@ -107,13 +106,13 @@ ListenSocketIO::ListenSocketIO(MessageLoop* mIOLoop,
|
||||
ListenSocket* aListenSocket,
|
||||
UnixSocketConnector* aConnector,
|
||||
const nsACString& aAddress)
|
||||
: UnixSocketWatcher(mIOLoop)
|
||||
, SocketIOBase(MAX_READ_SIZE)
|
||||
, mListenSocket(aListenSocket)
|
||||
, mConnector(aConnector)
|
||||
, mShuttingDownOnIOThread(false)
|
||||
, mAddress(aAddress)
|
||||
, mCOSocketIO(nullptr)
|
||||
: UnixSocketWatcher(mIOLoop)
|
||||
, SocketIOBase()
|
||||
, mListenSocket(aListenSocket)
|
||||
, mConnector(aConnector)
|
||||
, mShuttingDownOnIOThread(false)
|
||||
, mAddress(aAddress)
|
||||
, mCOSocketIO(nullptr)
|
||||
{
|
||||
MOZ_ASSERT(mListenSocket);
|
||||
MOZ_ASSERT(mConnector);
|
||||
@ -136,6 +135,14 @@ ListenSocketIO::GetSocketAddr(nsAString& aAddrStr) const
|
||||
mConnector->GetSocketAddr(mAddr, aAddrStr);
|
||||
}
|
||||
|
||||
DataSocket*
|
||||
ListenSocketIO::GetDataSocket()
|
||||
{
|
||||
MOZ_CRASH("Listen sockets cannot transfer data");
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SocketBase*
|
||||
ListenSocketIO::GetSocketBase()
|
||||
{
|
||||
|
@ -268,41 +268,15 @@ SocketBase::SetConnectionStatus(SocketConnectionStatus aConnectionStatus)
|
||||
mConnectionStatus = aConnectionStatus;
|
||||
}
|
||||
|
||||
//
|
||||
// SocketConsumerBase
|
||||
//
|
||||
|
||||
SocketConsumerBase::~SocketConsumerBase()
|
||||
{ }
|
||||
|
||||
//
|
||||
// SocketIOBase
|
||||
//
|
||||
|
||||
SocketIOBase::SocketIOBase()
|
||||
{ }
|
||||
|
||||
SocketIOBase::~SocketIOBase()
|
||||
{ }
|
||||
|
||||
void
|
||||
SocketIOBase::EnqueueData(UnixSocketIOBuffer* aBuffer)
|
||||
{
|
||||
if (!aBuffer->GetSize()) {
|
||||
delete aBuffer; // delete empty data immediately
|
||||
return;
|
||||
}
|
||||
mOutgoingQ.AppendElement(aBuffer);
|
||||
}
|
||||
|
||||
bool
|
||||
SocketIOBase::HasPendingData() const
|
||||
{
|
||||
return !mOutgoingQ.IsEmpty();
|
||||
}
|
||||
|
||||
SocketIOBase::SocketIOBase(size_t aMaxReadSize)
|
||||
: mMaxReadSize(aMaxReadSize)
|
||||
{
|
||||
MOZ_ASSERT(mMaxReadSize);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -310,32 +310,6 @@ private:
|
||||
uint32_t mConnectDelayMs;
|
||||
};
|
||||
|
||||
//
|
||||
// SocketConsumerBase
|
||||
//
|
||||
|
||||
class SocketConsumerBase : public SocketBase
|
||||
{
|
||||
public:
|
||||
virtual ~SocketConsumerBase();
|
||||
|
||||
/**
|
||||
* Function to be called whenever data is received. This is only called on the
|
||||
* main thread.
|
||||
*
|
||||
* @param aBuffer Data received from the socket.
|
||||
*/
|
||||
virtual void ReceiveSocketData(nsAutoPtr<UnixSocketBuffer>& aBuffer) = 0;
|
||||
|
||||
/**
|
||||
* Queue data to be sent to the socket on the IO thread. Can only be called on
|
||||
* originating thread.
|
||||
*
|
||||
* @param aBuffer Data to be sent to socket
|
||||
*/
|
||||
virtual void SendSocketData(UnixSocketIOBuffer* aBuffer) = 0;
|
||||
};
|
||||
|
||||
//
|
||||
// Socket I/O runnables
|
||||
//
|
||||
@ -415,43 +389,6 @@ private:
|
||||
SocketEvent mEvent;
|
||||
};
|
||||
|
||||
/* |SocketReceiveRunnable| transfers data received on the I/O thread
|
||||
* to the consumer on the main thread.
|
||||
*/
|
||||
template <typename T>
|
||||
class SocketIOReceiveRunnable final : public SocketIORunnable<T>
|
||||
{
|
||||
public:
|
||||
SocketIOReceiveRunnable(T* aIO, UnixSocketBuffer* aBuffer)
|
||||
: SocketIORunnable<T>(aIO)
|
||||
, mBuffer(aBuffer)
|
||||
{ }
|
||||
|
||||
NS_IMETHOD Run() override
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
|
||||
T* io = SocketIORunnable<T>::GetIO();
|
||||
|
||||
if (io->IsShutdownOnMainThread()) {
|
||||
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;
|
||||
}
|
||||
|
||||
SocketConsumerBase* consumer = io->GetConsumer();
|
||||
MOZ_ASSERT(consumer);
|
||||
|
||||
consumer->ReceiveSocketData(mBuffer);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
private:
|
||||
nsAutoPtr<UnixSocketBuffer> mBuffer;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class SocketIORequestClosingRunnable final : public SocketIORunnable<T>
|
||||
{
|
||||
@ -507,91 +444,17 @@ private:
|
||||
// SocketIOBase
|
||||
//
|
||||
|
||||
/* |SocketIOBase| is a base class for Socket I/O classes that
|
||||
* perform operations on the I/O thread. It provides methods
|
||||
* for the most common read and write scenarios.
|
||||
/**
|
||||
* |SocketIOBase| is a base class for Socket I/O classes that
|
||||
* perform operations on the I/O thread.
|
||||
*/
|
||||
class SocketIOBase
|
||||
{
|
||||
public:
|
||||
virtual ~SocketIOBase();
|
||||
|
||||
void EnqueueData(UnixSocketIOBuffer* aBuffer);
|
||||
bool HasPendingData() const;
|
||||
|
||||
template <typename T>
|
||||
ssize_t ReceiveData(int aFd, T* aIO)
|
||||
{
|
||||
MOZ_ASSERT(aFd >= 0);
|
||||
MOZ_ASSERT(aIO);
|
||||
|
||||
nsAutoPtr<UnixSocketRawData> incoming(
|
||||
new UnixSocketRawData(mMaxReadSize));
|
||||
|
||||
ssize_t res = incoming->Receive(aFd);
|
||||
if (res < 0) {
|
||||
/* an I/O error occured */
|
||||
nsRefPtr<nsRunnable> r = new SocketIORequestClosingRunnable<T>(aIO);
|
||||
NS_DispatchToMainThread(r);
|
||||
return -1;
|
||||
} else if (!res) {
|
||||
/* EOF or peer shut down sending */
|
||||
nsRefPtr<nsRunnable> r = new SocketIORequestClosingRunnable<T>(aIO);
|
||||
NS_DispatchToMainThread(r);
|
||||
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
|
||||
|
||||
nsRefPtr<nsRunnable> r =
|
||||
new SocketIOReceiveRunnable<T>(aIO, incoming.forget());
|
||||
NS_DispatchToMainThread(r);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
nsresult SendPendingData(int aFd, T* aIO)
|
||||
{
|
||||
MOZ_ASSERT(aFd >= 0);
|
||||
MOZ_ASSERT(aIO);
|
||||
|
||||
while (HasPendingData()) {
|
||||
UnixSocketIOBuffer* outgoing = mOutgoingQ.ElementAt(0);
|
||||
|
||||
ssize_t res = outgoing->Send(aFd);
|
||||
if (res < 0) {
|
||||
/* an I/O error occured */
|
||||
nsRefPtr<nsRunnable> r = new SocketIORequestClosingRunnable<T>(aIO);
|
||||
NS_DispatchToMainThread(r);
|
||||
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;
|
||||
}
|
||||
|
||||
protected:
|
||||
SocketIOBase(size_t aMaxReadSize);
|
||||
|
||||
private:
|
||||
const size_t mMaxReadSize;
|
||||
|
||||
/**
|
||||
* Raw data queue. Must be pushed/popped from I/O thread only.
|
||||
*/
|
||||
nsTArray<UnixSocketIOBuffer*> mOutgoingQ;
|
||||
SocketIOBase();
|
||||
};
|
||||
|
||||
//
|
||||
@ -634,36 +497,6 @@ private:
|
||||
Tio* mIO;
|
||||
};
|
||||
|
||||
/* |SocketIOSendTask| transfers an instance of |Tdata|, such as
|
||||
* |UnixSocketRawData|, to the I/O thread and queues it up for
|
||||
* sending the contained data.
|
||||
*/
|
||||
template<typename Tio, typename Tdata>
|
||||
class SocketIOSendTask final : public SocketIOTask<Tio>
|
||||
{
|
||||
public:
|
||||
SocketIOSendTask(Tio* aIO, Tdata* aData)
|
||||
: SocketIOTask<Tio>(aIO)
|
||||
, mData(aData)
|
||||
{
|
||||
MOZ_ASSERT(aData);
|
||||
}
|
||||
|
||||
void Run() override
|
||||
{
|
||||
MOZ_ASSERT(!NS_IsMainThread());
|
||||
MOZ_ASSERT(!SocketIOTask<Tio>::IsCanceled());
|
||||
|
||||
Tio* io = SocketIOTask<Tio>::GetIO();
|
||||
MOZ_ASSERT(!io->IsShutdownOnIOThread());
|
||||
|
||||
io->Send(mData);
|
||||
}
|
||||
|
||||
private:
|
||||
Tdata* mData;
|
||||
};
|
||||
|
||||
/* |SocketIOShutdownTask| signals shutdown to the Socket I/O object on
|
||||
* the I/O thread and sends it to the main thread for destruction.
|
||||
*/
|
||||
|
@ -19,9 +19,10 @@ namespace ipc {
|
||||
// StreamSocketIO
|
||||
//
|
||||
|
||||
class StreamSocketIO final : public UnixSocketWatcher
|
||||
, protected SocketIOBase
|
||||
, public ConnectionOrientedSocketIO
|
||||
class StreamSocketIO final
|
||||
: public UnixSocketWatcher
|
||||
, protected DataSocketIO
|
||||
, public ConnectionOrientedSocketIO
|
||||
{
|
||||
public:
|
||||
class ConnectTask;
|
||||
@ -38,9 +39,9 @@ public:
|
||||
const nsACString& aAddress);
|
||||
~StreamSocketIO();
|
||||
|
||||
void GetSocketAddr(nsAString& aAddrStr) const;
|
||||
SocketConsumerBase* GetConsumer();
|
||||
SocketBase* GetSocketBase();
|
||||
void GetSocketAddr(nsAString& aAddrStr) const;
|
||||
DataSocket* GetDataSocket();
|
||||
SocketBase* GetSocketBase();
|
||||
|
||||
// StreamSocketIOBase
|
||||
//
|
||||
@ -133,13 +134,13 @@ StreamSocketIO::StreamSocketIO(MessageLoop* mIOLoop,
|
||||
StreamSocket* aStreamSocket,
|
||||
UnixSocketConnector* aConnector,
|
||||
const nsACString& aAddress)
|
||||
: UnixSocketWatcher(mIOLoop)
|
||||
, SocketIOBase(MAX_READ_SIZE)
|
||||
, mStreamSocket(aStreamSocket)
|
||||
, mConnector(aConnector)
|
||||
, mShuttingDownOnIOThread(false)
|
||||
, mAddress(aAddress)
|
||||
, mDelayedConnectTask(nullptr)
|
||||
: UnixSocketWatcher(mIOLoop)
|
||||
, DataSocketIO(MAX_READ_SIZE)
|
||||
, mStreamSocket(aStreamSocket)
|
||||
, mConnector(aConnector)
|
||||
, mShuttingDownOnIOThread(false)
|
||||
, mAddress(aAddress)
|
||||
, mDelayedConnectTask(nullptr)
|
||||
{
|
||||
MOZ_ASSERT(mStreamSocket);
|
||||
MOZ_ASSERT(mConnector);
|
||||
@ -150,13 +151,13 @@ StreamSocketIO::StreamSocketIO(MessageLoop* mIOLoop, int aFd,
|
||||
StreamSocket* aStreamSocket,
|
||||
UnixSocketConnector* aConnector,
|
||||
const nsACString& aAddress)
|
||||
: UnixSocketWatcher(mIOLoop, aFd, aConnectionStatus)
|
||||
, SocketIOBase(MAX_READ_SIZE)
|
||||
, mStreamSocket(aStreamSocket)
|
||||
, mConnector(aConnector)
|
||||
, mShuttingDownOnIOThread(false)
|
||||
, mAddress(aAddress)
|
||||
, mDelayedConnectTask(nullptr)
|
||||
: UnixSocketWatcher(mIOLoop, aFd, aConnectionStatus)
|
||||
, DataSocketIO(MAX_READ_SIZE)
|
||||
, mStreamSocket(aStreamSocket)
|
||||
, mConnector(aConnector)
|
||||
, mShuttingDownOnIOThread(false)
|
||||
, mAddress(aAddress)
|
||||
, mDelayedConnectTask(nullptr)
|
||||
{
|
||||
MOZ_ASSERT(mStreamSocket);
|
||||
MOZ_ASSERT(mConnector);
|
||||
@ -179,8 +180,8 @@ StreamSocketIO::GetSocketAddr(nsAString& aAddrStr) const
|
||||
mConnector->GetSocketAddr(mAddr, aAddrStr);
|
||||
}
|
||||
|
||||
SocketConsumerBase*
|
||||
StreamSocketIO::GetConsumer()
|
||||
DataSocket*
|
||||
StreamSocketIO::GetDataSocket()
|
||||
{
|
||||
return mStreamSocket.get();
|
||||
}
|
||||
@ -188,7 +189,7 @@ StreamSocketIO::GetConsumer()
|
||||
SocketBase*
|
||||
StreamSocketIO::GetSocketBase()
|
||||
{
|
||||
return GetConsumer();
|
||||
return GetDataSocket();
|
||||
}
|
||||
|
||||
bool
|
||||
|
@ -7,7 +7,7 @@
|
||||
#ifndef mozilla_ipc_streamsocket_h
|
||||
#define mozilla_ipc_streamsocket_h
|
||||
|
||||
#include "mozilla/ipc/SocketBase.h"
|
||||
#include "mozilla/ipc/DataSocket.h"
|
||||
#include "ConnectionOrientedSocket.h"
|
||||
|
||||
namespace mozilla {
|
||||
@ -16,7 +16,7 @@ namespace ipc {
|
||||
class StreamSocketIO;
|
||||
class UnixSocketConnector;
|
||||
|
||||
class StreamSocket : public SocketConsumerBase
|
||||
class StreamSocket : public DataSocket
|
||||
, public ConnectionOrientedSocket
|
||||
{
|
||||
public:
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
EXPORTS.mozilla.ipc += [
|
||||
'ConnectionOrientedSocket.h',
|
||||
'DataSocket.h',
|
||||
'ListenSocket.h',
|
||||
'SocketBase.h',
|
||||
'StreamSocket.h',
|
||||
@ -14,6 +15,7 @@ EXPORTS.mozilla.ipc += [
|
||||
|
||||
SOURCES += [
|
||||
'ConnectionOrientedSocket.cpp',
|
||||
'DataSocket.cpp',
|
||||
'ListenSocket.cpp',
|
||||
'SocketBase.cpp',
|
||||
'StreamSocket.cpp',
|
||||
|
Loading…
Reference in New Issue
Block a user