#include <winsock2.h>
#include <ws2tcpip.h>
#include <tchar.h>
#include "KeepAlive.h"
#include "ECTrainer.h"
#include "myWinUtils.h"
// コンストラクタ
KeepAlive::KeepAlive(ECTrainer* pEct)
: BaseProcess(pEct)
, _sent(false)
, _socket(NULL)
, _count(0) {
_socketAddrLen = sizeof(_socketAddr);
memset((char*)&_socketAddr, 0, _socketAddrLen);
}
// 初期化
bool KeepAlive::Init() {
#if defined(EYEDEVICE_GLASS2)
// Initialize winsock
WSADATA wsa;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
mwut::DebugPrintf(_T("Initialising Winsock Failed with error Code : %d\n"), WSAGetLastError());
return false;
}
// Create socket
if ((_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR) {
mwut::DebugPrintf(_T("Creating socket failed with error Code : %d\n"), WSAGetLastError());
return false;
}
// setup address structure
_socketAddr.sin_family = AF_INET;
_socketAddr.sin_port = htons(PORT);
InetPton(_socketAddr.sin_family, _T(ADDR), &_socketAddr.sin_addr.S_un.S_addr);
#endif
return true;
}
// メインループ
bool KeepAlive::Routine() {
#if defined(EYEDEVICE_GLASS2)
const std::string KA_DATA_MSG = "{\"type\": \"live.data.unicast\", \"key\": \"some_GUID\", \"op\": \"start\"}";
char message[SOCKET_BUF_LEN];
strcpy_s(message, SOCKET_BUF_LEN, KA_DATA_MSG.c_str());
Sleep(100);
if (++_count < KEEP_ALIVE_WAIT_COUNT) return true;
_count = 0;
if (sendto(_socket, message, (int)strlen(message), 0,
(struct sockaddr*)&_socketAddr, _socketAddrLen) == SOCKET_ERROR) {
mwut::DebugPrintf(_T("Keep Alive Data Sent Error\n"));
} else {
_sent = true;
}
#endif
return true;
}