GuardianBase.cpp 8.6 KB

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