// interface.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "interface1.h"
#include "winsock2.h"
#include "namepipe.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define DATASIZE	1024

int Version = 0;

BEGIN_MESSAGE_MAP(CinterfaceApp, CWinApp)
END_MESSAGE_MAP()




GuaJiCallBack guajicallback = NULL;
BonusCallBack bonuscallback = NULL;;

// CinterfaceApp construction

CinterfaceApp::CinterfaceApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}


// The one and only CinterfaceApp object

CinterfaceApp theApp;

int SendIdleToServer(void)
{
	unsigned char SendData[DATASIZE] = {0,};
	struct sockaddr_in m_addr;

	SOCKET socket1 = 0;	

	if(socket1 == 0)
	{
		socket1 = socket(AF_INET,SOCK_STREAM,0);
		ASSERT(socket1 != 0);
	}
	unsigned int a = 127;
	unsigned int b = 0;
	unsigned int c = 0;
	unsigned int d = 1;
	m_addr.sin_family = AF_INET;
	m_addr.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a;
	m_addr.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)b;
	m_addr.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)c;
	m_addr.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)d;
	m_addr.sin_port = htons(PORT);

	int ret = connect(socket1,(LPSOCKADDR)&m_addr,sizeof(m_addr));
	if(ret == SOCKET_ERROR)
	{
		printf("connect error\n");	
		return 0;
	}

	*(int*)&SendData[0] = 9;
	SendData[4] = PROTOCOL_IDLE;
	*(int*)&SendData[5] = Version;

	ret = send(socket1,(char*)SendData,9,0);
	if(ret == -1)
	{
		printf("send error\n");
		return 0;
	}
	closesocket(socket1);
	return 1;

}

int SendInformationToServer(char *information)
{
	unsigned char SendData[DATASIZE] = {0,};
	struct sockaddr_in m_addr;

	SOCKET socket1 = 0;	

	if(socket1 == 0)
	{
		socket1 = socket(AF_INET,SOCK_STREAM,0);
		ASSERT(socket1 != 0);
	}
	unsigned int a = 127;
	unsigned int b = 0;
	unsigned int c = 0;
	unsigned int d = 1;
	m_addr.sin_family = AF_INET;
	m_addr.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a;
	m_addr.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)b;
	m_addr.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)c;
	m_addr.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)d;
	m_addr.sin_port = htons(PORT);

	int ret = connect(socket1,(LPSOCKADDR)&m_addr,sizeof(m_addr));
	if(ret == SOCKET_ERROR)
	{
		printf("connect error\n");	
		return 0;
	}

	*(int*)&SendData[0] = 5 + strlen(information) + 1;
	SendData[4] = PROTOCOL_INFORMATION;
	strcpy((char*)&SendData[5],information);

	ret = send(socket1,(char*)SendData,5 + strlen(information) + 1,0);
	if(ret == -1)
	{
		printf("send error\n");
		return 0;
	}
	closesocket(socket1);
	return 1;

}


int SendAlarmToServer(int flag,char *alarminfo)
{
	unsigned char SendData[DATASIZE] = {0,};
	struct sockaddr_in m_addr;

	SOCKET socket1 = 0;	

	if(socket1 == 0)
	{
		socket1 = socket(AF_INET,SOCK_STREAM,0);
		ASSERT(socket1 != 0);
	}
	unsigned int a = 127;
	unsigned int b = 0;
	unsigned int c = 0;
	unsigned int d = 1;
	m_addr.sin_family = AF_INET;
	m_addr.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a;
	m_addr.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)b;
	m_addr.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)c;
	m_addr.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)d;
	m_addr.sin_port = htons(PORT);

	int ret = connect(socket1,(LPSOCKADDR)&m_addr,sizeof(m_addr));
	if(ret == SOCKET_ERROR)
	{
		printf("connect error\n");	
		return 0;
	}

	*(int*)&SendData[0] = 6 + strlen(alarminfo) + 1;
	SendData[4] = PROTOCOL_ALARM;
	SendData[5] = flag;
	strcpy((char*)&SendData[6],alarminfo);

	ret = send(socket1,(char*)SendData,6 + strlen(alarminfo) + 1,0);
	if(ret == -1)
	{
		printf("send error\n");
		return 0;
	}
	closesocket(socket1);
	return 1;

}

