#include "TcpServerPlugin.h"
|
#include <iostream>
|
|
TcpServerPlugin::TcpServerPlugin(IPC *ipc) : ipc(ipc), running(false)
|
{
|
DLOG(INFO) << "TcpServerPlugin constructor called";
|
}
|
|
TcpServerPlugin::~TcpServerPlugin()
|
{
|
DLOG(INFO) << "TcpServerPlugin destructor called";
|
stopServer();
|
}
|
|
void TcpServerPlugin::handleMessage(const Command &cmd)
|
{
|
DLOG(INFO) << "TcpServerPlugin::handleMessage called with command: " << CommandTypeToString.at(cmd.commandType);
|
}
|
|
void TcpServerPlugin::updateConfig(const nlohmann::json &config)
|
{
|
DLOG(INFO) << "TcpServerPlugin::updateConfig called";
|
this->config = config;
|
if (running)
|
{
|
stopServer();
|
}
|
startServer();
|
}
|
|
const std::string TcpServerPlugin::getPluginName()
|
{
|
return this->plugin_name;
|
}
|
|
void TcpServerPlugin::startServer()
|
{
|
DLOG(INFO) << "TcpServerPlugin::startServer called";
|
|
sockets::SocketOpt options;
|
options.m_listenAddr = config["listen_addr"].get<std::string>();
|
|
options.m_rxBufSize = 65536;
|
options.m_txBufSize = 65536;
|
if (config.contains("rxBufSize"))
|
{
|
options.m_rxBufSize = config["rxBufSize"].get<int>();
|
}
|
if (config.contains("txBufSize"))
|
{
|
options.m_txBufSize = config["txBufSize"].get<int>();
|
}
|
|
server = std::make_unique<sockets::TcpServer<TcpServerPlugin>>(*this, &options);
|
auto result = server->start(config["port"].get<uint16_t>());
|
if (!result.m_success)
|
{
|
LOG(FATAL) << "Failed to start TCP server: " << result.m_msg;
|
return;
|
}
|
|
running = true;
|
|
Command cmd;
|
cmd.commandType = CommandType::PluginRegistered;
|
cmd.payload = "TCP Server started";
|
ipc->sendMessage(cmd);
|
}
|
|
void TcpServerPlugin::stopServer()
|
{
|
DLOG(INFO) << "TcpServerPlugin::stopServer called";
|
|
if (server)
|
{
|
server->finish();
|
server.reset();
|
}
|
|
running = false;
|
|
Command cmd;
|
cmd.commandType = CommandType::PluginList;
|
cmd.payload = "TCP Server stopped";
|
ipc->sendMessage(cmd);
|
}
|
|
void TcpServerPlugin::onClientConnect(sockets::ClientHandle client)
|
{
|
Command cmd;
|
cmd.commandType = CommandType::PluginRegistered;
|
cmd.payload = "New client connected: " + std::to_string(client);
|
ipc->sendMessage(cmd);
|
DLOG(INFO) << "New client connected: " + std::to_string(client);
|
}
|
|
void TcpServerPlugin::onClientDisconnect(sockets::ClientHandle client, const sockets::SocketRet &ret)
|
{
|
Command cmd;
|
cmd.commandType = CommandType::PluginRegistered;
|
cmd.payload = "Client disconnected: " + std::to_string(client);
|
ipc->sendMessage(cmd);
|
DLOG(INFO) << "Client disconnected: " + std::to_string(client);
|
}
|
|
void TcpServerPlugin::onReceiveClientData(sockets::ClientHandle client, const char *data, size_t size)
|
{
|
std::string message(data, size);
|
|
Command cmd;
|
cmd.commandType = CommandType::PluginRegistered;
|
cmd.payload = "Message from client: " + message;
|
ipc->sendMessage(cmd);
|
|
// Echo back the message to the client
|
server->sendClientMessage(client, data, size);
|
}
|
|
extern "C" IPlugin *create(IPC *ipc)
|
{
|
return new TcpServerPlugin(ipc);
|
}
|