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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/*
RailControl - Model Railway Control Software
Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
RailControl is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
RailControl is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RailControl; see the file LICENCE. If not see
<http://www.gnu.org/licenses/>.
*/
#include <arpa/inet.h>
#include <netinet/in.h>
#include "DataModel/LocoBase.h"
#include "DataModel/LocoFunctions.h"
#include "Manager.h"
#include "Network/Select.h"
#include "Network/UdpConnection.h"
#include "Server/CS2/CS2Client.h"
#include "Server/CS2/CS2Server.h"
#include "Utils/Network.h"
using DataModel::Loco;
using DataModel::LocoBase;
using DataModel::LocoFunctionNr;
using DataModel::LocoFunctionState;
namespace Network
{
class UdpClient;
}
namespace Server { namespace CS2
{
CS2Server::CS2Server(Manager& manager)
: ControlInterface(ControlTypeCS2Server),
Network::TcpServer("0.0.0.0", CS2ReceiverPort, "CS2Server"),
logger(Logger::Logger::GetLogger("CS2Server")),
manager(manager),
lastClientID(0),
runUdp(false)
{
}
CS2Server::~CS2Server()
{
CleanUpClients();
logger->Info(Languages::TextCS2ServerStopped);
}
void CS2Server::Start()
{
StartTcpServer();
StartUdpServer();
logger->Info(Languages::TextCS2ServerStarted);
}
void CS2Server::Stop()
{
TerminateUdpServer();
TerminateTcpServer();
// stopping all clients
for (auto client : clients)
{
client->Stop();
}
}
void CS2Server::StartUdpServer()
{
runUdp = true;
// Only IPv4 is used by clients
struct sockaddr_in serverAddr4;
memset(reinterpret_cast<char*>(&serverAddr4), 0, sizeof(serverAddr4));
serverAddr4.sin_family = AF_INET;
serverAddr4.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr4.sin_port = htons(CS2ReceiverPort);
UdpSocketCreateBindListen(serverAddr4.sin_family, reinterpret_cast<struct sockaddr*>(&serverAddr4));
}
void CS2Server::TerminateUdpServer()
{
runUdp = false;
udpServerThread.join();
}
void CS2Server::UdpSocketCreateBindListen(int family, struct sockaddr* address)
{
udpServerSocket = socket(family, SOCK_DGRAM, 0);
if (udpServerSocket < 0)
{
// error = "Unable to create socket for udp server. Unable to serve clients.";
return;
}
int on = 1;
int intResult = setsockopt(udpServerSocket, SOL_SOCKET, SO_REUSEADDR, (const void*) &on, sizeof(on));
if (intResult < 0)
{
// error = "Unable to set tcp server socket option SO_REUSEADDR.";
close(udpServerSocket);
return;
}
intResult = bind(udpServerSocket, address, sizeof(struct sockaddr_in));
if (intResult < 0)
{
// error = "Unable to bind socket for udp server to port. Unable to serve clients.";
close(udpServerSocket);
return;
}
if (!runUdp)
{
close(udpServerSocket);
return;
}
udpServerThread = std::thread(&Server::CS2::CS2Server::UdpWorker, this);
}
void CS2Server::UdpWorker()
{
Utils::Utils::SetThreadName("CS2 UDP Server");
Logger::Logger* udpLogger = Logger::Logger::GetLogger("CS2 UDP Server");
fd_set set;
struct timeval tv;
struct sockaddr_storage clientAddress;
socklen_t clientAddressLength = sizeof(clientAddress);
while (runUdp)
{
// wait for data and abort on shutdown
int ret;
do
{
FD_ZERO(&set);
FD_SET(udpServerSocket, &set);
tv.tv_sec = 1;
tv.tv_usec = 0;
ret = TEMP_FAILURE_RETRY(select(FD_SETSIZE, &set, NULL, NULL, &tv));
if (!runUdp)
{
return;
}
} while (ret == 0);
if (ret < 0)
{
continue;
}
static const int CANCommandBufferLength = 13;
unsigned char buffer[CANCommandBufferLength];
memset(reinterpret_cast<char*>(&clientAddress), 0, sizeof(clientAddressLength));
ssize_t size = recvfrom(udpServerSocket, buffer, sizeof(buffer), 0, reinterpret_cast<struct sockaddr*>(&clientAddress), &clientAddressLength);
udpLogger->HexIn(buffer, size);
if (size != CANCommandBufferLength)
{
continue;
}
buffer[0] = 0x00;
buffer[1] = 0x30;
buffer[2] = 0x00;
buffer[3] = 0x00;
buffer[4] = 0x08;
buffer[5] = 0x00;
buffer[6] = 0x00;
buffer[7] = 0x00;
buffer[8] = 0x00;
buffer[9] = 0x03;
buffer[10] = 0x08;
buffer[11] = 0xff;
buffer[12] = 0xff;
sendto(udpServerSocket, buffer, sizeof(buffer), 0, reinterpret_cast<struct sockaddr*>(&clientAddress), sizeof(clientAddress));
// sockaddr_in* clientAddress4 = reinterpret_cast<struct sockaddr_in*>(&clientAddress);
// clientAddress4->sin_port = htons(CS2SenderPort);
// Network::UdpConnection udpConnection(logger, reinterpret_cast<struct sockaddr*>(clientAddress4));
// udpConnection.Bind();
// udpConnection.Send(buffer, sizeof(buffer));
// udpConnection.Terminate();
}
}
void CS2Server::Booster(__attribute__((unused)) const ControlType controlType,
const BoosterState status)
{
if (status == BoosterStateGo)
{
// for (auto client : clients)
// {
// reinterpret_cast<CS2Client*>(client)->SendPowerOn();
// }
}
else
{
// for (auto client : clients)
// {
// reinterpret_cast<CS2Client*>(client)->SendPowerOff();
// }
}
}
void CS2Server::LocoBaseSpeed(__attribute__((unused)) const ControlType controlType,
const LocoBase* locoBase,
__attribute__((unused)) const Speed speed)
{
if (nullptr == locoBase)
{
return;
}
// for (auto client : clients)
// {
// reinterpret_cast<CS2Client*>(client)->SendLocoInfo(locoBase);
// }
}
void CS2Server::LocoBaseOrientation(__attribute__((unused)) const ControlType controlType,
const LocoBase* locoBase,
__attribute__((unused)) const Orientation orientation)
{
if (nullptr == locoBase)
{
return;
}
// for (auto client : clients)
// {
// reinterpret_cast<CS2Client*>(client)->SendLocoInfo(locoBase);
// }
}
void CS2Server::LocoBaseFunction(__attribute__((unused)) const ControlType controlType,
const LocoBase* locoBase,
__attribute__((unused)) const LocoFunctionNr function,
__attribute__((unused)) const LocoFunctionState state)
{
if (!locoBase)
{
return;
}
// for (auto client : clients)
// {
// reinterpret_cast<CS2Client*>(client)->SendLocoInfo(locoBase);
// }
}
void CS2Server::AccessoryState(__attribute__((unused)) const ControlType controlType,
const DataModel::Accessory* accessory)
{
AccessoryBaseState(accessory);
}
void CS2Server::SwitchState(__attribute__((unused)) const ControlType controlType,
const DataModel::Switch* mySwitch)
{
AccessoryBaseState(mySwitch);
}
void CS2Server::SignalState(__attribute__((unused)) const ControlType controlType,
const DataModel::Signal* signal)
{
AccessoryBaseState(signal);
}
void CS2Server::AccessoryBaseState(const DataModel::AccessoryBase* accessoryBase)
{
if (!accessoryBase)
{
return;
}
// for (auto client : clients)
// {
// reinterpret_cast<CS2Client*>(client)->SendTurnoutInfo(accessoryBase);
// }
}
void CS2Server::Work(Network::TcpConnection* connection)
{
clients.push_back(new CS2Client(++lastClientID, connection, manager));
CleanUpClients();
}
void CS2Server::CleanUpClients()
{
for (auto iterator = clients.begin(); iterator != clients.end();)
{
CS2Client* client = *iterator;
if (client->IsTerminated())
{
iterator = clients.erase(iterator);
delete client;
}
else
{
++iterator;
}
}
}
}} // namespace Server::CS2
|