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.

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