2 * @brief TcpIpHelper class implementation. This class represents a Helper to easily perform tcp/ip operations.
6 * @copyright Intra2net AG
10 #include "net_helper.hpp"
11 #include "tcp_service.hpp"
18 * Default Constructor.
20 NetHelper::NetHelper()
23 IPServicePtr = IPService::Ptr(new TCPService());
31 NetHelper::NetHelper( const Logger::Ptr _log )
34 IPServicePtr = IPService::Ptr(new TCPService());
41 NetHelper::~NetHelper()
47 * Open the connection to the peer.
48 * @return 0 if all is fine, -1 on error.
50 int NetHelper::open_connection(const string& hostname, const string& port) const
54 IPServicePtr->connect(hostname,port);
56 catch ( const boost::system::system_error& boost_exception )
59 out << "NetHelper::open_connection(): " << boost_exception.what() << " Host: " << hostname << " Port: " << port;
60 Log->print_network_error(out.str());
63 catch ( const exception& e )
66 out << "NetHelper::open_connection(): " << e.what() << " Host: " << hostname << " Port: " << port;
67 Log->print_network_error(out.str());
73 out << "Unknown exception caught while trying to connect to Host: " << hostname << " Port: " << port;
74 Log->print_network_error(out.str());
83 * @param data Data to send
84 * @return 0 if all is fine, -1 on error.
86 int NetHelper::send_data(const std::string& data) const
90 IPServicePtr->write_to_socket(data);
92 catch ( const boost::system::system_error& boost_exception )
95 out << "NetHelper::send_data(): " << boost_exception.what() << " Data to be send: " << data;
96 Log->print_network_error(out.str());
104 * Receive all available data from the peer.
105 * @return The data received.
107 std::string NetHelper::receive_data() const
109 string received_data;
112 received_data = IPServicePtr->read_from_socket();
114 catch ( const boost::system::system_error& boost_exception )
117 out << "NetHelper::receive_data(): " << boost_exception.what();
118 Log->print_network_error(out.str());
121 return received_data;
126 * Close the active session.
127 * @return 0 if all is fine, -1 on error
129 int NetHelper::close_connection() const
133 IPServicePtr->close();
135 catch ( const boost::system::system_error& boost_exception )
138 out << "NetHelper::close_connection(): " << boost_exception.what();
139 Log->print_network_error(out.str());