Bug 1158876: Move |DataSocket::ReceiveSocketData| into sub classes, r=kmachulis

|ReceiveSocketData| receives socket data on the main thread. This
is a specific detail of the current socket classes, which should not
be required by future implementations.

This patch moves the receive method and the corresponding runnable
into socket classes.
This commit is contained in:
Thomas Zimmermann 2015-04-28 10:18:13 +02:00
parent bd2114ae82
commit f348be8538
7 changed files with 167 additions and 63 deletions

View File

@ -134,11 +134,16 @@ public:
AddWatchers(WRITE_WATCHER, false);
}
DataSocket* GetDataSocket()
BluetoothSocket* GetBluetoothSocket()
{
return mConsumer.get();
}
DataSocket* GetDataSocket()
{
return GetBluetoothSocket();
}
SocketBase* GetSocketBase()
{
return GetDataSocket();
@ -159,6 +164,8 @@ public:
RefPtr<BluetoothSocket> mConsumer;
private:
class ReceiveRunnable;
/**
* libevent triggered functions that reads data from socket when available and
* guarenteed non-blocking. Only to be called on IO thread.
@ -517,11 +524,47 @@ DroidSocketImpl::QueryReceiveBuffer(
return NS_OK;
}
/**
* |ReceiveRunnable| transfers data received on the I/O thread
* to an instance of |BluetoothSocket| on the main thread.
*/
class DroidSocketImpl::ReceiveRunnable final
: public SocketIORunnable<DroidSocketImpl>
{
public:
ReceiveRunnable(DroidSocketImpl* aIO, UnixSocketBuffer* aBuffer)
: SocketIORunnable<DroidSocketImpl>(aIO)
, mBuffer(aBuffer)
{ }
NS_IMETHOD Run() override
{
MOZ_ASSERT(NS_IsMainThread());
DroidSocketImpl* io = SocketIORunnable<DroidSocketImpl>::GetIO();
if (NS_WARN_IF(io->IsShutdownOnMainThread())) {
// Since we've already explicitly closed and the close
// happened before this, this isn't really an error.
return NS_OK;
}
BluetoothSocket* bluetoothSocket = io->GetBluetoothSocket();
MOZ_ASSERT(bluetoothSocket);
bluetoothSocket->ReceiveSocketData(mBuffer);
return NS_OK;
}
private:
nsAutoPtr<UnixSocketBuffer> mBuffer;
};
void
DroidSocketImpl::ConsumeBuffer()
{
NS_DispatchToMainThread(
new SocketIOReceiveRunnable<DroidSocketImpl>(this, mBuffer.forget()));
NS_DispatchToMainThread(new ReceiveRunnable(this, mBuffer.forget()));
}
void

View File

@ -34,13 +34,19 @@ public:
void CloseSocket() override;
/**
* Method to be called whenever data is received. This is only called on the
* main thread.
*
* @param aBuffer Data received from the socket.
*/
void ReceiveSocketData(nsAutoPtr<mozilla::ipc::UnixSocketBuffer>& aBuffer);
void SendSocketData(mozilla::ipc::UnixSocketIOBuffer* aBuffer) override;
virtual void OnConnectSuccess() override;
virtual void OnConnectError() override;
virtual void OnDisconnect() override;
virtual void ReceiveSocketData(
nsAutoPtr<mozilla::ipc::UnixSocketBuffer>& aBuffer) override;
inline void GetAddress(nsAString& aDeviceAddress)
{

View File

@ -35,6 +35,8 @@ public:
~BluetoothSocketIO();
void GetSocketAddr(nsAString& aAddrStr) const;
BluetoothSocket* GetBluetoothSocket();
DataSocket* GetDataSocket();
SocketBase* GetSocketBase();
@ -88,6 +90,8 @@ public:
void DiscardBuffer();
private:
class ReceiveRunnable;
void FireSocketError();
// Set up flags on file descriptor.
@ -169,10 +173,16 @@ BluetoothSocket::BluetoothSocketIO::GetSocketAddr(nsAString& aAddrStr) const
mConnector->GetSocketAddr(mAddr, aAddrStr);
}
BluetoothSocket*
BluetoothSocket::BluetoothSocketIO::GetBluetoothSocket()
{
return mConsumer.get();
}
DataSocket*
BluetoothSocket::BluetoothSocketIO::GetDataSocket()
{
return mConsumer.get();
return GetBluetoothSocket();
}
SocketBase*
@ -500,11 +510,47 @@ BluetoothSocket::BluetoothSocketIO::QueryReceiveBuffer(
return NS_OK;
}
/**
* |ReceiveRunnable| transfers data received on the I/O thread
* to an instance of |BluetoothSocket| on the main thread.
*/
class BluetoothSocket::BluetoothSocketIO::ReceiveRunnable final
: public SocketIORunnable<BluetoothSocketIO>
{
public:
ReceiveRunnable(BluetoothSocketIO* aIO, UnixSocketBuffer* aBuffer)
: SocketIORunnable<BluetoothSocketIO>(aIO)
, mBuffer(aBuffer)
{ }
NS_IMETHOD Run() override
{
MOZ_ASSERT(NS_IsMainThread());
BluetoothSocketIO* io = SocketIORunnable<BluetoothSocketIO>::GetIO();
if (NS_WARN_IF(io->IsShutdownOnMainThread())) {
// Since we've already explicitly closed and the close
// happened before this, this isn't really an error.
return NS_OK;
}
BluetoothSocket* bluetoothSocket = io->GetBluetoothSocket();
MOZ_ASSERT(bluetoothSocket);
bluetoothSocket->ReceiveSocketData(mBuffer);
return NS_OK;
}
private:
nsAutoPtr<UnixSocketBuffer> mBuffer;
};
void
BluetoothSocket::BluetoothSocketIO::ConsumeBuffer()
{
NS_DispatchToMainThread(
new SocketIOReceiveRunnable<BluetoothSocketIO>(this, mBuffer.forget()));
NS_DispatchToMainThread(new ReceiveRunnable(this, mBuffer.forget()));
}
void

View File

@ -44,14 +44,20 @@ public:
virtual void OnConnectSuccess() override;
virtual void OnConnectError() override;
virtual void OnDisconnect() override;
virtual void ReceiveSocketData(
nsAutoPtr<mozilla::ipc::UnixSocketBuffer>& aBuffer) override;
inline void GetAddress(nsAString& aDeviceAddress)
{
GetSocketAddr(aDeviceAddress);
}
/**
* Method to be called whenever data is received. This is only called on the
* main thread.
*
* @param aBuffer Data received from the socket.
*/
void ReceiveSocketData(nsAutoPtr<mozilla::ipc::UnixSocketBuffer>& aBuffer);
/**
* Queue data to be sent to the socket on the IO thread. Can only be called on
* originating thread.

View File

@ -28,14 +28,6 @@ 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.
@ -45,48 +37,6 @@ public:
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
//

View File

@ -27,6 +27,7 @@ class StreamSocketIO final
public:
class ConnectTask;
class DelayedConnectTask;
class ReceiveRunnable;
StreamSocketIO(MessageLoop* mIOLoop,
StreamSocket* aStreamSocket,
@ -39,7 +40,9 @@ public:
const nsACString& aAddress);
~StreamSocketIO();
void GetSocketAddr(nsAString& aAddrStr) const;
void GetSocketAddr(nsAString& aAddrStr) const;
StreamSocket* GetStreamSocket();
DataSocket* GetDataSocket();
SocketBase* GetSocketBase();
@ -190,6 +193,12 @@ StreamSocketIO::GetSocketAddr(nsAString& aAddrStr) const
mConnector->GetSocketAddr(mAddr, aAddrStr);
}
StreamSocket*
StreamSocketIO::GetStreamSocket()
{
return mStreamSocket.get();
}
DataSocket*
StreamSocketIO::GetDataSocket()
{
@ -521,11 +530,47 @@ StreamSocketIO::QueryReceiveBuffer(UnixSocketIOBuffer** aBuffer)
return NS_OK;
}
/**
* |ReceiveRunnable| transfers data received on the I/O thread
* to an instance of |StreamSocket| on the main thread.
*/
class StreamSocketIO::ReceiveRunnable final
: public SocketIORunnable<StreamSocketIO>
{
public:
ReceiveRunnable(StreamSocketIO* aIO, UnixSocketBuffer* aBuffer)
: SocketIORunnable<StreamSocketIO>(aIO)
, mBuffer(aBuffer)
{ }
NS_IMETHOD Run() override
{
MOZ_ASSERT(NS_IsMainThread());
StreamSocketIO* io = SocketIORunnable<StreamSocketIO>::GetIO();
if (NS_WARN_IF(io->IsShutdownOnMainThread())) {
// Since we've already explicitly closed and the close
// happened before this, this isn't really an error.
return NS_OK;
}
StreamSocket* streamSocket = io->GetStreamSocket();
MOZ_ASSERT(streamSocket);
streamSocket->ReceiveSocketData(mBuffer);
return NS_OK;
}
private:
nsAutoPtr<UnixSocketBuffer> mBuffer;
};
void
StreamSocketIO::ConsumeBuffer()
{
NS_DispatchToMainThread(
new SocketIOReceiveRunnable<StreamSocketIO>(this, mBuffer.forget()));
NS_DispatchToMainThread(new ReceiveRunnable(this, mBuffer.forget()));
}
void

View File

@ -22,6 +22,14 @@ class StreamSocket : public DataSocket
public:
StreamSocket();
/**
* Method 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.