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.

599 lines
19 KiB

​
​
​
​
​
​
​
​
​
6 years ago
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "serialSOL.h"
  2. #define LOG( X ) m_pLog->Log( "SOL", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "SOL", this, _LOG_ERROR, X )
  4. serialSOL::serialSOL( SOL_TYPE t ): transportSocket(){
  5. m_pLog = m_pLog = basicLogCollection::getInstance();
  6. //memset( &m_sPortSettings, 0, sizeof( SOL_PORT_SETTINGS ));
  7. //m_sPortSettings.eConnectionType = t;
  8. m_eType = t;
  9. memset( m_cDeviceAddress, 0, 64 );
  10. m_nPort = 0;
  11. m_nSpeed = 0;
  12. #ifdef __linux__
  13. m_nSocketFD = -1;
  14. memset( &m_sServerAddr, 0, sizeof( m_sServerAddr ) );
  15. memset( &m_sClientAddr, 0, sizeof( m_sClientAddr ) );
  16. #endif
  17. };
  18. serialSOL::serialSOL( SOL_PORT_SETTINGS *PortSettings ): transportSocket(){
  19. m_pLog = m_pLog = basicLogCollection::getInstance();
  20. //memset( &m_sPortSettings, 0, sizeof( SOL_PORT_SETTINGS ));
  21. //m_sPortSettings.eConnectionType = t;
  22. m_eType = PortSettings->eConnectionType;
  23. #ifdef __linux__
  24. m_nSocketFD = -1;
  25. memset( &m_sServerAddr, 0, sizeof( m_sServerAddr ) );
  26. memset( &m_sClientAddr, 0, sizeof( m_sClientAddr ) );
  27. #endif
  28. };
  29. bool serialSOL::closePort(){
  30. char tBuff[128];
  31. setConnectionStatus( CS_DISCONNECTED );
  32. if( m_bThreadAStatus != TH_PRE_INIT && m_bThreadBStatus != TH_PRE_INIT ){
  33. uninit();
  34. };
  35. #ifdef __linux__
  36. if( m_nSocketFD != -1 ){
  37. #else
  38. if( m_sSocket != INVALID_SOCKET ){
  39. #endif
  40. switch( m_eType ) {
  41. case SOL_UDP_CLIENT:
  42. case SOL_TCP_CLIENT:
  43. case SOL_UDP_SERVER: {
  44. #ifdef __linux__
  45. close( m_nSocketFD );
  46. #else
  47. // When your application is finished receiving datagrams close the socket.
  48. LOG("Server: Finished receiving.Closing the listening socket...");
  49. if( closesocket(m_sSocket) != 0 ){
  50. sprintf(tBuff, "closesocket() failed!Error code : %ld", WSAGetLastError());
  51. LOG_ERROR(tBuff);
  52. } else {
  53. LOG("closesocket() is OK...");
  54. };
  55. // When your application is finished call WSACleanup.
  56. LOG("Server: Cleaning up...");
  57. if (WSACleanup() != 0) {
  58. sprintf(tBuff, "WSACleanup() failed!Error code : %ld", WSAGetLastError());
  59. LOG_ERROR(tBuff);
  60. } else {
  61. LOG("WSACleanup() is OK");
  62. };
  63. #endif
  64. break;
  65. };
  66. case SOL_TCP_SERVER:{
  67. #ifdef __linux__
  68. close(m_nSocketFD);
  69. #else
  70. // When your application is finished receiving datagrams close the socket.
  71. LOG("Server: Finished receiving.Closing the listening socket...");
  72. if (closesocket(m_sSocket) != 0) {
  73. sprintf(tBuff, "closesocket() failed!Error code : %ld", WSAGetLastError());
  74. LOG_ERROR(tBuff);
  75. } else {
  76. LOG("closesocket() is OK...");
  77. };
  78. // When your application is finished call WSACleanup.
  79. LOG("Server: Cleaning up...");
  80. if (WSACleanup() != 0) {
  81. sprintf(tBuff, "WSACleanup() failed!Error code : %ld", WSAGetLastError());
  82. LOG_ERROR(tBuff);
  83. } else {
  84. LOG("WSACleanup() is OK");
  85. };
  86. #endif
  87. break;
  88. };
  89. };
  90. #ifdef __linux__
  91. m_nSocketFD = -1;
  92. #else
  93. m_sSocket = INVALID_SOCKET;
  94. #endif
  95. };
  96. return true;
  97. };
  98. serialSOL::~serialSOL(){
  99. closePort();
  100. };
  101. #ifdef __linux__
  102. #else
  103. // Windows
  104. int recvfromTimeOutUDP(SOCKET socket, long sec, long usec) {
  105. // Setup timeval variable
  106. struct timeval timeout;
  107. struct fd_set fds;
  108. timeout.tv_sec = sec;
  109. timeout.tv_usec = usec;
  110. // Setup fd_set structure
  111. FD_ZERO(&fds);
  112. FD_SET(socket, &fds);
  113. // Return value:
  114. // -1: error occurred
  115. // 0: timed out
  116. // > 0: data ready to be read
  117. return select(0, &fds, 0, 0, &timeout);
  118. };
  119. #endif
  120. bool serialSOL::create( CSTR address, uint16_t port, int Speed, SOL_TYPE t ){
  121. if( m_eType == SOL_NOT_DEFINED ){
  122. if ( t == SOL_NOT_DEFINED ) {
  123. LOG_ERROR("No Connection Type Defined!");
  124. return false;
  125. };
  126. m_eType = t;
  127. } else {
  128. if ( t != SOL_NOT_DEFINED ) {
  129. m_eType = t;
  130. };
  131. };
  132. if( m_nPort == 0 ){
  133. if( port == 0 ) {
  134. LOG_ERROR("No Port Defined!");
  135. return false;
  136. };
  137. m_nPort = port;
  138. } else {
  139. if( port != 0 ) {
  140. m_nPort = port;
  141. };
  142. };
  143. if( m_nSpeed == 0 ){
  144. if( Speed == 0 ){
  145. LOG_ERROR("No Speed Defined!");
  146. return false;
  147. };
  148. m_nSpeed = Speed;
  149. } else {
  150. if( Speed != 0 ) {
  151. m_nSpeed = Speed;
  152. };
  153. };
  154. if( m_cDeviceAddress[0]==(char)0 ) {
  155. if( address== NULL) return false;
  156. strncpy(m_cDeviceAddress, address, 64 );
  157. } else {
  158. if( address != NULL ) {
  159. if( address[0]!= (char)0 ) {
  160. strncpy(m_cDeviceAddress, address, 64 );
  161. };
  162. };
  163. };
  164. #ifdef DEBUG
  165. LOG( m_cDeviceAddress );
  166. #endif
  167. switch( m_eType ) {
  168. case SOL_UDP_CLIENT:{
  169. #ifdef DEBUG
  170. LOG("PORT IS CLIENT - Create UDP Server");
  171. #endif
  172. #ifdef __linux__
  173. // Creating socket file descriptor
  174. if ( (m_nSocketFD = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 ) {
  175. LOG_ERROR("UDP Socket Creation Failed!");
  176. return false;
  177. };
  178. fcntl( m_nSocketFD, F_SETFL, O_NONBLOCK);
  179. // Filling server information
  180. m_sServerAddr.sin_family = AF_INET; // IPv4
  181. m_sServerAddr.sin_addr.s_addr = INADDR_ANY;
  182. m_sServerAddr.sin_port = htons( m_nPort );
  183. // Bind the socket with the server address
  184. if ( bind( m_nSocketFD, (const struct sockaddr *)&m_sServerAddr, sizeof(m_sServerAddr)) < 0 ){
  185. LOG_ERROR("UDP Bind Failed");
  186. return false;
  187. };
  188. #else
  189. WSADATA wsaData;
  190. // Initialize Winsock version 2.2
  191. char tBuff[128];
  192. if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
  193. sprintf(tBuff, "WSAStartup failed with error %ld", WSAGetLastError());
  194. LOG_ERROR(tBuff);
  195. return false;
  196. } else {
  197. sprintf(tBuff, "The Winsock DLL status is %s.", wsaData.szSystemStatus);
  198. LOG(tBuff);
  199. };
  200. // Set up a SOCKADDR_IN structure that will tell bind that we
  201. // want to receive datagrams from all interfaces using port 5150.
  202. // The IPv4 family
  203. m_sServerAddr.sin_family = AF_INET;
  204. // Port no. 5150
  205. m_sServerAddr.sin_port = htons( m_nPort );
  206. // From all interface (0.0.0.0)
  207. m_sServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  208. // Associate the address information with the socket using bind.
  209. // At this point you can receive datagrams on your bound socket.
  210. if( bind(m_sSocket, (SOCKADDR*)&m_sServerAddr, sizeof(m_sServerAddr)) == SOCKET_ERROR ){
  211. sprintf( tBuff, "Server: bind() failed!Error : %ld.", WSAGetLastError());
  212. // Close the socket
  213. closesocket(m_sSocket);
  214. // Do the clean up
  215. WSACleanup();
  216. // and exit with error
  217. LOG_ERROR(tBuff);
  218. return false;
  219. } else {
  220. LOG("UDP Server: bind() is OK!");
  221. };
  222. // Some info on the receiver side...
  223. SOCKADDR_IN tAddr;
  224. int tLen = sizeof(SOCKADDR_IN);
  225. getsockname(m_sSocket, (SOCKADDR*)&tAddr, &tLen);
  226. sprintf( tBuff, "Receiving IP(s) used : %s", inet_ntoa(tAddr.sin_addr));
  227. LOG(tBuff);
  228. sprintf(tBuff,"Receiving port used : %d", htons(tAddr.sin_port));
  229. LOG(tBuff);
  230. SelectTiming = recvfromTimeOutUDP(m_sSocket, 10, 0);
  231. #endif
  232. break;
  233. };
  234. case SOL_TCP_CLIENT:{
  235. #ifdef DEBUG
  236. LOG("PORT IS CLIENT - Create TCP Server");
  237. #endif
  238. break;
  239. };
  240. case SOL_UDP_SERVER:{
  241. #ifdef DEBUG
  242. LOG("PORT IS SERVER - Create UDP Client");
  243. #endif
  244. #ifdef __linux__
  245. // Creating socket file descriptor
  246. if ( (m_nSocketFD = socket( AF_INET, SOCK_DGRAM, 0)) < 0 ) {
  247. LOG_ERROR("Socket Creation Failed!");
  248. return false;
  249. };
  250. fcntl( m_nSocketFD, F_SETFL, O_NONBLOCK );
  251. // Filling server information
  252. m_sServerAddr.sin_family = AF_INET;
  253. m_sServerAddr.sin_addr.s_addr = INADDR_ANY;
  254. m_sServerAddr.sin_port = htons( m_nPort );
  255. #else
  256. #endif
  257. break;
  258. };
  259. case SOL_TCP_SERVER:{
  260. #ifdef DEBUG
  261. LOG("PORT IS SERVER - Create TCP Client");
  262. #endif
  263. // socket create and varification
  264. #ifdef __linux__
  265. m_nSocketFD = socket( AF_INET, SOCK_STREAM, 0 );
  266. if( m_nSocketFD == -1) {
  267. LOG_ERROR("Socket creation failed!");
  268. return false;
  269. }
  270. #else
  271. m_sSocket = socket(AF_INET, SOCK_STREAM, 0);
  272. if (m_sSocket == INVALID_SOCKET ) {
  273. LOG_ERROR("Socket creation failed!");
  274. return false;
  275. }
  276. #endif
  277. #ifdef DEBUG
  278. else LOG("Socket successfully created");
  279. #endif
  280. memset( &m_sServerAddr, 0, sizeof( m_sServerAddr ) );
  281. // assign IP, PORT
  282. m_sServerAddr.sin_family = AF_INET;
  283. m_sServerAddr.sin_addr.s_addr = inet_addr( m_cDeviceAddress );
  284. m_sServerAddr.sin_port = htons( m_nPort );
  285. #ifdef __linux__
  286. // Set non-blocking
  287. int arg;
  288. if( (arg = fcntl( m_nSocketFD, F_GETFL, NULL)) < 0) {
  289. char tBuf[128];
  290. sprintf( tBuf, "Error fcntl(..., F_GETFL) (%s)", strerror(errno));
  291. LOG_ERROR( tBuf );
  292. return false;
  293. };
  294. arg |= O_NONBLOCK;
  295. if( fcntl( m_nSocketFD, F_SETFL, arg) < 0) {
  296. char tBuf[128];
  297. sprintf( tBuf, "Error fcntl(..., F_SETFL) (%s)", strerror(errno));
  298. LOG_ERROR( tBuf );
  299. return false;
  300. };
  301. // connect the client socket to server socket
  302. if( connect( m_nSocketFD, (struct sockaddr *)&m_sServerAddr, sizeof( struct sockaddr ) ) < 0 ){
  303. if( errno == EINPROGRESS ){
  304. #ifdef DEBUG
  305. LOG( "EINPROGRESS in connect() - selecting" );
  306. #endif
  307. do {
  308. fd_set myset;
  309. struct timeval tv;
  310. tv.tv_sec = 3;
  311. tv.tv_usec = 0;
  312. FD_ZERO( &myset );
  313. FD_SET( m_nSocketFD, &myset );
  314. int res = select( m_nSocketFD+1, NULL, &myset, NULL, &tv );
  315. if( res < 0 && errno != EINTR ){
  316. char tBuff[128];
  317. sprintf( tBuff, "Error connecting %d - %s", errno, strerror(errno));
  318. LOG_ERROR( tBuff );
  319. return false;
  320. } else if( res > 0 ){
  321. // Socket selected for write
  322. int valopt;
  323. socklen_t lon = sizeof(int);
  324. if( getsockopt( m_nSocketFD, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) < 0 ){
  325. char tBuff[128];
  326. sprintf( tBuff, "Error in getsockopt() %d - %s", errno, strerror(errno));
  327. LOG_ERROR( tBuff );
  328. return false;
  329. };
  330. // Check the value returned...
  331. if( valopt ){
  332. char tBuff[128];
  333. sprintf( tBuff, "Error in delayed connection() %d - %s", valopt, strerror(valopt) );
  334. LOG_ERROR( tBuff );
  335. return false;
  336. };
  337. break;
  338. } else {
  339. LOG_ERROR("Timeout in select() - Cancelling!");
  340. return false;
  341. };
  342. } while( 1 );
  343. } else {
  344. char tBuff[128];
  345. sprintf( tBuff, "Error connecting %d - %s", errno, strerror(errno));
  346. LOG_ERROR( tBuff );
  347. return false;
  348. };
  349. }
  350. #else
  351. // Set non-blocking
  352. u_long arg = 1;
  353. if( ioctlsocket(m_sSocket, FIONBIO, &arg) < 0 ){
  354. char tBuf[128];
  355. sprintf(tBuf, "Error ioctlsocket(..., FIONBIO) (%s)", strerror(errno));
  356. LOG_ERROR(tBuf);
  357. return false;
  358. };
  359. // connect the client socket to server socket
  360. if (connect(m_sSocket, (struct sockaddr*) & m_sServerAddr, sizeof(struct sockaddr)) != 0) {
  361. char cBuffer[128];
  362. sprintf(cBuffer,"ERROR connect to [%s:%d] (%s)", m_cDeviceAddress, m_nPort, strerror(errno) );
  363. LOG_ERROR( cBuffer );
  364. return false;
  365. }
  366. #endif
  367. #ifdef DEBUG
  368. else LOG("Connected to the server");
  369. #endif
  370. setConnectionStatus( CS_CONNECTED );
  371. break;
  372. };
  373. };
  374. return init();
  375. };
  376. int serialSOL::sendBlock(unsigned char* Data, int DataSize) {
  377. int RetVal = 0;
  378. if( m_eConnectionStatus!= CS_CONNECTED ) return -1; // Exit after unsuccess reconnect
  379. #ifdef __linux__
  380. if (m_nSocketFD > -1) {
  381. #ifdef _DEBUG_SOL_COMMUNICATION_
  382. char cBuffer[256];
  383. sprintf(cBuffer, "Write into Socket %d bytes", DataSize);
  384. LOG(cBuffer);
  385. #endif
  386. socklen_t addr_len = sizeof(m_sClientAddr);
  387. RetVal = sendto(m_nSocketFD, (const char*)Data, DataSize, MSG_CONFIRM, (const struct sockaddr*) &m_sClientAddr, addr_len );
  388. if ((RetVal == -1) && (errno != EINTR) && (errno != EWOULDBLOCK)) {
  389. LOG_ERROR("Error acures while writing data into the Socket");
  390. shutdown(m_nSocketFD, 2);
  391. close(m_nSocketFD);
  392. m_nSocketFD = -1;
  393. setConnectionStatus(CS_DISCONNECTED);
  394. return -1;
  395. };
  396. if (RetVal < 0) RetVal = 0;
  397. #ifdef _DEBUG_SOL_COMMUNICATION_
  398. sprintf(cBuffer, "Bytes Sent: %ld", RetVal);
  399. LOG(cBuffer);
  400. for(int zz=0; zz < RetVal; zz++ ) {
  401. sprintf(cBuffer, "SD: 0x%02X", (uint8_t)Data[zz]);
  402. LOG(cBuffer);
  403. };
  404. #endif
  405. } else {
  406. LOG_ERROR("Not Connected!");
  407. //m_bConnected = false;
  408. setConnectionStatus(CS_DISCONNECTED);
  409. return -1;
  410. };
  411. #else
  412. if (sendto( m_sSocket, (char *)Data, DataSize, 0, (struct sockaddr*) & m_sServerAddr, sizeof(m_sServerAddr)) == SOCKET_ERROR) {
  413. printf("sendto() failed with error code : %d", WSAGetLastError());
  414. exit(EXIT_FAILURE);
  415. }
  416. #endif
  417. return RetVal;
  418. };
  419. int serialSOL::recptBlock( unsigned char *Buffer, int *BufferLength ){
  420. int RetVal = 0;
  421. #ifdef __linux__
  422. if (m_eConnectionStatus != CS_CONNECTED) {
  423. //make prep for connection
  424. if (m_eType == SOL_TCP_CLIENT) {
  425. //ACCEPT
  426. struct sockaddr ClientAddress;
  427. socklen_t size = sizeof(struct sockaddr_in);
  428. //int ClientSocket;
  429. m_nSocketFD = accept(m_nMasterSocketFD, &ClientAddress, &size);
  430. if (m_nSocketFD < 0) {
  431. setConnectionStatus(CS_DISCONNECTED);
  432. LOG_ERROR("Connection Accept Failure!");
  433. return -1;
  434. };
  435. }
  436. else {
  437. LOG_ERROR("RCPT ON not connected!");
  438. shutdown(m_nSocketFD, 2);
  439. close(m_nSocketFD);
  440. *BufferLength = 0;
  441. m_nSocketFD = -1;
  442. setConnectionStatus(CS_DISCONNECTED);
  443. return -1;
  444. };
  445. };
  446. if( m_nSocketFD > -1 ) {
  447. socklen_t addr_len = sizeof( m_sClientAddr ); //len is value/resuslt
  448. //RetVal = recvfrom( m_nSocketFD, (char *)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, MSG_WAITALL, ( struct sockaddr *) &m_sClientAddr, &addr_len );
  449. RetVal = recvfrom( m_nSocketFD, (char *)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, MSG_DONTWAIT, ( struct sockaddr *) &m_sClientAddr, &addr_len );
  450. if( RetVal < 1 ){
  451. if( errno!=EAGAIN && errno!=EINTR ) {
  452. LOG_ERROR("Peer Closes Connection!");
  453. shutdown( m_nSocketFD, 2 );
  454. close( m_nSocketFD );
  455. *BufferLength = 0;
  456. m_nSocketFD = -1;
  457. setConnectionStatus( CS_DISCONNECTED );
  458. return -1;
  459. };
  460. };
  461. } else {
  462. LOG_ERROR("Not Connected!");
  463. *BufferLength = 0;
  464. setConnectionStatus( CS_DISCONNECTED );
  465. return -1;
  466. };
  467. #else
  468. if (m_eConnectionStatus != CS_CONNECTED) {
  469. //make prep for connection
  470. if (m_eType == SOL_TCP_CLIENT) {
  471. //ACCEPT
  472. int size = sizeof(m_sClientAddr);
  473. //int ClientSocket;
  474. m_sSocket = accept(m_sMasterSocket, (sockaddr *) &m_sClientAddr, &size);
  475. if( m_sSocket == INVALID_SOCKET ){
  476. setConnectionStatus(CS_DISCONNECTED);
  477. LOG_ERROR("Connection Accept Failure!");
  478. return -1;
  479. };
  480. } else {
  481. LOG_ERROR("RCPT ON not connected!");
  482. shutdown(m_sSocket, 2);
  483. closesocket(m_sSocket);
  484. *BufferLength = 0;
  485. m_sSocket = INVALID_SOCKET;
  486. setConnectionStatus(CS_DISCONNECTED);
  487. return -1;
  488. };
  489. };
  490. if( m_sSocket != INVALID_SOCKET ) {
  491. switch (m_eType) {
  492. case SOL_UDP_CLIENT:
  493. case SOL_TCP_CLIENT:
  494. case SOL_UDP_SERVER: {
  495. int tLen = sizeof(m_sClientAddr);
  496. RetVal = recvfrom(m_sSocket, (char*)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, 0, (struct sockaddr*) &m_sClientAddr, &tLen);
  497. if (RetVal == SOCKET_ERROR) {
  498. char cBuffer[165];
  499. sprintf(cBuffer, "recvfrom() failed with error code : %d", WSAGetLastError());
  500. LOG_ERROR(cBuffer);
  501. };
  502. break;
  503. };
  504. };
  505. } else {
  506. LOG_ERROR("Not Connected!");
  507. *BufferLength = 0;
  508. setConnectionStatus(CS_DISCONNECTED);
  509. return -1;
  510. };
  511. #endif
  512. if (RetVal > 0) {
  513. #ifdef _DEBUG_SOL_COMMUNICATION_
  514. char cBuffer[165];
  515. sprintf(cBuffer, "Bytes received: %d", RetVal);
  516. LOG(cBuffer);
  517. #endif
  518. *BufferLength = RetVal;
  519. return RetVal;
  520. };
  521. *BufferLength = 0;
  522. if (RetVal == -1) return 0;
  523. if (RetVal == 0){
  524. errno = 0;
  525. return 0;
  526. };
  527. /* r < -1, should never happen. */
  528. errno = EIO;
  529. return 0;
  530. };
  531. /* In Case Server Instance - Accepting Connection Thread Is Used */
  532. /*
  533. void serialSOL::m_fThreadProc( void ){
  534. m_bThreadStatus = TH_WORKING;
  535. LOG( "Core Worker Thread Started" );
  536. LOG( "Ready to Accept Connections" );
  537. while( m_bThreadStatus < TH_ENDED ) {
  538. switch( m_sPortSettings.eConnectionType ){
  539. case SOL_UDP_CLIENT:{
  540. break;
  541. };
  542. case SOL_TCP_CLIENT:{
  543. struct sockaddr ClientAddress;
  544. socklen_t size;
  545. int ClientSocket;
  546. size = (socklen_t) sizeof( struct sockaddr_in );
  547. ClientSocket = accept( m_nSocketFD, &ClientAddress, &size );
  548. #ifdef SOL_SINGLE_CLIENT
  549. if( m_eConnectionStatus == CS_DISCONNECTED ) {
  550. setConnectionStatus( CS_CONNECTED );
  551. } else {
  552. LOG_ERROR("Another Client Tryes To Connect!");
  553. close( ClientSocket );
  554. };
  555. #endif
  556. break;
  557. };
  558. };
  559. };
  560. };
  561. */
  562. #undef LOG
  563. #undef LOG_ERROR