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

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "LRF_TwinConnector.h"
  2. #define LOG( X ) m_pLog->Log( "TWINC", this, X )
  3. #ifdef DEBUG
  4. #define LOG_ERROR( X ) {\
  5. char _tBfrErrMSG234[256];\
  6. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  7. m_pLog->Log( "TWINC", this, _LOG_ERROR, _tBfrErrMSG234 );\
  8. }
  9. #else
  10. #define LOG_ERROR( X ) m_pLog->Log( "TWINC", this, _LOG_ERROR, X );
  11. #endif
  12. LRF_TwinConnector::LRF_TwinConnector( config_setting_t *Settings ): basicThread(){
  13. m_pLog = basicLogCollection::getInstance();
  14. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, (CSTR)"TWIN_3DLRF_CONNECTION" );
  15. m_pProtocol = NULL;
  16. m_pPort = NULL;
  17. m_nMainSocket = -1;
  18. };
  19. LRF_TwinConnector::~LRF_TwinConnector(){
  20. if( m_pPort != NULL ) delete( m_pPort );
  21. if( m_pProtocol != NULL ) delete( m_pProtocol );
  22. };
  23. bool LRF_TwinConnector::create(){
  24. #ifdef PRU_SLAVE
  25. m_pProtocol = new protocolInternalLRF3D_SLAVE();
  26. #else
  27. m_pProtocol = new protocolInternalLRF3D_MASTER();
  28. #endif
  29. int RetCode;
  30. struct hostent *h_info;
  31. m_nMainSocket = socket( AF_INET, SOCK_STREAM, 0 );
  32. if( m_nMainSocket == -1){
  33. LOG_ERROR( "Some trouble with networking on this machine (Socket Error)!");
  34. return false;
  35. };
  36. MainSocketAddress = NULL;
  37. MainSocketAddress = ( struct sockaddr * ) malloc( sizeof( struct sockaddr_in ) );
  38. if( MainSocketAddress == NULL ){
  39. LOG_ERROR( "Some trouble with networking on this machine (Cannot Alloc Sockaddr Struct)!" );
  40. return false;
  41. };
  42. bzero( MainSocketAddress, sizeof( struct sockaddr_in ) );
  43. h_info = gethostbyname ( m_pBindAddress );
  44. if ( h_info == NULL ){
  45. LOG_ERROR( "Some trouble with networking on this machine (Cannot Get Hostname)!" );
  46. return false;
  47. };
  48. //*****************************************
  49. //* there we building our network address *
  50. //*****************************************
  51. bcopy( h_info->h_addr, &(((struct sockaddr_in *)MainSocketAddress)->sin_addr), h_info->h_length );
  52. ((struct sockaddr_in *)MainSocketAddress)->sin_family = h_info->h_addrtype;
  53. ((struct sockaddr_in *)MainSocketAddress)->sin_port = htons( m_nBindPort );
  54. RetCode = bind( m_nMainSocket , MainSocketAddress, sizeof( struct sockaddr_in ) );
  55. if ( RetCode != 0 ){
  56. LOG_ERROR( "Error Binding to Port/Address" );
  57. return false;
  58. };
  59. RetCode = listen( m_nMainSocket, 128);
  60. if ( RetCode != 0 ){
  61. LOG_ERROR( "Cannot Listen Port/Address" );
  62. return false;
  63. };
  64. U8 dontblock;
  65. dontblock = fcntl( m_nMainSocket, F_GETFL, NULL );
  66. dontblock |= O_NONBLOCK | O_NDELAY;
  67. RetCode = ioctl( m_nMainSocket, FIONBIO, (char *) &dontblock );
  68. if ( RetCode == 0 ){
  69. LOG( "Set N-Blocking Mode of Accepting Socket" );
  70. } else {
  71. LOG( "Can't set N-Blocking Mode of Accepting Socket" );
  72. };
  73. return init();
  74. };
  75. void LRF_TwinConnector::m_fThreadProc( void ){
  76. TWIN_COMMAND * tCMD = NULL;
  77. int ClientSocket = -1;
  78. struct sockaddr ClientAddress;
  79. socklen_t ClientAddressSize;
  80. m_bThreadStatus = TH_WORKING;
  81. while( m_bThreadStatus < TH_ENDED ){
  82. if( m_bCCMD == TWC_NO_COMMAND && tCMD == NULL ){
  83. // No CMD is EXEC so check new cmds
  84. CMD_PROTEC.lock();
  85. if( m_qCMD.size() > 0 ){
  86. tCMD = m_qCMD.front();
  87. m_qCMD.pop();
  88. };
  89. CMD_PROTEC.unlock();
  90. };
  91. if( m_pPort != NULL ){
  92. if( m_pPort->getConnectionStatus() == CS_DISCONNECTED ){
  93. if( tCMD!= NULL ){
  94. //reset commands on disconnect
  95. m_bCCMD = TWC_NO_COMMAND;
  96. if( tCMD->PTR == NULL ) free( tCMD->PTR );
  97. free( tCMD );
  98. tCMD = NULL;
  99. };
  100. delete( m_pPort );
  101. m_pPort = NULL;
  102. continue;
  103. };
  104. if( tCMD!= NULL ){
  105. if( m_bCCMD == TWC_NO_COMMAND ){
  106. // CMD not issued yet!
  107. switch( tCMD->CMD ){
  108. case TWC_GET_STATUS:{
  109. if( m_pProtocol->requestStatus() ){
  110. m_bCCMD = tCMD->CMD;
  111. };
  112. break;
  113. };
  114. case TWC_GET_CONFIG:{
  115. break;
  116. };
  117. case TWC_SET_CONFIG:{
  118. break;
  119. };
  120. case TWC_GET_THDRAW:{
  121. break;
  122. };
  123. case TWC_DO_REBOOT:{
  124. break;
  125. };
  126. };
  127. } else {
  128. // Waiting CMD exe status
  129. switch( m_bCCMD ){
  130. case TWC_GET_STATUS:{
  131. if( m_pProtocol->getLastPackageType() == STATUS_ACK ){
  132. m_bCCMD = TWC_NO_COMMAND;
  133. };
  134. break;
  135. };
  136. };
  137. if( m_bCCMD == TWC_NO_COMMAND && tCMD != NULL ){
  138. // The command is finished - Will fetch next command from queue
  139. if( tCMD->PTR == NULL ) free( tCMD->PTR );
  140. free( tCMD );
  141. tCMD = NULL;
  142. };
  143. };
  144. SLEEP( 5 );
  145. } else {
  146. SLEEP( 100 );
  147. };
  148. } else {
  149. // (re)Establish Connection
  150. #ifdef PRU_SLAVE
  151. m_pPort = new clientPort( );
  152. #else
  153. ClientAddressSize = (socklen_t) sizeof( struct sockaddr_in );
  154. ClientSocket = accept ( m_nMainSocket, &ClientAddress, &ClientAddressSize );
  155. if ( ClientSocket == -1 ) {
  156. switch( errno ){
  157. case EAGAIN:{
  158. SLEEP(10);
  159. continue;
  160. };
  161. //case EWOULDBLOCK: me->Log(" The socket is marked non-blocking and no connections are present to be accepted."); break;
  162. case EBADF: LOG_ERROR( "The descriptor is invalid." ); break;
  163. case ECONNABORTED: LOG_ERROR( "A connection has been aborted."); break;
  164. case EINTR: LOG_ERROR( "The system call was interrupted by a signal that was caught before a valid connection arrived."); break;
  165. case EINVAL: LOG_ERROR( "Socket is not listening for connections, or addrlen is invalid (e.g., is negative)."); break;
  166. case EMFILE: LOG_ERROR( "The per-process limit of open file descriptors has been reached."); break;
  167. case ENFILE: LOG_ERROR( "The system limit on the total number of open files has been reached."); break;
  168. case ENOTSOCK: LOG_ERROR( "The descriptor references a file, not a socket."); break;
  169. case EOPNOTSUPP: LOG_ERROR( "The referenced socket is not of type SOCK_STREAM."); break;
  170. case EFAULT: LOG_ERROR( "The addr argument is not in a writable part of the user address space."); break;
  171. case ENOBUFS:
  172. 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;
  173. case EPROTO: LOG_ERROR( "Protocol error."); break;
  174. case EPERM: LOG_ERROR( "Firewall rules forbid connection."); break;
  175. case ENOSR:
  176. case ESOCKTNOSUPPORT:
  177. case EPROTONOSUPPORT:
  178. case ETIMEDOUT: LOG_ERROR( " Unusual error in Kernel (Timeout, No Socket Support in Kernel, Unsupported Protocol)"); break;
  179. //case ERESTARTSYS: me->Log(" Unusual error by Kernel"); break;
  180. default: LOG_ERROR( " Unhandled error in Kernel"); break;
  181. };
  182. m_bThreadStatus = TH_ENDED;
  183. } else {
  184. char tstr[CQUERY];
  185. inet_ntop( AF_INET, &(((struct sockaddr_in*) &(ClientAddress ))->sin_addr ), tstr, size );
  186. };
  187. LOG( "SLAVE LRF Connection Accepted" );
  188. m_pPort = new serverPort( ClientSocket );
  189. };
  190. #endif
  191. if( m_pPort!= NULL ){
  192. if( !m_pPort->create() ){
  193. LOG_ERROR( "Connection not established!" );
  194. delete( m_pPort );
  195. m_pPort = NULL;
  196. } else {
  197. m_pPort->attachParser( m_pProtocol );
  198. };
  199. };
  200. };
  201. };
  202. };
  203. #undef LOG
  204. #undef LOG_ERROR