| 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 <cstring> //memset |
| 22 | |
| 23 | #include "DataModel/ObjectIdentifier.h" |
| 24 | #include "Manager.h" |
| 25 | #include "Server/CS2/CS2Client.h" |
| 26 | #include "Utils/Utils.h" |
| 27 | |
| 28 | using DataModel::ObjectIdentifier; |
| 29 | |
| 30 | namespace Server { namespace CS2 |
| 31 | { |
| 32 | // worker is the thread that handles client requests |
| 33 | void CS2Client::Receiver() |
| 34 | { |
| 35 | Utils::Utils::SetThreadName("CS2Client # " + std::to_string(id) + " receiver"); |
| 36 | logger->Debug(Languages::TextTcpConnectionEstablished, connection->AddressAsString()); |
| 37 | ReceiverImpl(); |
| 38 | logger->Debug(Languages::TextTcpConnectionClosed, connection->AddressAsString()); |
| 39 | terminated = true; |
| 40 | } |
| 41 | |
| 42 | void CS2Client::ReceiverImpl() |
| 43 | { |
| 44 | while (run) |
| 45 | { |
| 46 | unsigned char buffer[CANCommandBufferLength]; |
| 47 | int ret = connection->ReceiveExact(buffer, sizeof(buffer), 0); |
| 48 | |
| 49 | if (!run) |
| 50 | { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | if (ret == -1) |
| 55 | { |
| 56 | if (errno != ETIMEDOUT) |
| 57 | { |
| 58 | logger->Error(Languages::TextErrorReadingData, strerror(errno)); |
| 59 | return; |
| 60 | } |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | if (ret == 0) |
| 65 | { |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | if (ret != 13) |
| 70 | { |
| 71 | logger->Error(Languages::TextErrorReadingData, strerror(errno)); |
| 72 | } |
| 73 | |
| 74 | Parse(buffer); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void CS2Client::Send(const unsigned char* buffer) |
| 79 | { |
| 80 | if (connection->Send(buffer, CANCommandBufferLength) == -1) |
| 81 | { |
| 82 | logger->Error(Languages::TextUnableToSendDataToControl); |
| 83 | } |
| 84 | } |
| 85 | }} // namespace Server::CS2 |