cNetwork: Changed listening API.
The link-callbacks for each new accepted link are now received from the OnIncomingConnection listen-callback.
This commit is contained in:
@@ -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<cEchoLinkCallbacks>();
|
||||
}
|
||||
|
||||
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<cEchoServerCallbacks>(), std::make_shared<cEchoLinkCallbacks>());
|
||||
cServerHandlePtr Server = cNetwork::Listen(9876, std::make_shared<cEchoServerCallbacks>());
|
||||
if (!Server->IsListening())
|
||||
{
|
||||
LOGWARNING("Cannot listen on port 9876");
|
||||
|
||||
Reference in New Issue
Block a user