Line data Source code
1 : /****************************************************************************/ 2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 3 : // Copyright (C) 2006-2024 German Aerospace Center (DLR) and others. 4 : // This program and the accompanying materials are made available under the 5 : // terms of the Eclipse Public License 2.0 which is available at 6 : // https://www.eclipse.org/legal/epl-2.0/ 7 : // This Source Code may also be made available under the following Secondary 8 : // Licenses when the conditions for such availability set forth in the Eclipse 9 : // Public License 2.0 are satisfied: GNU General Public License, version 2 10 : // or later which is available at 11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html 12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later 13 : /****************************************************************************/ 14 : /// @file OutputDevice_Network.cpp 15 : /// @author Michael Behrisch 16 : /// @author Daniel Krajzewicz 17 : /// @author Felix Brack 18 : /// @date 2006 19 : /// 20 : // An output device for TCP/IP Network connections 21 : /****************************************************************************/ 22 : 23 : 24 : // ========================================================================== 25 : // included modules 26 : // ========================================================================== 27 : #include <config.h> 28 : 29 : #include <thread> 30 : #include <chrono> 31 : #include <vector> 32 : #include "OutputDevice_Network.h" 33 : #include "foreign/tcpip/socket.h" 34 : #include "utils/common/ToString.h" 35 : 36 : 37 : // ========================================================================== 38 : // method definitions 39 : // ========================================================================== 40 6 : OutputDevice_Network::OutputDevice_Network(const std::string& host, 41 12 : const int port) : OutputDevice(0, host + ":" + toString(port)) { 42 6 : mySocket = new tcpip::Socket(host, port); 43 8 : for (int wait = 1; wait < 10; wait += 1) { 44 : try { 45 8 : mySocket->connect(); 46 : break; 47 2 : } catch (tcpip::SocketException& e) { 48 2 : if (wait == 9) { 49 0 : throw IOError(toString(e.what()) + " (host: " + host + ", port: " + toString(port) + ")"); 50 : } 51 2 : std::this_thread::sleep_for(std::chrono::seconds(wait)); 52 2 : } 53 : } 54 6 : } 55 : 56 : 57 12 : OutputDevice_Network::~OutputDevice_Network() { 58 6 : mySocket->close(); 59 6 : delete mySocket; 60 12 : } 61 : 62 : 63 : std::ostream& 64 4056 : OutputDevice_Network::getOStream() { 65 4056 : return myMessage; 66 : } 67 : 68 : 69 : void 70 652 : OutputDevice_Network::postWriteHook() { 71 : const std::string toSend = myMessage.str(); 72 1306 : myMessage.str(""); 73 652 : if (toSend.empty() || !mySocket->has_client_connection()) { 74 : return; 75 : } 76 : std::vector<unsigned char> msg; 77 649 : msg.insert(msg.end(), toSend.begin(), toSend.end()); 78 : try { 79 647 : mySocket->send(msg); 80 2 : } catch (const tcpip::SocketException& e) { 81 2 : mySocket->close(); 82 4 : throw IOError(e.what()); 83 2 : } 84 : } 85 : 86 : 87 : /****************************************************************************/