123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- // GuardianBase.cpp : Defines the exported functions for the DLL application.
- //
- #include "stdafx.h"
- #ifdef linux
- #include <netinet/in.h> // for sockaddr_in
- #include <sys/types.h> // for socket
- #include <sys/socket.h> // for socket
- #include <arpa/inet.h>
- #include <string.h> // for bzero
- #include <fcntl.h>
- #include <unistd.h>
- #include <syslog.h>
- #include "GuardianBase.h"
- #define HELLO_WORLD_SERVER_PORT 6666
- #define BUFFER_SIZE 1024
- #define FILE_NAME_MAX_SIZE 512
- #else
- #ifndef WIN32_LEAN_AND_MEAN
- #define WIN32_LEAN_AND_MEAN
- #endif
- #include <windows.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include "GuardianBase.h"
- // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
- #pragma comment (lib, "Ws2_32.lib")
- #pragma comment (lib, "Mswsock.lib")
- #pragma comment (lib, "AdvApi32.lib")
- #endif //linux
- #include <stdlib.h>
- #include <stdio.h>
- #include "log4rvcother.h"
- #define DEFAULT_PORT "30005"
- const int default_port = 30005;
- int GetSocket()
- {
- #ifdef linux
- openlog("gdbase",LOG_CONS|LOG_PID,LOG_LOCAL0);
- syslog(LOG_ERR,"enter GetSocket");
- struct sockaddr_in client_addr;
- bzero(&client_addr, sizeof(client_addr));
- client_addr.sin_family = AF_INET;
- client_addr.sin_addr.s_addr = htons(INADDR_ANY);
- client_addr.sin_port = htons(0);
- int client_socket = socket(AF_INET, SOCK_STREAM, 0);
- if (client_socket < 0)
- {
- //LOG4VTM(ERROR, "Create Socket Failed!");
- return -1;
- }
- if (bind(client_socket, (struct sockaddr*) & client_addr, sizeof(client_addr)))
- {
- //LOG4VTM(ERROR, "Client Bind Port Failed!");
- return -1;
- }
- struct sockaddr_in server_addr;
- bzero(&server_addr, sizeof(server_addr));
- server_addr.sin_family = AF_INET;
- char* local_addr = "127.0.0.1";
- inet_aton(local_addr, &(server_addr.sin_addr));
- server_addr.sin_port = htons(default_port);
- socklen_t server_addr_length = sizeof(server_addr);
- //向服务器发起连接,连接成功后client_socket代表了客户机和服务器的一个socket连接
- if (connect(client_socket, (struct sockaddr*) & server_addr, server_addr_length) < 0)
- {
- //LOG4VTM(ERROR, "Can Not Connect To server!");
- return -1;
- }
- syslog(LOG_ERR,"after getsocket");
- return client_socket;
- #else
- WSADATA wsaData;
- SOCKET ConnectSocket = INVALID_SOCKET;
- struct addrinfo *result = NULL, *ptr = NULL,
- hints;
- int iResult;
- // Initialize Winsock
- iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (iResult != 0) {
- printf("WSAStartup failed with error: %d\n", iResult);
- return ConnectSocket;
- }
- ZeroMemory(&hints, sizeof(hints));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- // Resolve the server address and port
- iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
- if (iResult != 0) {
- printf("getaddrinfo failed with error: %d\n", iResult);
- WSACleanup();
- return ConnectSocket;
- }
- // 超时时间
- struct timeval tm;
- tm.tv_sec = 5;
- tm.tv_usec = 0;
- // Attempt to connect to an address until one succeeds
- for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
- // Create a SOCKET for connecting to server
- ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
- ptr->ai_protocol);
- if (ConnectSocket == INVALID_SOCKET) {
- printf("socket failed with error: %ld\n", WSAGetLastError());
- WSACleanup();
- return ConnectSocket;
- }
- // 设置为非阻塞的socket
- int iMode = 1;
- ioctlsocket(ConnectSocket, FIONBIO, (u_long FAR*)&iMode);
- bool isConnected = false;
- // Connect to server.
- iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
- if (iResult != SOCKET_ERROR)
- {
- isConnected = true;
- }
- else
- {
- fd_set set;
- FD_ZERO(&set);
- FD_SET(ConnectSocket, &set);
- //oiltmp need or not? FD_ISSET
- if (select(-1, NULL, &set, NULL, &tm) <= 0)
- {
- isConnected = false;
- }
- else
- {
- int error = -1;
- int optLen = sizeof(int);
- getsockopt(ConnectSocket, SOL_SOCKET, SO_ERROR, (char*)&error, &optLen);
- if (0 == error)
- isConnected = true;
- }
- }
- if (!isConnected) {
- closesocket(ConnectSocket);
- ConnectSocket = INVALID_SOCKET;
- continue;
- }
- break;
- }
- freeaddrinfo(result);
- return ConnectSocket;
- #endif //linux
- }
- int SendData(GuardianInfo& info,int &sendSocket)
- {
- //LOG4VTM(INFO, "SendData");
- #ifdef linux
- openlog("gdbase",LOG_CONS|LOG_PID,LOG_LOCAL0);
- syslog(LOG_ERR,"to SendData");
- int ConnectSocket = -1;
- ConnectSocket = GetSocket();
- if (ConnectSocket == -1) {
- //LOG4VTM(ERROR, "Unable to connect to server!" );
- return -1;
- }
- sendSocket = ConnectSocket;
- char sendbuf[DATA_BUFSIZE];
- memset(sendbuf, 0, sizeof(sendbuf));
- memcpy(sendbuf, &info, sizeof(GuardianInfo));
- send(sendSocket, sendbuf, sizeof(GuardianInfo), 0);//oiltmp size need to re calc
- syslog(LOG_ERR,"after send");
- return 0;
- #else
- SOCKET ConnectSocket = INVALID_SOCKET;
- ConnectSocket = GetSocket();
- if (ConnectSocket == INVALID_SOCKET) {
- printf("Unable to connect to server!\n");
- WSACleanup();
- return -1;
- }
- sendSocket = ConnectSocket;
- char sendbuf[DATA_BUFSIZE];
- int iResult;
- // Send an initial buffer
- memset(sendbuf, 0, sizeof(sendbuf));
- memcpy(sendbuf, &info, sizeof(GuardianInfo));
- iResult = send(ConnectSocket, sendbuf, (int)sizeof(sendbuf), 0);
- if (iResult == SOCKET_ERROR) {
- printf("send failed with error: %d\n", WSAGetLastError());
- closesocket(ConnectSocket);
- WSACleanup();
- return -1;
- }
- printf("Bytes Sent: %ld\n", iResult);
- // shutdown the connection since no more data will be sent
- iResult = shutdown(ConnectSocket, SD_SEND);
- if (iResult == SOCKET_ERROR) {
- printf("shutdown failed with error: %d\n", WSAGetLastError());
- closesocket(ConnectSocket);
- WSACleanup();
- return -1;
- }
- return 0;
- #endif //linux
- }
- void ReceiveDataOfOneWay(int socket)
- {
- #ifdef linux
- char recvbuf[DATA_BUFSIZE];
- int recvbuflen = DATA_BUFSIZE;
- bzero(recvbuf, DATA_BUFSIZE);
- int length = 0;
- while (length = recv(socket,recvbuf, DATA_BUFSIZE, 0))
- {
- if (length < 0)
- {
- printf("Recieve Data From Server Failed!\n");
- break;
- }
- bzero(recvbuf, DATA_BUFSIZE);
- }
- printf("Recieve From Server Finished\n");
- close(socket);
- #else
- char recvbuf[DATA_BUFSIZE];
- int recvbuflen = DATA_BUFSIZE;
- int iResult;
- // Receive until the peer closes the connection
- do {
- iResult = recv(socket, recvbuf, recvbuflen, 0);
- if (iResult > 0)
- {
- //printf("Bytes received: %d\n%s\n", iResult,recvbuf);
- switch (recvbuf[0])
- {
- default:
- break;
- }
- }
- else if (iResult == 0)
- printf("Connection closed\n");
- else
- printf("recv failed with error: %d\n", WSAGetLastError());
- } while (iResult > 0);
- // cleanup
- closesocket(socket);
- WSACleanup();
- #endif //linux
- }
- int ShakeHands(WorkStateEnum &eState)
- {
- GuardianInfo* pGdInfo = new GuardianInfo;
- memset(pGdInfo, 0, sizeof(GuardianInfo));
- pGdInfo->eType = GdOpShakeHand;
- int socket = -1;
- //LOG4VTM(INFO, "ShakeHands to SendData");
- int eErr = SendData(*pGdInfo, socket);
- if (eErr != 0)
- return eErr;
- char recvbuf[DATA_BUFSIZE];
- int recvbuflen = DATA_BUFSIZE;
- int iResult;
- // Receive until the peer closes the connection
- do {
- iResult = recv(socket, recvbuf, recvbuflen, 0);
- //LOG4VTM(INFO, "ShakeHands to recv:" << iResult << "recvbuflen:" << recvbuflen << "recvbuf:" << recvbuf);
- if (iResult > 0)
- {
- //printf("Bytes received: %d\n%s\n", iResult,recvbuf);
- switch (recvbuf[0])
- {
- case 'u':
- eState = WorkStateUpgrading;
- break;
- case 'g':
- eState = WorkStateGuardian;
- break;
- case 'r':
- eState = WorkStateReboot;
- break;
- case 'b':
- eState = WorkStateRollback;
- break;
- default:
- break;
- }
- }
- //else if (iResult == 0)
- // LOG4VTM(INFO, "Connection closed\n");
- //else
- // LOG4VTM(ERROR, "recv failed with error:" << iResult);
- } while( iResult > 0 );
- // cleanup
- #ifdef linux
- close(socket);
- #else
- closesocket(socket);
- WSACleanup();
- #endif
- return 0;
- }
- int PushUpdateTask(const char *pszPackName)
- {
- return 0;
- }
- bool IsInstalling()
- {
- return false;
- }
- int UpgradeRestart(const DWORD dwParam1,const DWORD dwParam2)
- {
- #ifdef linux
- openlog("gdbase",LOG_CONS|LOG_PID,LOG_LOCAL0);
- syslog(LOG_ERR,"UpgradeRestart");
- #endif
- GuardianInfo* pGdInfo = new GuardianInfo;
- memset(pGdInfo, 0, sizeof(GuardianInfo));
- pGdInfo->eType = GdOpUpgradeRestart;
- pGdInfo->dwParam1 = dwParam1;
- pGdInfo->dwParam2 = dwParam2;
- int socket = -1;
- int eErr = SendData(*pGdInfo, socket);
- if (eErr == 0)
- ReceiveDataOfOneWay(socket);
- if (pGdInfo != NULL)
- delete pGdInfo;
- return eErr;
- }
- int FrameworkQuit(int eReason)
- {
- #ifdef linux
- openlog("gdbase",LOG_CONS|LOG_PID,LOG_LOCAL0);
- syslog(LOG_ERR,"FrameworkQuit");
- #endif
- GuardianInfo* pGdInfo = new GuardianInfo;
- memset(pGdInfo, 0, sizeof(GuardianInfo));
- pGdInfo->eType = GdOpFrameQuit;
- int socket = -1;
- int eErr = SendData(*pGdInfo, socket);
- if (eErr == 0)
- ReceiveDataOfOneWay(socket);
- if (pGdInfo != NULL)
- delete pGdInfo;
- return eErr;
- }
|