From 158c6762d153f1408e98c40fe9ef74926879da6b Mon Sep 17 00:00:00 2001
From: Hoop77 <p.badenhoop@gmx.de>
Date: Mon, 15 Jan 2018 14:18:26 +0100
Subject: [PATCH] added some further test cases

---
 NetworkingLib/include/ServiceServer.h | 13 +++--
 NetworkingLib/src/Socket.cpp          |  2 +-
 NetworkingLib/src/Stream.cpp          |  5 +-
 NetworkingLib/test/Main.cpp           | 36 +++++++-------
 NetworkingLib/test/Test.cpp           | 69 +++++++++++++++++++++++++++
 NetworkingLib/test/Test.h             | 13 +++++
 6 files changed, 114 insertions(+), 24 deletions(-)

diff --git a/NetworkingLib/include/ServiceServer.h b/NetworkingLib/include/ServiceServer.h
index 7c218444..9ac34e46 100644
--- a/NetworkingLib/include/ServiceServer.h
+++ b/NetworkingLib/include/ServiceServer.h
@@ -28,10 +28,11 @@ public:
     using Ptr = std::shared_ptr<Server<Service>>;
 
     using Tcp = boost::asio::ip::tcp;
-    using Endpoint = Tcp::endpoint;
     using Socket = Tcp::socket;
     using Acceptor = Tcp::acceptor;
 
+    using Endpoint = Resolver::Endpoint;
+
     using RequestReceivedHandler =
     std::function<ResponseMessage(const Endpoint & clientEndpoint,
                                   RequestMessage & requestMessage)>;
