
// ClientTestDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "ClientTest.h"
#include "ClientTestDlg.h"

#include "IClientImpl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CClientTestDlg 对话框




CClientTestDlg::CClientTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CClientTestDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CClientTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CClientTestDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(Btn_ConnectServer, &CClientTestDlg::OnBnClickedConnectserver)
	ON_BN_CLICKED(Btn_DisConnectServer, &CClientTestDlg::OnBnClickedDisconnectserver)
	ON_BN_CLICKED(Btn_SendMsg, &CClientTestDlg::OnBnClickedSendmsg)
	ON_BN_CLICKED(Btn_SendFile, &CClientTestDlg::OnBnClickedSendfile)
	ON_BN_CLICKED(Btn_ConnectDB, &CClientTestDlg::OnBnClickedConnectdb)
END_MESSAGE_MAP()


// CClientTestDlg 消息处理程序

BOOL CClientTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	GetClientIniInfo();
	CSocketHandle::InitLibrary( MAKEWORD(2,2) );
	for ( int i = 0; i < MAX_CLIENT; i++)
	{
		m_IClientImpl[i] = NULL;
	}

	for ( int i = 0; i < MAX_CLIENT; i++)
	{
		m_IClientImpl[i] = new IClientImpl();
	}

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CClientTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CClientTestDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CClientTestDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CClientTestDlg::OnBnClickedConnectserver()
{
	// TODO: 在此添加控件通知处理程序代码
	//IClientImpl::GetInstancePtr(0)->ConnectServer(_T("192.168.86.1"),_T("64320"));
	for ( int i = 0; i < MAX_CLIENT; i++)
	{
		if( m_IClientImpl[i]->ConnectServer(_T(g_szServerIP),_T(g_szCmdPort)) )
			m_IClientImpl[i]->StartReConnectSrvThread();
		else
			LOG4C((LOG_NOTICE,"连接失败:%d",i));

		Sleep(20);
	}
}

void CClientTestDlg::OnBnClickedDisconnectserver()
{
	// TODO: 在此添加控件通知处理程序代码
	//ClientImpl::GetInstancePtr(0)->DisConnectServer();

	for ( int i = 0; i < MAX_CLIENT; i++)
	{
		m_IClientImpl[i]->DisConnectServer();
	}
}

void CClientTestDlg::OnBnClickedSendmsg()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strSend;
	GetDlgItemText(IDC_EDIT1,strSend);
	
#if 0
	STChatbody tChatbody;
	tChatbody.tPHeader.nCmd = CMD_TOCHAT;
	tChatbody.tPHeader.nCmdType = 0;
	tChatbody.tPHeader.nDataLen = strSend.GetLength();
	tChatbody.tPHeader.nVerify = crc32(0,reinterpret_cast<const unsigned char*>(strSend.GetBuffer()),strSend.GetLength());
	strSend.ReleaseBuffer();
	memset(tChatbody.szChat,0,1024*10);
	memcpy(tChatbody.szChat,strSend.GetBuffer(),strSend.GetLength());
	strSend.ReleaseBuffer();

	//IClientImpl::GetInstancePtr(0)->m_SocketClient->Write((const LPBYTE)&tChatbody,sizeof(STChatbody));
	for ( int i = 0; i < MAX_CLIENT; i++)
	{
		m_IClientImpl[i]->m_SocketClient->Write((const LPBYTE)&tChatbody,sizeof(STChatbody));
	}
#else
	pSTChatbody pChatbody = new STChatbody();
	//pChatbody->tPHeader.nCmd = CMD_TOCHAT;
	//pChatbody->tPHeader.nCmdType = 0;
	//pChatbody->tPHeader.nDataLen = sizeof(STChatbody);
	//pChatbody->tPHeader.nVerify = crc32(0,reinterpret_cast<const unsigned char*>(strSend.GetBuffer()),strSend.GetLength());
	
	//strSend.ReleaseBuffer();
	memset(pChatbody->szChat,0,MAX_CHATLENGTH);
	memcpy(pChatbody->szChat,strSend.GetBuffer(),strSend.GetLength());
	strSend.ReleaseBuffer();

	pChatbody->tPHeader.nVerify = VerifyIntegrityPacket(pChatbody,sizeof(STChatbody));

	LOG4C((LOG_NOTICE,"协议包大小：%d,文字长度：%d,内容：%s,校验：%d",pChatbody->tPHeader.nDataLen,strSend.GetLength(),pChatbody->szChat,pChatbody->tPHeader.nVerify));

	for ( int i = 0; i < MAX_CLIENT; i++)
	{
		m_IClientImpl[i]->m_bStopbeat = TRUE;
		USES_CONVERSION;
		m_IClientImpl[i]->m_SocketClient->Write((const LPBYTE)pChatbody,sizeof(STChatbody));
		m_IClientImpl[i]->m_bStopbeat = FALSE;
	}

	Sleep(1000);
	delete pChatbody;
#endif
}

void CClientTestDlg::OnBnClickedSendfile()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strFindFile = "";
	LPSTR lpszFile = (char*)(const char*)strFindFile;
	LPSTR lpszFilter = "(All*.*)|*.*||";
	DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT |OFN_ALLOWMULTISELECT|OFN_ENABLESIZING;
	CFileDialog dlg(TRUE, "default Files (*)", NULL, dwFlags,lpszFilter,NULL);
	dlg.m_ofn.lpstrTitle="请加载相关图层";
	// 最多可以打开20个文件
	dlg.m_ofn.nMaxFile = 20 * MAX_PATH; 
	dlg.m_ofn.lpstrFile = new TCHAR[dlg.m_ofn.nMaxFile]; 
	ZeroMemory(dlg.m_ofn.lpstrFile, sizeof(TCHAR) * dlg.m_ofn.nMaxFile); 

	// 显示文件对话框，获得文件名集合
	int retval = dlg.DoModal();
	if(retval==IDCANCEL)
		return ;

	POSITION pos_file;
	pos_file = dlg.GetStartPosition();
	CArray<CString, CString> ary_filename;
	while(pos_file != NULL)
		ary_filename.Add(dlg.GetNextPathName(pos_file));

	int nFileCount = ary_filename.GetSize();

	for ( int i = 0; i < nFileCount; i++)
	{
		for ( int j = 0; j < MAX_CLIENT; j++)
		{
			LOG4C((LOG_NOTICE,"%s",ary_filename.ElementAt(i)));
			m_IClientImpl[i]->SendFile(ary_filename.ElementAt(i),_T("64320"));
		}
	}
}

void CClientTestDlg::OnBnClickedConnectdb()
{
	// TODO: 在此添加控件通知处理程序代码
	STAccountInfobody tagAccountInfobody;
	strcpy(tagAccountInfobody.szAccount,"admin");
	strcpy(tagAccountInfobody.szPassword,"admin123456");
	tagAccountInfobody.GetVerify();

	//LOG4C((LOG_NOTICE,"协议包大小：%d,文字长度：%d,内容：%s,校验：%d",pChatbody->tPHeader.nDataLen,strSend.GetLength(),pChatbody->szChat,pChatbody->tPHeader.nVerify));

	for ( int i = 0; i < MAX_CLIENT; i++)
	{
		m_IClientImpl[i]->m_bStopbeat = TRUE;
		USES_CONVERSION;
		m_IClientImpl[i]->m_SocketClient->Write((const LPBYTE)&tagAccountInfobody,sizeof(STAccountInfobody));
		m_IClientImpl[i]->m_bStopbeat = FALSE;
	}

}
