From fde44cba0815f626253c0d352cd0d782eec94328 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 11 Jan 2015 11:21:18 +0100 Subject: cNetwork: Implemented HostnameToIP lookups. --- tests/Network/EchoServer.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/Network/EchoServer.cpp (limited to 'tests/Network/EchoServer.cpp') diff --git a/tests/Network/EchoServer.cpp b/tests/Network/EchoServer.cpp new file mode 100644 index 000000000..def064083 --- /dev/null +++ b/tests/Network/EchoServer.cpp @@ -0,0 +1,19 @@ + +// EchoServer.cpp + +// Implements an Echo server using the LibEvent-based cNetwork API, as a test of that API + + + + + + +int main() +{ + // TODO + return 0; +} + + + + -- cgit v1.2.3 From 28e97d54684a246ce2c46197fe229cb9cb152562 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Mon, 12 Jan 2015 09:38:00 +0100 Subject: cNetwork: Implemented basic server functionality. --- tests/Network/EchoServer.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) (limited to 'tests/Network/EchoServer.cpp') diff --git a/tests/Network/EchoServer.cpp b/tests/Network/EchoServer.cpp index def064083..e6571a57f 100644 --- a/tests/Network/EchoServer.cpp +++ b/tests/Network/EchoServer.cpp @@ -3,6 +3,53 @@ // Implements an Echo server using the LibEvent-based cNetwork API, as a test of that API +#include "Globals.h" +#include +#include +#include "OSSupport/Network.h" + + + + + +class cEchoServerCallbacks: + public cNetwork::cListenCallbacks +{ + virtual void OnAccepted(cTCPLink & a_Link) override + { + LOGD("New client accepted, sending welcome message."); + // Send a welcome message to each connecting client: + a_Link.Send("Welcome to the simple echo server.\r\n"); + LOGD("Welcome message queued."); + } +}; + + + + + +/** cTCPLink callbacks that echo everything they receive back to the remote peer. */ +class cEchoLinkCallbacks: + public cTCPLink::cCallbacks +{ + virtual void OnReceivedData(cTCPLink & a_Link, const char * a_Data, size_t a_Size) override + { + // Echo the incoming data back to outgoing data: + LOGD("%p: Data received (%u bytes), echoing back.", &a_Link, static_cast(a_Size)); + a_Link.Send(a_Data, a_Size); + LOGD("Echo queued"); + } + + virtual void OnRemoteClosed(cTCPLink & a_Link) override + { + LOGD("%p: Remote has closed the connection.", &a_Link); + } + + virtual void OnError(cTCPLink & a_Link, int a_ErrorCode) override + { + LOGD("%p: Error %d in the cEchoLinkCallbacks.", &a_Link, a_ErrorCode); + } +}; @@ -10,7 +57,26 @@ int main() { - // TODO + LOGD("EchoServer: starting up"); + cServerHandlePtr Server = cNetwork::Listen(9876, std::make_shared(), std::make_shared()); + if (!Server->IsListening()) + { + LOGWARNING("Cannot listen on port 9876"); + abort(); + } + ASSERT(Server->IsListening()); + + // Wait for the user to terminate the server: + printf("Press enter to terminate the server.\n"); + AString line; + std::getline(std::cin, line); + + // Close the server and all its active connections: + LOG("Server terminating."); + Server->Close(); + ASSERT(!Server->IsListening()); + + LOG("Network test finished."); return 0; } -- cgit v1.2.3 From a2aa37bdc505c72edd5c9d41bac6e7679cea13c3 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Mon, 12 Jan 2015 10:17:11 +0100 Subject: cNetwork: Implemented link address getting. --- tests/Network/EchoServer.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests/Network/EchoServer.cpp') diff --git a/tests/Network/EchoServer.cpp b/tests/Network/EchoServer.cpp index e6571a57f..2316d1177 100644 --- a/tests/Network/EchoServer.cpp +++ b/tests/Network/EchoServer.cpp @@ -17,7 +17,7 @@ class cEchoServerCallbacks: { virtual void OnAccepted(cTCPLink & a_Link) override { - LOGD("New client accepted, sending welcome message."); + LOGD("New client accepted (%s:%p), sending welcome message.", a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort()); // Send a welcome message to each connecting client: a_Link.Send("Welcome to the simple echo server.\r\n"); LOGD("Welcome message queued."); @@ -35,19 +35,19 @@ class cEchoLinkCallbacks: virtual void OnReceivedData(cTCPLink & a_Link, const char * a_Data, size_t a_Size) override { // Echo the incoming data back to outgoing data: - LOGD("%p: Data received (%u bytes), echoing back.", &a_Link, static_cast(a_Size)); + LOGD("%p (%s:%d): Data received (%u bytes), echoing back.", &a_Link, a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort(), static_cast(a_Size)); a_Link.Send(a_Data, a_Size); LOGD("Echo queued"); } virtual void OnRemoteClosed(cTCPLink & a_Link) override { - LOGD("%p: Remote has closed the connection.", &a_Link); + LOGD("%p (%s:%d): Remote has closed the connection.", &a_Link, a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort()); } virtual void OnError(cTCPLink & a_Link, int a_ErrorCode) override { - LOGD("%p: Error %d in the cEchoLinkCallbacks.", &a_Link, a_ErrorCode); + LOGD("%p (%s:%d): Error %d in the cEchoLinkCallbacks.", &a_Link, a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort(), a_ErrorCode); } }; -- cgit v1.2.3 From 9ffca127090e98f473a3a6b686804672fa0c40b0 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Mon, 12 Jan 2015 10:54:28 +0100 Subject: cNetwork: Fixed Linux compilation. --- tests/Network/EchoServer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/Network/EchoServer.cpp') diff --git a/tests/Network/EchoServer.cpp b/tests/Network/EchoServer.cpp index 2316d1177..3d9a6228d 100644 --- a/tests/Network/EchoServer.cpp +++ b/tests/Network/EchoServer.cpp @@ -17,7 +17,7 @@ class cEchoServerCallbacks: { virtual void OnAccepted(cTCPLink & a_Link) override { - LOGD("New client accepted (%s:%p), sending welcome message.", a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort()); + LOGD("New client accepted (%s:%d), sending welcome message.", a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort()); // Send a welcome message to each connecting client: a_Link.Send("Welcome to the simple echo server.\r\n"); LOGD("Welcome message queued."); -- cgit v1.2.3 From d8ac99a0374b528caca66ad8ecb5fb36f6334e77 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Mon, 12 Jan 2015 14:58:52 +0100 Subject: cNetwork: Implemented connection shutdown and close. --- tests/Network/EchoServer.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'tests/Network/EchoServer.cpp') diff --git a/tests/Network/EchoServer.cpp b/tests/Network/EchoServer.cpp index 3d9a6228d..1e0ac023f 100644 --- a/tests/Network/EchoServer.cpp +++ b/tests/Network/EchoServer.cpp @@ -38,6 +38,16 @@ class cEchoLinkCallbacks: LOGD("%p (%s:%d): Data received (%u bytes), echoing back.", &a_Link, a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort(), static_cast(a_Size)); a_Link.Send(a_Data, a_Size); LOGD("Echo queued"); + + // Search for a Ctrl+Z, if found, drop the connection: + for (size_t i = 0; i < a_Size; i++) + { + if (a_Data[i] == '\x1a') + { + a_Link.Close(); + return; + } + } } virtual void OnRemoteClosed(cTCPLink & a_Link) override @@ -67,7 +77,7 @@ int main() ASSERT(Server->IsListening()); // Wait for the user to terminate the server: - printf("Press enter to terminate the server.\n"); + printf("Press enter to close the server.\n"); AString line; std::getline(std::cin, line); @@ -75,6 +85,10 @@ int main() LOG("Server terminating."); Server->Close(); ASSERT(!Server->IsListening()); + LOGD("Server has been closed."); + + printf("Press enter to exit test.\n"); + std::getline(std::cin, line); LOG("Network test finished."); return 0; -- cgit v1.2.3 From 7cddb6237418f2d7ec984cd0d4cbdac7140330b0 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 15 Jan 2015 21:10:14 +0100 Subject: cNetwork: Added an OnError callback for listening servers. The callback receives the error details. --- tests/Network/EchoServer.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'tests/Network/EchoServer.cpp') diff --git a/tests/Network/EchoServer.cpp b/tests/Network/EchoServer.cpp index 1e0ac023f..333c31e08 100644 --- a/tests/Network/EchoServer.cpp +++ b/tests/Network/EchoServer.cpp @@ -22,6 +22,11 @@ class cEchoServerCallbacks: a_Link.Send("Welcome to the simple echo server.\r\n"); LOGD("Welcome message queued."); } + + virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override + { + LOGWARNING("An error occured while listening for connections: %d (%s).", a_ErrorCode, a_ErrorMsg.c_str()); + } }; -- cgit v1.2.3 From 64855ed340e76779b99f37fbc866a7f5952e11db Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 20 Jan 2015 11:27:05 +0100 Subject: cNetwork: Added error message to error callbacks. --- tests/Network/EchoServer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/Network/EchoServer.cpp') diff --git a/tests/Network/EchoServer.cpp b/tests/Network/EchoServer.cpp index 333c31e08..86b517245 100644 --- a/tests/Network/EchoServer.cpp +++ b/tests/Network/EchoServer.cpp @@ -60,9 +60,9 @@ class cEchoLinkCallbacks: LOGD("%p (%s:%d): Remote has closed the connection.", &a_Link, a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort()); } - virtual void OnError(cTCPLink & a_Link, int a_ErrorCode) override + virtual void OnError(cTCPLink & a_Link, int a_ErrorCode, const AString & a_ErrorMsg) override { - LOGD("%p (%s:%d): Error %d in the cEchoLinkCallbacks.", &a_Link, a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort(), a_ErrorCode); + LOGD("%p (%s:%d): Error %d in the cEchoLinkCallbacks: %s", &a_Link, a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort(), a_ErrorCode, a_ErrorMsg.c_str()); } }; -- cgit v1.2.3 From 5b4c5cf2befebb78ff2b16955c244e79841648a7 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Wed, 21 Jan 2015 21:12:11 +0100 Subject: cNetwork: Changed listening API. The link-callbacks for each new accepted link are now received from the OnIncomingConnection listen-callback. --- tests/Network/EchoServer.cpp | 50 +++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 22 deletions(-) (limited to 'tests/Network/EchoServer.cpp') diff --git a/tests/Network/EchoServer.cpp b/tests/Network/EchoServer.cpp index 86b517245..061310c82 100644 --- a/tests/Network/EchoServer.cpp +++ b/tests/Network/EchoServer.cpp @@ -12,27 +12,6 @@ -class cEchoServerCallbacks: - public cNetwork::cListenCallbacks -{ - virtual void OnAccepted(cTCPLink & a_Link) override - { - LOGD("New client accepted (%s:%d), sending welcome message.", a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort()); - // Send a welcome message to each connecting client: - a_Link.Send("Welcome to the simple echo server.\r\n"); - LOGD("Welcome message queued."); - } - - virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override - { - LOGWARNING("An error occured while listening for connections: %d (%s).", a_ErrorCode, a_ErrorMsg.c_str()); - } -}; - - - - - /** cTCPLink callbacks that echo everything they receive back to the remote peer. */ class cEchoLinkCallbacks: public cTCPLink::cCallbacks @@ -70,10 +49,37 @@ class cEchoLinkCallbacks: +class cEchoServerCallbacks: + public cNetwork::cListenCallbacks +{ + virtual cTCPLink::cCallbacksPtr OnIncomingConnection(const AString & a_RemoteIPAddress, UInt16 a_RemotePort) + { + LOGD("New incoming connection(%s:%d).", a_RemoteIPAddress.c_str(), a_RemotePort); + return std::make_shared(); + } + + virtual void OnAccepted(cTCPLink & a_Link) override + { + LOGD("New client accepted (%s:%d), sending welcome message.", a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort()); + // Send a welcome message to each connecting client: + a_Link.Send("Welcome to the simple echo server.\r\n"); + LOGD("Welcome message queued."); + } + + virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override + { + LOGWARNING("An error occured while listening for connections: %d (%s).", a_ErrorCode, a_ErrorMsg.c_str()); + } +}; + + + + + int main() { LOGD("EchoServer: starting up"); - cServerHandlePtr Server = cNetwork::Listen(9876, std::make_shared(), std::make_shared()); + cServerHandlePtr Server = cNetwork::Listen(9876, std::make_shared()); if (!Server->IsListening()) { LOGWARNING("Cannot listen on port 9876"); -- cgit v1.2.3 From dbf7f13bd414daea5e787da2543df186dc465c34 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 22 Jan 2015 13:00:32 +0100 Subject: cNetwork: Added link creation callback. This allows the callback classes to store the link inside them and use it internally later on, mainly for sending data. --- tests/Network/EchoServer.cpp | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) (limited to 'tests/Network/EchoServer.cpp') diff --git a/tests/Network/EchoServer.cpp b/tests/Network/EchoServer.cpp index 061310c82..728db0b7c 100644 --- a/tests/Network/EchoServer.cpp +++ b/tests/Network/EchoServer.cpp @@ -16,11 +16,21 @@ class cEchoLinkCallbacks: public cTCPLink::cCallbacks { - virtual void OnReceivedData(cTCPLink & a_Link, const char * a_Data, size_t a_Size) override + // cTCPLink::cCallbacks overrides: + virtual void OnLinkCreated(cTCPLinkPtr a_Link) override { + ASSERT(m_Link == nullptr); + m_Link = a_Link; + } + + + virtual void OnReceivedData(const char * a_Data, size_t a_Size) override + { + ASSERT(m_Link != nullptr); + // Echo the incoming data back to outgoing data: - LOGD("%p (%s:%d): Data received (%u bytes), echoing back.", &a_Link, a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort(), static_cast(a_Size)); - a_Link.Send(a_Data, a_Size); + LOGD("%p (%s:%d): Data received (%u bytes), echoing back.", m_Link.get(), m_Link->GetRemoteIP().c_str(), m_Link->GetRemotePort(), static_cast(a_Size)); + m_Link->Send(a_Data, a_Size); LOGD("Echo queued"); // Search for a Ctrl+Z, if found, drop the connection: @@ -28,21 +38,33 @@ class cEchoLinkCallbacks: { if (a_Data[i] == '\x1a') { - a_Link.Close(); + m_Link->Close(); + m_Link.reset(); return; } } } - virtual void OnRemoteClosed(cTCPLink & a_Link) override + + virtual void OnRemoteClosed(void) override { - LOGD("%p (%s:%d): Remote has closed the connection.", &a_Link, a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort()); + ASSERT(m_Link != nullptr); + + LOGD("%p (%s:%d): Remote has closed the connection.", m_Link.get(), m_Link->GetRemoteIP().c_str(), m_Link->GetRemotePort()); + m_Link.reset(); } - virtual void OnError(cTCPLink & a_Link, int a_ErrorCode, const AString & a_ErrorMsg) override + + virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override { - LOGD("%p (%s:%d): Error %d in the cEchoLinkCallbacks: %s", &a_Link, a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort(), a_ErrorCode, a_ErrorMsg.c_str()); + ASSERT(m_Link != nullptr); + + LOGD("%p (%s:%d): Error %d in the cEchoLinkCallbacks: %s", m_Link.get(), m_Link->GetRemoteIP().c_str(), m_Link->GetRemotePort(), a_ErrorCode, a_ErrorMsg.c_str()); + m_Link.reset(); } + + /** The link attached to this callbacks instance. */ + cTCPLinkPtr m_Link; }; -- cgit v1.2.3