Ferenc Szontágh
2024-06-27 920507bff803647c79dfce27c4c265b2caee7f8d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#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);
}