| 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 <unistd.h> // close & TEMP_FAILURE_RETRY; |
| 23 | |
| 24 | #include "Network/Select.h" |
| 25 | #include "Network/TcpConnection.h" |
| 26 | #include "Utils/Utils.h" |
| 27 | |
| 28 | #ifndef MSG_NOSIGNAL |
| 29 | #define MSG_NOSIGNAL 0 |
| 30 | #endif |
| 31 | |
| 32 | namespace Network |
| 33 | { |
| 34 | void TcpConnection::Terminate() const |
| 35 | { |
| 36 | if (connected) |
| 37 | { |
| 38 | connected = false; |
| 39 | close(connectionSocket); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | int TcpConnection::Send(const unsigned char* buffer, const size_t bufferLength, const int flags) const |
| 44 | { |
| 45 | if (connectionSocket == 0 || connected == false) |
| 46 | { |
| 47 | errno = ENOTCONN; |
| 48 | return -1; |
| 49 | } |
| 50 | errno = 0; |
| 51 | fd_set set; |
| 52 | FD_ZERO(&set); |
| 53 | FD_SET(connectionSocket, &set); |
| 54 | struct timeval timeout; |
| 55 | timeout.tv_sec = 5; |
| 56 | timeout.tv_usec = 0; |
| 57 | |
| 58 | int ret = TEMP_FAILURE_RETRY(select(FD_SETSIZE, NULL, &set, NULL, &timeout)); |
| 59 | if (ret < 0) |
| 60 | { |
| 61 | return ret; |
| 62 | } |
| 63 | if (ret == 0) |
| 64 | { |
| 65 | errno = ETIMEDOUT; |
| 66 | return -1; |
| 67 | } |
| 68 | ret = send(connectionSocket, buffer, bufferLength, flags | MSG_NOSIGNAL); |
| 69 | if (ret <= 0) |
| 70 | { |
| 71 | errno = ECONNRESET; |
| 72 | Terminate(); |
| 73 | return -1; |
| 74 | } |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | int TcpConnection::Receive(unsigned char* buffer, const size_t bufferLength, const int flags) const |
| 79 | { |
| 80 | if (connectionSocket == 0 || connected == false) |
| 81 | { |
| 82 | errno = ENOTCONN; |
| 83 | return -1; |
| 84 | } |
| 85 | errno = 0; |
| 86 | fd_set set; |
| 87 | FD_ZERO(&set); |
| 88 | FD_SET(connectionSocket, &set); |
| 89 | struct timeval timeout; |
| 90 | timeout.tv_sec = 1; |
| 91 | timeout.tv_usec = 0; |
| 92 | |
| 93 | int ret = TEMP_FAILURE_RETRY(select(FD_SETSIZE, &set, NULL, NULL, &timeout)); |
| 94 | if (ret < 0) |
| 95 | { |
| 96 | return ret; |
| 97 | } |
| 98 | if (ret == 0) |
| 99 | { |
| 100 | errno = ETIMEDOUT; |
| 101 | return -1; |
| 102 | } |
| 103 | ret = recv(connectionSocket, buffer, bufferLength, flags); |
| 104 | if (ret <= 0) |
| 105 | { |
| 106 | errno = ECONNRESET; |
| 107 | Terminate(); |
| 108 | return -1; |
| 109 | } |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | bool TcpConnection::Receive(std::string& data, const size_t maxData, const int flags) |
| 114 | { |
| 115 | const size_t dataBufferSize = 1024; |
| 116 | unsigned char dataBuffer[dataBufferSize]; |
| 117 | const size_t max = maxData > dataBufferSize ? dataBufferSize : maxData; |
| 118 | ssize_t ret = Receive(dataBuffer, max, flags); |
| 119 | if (ret <= 0) |
| 120 | { |
| 121 | return false; |
| 122 | } |
| 123 | data.append(reinterpret_cast<char*>(dataBuffer), ret); |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | int TcpConnection::ReceiveExact(unsigned char* data, const size_t length, const int flags) const |
| 129 | { |
| 130 | int actualSize = 0; |
| 131 | int endSize = length; |
| 132 | while (actualSize < endSize) |
| 133 | { |
| 134 | int ret = Receive(data + actualSize, endSize - actualSize, flags); |
| 135 | if (ret <= 0) |
| 136 | { |
| 137 | return actualSize; |
| 138 | } |
| 139 | actualSize += ret; |
| 140 | } |
| 141 | return actualSize; |
| 142 | } |
| 143 | } |