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.

355 lines
9.8 KiB

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