📄 connection.cpp
字号:
/*********************************************************************************** In the name of Almighty ** ** Connection.cpp : Robocup 3D Soccer Simulation Team Zigorat ** (This team was previously named Gcyrus) ** ** Date: 03/20/2007 ** Author: Mahdi Hamdarsi ** Comments: Class definitions for TDataPorter, TConnection, TRoboCupConnection ** utilities for network manipulation ** ***********************************************************************************//*! \file Connection.cpp<pre><b>File:</b> Connection.cpp<b>Project:</b> Robocup Soccer Simulation Team: Zigorat<b>Authors:</b> Mahdi Hamdarsi<b>Created:</b> 03/20/2007<b>Last Revision:</b> $ID$<b>Contents:</b> Class definitions for TDataPorter, TConnection, TRoboCupConnection utilities for network manipulation<hr size=2><h2><b>Changes</b></h2><b>Date</b> <b>Author</b> <b>Comment</b>03/20/2007 Mahdi Initial version created</pre>*/#include "Connection.h"#include "Logger.h"#include <sys/wait.h>#include <unistd.h>#include <netdb.h>#include <arpa/inet.h>#include <iostream>#define MAX_CONN_QUEUE 5 /*!< Maximum connection waiting to be listened */using namespace std;/********************************************************************//*********************** Class TDataPorter ************************//********************************************************************//*! Constructor of TDataPorter, initializes connection mode (UDP/TCP) & creates sockets to send/recv data \param conn ConnectionT connection mode: UDP/TCP \param socket_fd int pre-initialized socket descriptor */TDataPorter::TDataPorter( ConnectionT conn, int socket_fd ){ m_SocketFD = -1; setSocketFD( socket_fd, conn );}/*! Destructor of TDataPorter, closes available connections */TDataPorter::~TDataPorter(){ if( m_SocketFD != -1 ) close(m_SocketFD);}/*! This method returns connection mode: UDP/TCP \return ConnectionT connection mode */ConnectionT TDataPorter::getConnectionType() const{ return m_ConnectionType;}/*! This methos returns socket descriptor \return socket descriptor of current socket conenction */int TDataPorter::getSocketFD() const{ return m_SocketFD;}/*! This method sets the current socket descriptor to a pre-initialiazed socket holder, and sets the connection mode \param socket_fd pre-initialized socket descriptor \param conn Connection mode: TCP/UDP \return bool inidicating wheather class initialized or not */bool TDataPorter::setSocketFD( int socket_fd, ConnectionT conn ){ if( active() ) return false; m_ConnectionType = conn; m_SocketFD = socket_fd; return true;}/*! This method sends a message through socket connection which is made \param msg message or data to send \param iLength length of message to send \return int number of bytes totally sent */int TDataPorter::sendMessage( const void * msg, int iLength ) const{ if( !active() ) return -1; int iRet = -1; if( getConnectionType() == CONN_TCP ) iRet = send( m_SocketFD, msg, iLength, 0 ); else { cerr << "TDataPorter::sendMessage(TCP): attempted to send to a UDP packet via a TCP socket" << endl; return -1; } if( iRet == -1 ) perror( "TDataPorter::sendMessage" ); return iRet;}/*! This method sends a message through a UDP socket connection which is made \param msg message or data to send \param iLength length of message to send \param remote Socket descriptor of the data porter \return int number of bytes totally sent */int TDataPorter::sendMessageUDP( const void * msg, int iLength, const sockaddr_in * remote ) const{ if( !active() ) return -1; int iRet = -1; if( getConnectionType() == CONN_UDP ) iRet = sendto( m_SocketFD, msg, iLength, 0, (sockaddr*)remote, sizeof( *remote ) ); else { cerr << "TDataPorter::sendMessage(UDP): attempted to send to a TCP packet via a UDP socket" << endl; return 0; } if( iRet == -1 ) perror( "TDataPorter::sendMessage" ); return iRet;}/*! This method recieves any data which is ready in connection to read \param msg pointer to write recieved bytes to \param iMaxLength maximum length of bytes that can be stored in msg \param time_out Maximum tolerable time for data to be recieved \return int number of bytes totally recieved */int TDataPorter::recvMessage( void * msg, int iMaxLength, int time_out ) const{ if( !active() ) return -1; fd_set fds; int iReturn = waitForData( fds, time_out ); if( iReturn == -1 ) { perror( "TDataPorter::recvMessage(select)" ); return false; } else if( iReturn == 0 ) { cerr << "TDataPorter::recvMessage: Connection Timed out" << endl; return false; } if( FD_ISSET( getSocketFD(), &fds ) ) { if( getConnectionType() == CONN_TCP ) iReturn = recv( getSocketFD(), msg, iMaxLength, 0); else { sockaddr remote; socklen_t from_len = sizeof( sockaddr_in ); iReturn = recvfrom( getSocketFD(), msg, iMaxLength, 0, &remote, &from_len ); } } else return false; if( iReturn == -1 ) { perror( "TDataPorter::recvMessage" ); return false; } else if( iReturn == 0 ) return false; else ( ( (char*)msg )[ iReturn ] ) = '\0'; return iReturn;}/*! This method writes a message to its peer \param msg message to be written for peer \return int number of charactors sent */int TDataPorter::writeMessage( const char * msg ) const{ return writeMessage( (void*)(msg), strlen(msg) );}/*! This method writes a message to its peer \param msg message to be written for peer \param iLength message length \return int number of charactors sent */int TDataPorter::writeMessage( const void * msg, int iLength ) const{ if( !active() || getConnectionType() != CONN_TCP ) return -1; return ( write( getSocketFD(), msg, iLength ) );}/*! This method reads messages from a network connection \param msg buffer to write message to \param iLength Maximum length of buffer \return int number of bytes totally read */int TDataPorter::readMessage( void * msg, int iLength ) const{ fd_set fds; FD_ZERO( &fds ); FD_SET( getSocketFD(), &fds ); ((char*)msg)[0] = '\0'; if( select( getSocketFD() + 1, &fds, 0, 0, 0 ) > 0 ) read( getSocketFD(), msg, iLength ); else return false; return true;}/*! This method determines wheather this socket is active or not \return bool wheather connection is open */bool TDataPorter::active() const{ return ( m_SocketFD != -1 );}/*! This method waits for data to arrive on socket, waits until data comes or a time-out event occures \param fds Internal variable to synch \param iSecs seconds to wait until data time-out occures */int TDataPorter::waitForData( fd_set & fds, int iSecs ) const{ timeval tv; tv.tv_sec = iSecs; tv.tv_usec = 1000000; FD_ZERO( &fds ); FD_SET( this->getSocketFD(), &fds ); return ( select( getSocketFD() + 1, &fds, NULL, NULL, &tv ) >= 0 );}/*! This method opens the socket for the first time & initializes the socket properties \return bool indicating update was successful */bool TDataPorter::open(){ if( active() ) return false; int conn = ( m_ConnectionType == CONN_TCP ? SOCK_STREAM : SOCK_DGRAM ); int yes = 1; if( ( m_SocketFD = socket(PF_INET, conn, 0) ) == -1 ) { perror( "TDataPorter::socket" ); return false; } if( setsockopt( m_SocketFD, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int) ) == -1 ) { perror( "TDataPorter::setsockopt" ); return false; } return true;}/*! This method re-initializes the socket connection \return bool indicating wheather update was successful */bool TDataPorter::reOpen(){ closeAll(); open(); return true;}/*! This method closes socket connections and clears internal fields \return bool inidicating wheather socket closed normally or not*/bool TDataPorter::closeAll(){ if( !active() ) return false; close( m_SocketFD ); m_SocketFD = -1; return true;}/********************************************************************//*********************** Class TConnection ************************//********************************************************************//*! This is the constructor for TConnection class which encapsulates connection routines under linux API, like listen(), accept() and others; Initializes with connection mode: TCP/UDP \param conn Connection mode to use: TCP/IP & UDP/IP are supported \param strHost hostname to connect to \param iPort Port to connect to on remote host*/TConnection::TConnection( ConnectionT conn, const char *strHost, int iPort ){ m_SelfAddress.sin_family = AF_INET; // host byte order m_SelfAddress.sin_port = htons( 0 ); // short, network byte order m_SelfAddress.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP memset( &(m_SelfAddress.sin_zero), '\0', 8 ); // zero the rest of the struct
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -