|
|
@@ -3,27 +3,80 @@
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
#include "GuardianBase.h"
|
|
|
-#define WIN32_LEAN_AND_MEAN
|
|
|
-
|
|
|
|
|
|
+#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>
|
|
|
+#define HELLO_WORLD_SERVER_PORT 6666
|
|
|
+#define BUFFER_SIZE 1024
|
|
|
+#define FILE_NAME_MAX_SIZE 512
|
|
|
+#else
|
|
|
+#define WIN32_LEAN_AND_MEAN
|
|
|
#include <windows.h>
|
|
|
#include <winsock2.h>
|
|
|
#include <ws2tcpip.h>
|
|
|
-#include <stdlib.h>
|
|
|
-#include <stdio.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>
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
//#define DEFAULT_BUFLEN 2048
|
|
|
#define DEFAULT_PORT "30005"
|
|
|
const int default_port = 30005;
|
|
|
-SOCKET GetSocket()
|
|
|
+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)
|
|
|
+ {
|
|
|
+ printf("Create Socket Failed!\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ if (bind(client_socket, (struct sockaddr*) & client_addr, sizeof(client_addr)))
|
|
|
+ {
|
|
|
+ printf("Client Bind Port Failed!\n");
|
|
|
+ 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)
|
|
|
+ {
|
|
|
+ printf("Can Not Connect To server!\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ syslog(LOG_ERR,"after getsocket");
|
|
|
+
|
|
|
+ return client_socket;
|
|
|
+
|
|
|
+#else
|
|
|
WSADATA wsaData;
|
|
|
SOCKET ConnectSocket = INVALID_SOCKET;
|
|
|
struct addrinfo *result = NULL, *ptr = NULL,
|
|
|
@@ -109,9 +162,28 @@ SOCKET GetSocket()
|
|
|
freeaddrinfo(result);
|
|
|
|
|
|
return ConnectSocket;
|
|
|
+#endif //linux
|
|
|
}
|
|
|
-ErrorCodeEnum ShakeHands(WorkStateEnum &eState)
|
|
|
+ErrorCodeEnum SendData(GuardianInfo& info,int &sendSocket)
|
|
|
{
|
|
|
+#ifdef linux
|
|
|
+
|
|
|
+ openlog("gdbase",LOG_CONS|LOG_PID,LOG_LOCAL0);
|
|
|
+ syslog(LOG_ERR,"to SendData");
|
|
|
+ int ConnectSocket = -1;
|
|
|
+ ConnectSocket = GetSocket();
|
|
|
+
|
|
|
+ if (ConnectSocket == -1) {
|
|
|
+ printf("Unable to connect to server!\n");
|
|
|
+ return Error_IO;
|
|
|
+}
|
|
|
+ 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");
|
|
|
+#else
|
|
|
SOCKET ConnectSocket = INVALID_SOCKET;
|
|
|
ConnectSocket = GetSocket();
|
|
|
|
|
|
@@ -120,24 +192,16 @@ ErrorCodeEnum ShakeHands(WorkStateEnum &eState)
|
|
|
WSACleanup();
|
|
|
return Error_IO;
|
|
|
}
|
|
|
+ sendSocket = ConnectSocket;
|
|
|
char sendbuf[DATA_BUFSIZE];
|
|
|
char recvbuf[DATA_BUFSIZE];
|
|
|
int recvbuflen = DATA_BUFSIZE;
|
|
|
int iResult;
|
|
|
|
|
|
-
|
|
|
- if (ConnectSocket == INVALID_SOCKET) {
|
|
|
- printf("Unable to connect to server!\n");
|
|
|
- WSACleanup();
|
|
|
- return Error_IO;
|
|
|
- }
|
|
|
-
|
|
|
- // Send an initial buffer
|
|
|
- memset(sendbuf,0,sizeof(sendbuf));
|
|
|
- GuardianInfo* pGdInfo = new GuardianInfo;
|
|
|
- pGdInfo->eType = GdOpShakeHand;
|
|
|
- memcpy(sendbuf,pGdInfo,sizeof(GuardianInfo));
|
|
|
- iResult = send( ConnectSocket, sendbuf, (int)sizeof(sendbuf), 0 );
|
|
|
+ // 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);
|
|
|
@@ -146,20 +210,81 @@ ErrorCodeEnum ShakeHands(WorkStateEnum &eState)
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
+ iResult = shutdown(ConnectSocket, SD_SEND);
|
|
|
+ if (iResult == SOCKET_ERROR) {
|
|
|
+ printf("shutdown failed with error: %d\n", WSAGetLastError());
|
|
|
+ closesocket(ConnectSocket);
|
|
|
+ WSACleanup();
|
|
|
return Error_IO;
|
|
|
- }
|
|
|
+ }
|
|
|
+#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
|
|
|
+}
|
|
|
+ErrorCodeEnum ShakeHands(WorkStateEnum &eState)
|
|
|
+{
|
|
|
+ GuardianInfo* pGdInfo = new GuardianInfo;
|
|
|
+ memset(pGdInfo, 0, sizeof(GuardianInfo));
|
|
|
+ pGdInfo->eType = GdOpShakeHand;
|
|
|
+ int socket = -1;
|
|
|
+ ErrorCodeEnum eErr = SendData(*pGdInfo, socket);
|
|
|
+ if (eErr != Error_Succeed)
|
|
|
+ return eErr;
|
|
|
+ char recvbuf[DATA_BUFSIZE];
|
|
|
+ int recvbuflen = DATA_BUFSIZE;
|
|
|
+ int iResult;
|
|
|
// Receive until the peer closes the connection
|
|
|
do {
|
|
|
|
|
|
- iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
|
|
|
+ iResult = recv(socket, recvbuf, recvbuflen, 0);
|
|
|
if ( iResult > 0 )
|
|
|
{
|
|
|
//printf("Bytes received: %d\n%s\n", iResult,recvbuf);
|
|
|
@@ -184,13 +309,17 @@ ErrorCodeEnum ShakeHands(WorkStateEnum &eState)
|
|
|
else if ( iResult == 0 )
|
|
|
printf("Connection closed\n");
|
|
|
else
|
|
|
- printf("recv failed with error: %d\n", WSAGetLastError());
|
|
|
+ printf("recv failed with error\n");
|
|
|
|
|
|
} while( iResult > 0 );
|
|
|
|
|
|
// cleanup
|
|
|
- closesocket(ConnectSocket);
|
|
|
+#ifdef linux
|
|
|
+ close(socket);
|
|
|
+#else
|
|
|
+ closesocket(socket);
|
|
|
WSACleanup();
|
|
|
+#endif
|
|
|
|
|
|
return Error_Succeed;
|
|
|
}
|
|
|
@@ -204,137 +333,37 @@ bool IsInstalling()
|
|
|
}
|
|
|
ErrorCodeEnum UpgradeRestart(const DWORD dwParam1,const DWORD dwParam2)
|
|
|
{
|
|
|
- SOCKET ConnectSocket = INVALID_SOCKET;
|
|
|
- ConnectSocket = GetSocket();
|
|
|
-
|
|
|
- if (ConnectSocket == INVALID_SOCKET) {
|
|
|
- printf("Unable to connect to server!\n");
|
|
|
- WSACleanup();
|
|
|
- return Error_IO;
|
|
|
- }
|
|
|
- char sendbuf[DATA_BUFSIZE];
|
|
|
- char recvbuf[DATA_BUFSIZE];
|
|
|
- int recvbuflen = DATA_BUFSIZE;
|
|
|
- int iResult;
|
|
|
-
|
|
|
- // Send an initial buffer
|
|
|
- memset(sendbuf,0,sizeof(sendbuf));
|
|
|
+#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;
|
|
|
- memcpy(sendbuf,pGdInfo,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 Error_IO;
|
|
|
- }
|
|
|
-
|
|
|
- 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 Error_IO;
|
|
|
- }
|
|
|
-
|
|
|
- // Receive until the peer closes the connection
|
|
|
- do {
|
|
|
-
|
|
|
- iResult = recv(ConnectSocket, 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(ConnectSocket);
|
|
|
- WSACleanup();
|
|
|
-
|
|
|
- return Error_Succeed;
|
|
|
+ int socket = -1;
|
|
|
+ ErrorCodeEnum eErr = SendData(*pGdInfo, socket);
|
|
|
+ if (eErr == Error_Succeed)
|
|
|
+ ReceiveDataOfOneWay(socket);
|
|
|
+ if (pGdInfo != NULL)
|
|
|
+ delete pGdInfo;
|
|
|
+ return eErr;
|
|
|
}
|
|
|
ErrorCodeEnum FrameworkQuit(int eReason)
|
|
|
{
|
|
|
- SOCKET ConnectSocket = INVALID_SOCKET;
|
|
|
- ConnectSocket = GetSocket();
|
|
|
-
|
|
|
- if (ConnectSocket == INVALID_SOCKET) {
|
|
|
- printf("Unable to connect to server!\n");
|
|
|
- WSACleanup();
|
|
|
- return Error_IO;
|
|
|
- }
|
|
|
- char sendbuf[DATA_BUFSIZE];
|
|
|
- char recvbuf[DATA_BUFSIZE];
|
|
|
- int recvbuflen = DATA_BUFSIZE;
|
|
|
- int iResult;
|
|
|
- // Send an initial buffer
|
|
|
- memset(sendbuf,0,sizeof(sendbuf));
|
|
|
+#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;
|
|
|
- memcpy(sendbuf,pGdInfo,sizeof(GuardianInfo));
|
|
|
- iResult = send( ConnectSocket, sendbuf, (int)sizeof(sendbuf), 0 );
|
|
|
- if (iResult == SOCKET_ERROR) {
|
|
|
- printf("send failed with error: %d\n", WSAGetLastError());
|
|
|
- closesocket(ConnectSocket);
|
|
|
+ int socket = -1;
|
|
|
+ ErrorCodeEnum eErr = SendData(*pGdInfo, socket);
|
|
|
+ if (eErr == Error_Succeed)
|
|
|
+ ReceiveDataOfOneWay(socket);
|
|
|
+ if (pGdInfo != NULL)
|
|
|
delete pGdInfo;
|
|
|
- WSACleanup();
|
|
|
- return Error_IO;
|
|
|
- }
|
|
|
-
|
|
|
- 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);
|
|
|
- delete pGdInfo;
|
|
|
- WSACleanup();
|
|
|
- return Error_IO;
|
|
|
- }
|
|
|
-
|
|
|
- // Receive until the peer closes the connection
|
|
|
- do {
|
|
|
-
|
|
|
- iResult = recv(ConnectSocket, 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(ConnectSocket);
|
|
|
- WSACleanup();
|
|
|
-
|
|
|
- return Error_Succeed;
|
|
|
-
|
|
|
+ return eErr;
|
|
|
}
|