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