123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
- // ClientTestDlg.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "ClientTest.h"
- #include "ClientTestDlg.h"
- #include "struct_def.h"
- #include "msgId_def.h"
- #include "lzari.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- typedef struct
- {
- BYTE bsql;
- BYTE tabcount;
- BYTE code[100];
- DWORD count[100];
- DWORD length[100];
- }SENDHEAD;
- BOOL g_bSendOK = FALSE;
- BYTE* g_pData2 = NULL;
- BYTE *g_pData = NULL;
- SENDHEAD g_sendhead;
- DWORD g_nLeng = 0;
- DWORD g_nLeng2 = 0;
- DWORD g_ncount = 0;
- static void HandleClientNetEvent(IN SOCKET hSocket,
- IN ETransportEvent eEvent,
- IN void *pDataBuf,
- IN unsigned long nDataLen,
- IN int nError,
- IN void *pContext);
- // 用于应用程序“关于”菜单项的 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);
- m_bIsConnected = FALSE;
- m_bIsLogined = FALSE;
- }
- void CClientTestDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CClientTestDlg, CDialog)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_DESTROY()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- ON_BN_CLICKED(IDOK, &CClientTestDlg::OnBnClickedOk)
- ON_BN_CLICKED(IDCANCEL, &CClientTestDlg::OnBnClickedCancel)
- ON_BN_CLICKED(IDC_BUTTON2, &CClientTestDlg::OnBnClickedButton2)
- ON_BN_CLICKED(IDC_BUTTON3, &CClientTestDlg::OnBnClickedButton3)
- 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);
- }
- }
- CClientTunnel::Transport_Init();
- // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
- // 执行此操作
- SetIcon(m_hIcon, TRUE); // 设置大图标
- SetIcon(m_hIcon, FALSE); // 设置小图标
- // TODO: 在此添加额外的初始化代码
- 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::OnDestroy()
- {
- if (g_pData)
- delete[]g_pData;
- g_pData = NULL;
- CClientTunnel::Transport_UnInit();
- }
- static void HandleClientNetEvent(IN SOCKET hSocket,
- IN ETransportEvent eEvent,
- IN void *pDataBuf,
- IN unsigned long nDataLen,
- IN int nError,
- IN void *pContext)
- {
- if (nError != TRANSPORT_OK)
- return;
- CClientTestDlg *pDlg = (CClientTestDlg *)pContext;
- if (NULL == pDlg)
- return;
- pDlg->ProcessNetEvent(eEvent, pDataBuf, nDataLen);
- }
- void CClientTestDlg::ProcessNetEvent(int nEventType, void *pRecvMsg, DWORD dwDataLen)
- {
- if (Transport_ReadEv == nEventType)
- {
- if (NULL == pRecvMsg) return;
- TMessageHeader* pHeader = (TMessageHeader *)pRecvMsg;
- char *pDataBuf = (char *)pRecvMsg + MESSAGE_HEADER_LEN;
- WORD dwMessageID = pHeader->wMessageId;
- switch (dwMessageID)
- {
- case MSG_LOGIN_RESP:
- case (MSG_LOGIN_RESP + 0X2FFF) :
- {
- LOGIN_RESULT_STRU tLoginResult = { 0 };
- memcpy(&tLoginResult, pDataBuf, sizeof(LOGIN_RESULT_STRU));
- ProcessLoginResponse(&tLoginResult);
- break;
- }
- case 5:
- {
- break;
- }
- case 3:
- {
- break;
- }
- case MSG_CHATMESSAGE_RESP:
- case (MSG_CHATMESSAGE_RESP + 0X4FFF):
- {
- TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pDataBuf;
- ProcessChatMessageResponse((void *)pChatMessage);
- }
- default:
- {
- break;
- }
- }
- }
- else if (Transport_CloseEv == nEventType)
- {
- // OnDisconnect();
- }
- }
- //登录返回
- void CClientTestDlg::ProcessLoginResponse(void *pLoginResult)
- {
- if (NULL == pLoginResult) return;
- LOGIN_RESULT_STRU *ptLoginResult = (LOGIN_RESULT_STRU *)pLoginResult;
- DWORD dwConnectionID = ptLoginResult->tCommonMsg.dwConnectionID;
- BYTE byResult = ptLoginResult->byResult;
- if (LOGIN_RESULT_SUC == byResult)
- {
- m_bIsLogined = TRUE;
- OutputDebugString(_T("登录成功\n"));
- }
- else
- OutputDebugString(_T("登录失败\n"));
- }
- //边接服务器
- void CClientTestDlg::OnBnClickedOk()
- {
- // TODO: 在此添加控件通知处理程序代码
- int nResult = m_tClientTunnel.net_OpenSocket(
- Transport_Client,
- 0,
- HandleClientNetEvent,
- this);
- if (TRANSPORT_OK != nResult)
- {
- MessageBox(_T("网络初始化失败!"));
- return;
- }
- unsigned long ulIPValue = inet_addr("127.0.0.1");
- unsigned short usPort = 5678;
- if(m_tClientTunnel.net_Connect(ulIPValue, usPort) == -1)
- {
- MessageBox(_T("连接失败!"));
- return;
- }
- ((CButton*)GetDlgItem(IDOK))->EnableWindow(FALSE);
- m_bIsConnected = TRUE;
- }
- void CClientTestDlg::ProcessChatMessageResponse(void *pResponse)
- {
- //printf("Jeff:ProcessChatMessageResponse**************\n\n");//Jeff Printf;
- // 1.数据空,直接返回;
- if (NULL == pResponse)
- return;
- // 2.数据解析过程;
- //printf("Jeff:ProcessChatMessageResponse**************\数据解析过程\n\n");//Jeff Printf;
- TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pResponse;
- int nMessageLen = pChatMessage->wMessageLen;
- int nMessageLen2 = pChatMessage->dwToUserID; // 在服务器中被处理了,一般使dwToUserID=wMessageLen;
- // Jeff.dwFromUserID==8888表示的内容是更新客户端skin界面数据,只有两处dwToUserID==211和dwToUserID==21;
- // Jeff.其余的大多数都是 dwFromUserID = wMessageLen = dwToUserID 三者相等;
- if (nMessageLen == nMessageLen2 && pChatMessage->dwFromUserID != 8888)
- {
- // 2.1.清除全局变量g_pData数据,用于保存接收的实际数据;
- if (g_pData)
- delete[]g_pData;
- g_pData = NULL;
- g_pData = new BYTE[nMessageLen];
- memcpy(g_pData, pChatMessage->byFileContent, nMessageLen);
- memcpy(&g_sendhead, pChatMessage, sizeof(g_sendhead));
- g_nLeng = nMessageLen;
- if (nMessageLen <= sizeof(DWORD))
- memcpy(&g_ncount, pChatMessage->byFileContent, sizeof(DWORD));
- }
- else if (nMessageLen == nMessageLen2 && pChatMessage->dwFromUserID == 8888)
- {
- if (g_pData2)
- delete[]g_pData2;
- g_pData2 = NULL;
- g_pData2 = new BYTE[nMessageLen2];
- memcpy(g_pData2, pChatMessage->byFileContent, nMessageLen2);
- g_nLeng2 = nMessageLen2;
- }
- else
- {
- //登录返回
- OutputDebugString(_T("登录返回\n"));
- return;
- }
- if(g_pData != NULL && _tcscmp((char*)g_pData, _T("")) != 0)
- {
- CArray<CStringArray,CStringArray> AryList;
- DataToArray( g_pData, g_nLeng, &AryList);
- for(int i=0; i<AryList.GetSize(); i++)
- {
- CString strMsg = _T("");
- for(int j=0; j<AryList.ElementAt(i).GetSize(); j++)
- {
- strMsg += AryList.ElementAt(i).ElementAt(j);
- strMsg += _T(" ");
- }
- strMsg += _T("\n");
- OutputDebugString(strMsg);
- }
- CString strCount = _T("");
- strCount.Format(_T("------------------- %d --------------------\n"), AryList.GetSize());
- OutputDebugString(strCount);
- }
- }
- void CClientTestDlg::OnBnClickedCancel()
- {
- // TODO: 在此添加控件通知处理程序代码
- m_tClientTunnel.net_CloseSocket();
- OnCancel();
- }
- void CClientTestDlg::ProcessLoginRequest()
- {
- // 1.设置通信类型MSG_LOGIN_REQ,为登陆请求;
- WORD wMessageId = MSG_LOGIN_REQ + 0X1FFF;
- // 2.填充发送数据;
- TLOGIN_STRU tLogonInfo = { 0 };
- tLogonInfo.tCommonMsg.dwConnectionID = INVALID_SOCKET;
- tLogonInfo.tCommonMsg.wMessageId = wMessageId;
- TCHAR szHostName[MAX_PATH] = {0};
- gethostname(szHostName, MAX_PATH);
- strcpy(tLogonInfo.tUserInfo.szUserName, szHostName);
- DWORD dwDataLen = sizeof(TLOGIN_STRU);
- // 3.填充数据头;
- TMessageHeader tHeader = { 0 };
- tHeader.wMessageId = wMessageId;
- tHeader.dwDataLen = dwDataLen;
- // 4.发送数据;
- unsigned long ulSendLen = m_tClientTunnel.net_Send((void *)&tHeader, (void *)&tLogonInfo, dwDataLen);
- if (ulSendLen == SOCKET_ERROR)
- {
- MessageBox(_T("发送数据失败"));
- return;
- }
- g_bSendOK = TRUE;
- }
- CString CClientTestDlg::newGUID()
- {
- CString str;
- GUID guid;
- CoInitialize(NULL);
- if (S_OK == ::CoCreateGuid(&guid))
- {
- str.Format(
- _T("{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"),
- guid.Data1,
- guid.Data2,
- guid.Data3,
- guid.Data4[0], guid.Data4[1],
- guid.Data4[2], guid.Data4[3],
- guid.Data4[4], guid.Data4[5],
- guid.Data4[6], guid.Data4[7]);
- }
- CoUninitialize();
- return str;
- }
- //登录
- void CClientTestDlg::OnBnClickedButton2()
- {
- // TODO: 在此添加控件通知处理程序代码
- if(!m_bIsConnected)
- {
- MessageBox(_T("没有连接服务端"));
- return;
- }
- //请求登录
- ProcessLoginRequest();
- }
- //读取数据
- void CClientTestDlg::OnBnClickedButton3()
- {
- if(!m_bIsLogined)
- {
- MessageBox(_T("还没登录"));
- return;
- }
- // TODO: 在此添加控件通知处理程序代码
- ZeroMemory(&g_sendhead, sizeof(SENDHEAD));
- CHAR szSQL[MAX_PATH] = "status3='未取'";
- g_sendhead.bsql = 0;
- g_sendhead.code[0] = 225;
- g_sendhead.tabcount = 1;
- ProcessChatMessageRequest(szSQL, strlen(szSQL) + 1) ;
- /*
- CHAR szSQL[MAX_PATH] = "dimission='在职'";
- g_sendhead.bsql = 0;
- g_sendhead.code[0] = 5;
- g_sendhead.tabcount = 1;
- ProcessChatMessageRequest(szSQL, strlen(szSQL) + 1) ;
- g_sendhead.bsql = 1;
- CHAR szSQL[MAX_PATH] = _T("select * from dindan");
- int nLen = strlen(szSQL) + 1;
- szSQL[nLen] = '\0';
- ProcessChatMessageRequest((void*)szSQL, strlen(szSQL) + 1);
- */
- if(g_bSendOK == FALSE)
- return;
- }
- //解析消息
- void CClientTestDlg::DataToArray(IN BYTE *pData, IN CONST DWORD &dwLength, IN CArray<CStringArray, CStringArray>* List1array)
- {
- List1array->RemoveAll();
- if (dwLength == 0)return;
- if (g_sendhead.code[0])
- {
- BYTE *lpszOut = NULL;
- int nOutSize = 0;
- LZARI Lzari;
- Lzari.UnCompress(pData, dwLength, (const BYTE*&)lpszOut, nOutSize);
- CMemFile memfile;
- memfile.Attach(lpszOut, nOutSize);
- Lzari.Release();
- try
- {
- CArchive ar(&memfile, CArchive::load);
- List1array->SetSize(g_sendhead.count[0]);
- for (int i = 0; i < List1array->GetSize(); i++)
- {
- List1array->ElementAt(i).Serialize(ar);
- }
- ar.Close();
- memfile.Detach();
- }
- catch (CException* e)
- {
- e->ReportError();
- }
- }
- else
- {
- CMemFile memfile;
- memfile.Attach(pData, dwLength);
- CArchive ar(&memfile, CArchive::load);
- List1array->SetSize(g_sendhead.count[0]);
- for (int ii = 0; ii < List1array->GetSize(); ii++)
- List1array->ElementAt(ii).Serialize(ar);
- ar.Close();
- memfile.Detach();
- }
- }
- //发送数据
- BOOL CClientTestDlg::ProcessChatMessageRequest(void *szDataBuf, int nDataLen)
- {
- BOOL bRecevie = FALSE;
- DWORD dwFromUserID = 0;
- WORD wMessageId = MSG_CHATMESSAGE_REQ + 0X3FFF;
- DWORD dwDataLen = sizeof(TCHAT_MESSAGE_STRU) + sizeof(TMessageHeader) + nDataLen;
- BYTE *pSendData = NULL;
- pSendData = new BYTE[dwDataLen];
- memset(pSendData, 0, dwDataLen);
- TMessageHeader* ptHeader = (TMessageHeader*)pSendData;
- ptHeader->wMessageId = wMessageId;
- ptHeader->dwDataLen = sizeof(TCHAT_MESSAGE_STRU) + nDataLen;
- ptHeader->byVersion = 101;
- ptHeader->wHeaderFlag = MESSAGE_HEADER_FLAG;
- ptHeader->wReserve = 0;
- TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU*)(pSendData + sizeof(TMessageHeader));
- g_sendhead.length[98] = 987123768;
- memcpy(pChatMessage, &g_sendhead, sizeof(g_sendhead));
- pChatMessage->tCommonMsg.dwConnectionID = dwFromUserID;
- pChatMessage->tCommonMsg.wMessageId = wMessageId;
- pChatMessage->dwFromUserID = dwFromUserID;
- pChatMessage->dwToUserID = 15;
- pChatMessage->wMessageLen = nDataLen;
- memcpy(pChatMessage->byFileContent, szDataBuf, nDataLen);
- // 6.发送数据;
- BOOL bRet = 0;
- unsigned long ulSendLen = m_tClientTunnel.net_Send(ptHeader, (void *)pChatMessage, dwDataLen);
- if (ulSendLen != SOCKET_ERROR)
- bRet = 1;
- // 7.发送完毕,释放内存;
- delete[] pSendData;
- g_bSendOK = TRUE;
- return bRet;
- }
|