#include "asio_service.h"
#include <iostream>
{
public:
protected:
{
std::cout <<
"Chat TCP session with Id " <<
id() <<
" connected!" << std::endl;
std::string message("Hello from TCP chat! Please send a message or '!' to disconnect the client!");
}
{
std::cout <<
"Chat TCP session with Id " <<
id() <<
" disconnected!" << std::endl;
}
void onReceived(
const void* buffer,
size_t size)
override
{
std::string message((const char*)buffer, size);
std::cout << "Incoming: " << message << std::endl;
if (message == "!")
}
void onError(
int error,
const std::string& category,
const std::string& message)
override
{
std::cout << "Chat TCP session caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
{
public:
protected:
std::shared_ptr<CppServer::Asio::TCPSession>
CreateSession(
const std::shared_ptr<CppServer::Asio::TCPServer>& server)
override
{
return std::make_shared<ChatSession>(server);
}
protected:
void onError(
int error,
const std::string& category,
const std::string& message)
override
{
std::cout << "Chat TCP server caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
int main(int argc, char** argv)
{
int port = 1111;
if (argc > 1)
port = std::atoi(argv[1]);
std::cout << "TCP server port: " << port << std::endl;
std::cout << std::endl;
auto service = std::make_shared<AsioService>();
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
auto server = std::make_shared<ChatServer>(service, port);
std::cout << "Server starting...";
server->Start();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the server or '!' to restart the server..." << std::endl;
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
if (line == "!")
{
std::cout << "Server restarting...";
server->Restart();
std::cout << "Done!" << std::endl;
continue;
}
line = "(admin) " + line;
server->Multicast(line);
}
std::cout << "Server stopping...";
server->Stop();
std::cout << "Done!" << std::endl;
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
TCPServer(const std::shared_ptr< Service > &service, int port, InternetProtocol protocol=InternetProtocol::IPv4)
Initialize TCP server with a given Asio service and port number.
virtual std::shared_ptr< TCPSession > CreateSession(const std::shared_ptr< TCPServer > &server)
Create TCP session factory method.
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
TCPSession(const std::shared_ptr< TCPServer > &server)
Initialize the session with a given server.
virtual void onConnected()
Handle session connected notification.
virtual bool SendAsync(const void *buffer, size_t size)
Send data to the client (asynchronous)
virtual void onReceived(const void *buffer, size_t size)
Handle buffer received notification.
virtual void onDisconnected()
Handle session disconnected notification.
std::shared_ptr< TCPServer > & server() noexcept
Get the server.
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
virtual bool Disconnect()
Disconnect the session.
const CppCommon::UUID & id() const noexcept
Get the session Id.