Example #1
0
 static void CloseConnection(tcp::acceptor &_acceptor, tcp::socket &_socket) {
   log_info(LOG_PREFIX, "disconnecting");
   if (_acceptor.is_open()) {
     _acceptor.close();
   }
   if (_socket.is_open()) {
     _socket.close();
   }
 }
		void EthernetRelayDriver::configure(std::string host, int port){

			tcp::endpoint endpoint(boost::asio::ip::address::from_string(host.c_str()), port);
			socket.connect(endpoint);

			if(socket.is_open()){
				ROS_INFO("TCP/IP socket opened.");
			}
		}
Example #3
0
inline bool TCPServerSession::Close()
{
	try
	{
		if (!mSocket.is_open()) return false;
		mSocket.shutdown(socket_base::shutdown_both);
		mSocket.close();
		return true;
	}
	catch (...)
	{
		return false;
	}
}
Example #4
0
error_code Packet::processPackets( tcp::socket& socket )
{
    error_code ec;

    // Send welcome message to client
    ec = welcomeClient( socket );
    if ( ec ) return ec;

    // Communicate with client using defined protocol
    do
    {
        ec = recv( socket );
        if ( !ec )
        {
            if ( isValidVersion() && isValidMsgType() )
            {
                if ( getMsgType() == MSG_EXIT )
                {
                    lockStream();
                    LOG_INF() << "EXIT packet received! -> " << *this << endl;
                    unlockStream();
                }
                else
                if ( getMsgType() == MSG_COMMAND )
                {
                    ec = processCommand( getMessage(), socket );

                    lockStream();
                    LOG_INF() << "COMMAND PROCESSING COMPLETED!" << endl;
                    unlockStream();
                }
            }
            else
            {
                lockStream();
                LOG_ERR() << "Invalid message format! -> " << *this << endl;
                unlockStream();
            }
        }

    } while ( !ec && socket.is_open()  &&
              getMsgType() != MSG_EXIT &&
              getMessage() != "EXIT" );

    lockStream();
    LOG_INF() << "Session ended with client!" << endl;
    unlockStream();

    return ec;
}
Example #5
0
 static inline int GetPort(const tcp::socket &socket) {
   return (socket.is_open() ? socket.local_endpoint().port() : 0);
 }