// test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "stdafx.h"
#include "framework.h"
#include "test.h"
// winsocket库;
#include <Ws2tcpip.h>
#pragma comment(lib,"Ws2_32.lib")
#include <thread>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 唯一的应用程序对象

CWinApp theApp;

using namespace std;

#define BUF_LEN 8192
class logClient
{
public:
	logClient() {
		m_port = 0;
		m_sock = INVALID_SOCKET;
	};

	~logClient() {
		disconnect();
		WSACleanup();
	};

private:
	// 客户端套接字;
	SOCKET m_sock;
	// 客户端地址;
	sockaddr_in m_sin;
	// ip;
	std::string m_ip;
	// port;
	int m_port;

public:
	bool InitSocket() {
		WSADATA wsaData;
		return (NO_ERROR == WSAStartup(MAKEWORD(2, 2), &wsaData));
	}

	bool Connect(std::string ip, int port) {
		// 创建;
		m_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
		if (m_sock == INVALID_SOCKET)
			return false;

		// 连接服务器;
		m_sin.sin_family = AF_INET;
		m_sin.sin_port = htons(port);
		if (-1 == inet_pton(AF_INET, ip.c_str(), &m_sin.sin_addr))
		{
			return false;
		}

		if (connect(m_sock, (LPSOCKADDR)& m_sin, sizeof(m_sin)) == SOCKET_ERROR)
		{
			//Global::WriteTextLog(_T("连接服务端失败：%d"), WSAGetLastError());
			closesocket(m_sock);
			return false;
		}

		// 设置收发超时;
		int nNetTimeout = 3000; //1秒
		//发送时限
		setsockopt(m_sock, SOL_SOCKET, SO_SNDTIMEO, (char*)& nNetTimeout, sizeof(int));
		//接收时限
		setsockopt(m_sock, SOL_SOCKET, SO_RCVTIMEO, (char*)& nNetTimeout, sizeof(int));

		// 设置缓存大小;
		int bufSize = BUF_LEN;
		setsockopt(m_sock, SOL_SOCKET, SO_SNDBUF, (char*)& bufSize, sizeof(int));
		setsockopt(m_sock, SOL_SOCKET, SO_RCVBUF, (char*)& bufSize, sizeof(int));

		return true;
	};

	void disconnect() {
		if (m_sock != INVALID_SOCKET)
		{
			shutdown(m_sock, SD_BOTH);
			closesocket(m_sock);
		}
	}

	void ReConnect(DWORD dwError)
	{
		bool bReConnect = false;
		// send/recv error;
		if (dwError == WSAENETRESET)
		{
			bReConnect = true;
			//Global::WriteTextLog(_T("由于保持活动活动在操作正在进行时检测到故障，因此连接已断开。"));
		}
		else if (dwError == WSAECONNABORTED)
		{
			bReConnect = true;
			//Global::WriteTextLog(_T("由于超时或其他故障，虚电路终止。应用程序应关闭套接字，因为它不再可用。"));
		}
		else if (dwError == WSAECONNRESET)
		{
			bReConnect = true;
			//Global::WriteTextLog(_T("虚拟电路由远程端执行硬关闭或中止关闭重置。对于UDP套接字，远程主机无法传送先前发送的UDP数据报，并使用“端口无法访问”ICMP数据包进行响应。应用程序应关闭套接字，因为它不再可用。"));
		}
		else if (dwError == WSAETIMEDOUT)
		{//由于网络故障或另一端的系统在没有通知的情况下发生故障，连接已被丢弃。
			bReConnect = true;
			//Global::WriteTextLog(_T("由于超时或其他故障，虚电路终止。应用程序应关闭套接字，因为它不再可用。"));
		}

		if (bReConnect)
		{
			disconnect();
			Connect(m_ip, m_port);
		}
	}

