You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

218 lines
7.2 KiB

#include "LRF_TwinConnector.h"
#define LOG( X ) m_pLog->Log( "TWINC", this, X )
#ifdef DEBUG
#define LOG_ERROR( X ) {\
char _tBfrErrMSG234[256];\
sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
m_pLog->Log( "TWINC", this, _LOG_ERROR, _tBfrErrMSG234 );\
}
#else
#define LOG_ERROR( X ) m_pLog->Log( "TWINC", this, _LOG_ERROR, X );
#endif
LRF_TwinConnector::LRF_TwinConnector( config_setting_t *Settings ): basicThread(){
m_pLog = basicLogCollection::getInstance();
m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, (CSTR)"TWIN_3DLRF_CONNECTION" );
m_pProtocol = NULL;
m_pPort = NULL;
m_nMainSocket = -1;
};
LRF_TwinConnector::~LRF_TwinConnector(){
if( m_pPort != NULL ) delete( m_pPort );
if( m_pProtocol != NULL ) delete( m_pProtocol );
};
bool LRF_TwinConnector::create(){
#ifdef PRU_SLAVE
m_pProtocol = new protocolInternalLRF3D_SLAVE();
#else
m_pProtocol = new protocolInternalLRF3D_MASTER();
#endif
int RetCode;
struct hostent *h_info;
m_nMainSocket = socket( AF_INET, SOCK_STREAM, 0 );
if( m_nMainSocket == -1){
LOG_ERROR( "Some trouble with networking on this machine (Socket Error)!");
return false;
};
MainSocketAddress = NULL;
MainSocketAddress = ( struct sockaddr * ) malloc( sizeof( struct sockaddr_in ) );
if( MainSocketAddress == NULL ){
LOG_ERROR( "Some trouble with networking on this machine (Cannot Alloc Sockaddr Struct)!" );
return false;
};
bzero( MainSocketAddress, sizeof( struct sockaddr_in ) );
h_info = gethostbyname ( m_pBindAddress );
if ( h_info == NULL ){
LOG_ERROR( "Some trouble with networking on this machine (Cannot Get Hostname)!" );
return false;
};
//*****************************************
//* there we building our network address *
//*****************************************
bcopy( h_info->h_addr, &(((struct sockaddr_in *)MainSocketAddress)->sin_addr), h_info->h_length );
((struct sockaddr_in *)MainSocketAddress)->sin_family = h_info->h_addrtype;
((struct sockaddr_in *)MainSocketAddress)->sin_port = htons( m_nBindPort );
RetCode = bind( m_nMainSocket , MainSocketAddress, sizeof( struct sockaddr_in ) );
if ( RetCode != 0 ){
LOG_ERROR( "Error Binding to Port/Address" );
return false;
};
RetCode = listen( m_nMainSocket, 128);
if ( RetCode != 0 ){
LOG_ERROR( "Cannot Listen Port/Address" );
return false;
};
U8 dontblock;
dontblock = fcntl( m_nMainSocket, F_GETFL, NULL );
dontblock |= O_NONBLOCK | O_NDELAY;
RetCode = ioctl( m_nMainSocket, FIONBIO, (char *) &dontblock );
if ( RetCode == 0 ){
LOG( "Set N-Blocking Mode of Accepting Socket" );
} else {
LOG( "Can't set N-Blocking Mode of Accepting Socket" );
};
return init();
};
void LRF_TwinConnector::m_fThreadProc( void ){
TWIN_COMMAND * tCMD = NULL;
int ClientSocket = -1;
struct sockaddr ClientAddress;
socklen_t ClientAddressSize;
m_bThreadStatus = TH_WORKING;
while( m_bThreadStatus < TH_ENDED ){
if( m_bCCMD == TWC_NO_COMMAND && tCMD == NULL ){
// No CMD is EXEC so check new cmds
CMD_PROTEC.lock();
if( m_qCMD.size() > 0 ){
tCMD = m_qCMD.front();
m_qCMD.pop();
};
CMD_PROTEC.unlock();
};
if( m_pPort != NULL ){
if( m_pPort->getConnectionStatus() == CS_DISCONNECTED ){
if( tCMD!= NULL ){
//reset commands on disconnect
m_bCCMD = TWC_NO_COMMAND;
if( tCMD->PTR == NULL ) free( tCMD->PTR );
free( tCMD );
tCMD = NULL;
};
delete( m_pPort );
m_pPort = NULL;
continue;
};
if( tCMD!= NULL ){
if( m_bCCMD == TWC_NO_COMMAND ){
// CMD not issued yet!
switch( tCMD->CMD ){
case TWC_GET_STATUS:{
if( m_pProtocol->requestStatus() ){
m_bCCMD = tCMD->CMD;
};
break;
};
case TWC_GET_CONFIG:{
break;
};
case TWC_SET_CONFIG:{
break;
};
case TWC_GET_THDRAW:{
break;
};
case TWC_DO_REBOOT:{
break;
};
};
} else {
// Waiting CMD exe status
switch( m_bCCMD ){
case TWC_GET_STATUS:{
if( m_pProtocol->getLastPackageType() == STATUS_ACK ){
m_bCCMD = TWC_NO_COMMAND;
};
break;
};
};
if( m_bCCMD == TWC_NO_COMMAND && tCMD != NULL ){
// The command is finished - Will fetch next command from queue
if( tCMD->PTR == NULL ) free( tCMD->PTR );
free( tCMD );
tCMD = NULL;
};
};
SLEEP( 5 );
} else {
SLEEP( 100 );
};
} else {
// (re)Establish Connection
#ifdef PRU_SLAVE
m_pPort = new clientPort( );
#else
ClientAddressSize = (socklen_t) sizeof( struct sockaddr_in );
ClientSocket = accept ( m_nMainSocket, &ClientAddress, &ClientAddressSize );
if ( ClientSocket == -1 ) {
switch( errno ){
case EAGAIN:{
SLEEP(10);
continue;
};
//case EWOULDBLOCK: me->Log(" The socket is marked non-blocking and no connections are present to be accepted."); break;
case EBADF: LOG_ERROR( "The descriptor is invalid." ); break;
case ECONNABORTED: LOG_ERROR( "A connection has been aborted."); break;
case EINTR: LOG_ERROR( "The system call was interrupted by a signal that was caught before a valid connection arrived."); break;
case EINVAL: LOG_ERROR( "Socket is not listening for connections, or addrlen is invalid (e.g., is negative)."); break;
case EMFILE: LOG_ERROR( "The per-process limit of open file descriptors has been reached."); break;
case ENFILE: LOG_ERROR( "The system limit on the total number of open files has been reached."); break;
case ENOTSOCK: LOG_ERROR( "The descriptor references a file, not a socket."); break;
case EOPNOTSUPP: LOG_ERROR( "The referenced socket is not of type SOCK_STREAM."); break;
case EFAULT: LOG_ERROR( "The addr argument is not in a writable part of the user address space."); break;
case ENOBUFS:
case ENOMEM: LOG_ERROR( "Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory."); break;
case EPROTO: LOG_ERROR( "Protocol error."); break;
case EPERM: LOG_ERROR( "Firewall rules forbid connection."); break;
case ENOSR:
case ESOCKTNOSUPPORT:
case EPROTONOSUPPORT:
case ETIMEDOUT: LOG_ERROR( " Unusual error in Kernel (Timeout, No Socket Support in Kernel, Unsupported Protocol)"); break;
//case ERESTARTSYS: me->Log(" Unusual error by Kernel"); break;
default: LOG_ERROR( " Unhandled error in Kernel"); break;
};
m_bThreadStatus = TH_ENDED;
} else {
char tstr[CQUERY];
inet_ntop( AF_INET, &(((struct sockaddr_in*) &(ClientAddress ))->sin_addr ), tstr, size );
};
LOG( "SLAVE LRF Connection Accepted" );
m_pPort = new serverPort( ClientSocket );
};
#endif
if( m_pPort!= NULL ){
if( !m_pPort->create() ){
LOG_ERROR( "Connection not established!" );
delete( m_pPort );
m_pPort = NULL;
} else {
m_pPort->attachParser( m_pProtocol );
};
};
};
};
};
#undef LOG
#undef LOG_ERROR