15,471
社区成员
发帖
与我相关
我的任务
分享
DWORD WINAPI CClientApp::recvCenter(LPVOID lpParameter)
{
char recvBuffer[500];
memset(recvBuffer, 0, sizeof(recvBuffer));
while (true)
{
if (0 == theApp.msgCenter.m_socket || INVALID_SOCKET == theApp.msgCenter.m_socket || !theApp.msgCenter.bindSuccess)
{
continue;
}
// 接收消息
if (-1 != theApp.msgCenter.RecvFrom(recvBuffer, 500))
{
// 根据首字符分发消息
theApp.dispatchMsg(recvBuffer);
}
}
}
// 根据首字符分发消息
bool CClientApp::dispatchMsg(char* recvBuffer) const
{
CString recvMsg(recvBuffer);
CString allWords;
switch (recvBuffer[0])
{
case '1':
switch (recvBuffer[2])
{
case '2':// 登陆请求之后返回房间信息
theApp.m_bIsConnect = TRUE;
//theApp.roomDlg.updateRoomInfo(recvMsg);// 更新房间信息
theApp.roomDlg.storeRoomInfo = recvMsg;
::PostMessage(theApp.waitDlg.GetSafeHwnd(), WM_CLOSE, 0, 0);
break;
case '4':// 更换房间之后返回房间信息
theApp.roomDlg.updateRoomInfo(recvMsg);// 更新房间信息
break;
case '7':// 游戏开始,得到对方角色ID,并关闭roomDlg
theApp.roomDlg.m_nFigureIDhe = recvMsg[4] - '0';
//theApp.roomDlg.SendMessage(WM_CLOSE, 0, 0);
::PostMessage(theApp.roomDlg.GetSafeHwnd(), WM_CLOSE, 0, 0);
//theApp.roomDlg.OK();
//theApp.roomDlg.ShowWindow(SW_HIDE);
break;
default:
break;
}
break;
……
……
}
if (0 == theApp.msgCenter.m_socket || INVALID_SOCKET == theApp.msgCenter.m_socket || !theApp.msgCenter.bindSuccess)
始终未FALSE,按下F5,程序显示:
Source Not Availablevoid CLoginDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
UpdateData();
m_strUserName.Trim();// 去除空格
if ("" == m_strUserName)
{
MessageBox(_T("昵称不能为空"));
return;
}
/////////////////////////////// 发现主机 ////////////////////////////////////
// 发送昵称和IP地址到服务器
// 接收房间信息等待……
USES_CONVERSION;
string tmp1(W2A(m_strUserName));
string tmp = string("1 1 ") + tmp1;
if (-1 == theApp.msgCenter.FindHost((u_short)6000, tmp.c_str(), tmp.length()))
{
MessageBox(_T("发现主机失败!"));
return;
}
ShowWindow(SW_HIDE);// 先隐藏登陆对话框
theApp.waitDlg.DoModal();// 显示连接服务器对话框
if (theApp.m_bIsConnect)
{
CDialog::OnOK();// 关闭登陆对话框
theApp.roomDlg.DoModal(); // 登录对话框成功登录后再显示游戏大厅
}
else
{
MessageBox(_T("链接服务器失败!请检查服务器是否开启"));
ShowWindow(SW_SHOW);// 显示登陆对话框
}
//CDialog::OnOK();// 关闭登陆对话框
}
::PostMessage(theApp.roomDlg.GetSafeHwnd(), WM_CLOSE, 0, 0);
关闭游戏大厅,让阻塞的主程序顺序执行,去CClientView那里,显示格斗场面……BOOL CClientApp::InitInstance()
{
CWinApp::InitInstance();
// 初始化 OLE 库
if (!AfxOleInit())
{
AfxMessageBox(IDP_OLE_INIT_FAILED);
return FALSE;
}
AfxEnableControlContainer();
// 初始化socket
if (!msgCenter.SocketInit())
{
AfxMessageBox(_T("初始化套接字失败!"));
PostQuitMessage(WM_QUIT);
return false;
}
else
{
HANDLE hThreadMsg = ::CreateThread(NULL, 0, recvCenter, NULL, 0, NULL);
CloseHandle(hThreadMsg);
}
//////////////////////// 显示欢迎界面 ////////////////////////////
{// 使用块是希望欢迎界面播放结束后,自动析构,不然进入下面的loginDlg里面,
// 是无法使用mci播放视频的,估计是mci设备被占用,没有得到释放
//CWaitDlg welcomeDlg(CString(_T("welcome.avi")), WELCOME);
//welcomeDlg.DoModal();
}
/////////////////////// 显示登录对话框 ///////////////////////////
CLoginDlg loginDlg;
if (!loginDlg.DoModal())
{
return FALSE;
}
////////////////////////////////////////////////////////////////
EnableTaskbarInteraction(FALSE);
// 使用 RichEdit 控件需要 AfxInitRichEdit2()
// AfxInitRichEdit2();
// 标准初始化
// 如果未使用这些功能并希望减小
// 最终可执行文件的大小,则应移除下列
// 不需要的特定初始化例程
// 更改用于存储设置的注册表项
// TODO: 应适当修改该字符串,
// 例如修改为公司或组织名
SetRegistryKey(_T("应用程序向导生成的本地应用程序"));
LoadStdProfileSettings(4); // 加载标准 INI 文件选项(包括 MRU)
// 注册应用程序的文档模板。文档模板
// 将用作文档、框架窗口和视图之间的连接
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CClientDoc),
RUNTIME_CLASS(CMainFrame), // 主 SDI 框架窗口
RUNTIME_CLASS(CClientView));
if (!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
// 分析标准 shell 命令、DDE、打开文件操作的命令行
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// 调度在命令行中指定的命令。如果
// 用 /RegServer、/Register、/Unregserver 或 /Unregister 启动应用程序,则返回 FALSE。
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// 唯一的一个窗口已初始化,因此显示它并对其进行更新
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}