[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - 3 commits - net/ServerSocket.hpp net/Socket.hpp net/SslSocket.hpp wsd/LOOLWSD.cpp
Ashod Nakashian
ashod.nakashian at collabora.co.uk
Sat Mar 4 17:13:56 UTC 2017
net/ServerSocket.hpp | 12 ++++++++++-
net/Socket.hpp | 6 ++++-
net/SslSocket.hpp | 12 ++++++++---
wsd/LOOLWSD.cpp | 55 ++++++++++++++++++++++++++++++++-------------------
4 files changed, 60 insertions(+), 25 deletions(-)
New commits:
commit 9384a405fd8762afcd0004a571657e58d806c06a
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date: Sat Mar 4 12:08:02 2017 -0500
nb: ReadOrWrite -> Neither
SSL only requests what to poll for next.
So it's more accurate to rename ReadOrWrite
to Neither, since in that case SSL really
isn't blocked on either read or write.
Change-Id: I62dd4f94730d51666a7661b10a9d582d69fbf45e
diff --git a/net/SslSocket.hpp b/net/SslSocket.hpp
index 4b01398..54e7be7 100644
--- a/net/SslSocket.hpp
+++ b/net/SslSocket.hpp
@@ -24,7 +24,7 @@ public:
SslStreamSocket(const int fd, std::unique_ptr<SocketHandlerInterface> responseClient) :
StreamSocket(fd, std::move(responseClient)),
_ssl(nullptr),
- _sslWantsTo(SslWantsTo::ReadOrWrite),
+ _sslWantsTo(SslWantsTo::Neither),
_doHandshake(true)
{
LOG_DBG("SslStreamSocket ctor #" << fd);
@@ -125,7 +125,7 @@ private:
/// The possible next I/O operation that SSL want to do.
enum class SslWantsTo
{
- ReadOrWrite,
+ Neither,
Read,
Write
};
@@ -163,7 +163,7 @@ private:
if (rc > 0)
{
// Success: Reset so we can do either.
- _sslWantsTo = SslWantsTo::ReadOrWrite;
+ _sslWantsTo = SslWantsTo::Neither;
return rc;
}
commit af1dc47a158859a72cd22e09b952b828d2a264f3
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date: Sat Mar 4 12:06:23 2017 -0500
nb: log more socket activity
Change-Id: Ibcdf5abc3054691c93382c00bb8a9ecc4b882652
diff --git a/net/ServerSocket.hpp b/net/ServerSocket.hpp
index d831172..881a685 100644
--- a/net/ServerSocket.hpp
+++ b/net/ServerSocket.hpp
@@ -66,7 +66,17 @@ public:
// Accept a connection (if any) and set it to non-blocking.
// We don't care about the client's address, so ignored.
const int rc = ::accept4(getFD(), nullptr, nullptr, SOCK_NONBLOCK);
- return (rc != -1 ? _sockFactory->create(rc) : std::shared_ptr<Socket>(nullptr));
+ LOG_DBG("Accepted socket #" << rc << ", creating socket object.");
+ try
+ {
+ return (rc != -1 ? _sockFactory->create(rc) : std::shared_ptr<Socket>(nullptr));
+ }
+ catch (const std::exception& ex)
+ {
+ LOG_SYS("Failed to create client socket #" << rc << ". Error: " << ex.what());
+ }
+
+ return nullptr;
}
int getPollEvents() override
diff --git a/net/Socket.hpp b/net/Socket.hpp
index ab077dc..a601cb1 100644
--- a/net/Socket.hpp
+++ b/net/Socket.hpp
@@ -370,13 +370,17 @@ public:
_socketHandler(std::move(socketHandler)),
_closed(false)
{
- // Without a handler we make no sense.
+ LOG_DBG("StreamSocket ctor #" << fd);
+
+ // Without a handler we make no sense object.
if (!_socketHandler)
throw std::runtime_error("StreamSocket expects a valid SocketHandler instance.");
}
~StreamSocket()
{
+ LOG_DBG("StreamSocket dtor #" << getFD());
+
if (!_closed)
_socketHandler->onDisconnect();
}
diff --git a/net/SslSocket.hpp b/net/SslSocket.hpp
index ada6568..4b01398 100644
--- a/net/SslSocket.hpp
+++ b/net/SslSocket.hpp
@@ -27,6 +27,8 @@ public:
_sslWantsTo(SslWantsTo::ReadOrWrite),
_doHandshake(true)
{
+ LOG_DBG("SslStreamSocket ctor #" << fd);
+
BIO* bio = BIO_new(BIO_s_socket());
if (bio == nullptr)
{
@@ -50,6 +52,8 @@ public:
~SslStreamSocket()
{
+ LOG_DBG("SslStreamSocket dtor #" << getFD());
+
shutdown();
SSL_free(_ssl);
}
@@ -166,6 +170,7 @@ private:
// Last operation failed. Find out if SSL was trying
// to do something different that failed, or not.
const int sslError = SSL_get_error(_ssl, rc);
+ LOG_TRC("SSL error: " << sslError);
switch (sslError)
{
case SSL_ERROR_ZERO_RETURN:
@@ -198,6 +203,7 @@ private:
{
// The error is comming from BIO. Find out what happened.
const long bioError = ERR_get_error();
+ LOG_TRC("BIO error: " << bioError);
if (bioError == 0)
{
if (rc == 0)
commit abcaf06b4196b40334c3964988850b359bebcd65
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date: Thu Mar 2 22:55:52 2017 -0500
nb: search for free client port
Change-Id: I33238e83756481267c222b3cd3b9d30f4fcd3d48
diff --git a/wsd/LOOLWSD.cpp b/wsd/LOOLWSD.cpp
index 5996e79..8d9a3a4 100644
--- a/wsd/LOOLWSD.cpp
+++ b/wsd/LOOLWSD.cpp
@@ -3339,25 +3339,9 @@ public:
_documentThread.join();
}
- void start(const Poco::Net::SocketAddress& addr)
+ void start(const int port)
{
- std::shared_ptr<ServerSocket> serverSocket = std::make_shared<ServerSocket>(_documentPoll,
-#if ENABLE_SSL
- LOOLWSD::isSSLEnabled() ? std::unique_ptr<SocketFactory>{ new SslSocketFactory() } :
-#endif
- std::unique_ptr<SocketFactory>{ new PlainSocketFactory() });
-
- if (!serverSocket->bind(addr))
- {
- const std::string msg = "Failed to bind. (errno: ";
- throw std::runtime_error(msg + std::strerror(errno) + ")");
- }
-
- if (!serverSocket->listen())
- {
- const std::string msg = "Failed to listen. (errno: ";
- throw std::runtime_error(msg + std::strerror(errno) + ")");
- }
+ std::shared_ptr<ServerSocket> serverSocket = findServerPort(port);
_serverPoll.insertNewSocket(serverSocket);
@@ -3416,6 +3400,38 @@ private:
documentPoll.poll(5000);
}
}
+
+ std::shared_ptr<ServerSocket> getServerSocket(const Poco::Net::SocketAddress& addr)
+ {
+ std::shared_ptr<ServerSocket> serverSocket = std::make_shared<ServerSocket>(_documentPoll,
+#if ENABLE_SSL
+ LOOLWSD::isSSLEnabled() ? std::unique_ptr<SocketFactory>{ new SslSocketFactory() } :
+#endif
+ std::unique_ptr<SocketFactory>{ new PlainSocketFactory() });
+
+ if (serverSocket->bind(addr) &&
+ serverSocket->listen())
+ {
+ return serverSocket;
+ }
+
+ return nullptr;
+ }
+
+ std::shared_ptr<ServerSocket> findServerPort(int port)
+ {
+ LOG_INF("Trying to listen on client port " << port << ".");
+ std::shared_ptr<ServerSocket> socket = getServerSocket(SocketAddress("127.0.0.1", port));
+ while (!socket)
+ {
+ ++port;
+ LOG_INF("Client port " << (port - 1) << " is busy, trying " << port << ".");
+ socket = getServerSocket(SocketAddress("127.0.0.1", port));
+ }
+
+ LOG_INF("Listening to client connections on port " << port);
+ return socket;
+ }
};
LOOLWSDServer srv;
@@ -3559,8 +3575,7 @@ int LOOLWSD::main(const std::vector<std::string>& /*args*/)
#endif
// TODO loolnb
- SocketAddress addr("127.0.0.1", ClientPortNumber);
- srv.start(addr);
+ srv.start(ClientPortNumber);
#if ENABLE_DEBUG
time_t startTimeSpan = time(nullptr);
More information about the Libreoffice-commits
mailing list