[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - net/loolnb.cpp

Ashod Nakashian ashod.nakashian at collabora.co.uk
Tue Feb 14 03:24:39 UTC 2017


 net/loolnb.cpp |   69 ++++++++++++++++++++++++++++++---------------------------
 1 file changed, 37 insertions(+), 32 deletions(-)

New commits:
commit 9bae6652d60ae636eba392dbe0c0401739fdb027
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Mon Feb 13 20:55:01 2017 -0500

    nb: refactor server code into own function
    
    Change-Id: Iba7363df7452da271fcf9afb54ad1f6177260ddd
    Reviewed-on: https://gerrit.libreoffice.org/34235
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/net/loolnb.cpp b/net/loolnb.cpp
index cd197cf..36898fa 100644
--- a/net/loolnb.cpp
+++ b/net/loolnb.cpp
@@ -465,10 +465,10 @@ private:
     std::thread _thread;
 };
 
+SocketAddress addr("127.0.0.1", PortNumber);
+
 std::shared_ptr<Socket> connectClient(const int timeoutMs)
 {
-    SocketAddress addr("127.0.0.1", PortNumber);
-
     const auto client = std::make_shared<Socket>();
     if (!client->connect(addr, timeoutMs) && errno != EINPROGRESS)
     {
@@ -481,6 +481,40 @@ std::shared_ptr<Socket> connectClient(const int timeoutMs)
     return client;
 }
 
+void server(SocketPoll<Socket>& poller)
+{
+    // Start server.
+    auto server = std::make_shared<Socket>();
+    if (!server->bind(addr))
+    {
+        const std::string msg = "Failed to bind. (errno: ";
+        throw std::runtime_error(msg + std::strerror(errno) + ")");
+    }
+
+    if (!server->listen())
+    {
+        const std::string msg = "Failed to listen. (errno: ";
+        throw std::runtime_error(msg + std::strerror(errno) + ")");
+    }
+
+    std::cout << "Listening." << std::endl;
+    for (;;)
+    {
+        if (server->pollRead(30000))
+        {
+            std::shared_ptr<Socket> clientSocket = server->accept();
+            if (!clientSocket)
+            {
+                const std::string msg = "Failed to accept. (errno: ";
+                throw std::runtime_error(msg + std::strerror(errno) + ")");
+            }
+
+            std::cout << "Accepted client #" << clientSocket->fd() << std::endl;
+            poller.insertNewSocket(clientSocket);
+        }
+    }
+}
+
 int main(int argc, const char**)
 {
     SocketAddress addr("127.0.0.1", PortNumber);
@@ -561,36 +595,7 @@ int main(int argc, const char**)
         }
     });
 
-    // Start server.
-    auto server = std::make_shared<Socket>();
-    if (!server->bind(addr))
-    {
-        const std::string msg = "Failed to bind. (errno: ";
-        throw std::runtime_error(msg + std::strerror(errno) + ")");
-    }
-
-    if (!server->listen())
-    {
-        const std::string msg = "Failed to listen. (errno: ";
-        throw std::runtime_error(msg + std::strerror(errno) + ")");
-    }
-
-    std::cout << "Listening." << std::endl;
-    for (;;)
-    {
-        if (server->pollRead(30000))
-        {
-            std::shared_ptr<Socket> clientSocket = server->accept();
-            if (!clientSocket)
-            {
-                const std::string msg = "Failed to accept. (errno: ";
-                throw std::runtime_error(msg + std::strerror(errno) + ")");
-            }
-
-            std::cout << "Accepted client #" << clientSocket->fd() << std::endl;
-            poller.insertNewSocket(clientSocket);
-        }
-    }
+    server(poller);
 
     std::cout << "Shutting down server." << std::endl;
 


More information about the Libreoffice-commits mailing list