CppServer  1.0.4.0
C++ Server Library
ssl_chat_client.cpp

SSL chat client example

#include "asio_service.h"
#include "threads/thread.h"
#include <atomic>
#include <iostream>
class ChatClient : public CppServer::Asio::SSLClient
{
public:
void DisconnectAndStop()
{
_stop = true;
while (IsConnected())
CppCommon::Thread::Yield();
}
protected:
void onConnected() override
{
std::cout << "Chat SSL client connected a new session with Id " << id() << std::endl;
}
void onHandshaked() override
{
std::cout << "Chat SSL client handshaked a new session with Id " << id() << std::endl;
}
void onDisconnected() override
{
std::cout << "Chat SSL client disconnected a session with Id " << id() << std::endl;
// Wait for a while...
CppCommon::Thread::Sleep(1000);
// Try to connect again
if (!_stop)
}
void onReceived(const void* buffer, size_t size) override
{
std::cout << "Incoming: " << std::string((const char*)buffer, size) << std::endl;
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Chat SSL client caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
private:
std::atomic<bool> _stop{false};
};
int main(int argc, char** argv)
{
// SSL server address
std::string address = "127.0.0.1";
if (argc > 1)
address = argv[1];
// SSL server port
int port = 2222;
if (argc > 2)
port = std::atoi(argv[2]);
std::cout << "SSL server address: " << address << std::endl;
std::cout << "SSL server port: " << port << std::endl;
std::cout << std::endl;
// Create a new Asio service
auto service = std::make_shared<AsioService>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create and prepare a new SSL client context
auto context = std::make_shared<CppServer::Asio::SSLContext>(asio::ssl::context::tlsv13);
context->set_default_verify_paths();
context->set_root_certs();
context->set_verify_mode(asio::ssl::verify_peer | asio::ssl::verify_fail_if_no_peer_cert);
context->load_verify_file("../tools/certificates/ca.pem");
// Create a new SSL chat client
auto client = std::make_shared<ChatClient>(service, context, address, port);
// Connect the client
std::cout << "Client connecting...";
client->ConnectAsync();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the client or '!' to reconnect the client..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Reconnect the client
if (line == "!")
{
std::cout << "Client reconnecting...";
client->IsConnected() ? client->ReconnectAsync() : client->ConnectAsync();
std::cout << "Done!" << std::endl;
continue;
}
// Send the entered text to the chat server
client->SendAsync(line);
}
// Disconnect the client
std::cout << "Client disconnecting...";
client->DisconnectAndStop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
Definition: ssl_client.h:332
SSLClient(const std::shared_ptr< Service > &service, const std::shared_ptr< SSLContext > &context, const std::string &address, int port)
Initialize SSL client with a given Asio service, SSL context, server address and port number.
Definition: ssl_client.cpp:14
virtual void onHandshaked()
Handle session handshaked notification.
Definition: ssl_client.h:291
bool IsConnected() const noexcept
Is the client connected?
Definition: ssl_client.h:109
virtual bool ConnectAsync()
Connect the client (asynchronous)
Definition: ssl_client.cpp:352
virtual bool DisconnectAsync()
Disconnect the client (asynchronous)
Definition: ssl_client.h:156
virtual void onDisconnected()
Handle client disconnected notification.
Definition: ssl_client.h:293
const CppCommon::UUID & id() const noexcept
Get the client Id.
Definition: ssl_client.h:64
virtual void onReceived(const void *buffer, size_t size)
Handle buffer received notification.
Definition: ssl_client.h:303
virtual void onConnected()
Handle client connected notification.
Definition: ssl_client.h:289
SSL client definition.