net: add socketpair wrapper.
Useful to have a Unix socket-pair to communicate with a forked process. Signed-off-by: Michael Meeks <michael.meeks@collabora.com> Change-Id: Ic4ad1eee62b6d3b40a03bc8e59bce6e0e16efc28private/skyler/gitpod
parent
a8d97dc16d
commit
c9cac383b6
|
@ -80,6 +80,26 @@ int Socket::createSocket([[maybe_unused]] Socket::Type type)
|
|||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool StreamSocket::socketpair(std::shared_ptr<StreamSocket> &parent,
|
||||
std::shared_ptr<StreamSocket> &child)
|
||||
{
|
||||
#if MOBILEAPP
|
||||
return false;
|
||||
#else
|
||||
int pair[2];
|
||||
int rc = ::socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, pair);
|
||||
if (rc != 0)
|
||||
return false;
|
||||
|
||||
child = std::shared_ptr<StreamSocket>(new StreamSocket("save-child", pair[0], Socket::Type::Unix, true));
|
||||
parent = std::shared_ptr<StreamSocket>(new StreamSocket("save-kit-parent", pair[1], Socket::Type::Unix, true));
|
||||
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
#if ENABLE_DEBUG
|
||||
static std::atomic<long> socketErrorCount;
|
||||
|
||||
|
|
|
@ -926,11 +926,9 @@ public:
|
|||
|
||||
/// Create a StreamSocket from native FD.
|
||||
StreamSocket(std::string host, const int fd, Type type, bool /* isClient */,
|
||||
std::shared_ptr<ProtocolHandlerInterface> socketHandler,
|
||||
ReadType readType = NormalRead) :
|
||||
Socket(fd, type),
|
||||
_hostname(std::move(host)),
|
||||
_socketHandler(std::move(socketHandler)),
|
||||
_bytesSent(0),
|
||||
_bytesRecvd(0),
|
||||
_wsState(WSState::HTTP),
|
||||
|
@ -941,11 +939,6 @@ public:
|
|||
_inputProcessingEnabled(true)
|
||||
{
|
||||
LOG_TRC("StreamSocket ctor");
|
||||
|
||||
// Without a handler we make no sense object.
|
||||
if (!_socketHandler)
|
||||
throw std::runtime_error("StreamSocket " + std::to_string(fd) +
|
||||
" expects a valid SocketHandler instance.");
|
||||
}
|
||||
|
||||
~StreamSocket()
|
||||
|
@ -956,7 +949,8 @@ public:
|
|||
if (!_closed)
|
||||
{
|
||||
ASSERT_CORRECT_SOCKET_THREAD(this);
|
||||
_socketHandler->onDisconnect();
|
||||
if (_socketHandler)
|
||||
_socketHandler->onDisconnect();
|
||||
_socketHandler.reset();
|
||||
}
|
||||
|
||||
|
@ -1009,6 +1003,10 @@ public:
|
|||
return !_outBuffer.empty() || !_inBuffer.empty();
|
||||
}
|
||||
|
||||
/// Create a pair of connected stream sockets
|
||||
static bool socketpair(std::shared_ptr<StreamSocket> &parent,
|
||||
std::shared_ptr<StreamSocket> &child);
|
||||
|
||||
/// Send data to the socket peer.
|
||||
void send(const char* data, const int len, const bool doFlush = true)
|
||||
{
|
||||
|
@ -1202,10 +1200,14 @@ public:
|
|||
std::shared_ptr<ProtocolHandlerInterface> handler,
|
||||
ReadType readType = NormalRead)
|
||||
{
|
||||
ProtocolHandlerInterface* pHandler = handler.get();
|
||||
auto socket = std::make_shared<TSocket>(std::move(hostname), fd, type, isClient,
|
||||
std::move(handler), readType);
|
||||
pHandler->onConnect(socket);
|
||||
// Without a handler we make no sense object.
|
||||
if (!handler)
|
||||
throw std::runtime_error("StreamSocket " + std::to_string(fd) +
|
||||
" expects a valid SocketHandler instance.");
|
||||
|
||||
auto socket = std::make_shared<TSocket>(std::move(hostname), fd, type, isClient, readType);
|
||||
socket->setHandler(handler);
|
||||
|
||||
return socket;
|
||||
}
|
||||
|
||||
|
|
|
@ -24,9 +24,8 @@ class SslStreamSocket final : public StreamSocket
|
|||
{
|
||||
public:
|
||||
SslStreamSocket(const std::string& host, const int fd, Type type, bool isClient,
|
||||
std::shared_ptr<ProtocolHandlerInterface> responseClient,
|
||||
ReadType readType = NormalRead)
|
||||
: StreamSocket(host, fd, type, isClient, std::move(responseClient), readType)
|
||||
: StreamSocket(host, fd, type, isClient, readType)
|
||||
, _bio(nullptr)
|
||||
, _ssl(nullptr)
|
||||
, _sslWantsTo(SslWantsTo::Neither)
|
||||
|
|
Loading…
Reference in New Issue