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.

320 lines
8.6 KiB

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