GuardianBase.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // GuardianBase.cpp : Defines the exported functions for the DLL application.
  2. //
  3. #include "stdafx.h"
  4. #ifdef linux
  5. #include <netinet/in.h> // for sockaddr_in
  6. #include <sys/types.h> // for socket
  7. #include <sys/socket.h> // for socket
  8. #include <arpa/inet.h>
  9. #include <string.h> // for bzero
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <syslog.h>
  13. #include "GuardianBase.h"
  14. #define HELLO_WORLD_SERVER_PORT 6666
  15. #define BUFFER_SIZE 1024
  16. #define FILE_NAME_MAX_SIZE 512
  17. #else
  18. #ifndef WIN32_LEAN_AND_MEAN
  19. #define WIN32_LEAN_AND_MEAN
  20. #endif
  21. #include <windows.h>
  22. #include <winsock2.h>
  23. #include <ws2tcpip.h>
  24. #include "GuardianBase.h"
  25. // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
  26. #pragma comment (lib, "Ws2_32.lib")
  27. #pragma comment (lib, "Mswsock.lib")
  28. #pragma comment (lib, "AdvApi32.lib")
  29. #endif //linux
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include "log4rvcother.h"
  33. #define DEFAULT_PORT "30005"
  34. const int default_port = 30005;
  35. int GetSocket()
  36. {
  37. #ifdef linux
  38. openlog("gdbase",LOG_CONS|LOG_PID,LOG_LOCAL0);
  39. syslog(LOG_ERR,"enter GetSocket");
  40. struct sockaddr_in client_addr;
  41. bzero(&client_addr, sizeof(client_addr));
  42. client_addr.sin_family = AF_INET;
  43. client_addr.sin_addr.s_addr = htons(INADDR_ANY);
  44. client_addr.sin_port = htons(0);
  45. int client_socket = socket(AF_INET, SOCK_STREAM, 0);
  46. if (client_socket < 0)
  47. {
  48. //LOG4VTM(ERROR, "Create Socket Failed!");
  49. return -1;
  50. }
  51. if (bind(client_socket, (struct sockaddr*) & client_addr, sizeof(client_addr)))
  52. {
  53. //LOG4VTM(ERROR, "Client Bind Port Failed!");
  54. return -1;
  55. }
  56. struct sockaddr_in server_addr;
  57. bzero(&server_addr, sizeof(server_addr));
  58. server_addr.sin_family = AF_INET;
  59. char* local_addr = "127.0.0.1";
  60. inet_aton(local_addr, &(server_addr.sin_addr));
  61. server_addr.sin_port = htons(default_port);
  62. socklen_t server_addr_length = sizeof(server_addr);
  63. //向服务器发起连接,连接成功后client_socket代表了客户机和服务器的一个socket连接
  64. if (connect(client_socket, (struct sockaddr*) & server_addr, server_addr_length) < 0)
  65. {
  66. //LOG4VTM(ERROR, "Can Not Connect To server!");
  67. return -1;
  68. }
  69. syslog(LOG_ERR,"after getsocket");
  70. return client_socket;
  71. #else
  72. WSADATA wsaData;
  73. SOCKET ConnectSocket = INVALID_SOCKET;
  74. struct addrinfo *result = NULL, *ptr = NULL,
  75. hints;
  76. int iResult;
  77. // Initialize Winsock
  78. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  79. if (iResult != 0) {
  80. printf("WSAStartup failed with error: %d\n", iResult);
  81. return ConnectSocket;
  82. }
  83. ZeroMemory(&hints, sizeof(hints));
  84. hints.ai_family = AF_INET;
  85. hints.ai_socktype = SOCK_STREAM;
  86. hints.ai_protocol = IPPROTO_TCP;
  87. // Resolve the server address and port
  88. iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
  89. if (iResult != 0) {
  90. printf("getaddrinfo failed with error: %d\n", iResult);
  91. WSACleanup();
  92. return ConnectSocket;
  93. }
  94. // 超时时间
  95. struct timeval tm;
  96. tm.tv_sec = 5;
  97. tm.tv_usec = 0;
  98. // Attempt to connect to an address until one succeeds
  99. for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
  100. // Create a SOCKET for connecting to server
  101. ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
  102. ptr->ai_protocol);
  103. if (ConnectSocket == INVALID_SOCKET) {
  104. printf("socket failed with error: %ld\n", WSAGetLastError());
  105. WSACleanup();
  106. return ConnectSocket;
  107. }
  108. // 设置为非阻塞的socket
  109. int iMode = 1;
  110. ioctlsocket(ConnectSocket, FIONBIO, (u_long FAR*)&iMode);
  111. bool isConnected = false;
  112. // Connect to server.
  113. iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
  114. if (iResult != SOCKET_ERROR)
  115. {
  116. isConnected = true;
  117. }
  118. else
  119. {
  120. fd_set set;
  121. FD_ZERO(&set);
  122. FD_SET(ConnectSocket, &set);
  123. //oiltmp need or not? FD_ISSET
  124. if (select(-1, NULL, &set, NULL, &tm) <= 0)
  125. {
  126. isConnected = false;
  127. }
  128. else
  129. {
  130. int error = -1;
  131. int optLen = sizeof(int);
  132. getsockopt(ConnectSocket, SOL_SOCKET, SO_ERROR, (char*)&error, &optLen);
  133. if (0 == error)
  134. isConnected = true;
  135. }
  136. }
  137. if (!isConnected) {
  138. closesocket(ConnectSocket);
  139. ConnectSocket = INVALID_SOCKET;
  140. continue;
  141. }
  142. break;
  143. }
  144. freeaddrinfo(result);
  145. return ConnectSocket;
  146. #endif //linux
  147. }
  148. int SendData(GuardianInfo& info,int &sendSocket)
  149. {
  150. //LOG4VTM(INFO, "SendData");
  151. #ifdef linux
  152. openlog("gdbase",LOG_CONS|LOG_PID,LOG_LOCAL0);
  153. syslog(LOG_ERR,"to SendData");
  154. int ConnectSocket = -1;
  155. ConnectSocket = GetSocket();
  156. if (ConnectSocket == -1) {
  157. //LOG4VTM(ERROR, "Unable to connect to server!" );
  158. return -1;
  159. }
  160. sendSocket = ConnectSocket;
  161. char sendbuf[DATA_BUFSIZE];
  162. memset(sendbuf, 0, sizeof(sendbuf));
  163. memcpy(sendbuf, &info, sizeof(GuardianInfo));
  164. send(sendSocket, sendbuf, sizeof(GuardianInfo), 0);//oiltmp size need to re calc
  165. syslog(LOG_ERR,"after send");
  166. return 0;
  167. #else
  168. SOCKET ConnectSocket = INVALID_SOCKET;
  169. ConnectSocket = GetSocket();
  170. if (ConnectSocket == INVALID_SOCKET) {
  171. printf("Unable to connect to server!\n");
  172. WSACleanup();
  173. return -1;
  174. }
  175. sendSocket = ConnectSocket;
  176. char sendbuf[DATA_BUFSIZE];
  177. int iResult;
  178. // Send an initial buffer
  179. memset(sendbuf, 0, sizeof(sendbuf));
  180. memcpy(sendbuf, &info, sizeof(GuardianInfo));
  181. iResult = send(ConnectSocket, sendbuf, (int)sizeof(sendbuf), 0);
  182. if (iResult == SOCKET_ERROR) {
  183. printf("send failed with error: %d\n", WSAGetLastError());
  184. closesocket(ConnectSocket);
  185. WSACleanup();
  186. return -1;
  187. }
  188. printf("Bytes Sent: %ld\n", iResult);
  189. // shutdown the connection since no more data will be sent
  190. iResult = shutdown(ConnectSocket, SD_SEND);
  191. if (iResult == SOCKET_ERROR) {
  192. printf("shutdown failed with error: %d\n", WSAGetLastError());
  193. closesocket(ConnectSocket);
  194. WSACleanup();
  195. return -1;
  196. }
  197. return 0;
  198. #endif //linux
  199. }
  200. void ReceiveDataOfOneWay(int socket)
  201. {
  202. #ifdef linux
  203. char recvbuf[DATA_BUFSIZE];
  204. int recvbuflen = DATA_BUFSIZE;
  205. bzero(recvbuf, DATA_BUFSIZE);
  206. int length = 0;
  207. while (length = recv(socket,recvbuf, DATA_BUFSIZE, 0))
  208. {
  209. if (length < 0)
  210. {
  211. printf("Recieve Data From Server Failed!\n");
  212. break;
  213. }
  214. bzero(recvbuf, DATA_BUFSIZE);
  215. }
  216. printf("Recieve From Server Finished\n");
  217. close(socket);
  218. #else
  219. char recvbuf[DATA_BUFSIZE];
  220. int recvbuflen = DATA_BUFSIZE;
  221. int iResult;
  222. // Receive until the peer closes the connection
  223. do {
  224. iResult = recv(socket, recvbuf, recvbuflen, 0);
  225. if (iResult > 0)
  226. {
  227. //printf("Bytes received: %d\n%s\n", iResult,recvbuf);
  228. switch (recvbuf[0])
  229. {
  230. default:
  231. break;
  232. }
  233. }
  234. else if (iResult == 0)
  235. printf("Connection closed\n");
  236. else
  237. printf("recv failed with error: %d\n", WSAGetLastError());
  238. } while (iResult > 0);
  239. // cleanup
  240. closesocket(socket);
  241. WSACleanup();
  242. #endif //linux
  243. }
  244. int ShakeHands(WorkStateEnum &eState)
  245. {
  246. GuardianInfo* pGdInfo = new GuardianInfo;
  247. memset(pGdInfo, 0, sizeof(GuardianInfo));
  248. pGdInfo->eType = GdOpShakeHand;
  249. int socket = -1;
  250. //LOG4VTM(INFO, "ShakeHands to SendData");
  251. int eErr = SendData(*pGdInfo, socket);
  252. if (eErr != 0)
  253. return eErr;
  254. char recvbuf[DATA_BUFSIZE];
  255. int recvbuflen = DATA_BUFSIZE;
  256. int iResult;
  257. // Receive until the peer closes the connection
  258. do {
  259. iResult = recv(socket, recvbuf, recvbuflen, 0);
  260. //LOG4VTM(INFO, "ShakeHands to recv:" << iResult << "recvbuflen:" << recvbuflen << "recvbuf:" << recvbuf);
  261. if (iResult > 0)
  262. {
  263. //printf("Bytes received: %d\n%s\n", iResult,recvbuf);
  264. switch (recvbuf[0])
  265. {
  266. case 'u':
  267. eState = WorkStateUpgrading;
  268. break;
  269. case 'g':
  270. eState = WorkStateGuardian;
  271. break;
  272. case 'r':
  273. eState = WorkStateReboot;
  274. break;
  275. case 'b':
  276. eState = WorkStateRollback;
  277. break;
  278. default:
  279. break;
  280. }
  281. }
  282. //else if (iResult == 0)
  283. // LOG4VTM(INFO, "Connection closed\n");
  284. //else
  285. // LOG4VTM(ERROR, "recv failed with error:" << iResult);
  286. } while( iResult > 0 );
  287. // cleanup
  288. #ifdef linux
  289. close(socket);
  290. #else
  291. closesocket(socket);
  292. WSACleanup();
  293. #endif
  294. return 0;
  295. }
  296. int PushUpdateTask(const char *pszPackName)
  297. {
  298. return 0;
  299. }
  300. bool IsInstalling()
  301. {
  302. return false;
  303. }
  304. int UpgradeRestart(const DWORD dwParam1,const DWORD dwParam2)
  305. {
  306. #ifdef linux
  307. openlog("gdbase",LOG_CONS|LOG_PID,LOG_LOCAL0);
  308. syslog(LOG_ERR,"UpgradeRestart");
  309. #endif
  310. GuardianInfo* pGdInfo = new GuardianInfo;
  311. memset(pGdInfo, 0, sizeof(GuardianInfo));
  312. pGdInfo->eType = GdOpUpgradeRestart;
  313. pGdInfo->dwParam1 = dwParam1;
  314. pGdInfo->dwParam2 = dwParam2;
  315. int socket = -1;
  316. int eErr = SendData(*pGdInfo, socket);
  317. if (eErr == 0)
  318. ReceiveDataOfOneWay(socket);
  319. if (pGdInfo != NULL)
  320. delete pGdInfo;
  321. return eErr;
  322. }
  323. int FrameworkQuit(int eReason)
  324. {
  325. #ifdef linux
  326. openlog("gdbase",LOG_CONS|LOG_PID,LOG_LOCAL0);
  327. syslog(LOG_ERR,"FrameworkQuit");
  328. #endif
  329. GuardianInfo* pGdInfo = new GuardianInfo;
  330. memset(pGdInfo, 0, sizeof(GuardianInfo));
  331. pGdInfo->eType = GdOpFrameQuit;
  332. int socket = -1;
  333. int eErr = SendData(*pGdInfo, socket);
  334. if (eErr == 0)
  335. ReceiveDataOfOneWay(socket);
  336. if (pGdInfo != NULL)
  337. delete pGdInfo;
  338. return eErr;
  339. }