summaryrefslogtreecommitdiffstats
path: root/tests/Network/EchoServer.cpp
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2015-01-12 09:38:00 +0100
committerMattes D <github@xoft.cz>2015-01-22 20:12:46 +0100
commit28e97d54684a246ce2c46197fe229cb9cb152562 (patch)
treeb9075b7b2abf8068250dbbcaaae26cc28b04072f /tests/Network/EchoServer.cpp
parentcNetwork: Implemented IP-to-hostname lookup. (diff)
downloadcuberite-28e97d54684a246ce2c46197fe229cb9cb152562.tar
cuberite-28e97d54684a246ce2c46197fe229cb9cb152562.tar.gz
cuberite-28e97d54684a246ce2c46197fe229cb9cb152562.tar.bz2
cuberite-28e97d54684a246ce2c46197fe229cb9cb152562.tar.lz
cuberite-28e97d54684a246ce2c46197fe229cb9cb152562.tar.xz
cuberite-28e97d54684a246ce2c46197fe229cb9cb152562.tar.zst
cuberite-28e97d54684a246ce2c46197fe229cb9cb152562.zip
Diffstat (limited to 'tests/Network/EchoServer.cpp')
-rw-r--r--tests/Network/EchoServer.cpp68
1 files changed, 67 insertions, 1 deletions
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 <iostream>
+#include <string>
+#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<unsigned>(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<cEchoServerCallbacks>(), std::make_shared<cEchoLinkCallbacks>());
+ 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;
}