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.

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