| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Module: FGfdmSocket.cpp |
| 4 | Author: Jon S. Berndt |
| 5 | Date started: 11/08/99 |
| 6 | Purpose: Encapsulates a socket |
| 7 | Called by: FGOutput, et. al. |
| 8 | |
| 9 | ------------- Copyright (C) 1999 Jon S. Berndt ([email protected]) ------------- |
| 10 | |
| 11 | This program is free software; you can redistribute it and/or modify it under |
| 12 | the terms of the GNU Lesser General Public License as published by the Free |
| 13 | Software Foundation; either version 2 of the License, or (at your option) any |
| 14 | later version. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 19 | details. |
| 20 | |
| 21 | You should have received a copy of the GNU Lesser General Public License along |
| 22 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 23 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 24 | |
| 25 | Further information about the GNU Lesser General Public License can also be |
| 26 | found on the world wide web at http://www.gnu.org. |
| 27 | |
| 28 | FUNCTIONAL DESCRIPTION |
| 29 | -------------------------------------------------------------------------------- |
| 30 | This class excapsulates a socket for simple data writing |
| 31 | |
| 32 | HISTORY |
| 33 | -------------------------------------------------------------------------------- |
| 34 | 11/08/99 JSB Created |
| 35 | 11/08/07 HDW Added Generic Socket Send |
| 36 | |
| 37 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 38 | INCLUDES |
| 39 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 40 | |
| 41 | #if defined(_MSC_VER) || defined(__MINGW32__) |
| 42 | #include <ws2tcpip.h> |
| 43 | #elif defined(__OpenBSD__) |
| 44 | #include <sys/types.h> |
| 45 | #include <sys/socket.h> |
| 46 | #include <netdb.h> |
| 47 | #include <unistd.h> |
| 48 | #include <fcntl.h> |
| 49 | #else |
| 50 | #include <fcntl.h> |
| 51 | #include <unistd.h> |
| 52 | #endif |
| 53 | #include <iomanip> |
| 54 | #include <iostream> |
| 55 | #include <sstream> |
| 56 | #include <cstring> |
| 57 | #include "FGfdmSocket.h" |
| 58 | |
| 59 | using std::cout; |
| 60 | using std::cerr; |
| 61 | using std::endl; |
| 62 | using std::string; |
| 63 | |
| 64 | #ifndef _WIN32 |
| 65 | // On BSD/Unix, defining the flags INVALID_SOCKET and SOCKET_ERROR to -1 allows |
| 66 | // using the same syntax for Windows and BSD/Unix platforms. |
| 67 | #define INVALID_SOCKET -1 |
| 68 | #define SOCKET_ERROR -1 |
| 69 | #endif |
| 70 | |
| 71 | namespace JSBSim { |
| 72 | |
| 73 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 74 | CLASS IMPLEMENTATION |
| 75 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 76 | |
| 77 | #ifdef _WIN32 |
| 78 | static bool LoadWinSockDLL(int debug_lvl) |
| 79 | { |
| 80 | WSADATA wsaData; |
| 81 | if (WSAStartup(MAKEWORD(1, 1), &wsaData)) { |
| 82 | cerr << "Winsock DLL not initialized ..." << endl; |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | if (debug_lvl > 0) |
| 87 | cout << "Winsock DLL loaded ..." << endl; |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | #endif |
| 92 | |
| 93 | FGfdmSocket::FGfdmSocket(const string& address, int port, int protocol, int precision) |
| 94 | { |
| 95 | sckt = sckt_in = INVALID_SOCKET; |
| 96 | Protocol = (ProtocolType)protocol; |
| 97 | connected = false; |
| 98 | struct addrinfo *addr = nullptr; |
| 99 | this->precision = precision; |
| 100 | |
| 101 | #ifdef _WIN32 |
| 102 | if (!LoadWinSockDLL(debug_lvl)) return; |
| 103 | #endif |
| 104 | |
| 105 | struct addrinfo hints; |
| 106 | memset(&hints, 0, sizeof(struct addrinfo)); |
| 107 | hints.ai_family = AF_INET; |
| 108 | if (protocol == ptUDP) |
| 109 | hints.ai_socktype = SOCK_DGRAM; |
| 110 | else |
| 111 | hints.ai_socktype = SOCK_STREAM; |
| 112 | hints.ai_protocol = 0; |
| 113 | if (!is_number(address)) |
| 114 | hints.ai_flags = AI_ADDRCONFIG; |
| 115 | else |
| 116 | hints.ai_flags = AI_NUMERICHOST; |
| 117 | |
| 118 | int failure = getaddrinfo(address.c_str(), NULL, &hints, &addr); |
| 119 | if (failure || !addr) { |
| 120 | cerr << "Could not get host net address " << address; |
| 121 | |
| 122 | if (hints.ai_flags == AI_NUMERICHOST) |
| 123 | cerr << " by number..." << endl; |
| 124 | else |
| 125 | cerr << " by name..." << endl; |
| 126 | |
| 127 | cerr << gai_strerror(failure) << endl; |
| 128 | |
| 129 | freeaddrinfo(addr); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | sckt = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); |
| 134 | |
| 135 | if (debug_lvl > 0) { |
| 136 | if (protocol == ptUDP) //use udp protocol |
| 137 | cout << "Creating UDP socket on port " << port << endl; |
| 138 | else //use tcp protocol |
| 139 | cout << "Creating TCP socket on port " << port << endl; |
| 140 | } |
| 141 | |
| 142 | if (sckt != INVALID_SOCKET) { // successful |
| 143 | int len = sizeof(struct sockaddr_in); |
| 144 | memcpy(&scktName, addr->ai_addr, len); |
| 145 | scktName.sin_port = htons(port); |
| 146 | |
| 147 | if (connect(sckt, (struct sockaddr*)&scktName, len) == 0) { // successful |
| 148 | if (debug_lvl > 0) |
| 149 | cout << "Successfully connected to socket for output ..." << endl; |
| 150 | connected = true; |
| 151 | } else // unsuccessful |
| 152 | cerr << "Could not connect to socket for output ..." << endl; |
| 153 | } else // unsuccessful |
| 154 | cerr << "Could not create socket for FDM output, error = " << errno << endl; |
| 155 | |
| 156 | freeaddrinfo(addr); |
| 157 | |
| 158 | Debug(0); |
| 159 | } |
| 160 | |
| 161 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 162 | // assumes TCP or UDP socket on localhost, for inbound datagrams |
| 163 | FGfdmSocket::FGfdmSocket(int port, int protocol, int precision) |
| 164 | { |
| 165 | sckt = INVALID_SOCKET; |
| 166 | connected = false; |
| 167 | Protocol = (ProtocolType)protocol; |
| 168 | string ProtocolName; |
| 169 | this->precision = precision; |
| 170 | |
| 171 | #ifdef _WIN32 |
| 172 | if (!LoadWinSockDLL(debug_lvl)) return; |
| 173 | #endif |
| 174 | |
| 175 | if (Protocol == ptUDP) { //use udp protocol |
| 176 | ProtocolName = "UDP"; |
| 177 | sckt = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 178 | #ifdef _WIN32 |
| 179 | u_long NonBlock = 1; // True |
| 180 | ioctlsocket(sckt, FIONBIO, &NonBlock); |
| 181 | #else |
| 182 | int flags = fcntl(sckt, F_GETFL, 0); |
| 183 | fcntl(sckt, F_SETFL, flags | O_NONBLOCK); |
| 184 | #endif |
| 185 | } |
| 186 | else { |
| 187 | ProtocolName = "TCP"; |
| 188 | sckt = socket(AF_INET, SOCK_STREAM, 0); |
| 189 | } |
| 190 | |
| 191 | if (debug_lvl > 0) |
| 192 | cout << "Creating input " << ProtocolName << " socket on port " << port |
| 193 | << endl; |
| 194 | |
| 195 | if (sckt != INVALID_SOCKET) { |
| 196 | memset(&scktName, 0, sizeof(struct sockaddr_in)); |
| 197 | scktName.sin_family = AF_INET; |
| 198 | scktName.sin_port = htons(port); |
| 199 | |
| 200 | if (Protocol == ptUDP) |
| 201 | scktName.sin_addr.s_addr = htonl(INADDR_ANY); |
| 202 | |
| 203 | socklen_t len = sizeof(struct sockaddr_in); |
| 204 | if (bind(sckt, (struct sockaddr*)&scktName, len) != SOCKET_ERROR) { |
| 205 | if (debug_lvl > 0) |
| 206 | cout << "Successfully bound to " << ProtocolName |
| 207 | << " input socket on port " << port << endl << endl; |
| 208 | |
| 209 | if (Protocol == ptTCP) { |
| 210 | if (listen(sckt, 5) != SOCKET_ERROR) { // successful listen() |
| 211 | #ifdef _WIN32 |
| 212 | u_long NoBlock = 1; |
| 213 | ioctlsocket(sckt, FIONBIO, &NoBlock); |
| 214 | #else |
| 215 | int flags = fcntl(sckt, F_GETFL, 0); |
| 216 | fcntl(sckt, F_SETFL, flags | O_NONBLOCK); |
| 217 | #endif |
| 218 | sckt_in = accept(sckt, (struct sockaddr*)&scktName, &len); |
| 219 | connected = true; |
| 220 | } else |
| 221 | cerr << "Could not listen ..." << endl; |
| 222 | } else |
| 223 | connected = true; |
| 224 | } else // unsuccessful |
| 225 | cerr << "Could not bind to " << ProtocolName << " input socket, error = " |
| 226 | << errno << endl; |
| 227 | } else // unsuccessful |
| 228 | cerr << "Could not create " << ProtocolName << " socket for input, error = " |
| 229 | << errno << endl; |
| 230 | |
| 231 | Debug(0); |
| 232 | } |
| 233 | |
| 234 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 235 | |
| 236 | FGfdmSocket::~FGfdmSocket() |
| 237 | { |
| 238 | if (sckt != INVALID_SOCKET) shutdown(sckt,2); |
| 239 | if (sckt_in != INVALID_SOCKET) shutdown(sckt_in,2); |
| 240 | Debug(1); |
| 241 | } |
| 242 | |
| 243 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 244 | |
| 245 | string FGfdmSocket::Receive(void) |
| 246 | { |
| 247 | char buf[1024]; |
| 248 | socklen_t len = sizeof(struct sockaddr_in); |
| 249 | string data; // todo: should allocate this with a standard size as a |
| 250 | // class attribute and pass as a reference? |
| 251 | |
| 252 | if (sckt_in == INVALID_SOCKET && Protocol == ptTCP) { |
| 253 | sckt_in = accept(sckt, (struct sockaddr*)&scktName, &len); |
| 254 | if (sckt_in != INVALID_SOCKET) { |
| 255 | #ifdef _WIN32 |
| 256 | u_long NoBlock = 1; |
| 257 | ioctlsocket(sckt_in, FIONBIO, &NoBlock); |
| 258 | #else |
| 259 | int flags = fcntl(sckt_in, F_GETFL, 0); |
| 260 | fcntl(sckt_in, F_SETFL, flags | O_NONBLOCK); |
| 261 | #endif |
| 262 | send(sckt_in, "Connected to JSBSim server\n\rJSBSim> ", 36, 0); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (sckt_in != INVALID_SOCKET) { |
| 267 | int num_chars; |
| 268 | |
| 269 | while ((num_chars = recv(sckt_in, buf, sizeof buf, 0)) > 0) { |
| 270 | data.append(buf, num_chars); |
| 271 | } |
| 272 | |
| 273 | #ifdef _WIN32 |
| 274 | // when nothing received and the error isn't "would block" |
| 275 | // then assume that the client has closed the socket. |
| 276 | if (num_chars == 0) { |
| 277 | DWORD err = WSAGetLastError(); |
| 278 | if (err != WSAEWOULDBLOCK) { |
| 279 | cout << "Socket Closed. Back to listening" << endl; |
| 280 | closesocket(sckt_in); |
| 281 | sckt_in = INVALID_SOCKET; |
| 282 | } |
| 283 | } |
| 284 | #endif |
| 285 | } |
| 286 | |
| 287 | // this is for FGUDPInputSocket |
| 288 | if (sckt != INVALID_SOCKET && Protocol == ptUDP) { |
| 289 | struct sockaddr addr; |
| 290 | socklen_t fromlen = sizeof addr; |
| 291 | int num_chars = recvfrom(sckt, buf, sizeof buf, 0, (struct sockaddr*)&addr, &fromlen); |
| 292 | if (num_chars > 0) data.append(buf, num_chars); |
| 293 | } |
| 294 | |
| 295 | return data; |
| 296 | } |
| 297 | |
| 298 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 299 | |
| 300 | int FGfdmSocket::Reply(const string& text) |
| 301 | { |
| 302 | int num_chars_sent=0; |
| 303 | |
| 304 | if (sckt_in != INVALID_SOCKET) { |
| 305 | num_chars_sent = send(sckt_in, text.c_str(), text.size(), 0); |
| 306 | send(sckt_in, "JSBSim> ", 8, 0); |
| 307 | } else { |
| 308 | cerr << "Socket reply must be to a valid socket" << endl; |
| 309 | return -1; |
| 310 | } |
| 311 | return num_chars_sent; |
| 312 | } |
| 313 | |
| 314 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 315 | |
| 316 | void FGfdmSocket::Close(void) |
| 317 | { |
| 318 | #ifdef _WIN32 |
| 319 | closesocket(sckt_in); |
| 320 | #else |
| 321 | close(sckt_in); |
| 322 | #endif |
| 323 | } |
| 324 | |
| 325 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 326 | |
| 327 | void FGfdmSocket::Clear(void) |
| 328 | { |
| 329 | buffer.str(string()); |
| 330 | } |
| 331 | |
| 332 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 333 | |
| 334 | void FGfdmSocket::Clear(const string& s) |
| 335 | { |
| 336 | Clear(); |
| 337 | buffer << s << ' '; |
| 338 | } |
| 339 | |
| 340 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 341 | |
| 342 | void FGfdmSocket::Append(const char* item) |
| 343 | { |
| 344 | if (buffer.tellp() > 0) buffer << ','; |
| 345 | buffer << item; |
| 346 | } |
| 347 | |
| 348 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 349 | |
| 350 | void FGfdmSocket::Append(double item) |
| 351 | { |
| 352 | if (buffer.tellp() > 0) buffer << ','; |
| 353 | buffer << std::setw(12) << std::setprecision(precision) << item; |
| 354 | } |
| 355 | |
| 356 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 357 | |
| 358 | void FGfdmSocket::Append(long item) |
| 359 | { |
| 360 | if (buffer.tellp() > 0) buffer << ','; |
| 361 | buffer << std::setw(12) << item; |
| 362 | } |
| 363 | |
| 364 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 365 | |
| 366 | void FGfdmSocket::Send(void) |
| 367 | { |
| 368 | buffer << '\n'; |
| 369 | string str = buffer.str(); |
| 370 | if ((send(sckt,str.c_str(),str.size(),0)) <= 0) { |
| 371 | perror("send"); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 376 | |
| 377 | void FGfdmSocket::Send(const char *data, int length) |
| 378 | { |
| 379 | if ((send(sckt,data,length,0)) <= 0) { |
| 380 | perror("send"); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 385 | |
| 386 | void FGfdmSocket::WaitUntilReadable(void) |
| 387 | { |
| 388 | if (sckt_in == INVALID_SOCKET) |
| 389 | return; |
| 390 | |
| 391 | fd_set fds; |
| 392 | FD_ZERO(&fds); |
| 393 | FD_SET(sckt_in, &fds); |
| 394 | #ifdef _WIN32 |
| 395 | select(0, &fds, nullptr, nullptr, nullptr); |
| 396 | #else |
| 397 | select(sckt_in+1, &fds, nullptr, nullptr, nullptr); |
| 398 | #endif |
| 399 | |
| 400 | /* |
| 401 | If you want to check select return status: |
| 402 | |
| 403 | int recVal = select(sckt_in+1, &fds, nullptr, nullptr, nullptr); |
| 404 | recVal: received value |
| 405 | 0, if socket timeout |
| 406 | -1, if socket error |
| 407 | anithing else, if socket is readable |
| 408 | */ |
| 409 | } |
| 410 | |
| 411 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 412 | // The bitmasked value choices are as follows: |
| 413 | // unset: In this case (the default) JSBSim would only print |
| 414 | // out the normally expected messages, essentially echoing |
| 415 | // the config files as they are read. If the environment |
| 416 | // variable is not set, debug_lvl is set to 1 internally |
| 417 | // 0: This requests JSBSim not to output any messages |
| 418 | // whatsoever. |
| 419 | // 1: This value explicity requests the normal JSBSim |
| 420 | // startup messages |
| 421 | // 2: This value asks for a message to be printed out when |
| 422 | // a class is instantiated |
| 423 | // 4: When this value is set, a message is displayed when a |
| 424 | // FGModel object executes its Run() method |
| 425 | // 8: When this value is set, various runtime state variables |
| 426 | // are printed out periodically |
| 427 | // 16: When set various parameters are sanity checked and |
| 428 | // a message is printed out when they go out of bounds |
| 429 | |
| 430 | void FGfdmSocket::Debug(int from) |
| 431 | { |
| 432 | if (debug_lvl <= 0) return; |
| 433 | |
| 434 | if (debug_lvl & 1) { // Standard console startup message output |
| 435 | if (from == 0) { // Constructor |
| 436 | } |
| 437 | } |
| 438 | if (debug_lvl & 2 ) { // Instantiation/Destruction notification |
| 439 | if (from == 0) cout << "Instantiated: FGfdmSocket" << endl; |
| 440 | if (from == 1) cout << "Destroyed: FGfdmSocket" << endl; |
| 441 | } |
| 442 | if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects |
| 443 | } |
| 444 | if (debug_lvl & 8 ) { // Runtime state variables |
| 445 | } |
| 446 | if (debug_lvl & 16) { // Sanity checking |
| 447 | } |
| 448 | if (debug_lvl & 64) { |
| 449 | if (from == 0) { // Constructor |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | } |