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.

413 lines
11 KiB

7 years ago
​
​
​
​
2 years ago
​
​
7 years ago
​
5 years ago
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
5 years ago
​
​
7 years ago
5 years ago
7 years ago
​
​
5 years ago
​
​
5 years ago
​
5 years ago
​
​
​
​
​
​
7 years ago
​
​
​
2 years ago
​
2 years ago
7 years ago
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
5 years ago
7 years ago
5 years ago
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
5 years ago
7 years ago
5 years ago
7 years ago
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
5 years ago
7 years ago
​
​
​
​
5 years ago
​
​
​
​
​
​
5 years ago
​
​
7 years ago
​
​
​
5 years ago
7 years ago
​
​
​
​
​
​
​
​
​
5 years ago
​
7 years ago
​
​
​
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
2 years ago
5 years ago
​
​
​
​
​
​
​
​
5 years ago
7 years ago
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
7 years ago
7 years ago
7 years ago
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
6 years ago
7 years ago
​
7 years ago
​
​
7 years ago
​
​
​
​
7 years ago
​
7 years ago
​
​
7 years ago
​
7 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
7 years ago
​
​
​
​
  1. #include "clientPort.h"
  2. #define LOG( X ) m_pLog->Log( "clientPort", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "clientPort", this, _LOG_ERROR, X )
  4. //#define DUBUG_EXTRA
  5. //#define DUBUG_COMMUNICATION
  6. clientPort::clientPort():transportSocket(){
  7. m_pLog = basicLogCollection::getInstance();
  8. m_nPort = 0;
  9. memset( m_cHostName, 0, MAX_HOST_STRING_SIZE );
  10. //m_sHostName = NULL;
  11. m_bKeepAlive = true;
  12. };
  13. clientPort::clientPort(CSTR URI):transportSocket(){
  14. m_pLog = basicLogCollection::getInstance();
  15. m_bKeepAlive = true;
  16. if( URI!= NULL ) {
  17. char *ptChop = NULL;
  18. char *ptSt = NULL;
  19. int tSize = 0;
  20. ptChop = strstr(URI, "/");
  21. if (ptChop != NULL){
  22. ptChop = strstr(ptChop + 1, "/");
  23. ptSt = ptChop + 1;
  24. ptChop = strstr(ptChop + 1, "/");
  25. tSize = ptChop - ptSt;
  26. ptSt[tSize] = (char)0;
  27. strncpy( m_cHostName, ptSt, MAX_HOST_STRING_SIZE );
  28. m_nPort = 80;//??
  29. //if( connectTo(ptSt, m_nPort) ){
  30. // char MsgBuffer[64];
  31. // sprintf(MsgBuffer, "Connect %s [%d] OK", m_sHostName, m_nPort);
  32. // LOG(MsgBuffer);
  33. //} else {
  34. // LOG_ERROR("Cannot Connect!");
  35. //};
  36. };
  37. } else {
  38. memset( m_cHostName, 0, 128 );
  39. m_nPort = 0;
  40. };
  41. };
  42. clientPort::clientPort(CSTR Host, int Port, bool KeepALive ) :transportSocket(){
  43. m_pLog = basicLogCollection::getInstance();
  44. m_bKeepAlive = KeepALive;
  45. if( strlen(Host) ){
  46. strncpy( m_cHostName, Host, MAX_HOST_STRING_SIZE );
  47. m_nPort = Port;
  48. } else {
  49. memset( m_cHostName, 0, MAX_HOST_STRING_SIZE );
  50. m_nPort = 0;
  51. };
  52. //if( connectTo( Host, Port ) ) {
  53. // char MsgBuffer[64];
  54. // sprintf(MsgBuffer, "Connect %s [%d] OK", m_sHostName, m_nPort );
  55. // LOG(MsgBuffer);
  56. //} else {
  57. // LOG_ERROR("Cannot Connect!");
  58. //};
  59. };
  60. clientPort::~clientPort(){
  61. #ifdef DEBUG
  62. LOG("Destructor!");
  63. #endif
  64. disconnect();
  65. #ifdef DEBUG
  66. LOG("Disconnected!");
  67. LOG("Destructor Done!");
  68. #endif
  69. };
  70. bool clientPort::create(){
  71. if ( strlen( m_cHostName) == 0 ) {
  72. LOG_ERROR("Host to connect is not Set!");
  73. return false;
  74. };
  75. if( m_nPort == 0 ) {
  76. LOG_ERROR("Port to connect is not Set!");
  77. return false;
  78. };
  79. if( connectTo( m_cHostName, m_nPort ) ){
  80. #ifdef DEBUG
  81. char tB[256];
  82. sprintf(tB, "Connect %s [%d] OK", m_cHostName, m_nPort);
  83. LOG(tB);
  84. #endif
  85. return transportSocket::create();
  86. } else {
  87. LOG_ERROR("Cannot Connect!");
  88. };
  89. return false;
  90. };
  91. bool clientPort::_connect(void){
  92. int iResult;
  93. struct addrinfo * result, *ptr, hints;
  94. char cBuffer[256];
  95. struct sockaddr_in serv_addr;
  96. #ifdef __linux__
  97. struct hostent *server;
  98. m_nSocketFD = socket( AF_INET, SOCK_STREAM, 0);
  99. if( m_nSocketFD < 0 ){
  100. LOG_ERROR("ERROR creating socket");
  101. return false;
  102. };
  103. server = gethostbyname( m_cHostName );
  104. if( server == NULL ){
  105. sprintf( cBuffer, "ERROR, no such host [%s]", m_cHostName );
  106. LOG_ERROR(cBuffer);
  107. return false;
  108. };
  109. bzero((char *) &serv_addr, sizeof(serv_addr));
  110. serv_addr.sin_family = AF_INET;
  111. bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length );
  112. serv_addr.sin_port = htons( m_nPort );
  113. if ( connect(m_nSocketFD,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0){
  114. sprintf(cBuffer,"ERROR connect to [%s:%d] error: %d", m_cHostName, m_nPort, errno );
  115. LOG_ERROR( cBuffer );
  116. return false;
  117. };
  118. #ifdef CLIENT_SOCKET_NON_BLOCKING
  119. if ( fcntl(m_nSocketFD, F_SETFL, fcntl(m_nSocketFD, F_GETFL, 0) | O_NONBLOCK) < 0 ){
  120. sprintf(cBuffer,"ERROR cannot set NONBLOCK error: %d", errno );
  121. LOG_ERROR( cBuffer );
  122. return false;
  123. };
  124. #endif
  125. #else
  126. // Initialize Winsock
  127. WSADATA wsaData;
  128. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  129. if (iResult != 0) {
  130. sprintf(cBuffer, "WSAStartup failed with error: %d\n", iResult);
  131. LOG_ERROR(cBuffer);
  132. return false;
  133. };
  134. ZeroMemory( &hints, sizeof(hints) );
  135. hints.ai_family = AF_UNSPEC;
  136. hints.ai_socktype = SOCK_STREAM;
  137. hints.ai_protocol = IPPROTO_TCP;
  138. // Resolve the server address and port
  139. sprintf( cBuffer, "%d", m_nPort );
  140. iResult = getaddrinfo( m_cHostName, cBuffer, &hints, &result );
  141. if (iResult != 0) {
  142. sprintf(cBuffer, "getaddrinfo for [%s:%d] failed with error: %d", m_cHostName, m_nPort, iResult);
  143. LOG_ERROR(cBuffer);
  144. WSACleanup();
  145. return false;
  146. };
  147. // Attempt to connect to an address until one succeeds
  148. ptr = result;
  149. while( ptr != NULL ) {
  150. // Create a SOCKET for connecting to server
  151. m_nSocketFD = socket( ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol );
  152. if( m_nSocketFD == INVALID_SOCKET ){
  153. sprintf( cBuffer, "Socket Creation failed with error: %ld", WSAGetLastError());
  154. LOG_ERROR(cBuffer);
  155. WSACleanup();
  156. return false;
  157. };
  158. // Connect to server.
  159. iResult = connect( m_nSocketFD, ptr->ai_addr, (int)ptr->ai_addrlen );
  160. if (iResult == SOCKET_ERROR) {
  161. sprintf(cBuffer, "Socket Connection failed[%d] with error: %ld", iResult, WSAGetLastError());
  162. LOG_ERROR(cBuffer);
  163. closesocket(m_nSocketFD);
  164. m_nSocketFD = INVALID_SOCKET;
  165. ptr = ptr->ai_next;
  166. continue;
  167. };
  168. break;
  169. };
  170. freeaddrinfo(result);
  171. if (m_nSocketFD == INVALID_SOCKET) {
  172. sprintf( cBuffer, "Unable Connect to the Server[%s:%d]!", m_cHostName, m_nPort );
  173. LOG_ERROR(cBuffer);
  174. WSACleanup();
  175. return false;
  176. };
  177. #endif
  178. /*
  179. setConnectionStatus(CS_CONNECTED);
  180. #ifdef DEBUG
  181. LOG("In case THREADS are running -> SHUTTING DOWN THREADS");
  182. #endif
  183. if( uninit() ) {
  184. #ifdef DEBUG
  185. LOG("STARTING IO THREADS");
  186. #endif
  187. return init();
  188. } else {
  189. LOG_ERROR("CAN NOT SHUTDOWN IO THREADS!");
  190. };
  191. */
  192. return true;//false;
  193. };
  194. bool clientPort::connectTo(CSTR Host, int Port){
  195. if( m_eConnectionStatus == CS_CONNECTED ){
  196. setConnectionStatus( CS_DISCONNECTED );
  197. #ifdef __linux__
  198. shutdown( m_nSocketFD, SHUT_RDWR );
  199. close( m_nSocketFD );
  200. m_nSocketFD = -1;
  201. #else
  202. closesocket(m_nSocketFD);
  203. WSACleanup();
  204. m_nSocketFD = INVALID_SOCKET;
  205. #endif
  206. };
  207. //if( m_sHostName == NULL ) m_sHostName = (CSTR) malloc( strlen( Host ) +1 );
  208. if( m_cHostName != Host ) strncpy( m_cHostName, Host, MAX_HOST_STRING_SIZE );//sprintf( m_sHostName, "%s", Host );// Prevent Self Copy
  209. m_nPort = Port;
  210. return _connect();
  211. };
  212. int clientPort::sendBlock(unsigned char *Data, int DataSize){
  213. if( m_eConnectionStatus != CS_CONNECTED ){
  214. if( connectTo( m_cHostName, m_nPort ) ){
  215. char MsgBuffer[CLOG];
  216. sprintf(MsgBuffer, "Reconnect %s [%d] OK", m_cHostName, m_nPort);
  217. LOG(MsgBuffer);
  218. };
  219. };
  220. if( m_eConnectionStatus != CS_CONNECTED ) return -1; // Exit after unsuccess reconnect
  221. #ifdef __linux__
  222. int RetVal;
  223. if( m_nSocketFD > -1 ){
  224. //sprintf(cBuffer,"Write into Socket %p %d", Data, DataSize );
  225. //LOG(cBuffer);
  226. RetVal = write(m_nSocketFD, Data, DataSize);
  227. //LOG("Data writen into Socket");
  228. if( ( RetVal == -1 ) && (errno != EINTR ) && ( errno != EWOULDBLOCK ) ) {
  229. LOG_ERROR("Error acures while writing data into the Socket");
  230. //shutdown( m_nSocketFD, 2 );
  231. //close( m_nSocketFD );
  232. //m_nSocketFD = -1;
  233. disconnect();
  234. return -1;
  235. };
  236. if( RetVal < 0 ) RetVal = 0;
  237. #ifdef DUBUG_EXTRA
  238. {
  239. char cBuffer[128];
  240. sprintf(cBuffer,"Bytes Sent: %ld", RetVal);
  241. LOG(cBuffer);
  242. #ifdef DUBUG_COMMUNICATION
  243. for (int i = 0; i < RetVal; i++) {
  244. sprintf(cBuffer, "0x%02X", Data[i]);
  245. LOG(cBuffer);
  246. };
  247. #endif
  248. }
  249. #endif
  250. } else {
  251. LOG_ERROR("Not Connected!");
  252. return -1;
  253. };
  254. #else
  255. DWORD RetVal;
  256. if (m_nSocketFD != INVALID_SOCKET){
  257. // Send an initial buffer
  258. RetVal = send(m_nSocketFD, (const char *)Data, DataSize, NULL);
  259. if (RetVal == SOCKET_ERROR) {
  260. char cBuffer[CLOG];
  261. //m_bConnected = false;
  262. setConnectionStatus(CS_DISCONNECTED);
  263. sprintf(cBuffer,"send failed with error: %d", WSAGetLastError());
  264. LOG_ERROR(cBuffer);
  265. closesocket(m_nSocketFD);
  266. WSACleanup();
  267. return -1;
  268. };
  269. //sprintf(cBuffer,"Bytes Sent: %ld", RetVal);
  270. //LOG(cBuffer);
  271. if (!m_bKeepAlive){
  272. setConnectionStatus(CS_DISCONNECTED);
  273. closesocket(m_nSocketFD);
  274. WSACleanup();
  275. //m_bConnected = false;
  276. m_nSocketFD = INVALID_SOCKET;
  277. };
  278. } else {
  279. setConnectionStatus(CS_DISCONNECTED);
  280. //m_bConnected = false;
  281. return -1;
  282. };
  283. #endif
  284. return RetVal;
  285. };
  286. int clientPort::recptBlock(unsigned char *Buffer, int *BufferLength){
  287. if( m_eConnectionStatus != CS_CONNECTED ) return -1; // Exit after unsuccess reconnect
  288. #ifdef __linux__
  289. int RetVal;
  290. if( m_nSocketFD > -1 ) {
  291. RetVal = recv( m_nSocketFD, Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, 0 );
  292. if( RetVal < 1 ){
  293. if( errno!=EAGAIN && errno!=EINTR ) {
  294. LOG_ERROR("Peer Closes Connection!");
  295. //shutdown( m_nSocketFD, 2 );
  296. //close( m_nSocketFD );
  297. disconnect();
  298. *BufferLength = 0;
  299. //m_nSocketFD = -1;
  300. return -1;
  301. };
  302. };
  303. if( !m_bKeepAlive ){
  304. //shutdown( m_nSocketFD, SHUT_RDWR ); //SD_RECEIVE
  305. //close( m_nSocketFD );
  306. //m_nSocketFD = -1;
  307. disconnect();
  308. };
  309. } else {
  310. LOG_ERROR("Not Connected!");
  311. *BufferLength = 0;
  312. return -1;
  313. };
  314. #else
  315. DWORD RetVal;
  316. if( m_nSocketFD != INVALID_SOCKET ){
  317. // Receive until the peer closes the connection
  318. RetVal = recv( m_nSocketFD, (char *)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, 0);
  319. if( RetVal < 1 ){
  320. if( errno!=EAGAIN && errno!=EINTR ) {
  321. //setConnectionStatus(CS_DISCONNECTED);
  322. closesocket(m_nSocketFD);
  323. WSACleanup();
  324. *BufferLength = 0;
  325. //m_bConnected = false;
  326. m_nSocketFD = INVALID_SOCKET;
  327. return -1;
  328. };
  329. };
  330. if (!m_bKeepAlive){
  331. //setConnectionStatus(CS_DISCONNECTED);
  332. closesocket(m_nSocketFD);
  333. WSACleanup();
  334. //m_bConnected = false;
  335. m_nSocketFD = INVALID_SOCKET;
  336. };
  337. } else {
  338. //setConnectionStatus(CS_DISCONNECTED);
  339. LOG_ERROR("Not Connected!");
  340. *BufferLength = 0;
  341. //m_bConnected = false;
  342. return 0;
  343. };
  344. #endif
  345. if (RetVal > 0) {
  346. #ifdef DUBUG_EXTRA
  347. char cBuffer[165];
  348. sprintf(cBuffer, "Bytes received: %d", RetVal);
  349. LOG(cBuffer);
  350. #ifdef DUBUG_COMMUNICATION
  351. for (int i = 0; i < RetVal; i++) {
  352. sprintf(cBuffer, "0x%02X", Buffer[i]);
  353. LOG(cBuffer);
  354. };
  355. #endif
  356. #endif
  357. *BufferLength = RetVal;
  358. return RetVal;
  359. };
  360. *BufferLength = 0;
  361. if (RetVal == -1) return 0;
  362. if (RetVal == 0){
  363. errno = 0;
  364. return 0;
  365. };
  366. /* r < -1, should never happen. */
  367. errno = EIO;
  368. return 0;
  369. };
  370. bool clientPort::disconnect(void){
  371. if( m_eConnectionStatus == CS_CONNECTED ){
  372. setConnectionStatus(CS_DISCONNECTED);
  373. #ifdef __linux__
  374. shutdown( m_nSocketFD, SHUT_RDWR ); //SD_RECEIVE
  375. close( m_nSocketFD );
  376. m_nSocketFD = -1;
  377. #else
  378. closesocket(m_nSocketFD);
  379. WSACleanup();
  380. m_nSocketFD = INVALID_SOCKET;
  381. #endif
  382. };
  383. return true;
  384. };
  385. //#undef DUBUG_EXTRA
  386. //#undef DUBUG_COMMUNICATION
  387. #undef LOG
  388. #undef LOG_ERROR