int SendBanedToServer(int flag)
{
	unsigned char SendData[DATASIZE] = {0,};
	struct sockaddr_in m_addr;

	SOCKET socket1 = 0;	

	if(socket1 == 0)
	{
		socket1 = socket(AF_INET,SOCK_STREAM,0);
		ASSERT(socket1 != 0);
	}
	unsigned int a = 127;
	unsigned int b = 0;
	unsigned int c = 0;
	unsigned int d = 1;
	m_addr.sin_family = AF_INET;
	m_addr.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a;
	m_addr.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)b;
	m_addr.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)c;
	m_addr.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)d;
	m_addr.sin_port = htons(PORT);

	int ret = connect(socket1,(LPSOCKADDR)&m_addr,sizeof(m_addr));
	if(ret == SOCKET_ERROR)
	{
		printf("connect error\n");	
		return 0;
	}

	*(int*)&SendData[0] = 6;
	SendData[4] = PROTOCOL_BANED;
	SendData[5] = flag;

	ret = send(socket1,(char*)SendData,6,0);
	if(ret == -1)
	{
		printf("send error\n");
		return 0;
	}
	closesocket(socket1);
	return 1;

}


int SendLogToServer(char *Log)
{
	unsigned char SendData[DATASIZE] = {0,};
	struct sockaddr_in m_addr;

	SOCKET socket1 = 0;	

	if(socket1 == 0)
	{
		socket1 = socket(AF_INET,SOCK_STREAM,0);
		ASSERT(socket1 != 0);
	}
	unsigned int a = 127;
	unsigned int b = 0;
	unsigned int c = 0;
	unsigned int d = 1;
	m_addr.sin_family = AF_INET;
	m_addr.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a;
	m_addr.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)b;
	m_addr.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)c;
	m_addr.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)d;
	m_addr.sin_port = htons(PORT);

	int ret = connect(socket1,(LPSOCKADDR)&m_addr,sizeof(m_addr));
	if(ret == SOCKET_ERROR)
	{
		printf("connect error\n");	
		return 0;
	}

	*(int*)&SendData[0] = 5 + strlen(Log) + 1;
	SendData[4] = PROTOCOL_LOG;
	strcpy((char*)&SendData[5],Log);

	ret = send(socket1,(char*)SendData,5 + strlen(Log) + 1,0);
	if(ret == -1)
	{
		printf("send error\n");
		return 0;
	}
	closesocket(socket1);
	return 1;

}


DWORD WINAPI TimerTaskThread(LPVOID lpParameter)
{
	while(TRUE)
	{
		SendIdleToServer();
		Sleep(2000);

	}

	return 0; 
}

void SetVersion(int ver)
{
	Version = ver;
}

HANDLE hThreadCreatePipes = 0;

void SetGuajiCallBack(DWORD callbackfunc)
{
	guajicallback = (GuaJiCallBack)callbackfunc;
}

void SetBonusCallBack(DWORD callbackfunc)
{
	bonuscallback = (BonusCallBack)callbackfunc;
}

void InitInterface(void)
{
	if(hThreadCreatePipes == 0)
	{

		WSADATA wsaData;
		DWORD Ret;
		if ((Ret = WSAStartup(0x0202, &wsaData)) != 0)
		{
			return;
		}


		DWORD ThreadId;
		HANDLE Thread1 = ::CreateThread(NULL, 0, TimerTaskThread, 0, 0, &ThreadId);
		CloseHandle(Thread1);

		// Handle of create Named Pipe thread.
	

		// Create Named Pipe thread .
		hThreadCreatePipes = ::CreateThread( 
			NULL,              // no security attribute 
			0,                 // default stack size 
			FUNCreatePipes,    // thread proc
			0,              // thread parameter 
			0,                 // not suspended 
			NULL);             // returns thread ID 
		// Close Handle.
		CloseHandle(hThreadCreatePipes); 
		// Set handle null.
	}

}
// CinterfaceApp initialization

BOOL CinterfaceApp::InitInstance()
{
	CWinApp::InitInstance();

	InitInterface();

	return TRUE;
}


int CinterfaceApp::ExitInstance()
{
	// TODO: Add your specialized code here and/or call the base class

	return CWinApp::ExitInstance();
}