	bool Send(std::string s_data)
	{
		TCHAR szLogMsg[BUF_LEN] = {0};
		//CString date = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));
		_stprintf_s(szLogMsg, 
			_T("{\"ReportType\":\"printLog\",\"prinMsg\":\"%s %s\"}"), 
			CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")).GetString(), s_data.c_str());

		if (m_sock == INVALID_SOCKET)
		{
			//Global::WriteTextLog(_T("SOCKET未创建, 请创建并连接服务器"));
			return false;
		}

		// 向服务器发送数据;
		int nRet = send(m_sock, szLogMsg, _tcslen(szLogMsg)*sizeof(TCHAR), 0);
		if (nRet < 0)
		{
			DWORD dwError = WSAGetLastError();
			//Global::WriteTextLog(_T("发送数据失败：%d"), dwError);
			ReConnect(dwError);
			return false;
		}

		return true;
	}
};

int main()
{
    int nRetCode = 0;

    HMODULE hModule = ::GetModuleHandle(nullptr);

    if (hModule != nullptr)
    {
        // 初始化 MFC 并在失败时显示错误
        if (!AfxWinInit(hModule, nullptr, ::GetCommandLine(), 0))
        {
            // TODO: 在此处为应用程序的行为编写代码。
            wprintf(L"错误: MFC 初始化失败\n");
            nRetCode = 1;
        }
        else
        {
#if 1 // 验证logModule多线程下写日志是否线程安全;
			for ( int i = 0; i < 20; i++ )
			{
				// 创建10个线程;
				std::thread t([]() {
					TCHAR szLogMsg[1024] = {0};
					_stprintf_s(szLogMsg, _T("多线程抢占文件句柄：%ld"), (unsigned int)&std::this_thread::get_id());

					logClient log;
					if (log.InitSocket())
					{
						if ( log.Connect("127.0.0.1", 10006) )
						{
							while (true)
							{
								Sleep(20);
								log.Send(szLogMsg);
							}
						}
					}				
				});
				t.detach();
			}

			system("pause");

			return 0;
#endif
			if (LoadLogLibarary())
			{
				// 启动服务;
				if (g_IsAppRunning(_T("C:\\Python27\\Tools\\RedRatHub-V4.28\\RedRatHubCmd.exe")) )
					TRACE("启动红老鼠进程已运行\r");
				else
				{
					TRACE("启动红老鼠进程未运行\r");
					if (g_StartIRApp(_T("C:\\Python27\\Tools\\RedRatHub-V4.28\\RedRatHubCmd.exe"), _T("C:\\Python27\\Tools\\RedRatHub-V4.28\\DeviceDB.xml"), 40000))
					{
						TRACE("启动红老鼠进程启动成功\r");
					}
					else
					{
						TRACE("启动红老鼠进程启动失败\r");
					}
				}

				Sleep(1000); // 等待进程启动完全;
				// 连接服务器;
				if (!g_Connect(_T("127.0.0.1"), 40000))
					return 0;

				// 获取设备列表,以逗号分隔;
				std::string devices = g_getDeviceNames();

				// 加载指定目录的xml信号集文件;
				if ( !g_loadSignalDataSet("D:\\SAT\\resource\\RCU\\product\\ARTEL_WZ_RC311_2841.xml") )
				{
					TRACE("加载信号集文件失败\r");
				}
				else
				{
					TRACE("加载信号集文件成功\r");
				}

				Sleep(500);
				// 获取信号数据集，以\n分隔;
				std::string signals = g_getSignalsName();
				TRACE(signals.c_str());

				// 发送单信号;
				if ( g_sendSignal("HOME", 1, 1000) )
				{ 
					TRACE("发送单信号成功\n");
				}
				else
				{
					TRACE("发送单信号失败\n");
				}

				// 发送多信号;
				if (g_sendSignals("HOME;right;right;down;up;HOME", 2))
				{
					TRACE("发送多信号成功\n");
				}
				else
				{
					TRACE("发送多信号失败\n");
				}

				// 发送重复信号;//重复信号必须小于256
				if (g_sendRepeatsSignal("right", 2))
				{
					TRACE("发送多信号成功\n");
				}
				else
				{
					TRACE("发送多信号失败\n");
				}

				g_CloseApp();
				FreeLogLibarary();
			}
        }
    }
    else
    {
        // TODO: 更改错误代码以符合需要
        wprintf(L"错误: GetModuleHandle 失败\n");
        nRetCode = 1;
    }

    return nRetCode;
}