@@ -75,7 +76,7 @@ private:
     void accept(RequestReceivedHandler handler)
     {
         if (!acceptor.is_open())
-            acceptor = Acceptor(net.getIoService(), Endpoint{Tcp::v4(), bindingPort});
+            acceptor = Acceptor(net.getIoService(), Tcp::endpoint{Tcp::v4(), bindingPort});
 
         auto self = this->shared_from_this();
         auto state = std::make_shared<AsyncState>(self, handler);
@@ -91,7 +92,7 @@ private:
                     using namespace std::chrono_literals;
 
                     networking::message::asyncReceive<RequestMessage>(
-                        state->self->net, state->socket, state->buffer, 3s,
+                        state->self->net, state->socket, state->buffer, 10s,
                         [state](const auto & errorCode, auto & request)
                         {
                             // If a receive has timed out we treat it like we've never
@@ -99,10 +100,12 @@ private:
                             if (errorCode)
                                 return;
 
-                            auto response = state->requestReceivedHandler(state->socket.remote_endpoint(), request);
+                            Endpoint clientEndpoint{state->socket.remote_endpoint().address().to_string(),
+                                                    state->socket.remote_endpoint().port()};
+                            auto response = state->requestReceivedHandler(clientEndpoint, request);
 
                             networking::message::asyncSend(
-                                state->self->net, state->socket, response, 3s,
+                                state->self->net, state->socket, response, 5s,
                                 [state](const auto & errorCode)
                                 {
                                     // We cannot be sure that the message is going to be received at the other side anyway,
diff --git a/NetworkingLib/src/Socket.cpp b/NetworkingLib/src/Socket.cpp
index e4ba8eeb..64713fd2 100644
--- a/NetworkingLib/src/Socket.cpp
+++ b/NetworkingLib/src/Socket.cpp
@@ -27,7 +27,7 @@ bool isLastCharacterNull(const std::vector<char> & buffer, std::size_t numBytes)
 
 std::string stringFromBuffer(const std::vector<char> & buffer, std::size_t numBytes)
 {
-    return std::string{buffer.begin(), buffer.begin() + numBytes};
+    return std::string{buffer.begin(), buffer.begin() + numBytes - sizeof('\n')};
 }
 
 }
diff --git a/NetworkingLib/src/Stream.cpp b/NetworkingLib/src/Stream.cpp
index bcc613b3..ffbad334 100644
--- a/NetworkingLib/src/Stream.cpp
+++ b/NetworkingLib/src/Stream.cpp
@@ -20,10 +20,11 @@ boost::asio::const_buffers_1 stringToBuffer(const std::string & str)
 std::string stringFromStreambuf(boost::asio::streambuf & streambuf, std::size_t numBytes)
 {
     using boost::asio::buffers_begin;
-    if (numBytes == 0)
+    if (numBytes <= 1)
         return std::string{""};
     auto buffer = streambuf.data();
-    auto result = std::string{buffers_begin(buffer), buffers_begin(buffer) + numBytes - 1};
+    auto result = std::string{buffers_begin(buffer),
+                              buffers_begin(buffer) + numBytes - sizeof('\0')};
     streambuf.consume(numBytes);
     return result;
 }
diff --git a/NetworkingLib/test/Main.cpp b/NetworkingLib/test/Main.cpp
index 975a6707..66fb4c11 100644
--- a/NetworkingLib/test/Main.cpp
+++ b/NetworkingLib/test/Main.cpp
@@ -8,23 +8,27 @@
 
 int main(int argc, char ** argv)
 {
-//    std::cout << "\ntestService\n\n";
-//    networking::test::testServices();
-//    std::cout <<"\ntestTcpClientTimeout\n\n";
-//    networking::test::testTcpClientTimeout();
-//    std::cout <<"\ntestMultipleConnections\n\n";
-//    networking::test::testMultipleConnections();
-//    std::cout << "\ntestStoppingServiceServer\n\n";
-//    networking::test::testStoppingServiceServer();
-//    std::cout << "\ntestAsyncDatagramReceiver\n\n";
-//    networking::test::testAsyncDatagramReceiver();
-//    std::cout << "\ntestPeriodicTimer\n\n";
-//    networking::test::testPeriodicTimer();
-//    std::cout << "\ntestServiceClientAsyncCallTimeout\n\n";
-//    networking::test::testServiceClientAsyncCallTimeout();
-//    std::cout << "\ntestDatagramSenderAsyncSend\n\n";
-//    networking::test::testDatagramSenderAsyncSend();
+    std::cout << "\ntestService\n\n";
+    networking::test::testServices();
+    std::cout <<"\ntestTcpClientTimeout\n\n";
+    networking::test::testTcpClientTimeout();
+    std::cout <<"\ntestMultipleConnections\n\n";
+    networking::test::testMultipleConnections();
+    std::cout << "\ntestStoppingServiceServer\n\n";
+    networking::test::testStoppingServiceServer();
+    std::cout << "\ntestAsyncDatagramReceiver\n\n";
+    networking::test::testAsyncDatagramReceiver();
+    std::cout << "\ntestPeriodicTimer\n\n";
+    networking::test::testPeriodicTimer();
+    std::cout << "\ntestServiceClientAsyncCallTimeout\n\n";
+    networking::test::testServiceClientAsyncCallTimeout();
+    std::cout << "\ntestDatagramSenderAsyncSend\n\n";
+    networking::test::testDatagramSenderAsyncSend();
     std::cout << "\ntestResolver\n\n";
     networking::test::testResolver();
+    std::cout << "\ntestStringMessageOverDatagram\n\n";
+    networking::test::testStringMessageOverDatagram();
+    std::cout << "\ntestStringMessageOverService\n\n";
+    networking::test::testStringMessageOverService();
     return 0;
 }
\ No newline at end of file
diff --git a/NetworkingLib/test/Test.cpp b/NetworkingLib/test/Test.cpp
index c5b95f0a..ee5f58ea 100644
--- a/NetworkingLib/test/Test.cpp
+++ b/NetworkingLib/test/Test.cpp
@@ -334,6 +334,75 @@ void testResolver()
         std::cout << "SUCCESS!\n";
 }
 
+void testStringMessageOverDatagram()
+{
+    Networking net;
+
+    auto receiver = message::DatagramReceiver<std::string>::create(net, 10000);
+    auto sender = message::DatagramSender<std::string>::create(net);
+
+    std::atomic<bool> running{true};
+
+    std::thread receiverThread{
+        [receiver, &running]
+        {
+            std::string host;
+            std::uint16_t port;
+            auto message = receiver->receive(host, port, 3s);
+            std::cout << "received: host: " << host << " port: " << port << " message: " << message << "\n";
+            if (message == "Hello World!")
+                std::cout << "SUCCESS!\n";
+
+            running = false;
+        }};
+
+    sleep(1);
+
+    sender->send("Hello World!", "127.0.0.1", 10000, 3s);
+
+    while (running);
+    receiverThread.join();
+}
+
+void testStringMessageOverService()
+{
+    Networking net;
+
+    auto server = service::Server<StringService>::create(net, 10000);
+    auto client = service::Client<StringService>::create(net);
+
+    std::atomic<bool> running{true};
+
+    std::atomic<bool> failed{false};
+
+    std::thread receiverThread{
+        [server, &running, &failed]
+        {
+            server->advertiseService(
+                [&running, &failed](const auto & endpoint, auto & request) -> std::string
+                {
+                    std::cout << "Received request message: " << request << "\n";
+                    if (request != "Ping")
+                        failed = true;
+                    running = false;
+                    return std::string{"Pong"};
+                });
+        }};
+
+    sleep(1);
+
+    auto response = client->call("127.0.0.1", 10000, std::string{"Ping"}, 3s);
+    std::cout << "Received response message: " << response << "\n";
+    if (response != "Pong")
+        failed = true;
+
+    if (!failed)
+        std::cout << "SUCCESS!\n";
+
+    while (running);
+    receiverThread.join();
+}
+
 }
 }
 
diff --git a/NetworkingLib/test/Test.h b/NetworkingLib/test/Test.h
index f39fc156..d7b7e9ba 100644
--- a/NetworkingLib/test/Test.h
+++ b/NetworkingLib/test/Test.h
@@ -5,11 +5,20 @@
 #ifndef PROTOCOL_NETWORKTEST_H_H
 #define PROTOCOL_NETWORKTEST_H_H
 
+#include <string>
+
 namespace networking
 {
 namespace test
 {
 
+class StringService
+{
+public:
+    using RequestMessage = std::string;
+    using ResponseMessage = std::string;
+};
+
 void testServices();
 
 void testTcpClientTimeout();
@@ -28,6 +37,10 @@ void testDatagramSenderAsyncSend();
 
 void testResolver();
 
+void testStringMessageOverDatagram();
+
+void testStringMessageOverService();
+
 }
 }
 
-- 
GitLab