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.

386 lines
11 KiB

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