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.

401 lines
11 KiB

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