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.

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