📄 chessgatedlg.cpp
字号:
// ChessGateDlg.cpp : implementation file
//
#include "StdAfx.h"
#include "ChessGate.h"
#include "SocketListen.h"
#include "ChessGateDlg.h"
#include "..\..\inc\transmit.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//extern size_t CodeConvert(char* out_buf,size_t buf_len,char * in_str,char * fromcode,char * tocode,bool conv_begin = false);
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
BOOL threadstop=false;
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChessGateDlg dialog
CChessGateDlg::CChessGateDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChessGateDlg::IDD, pParent)
{
m_nRequestCount = 0;
// cts_SocketQueue[QUEUE_NUM];
// InitializeCriticalSection(loglock);
for(int i=0; i< QUEUE_NUM; i++){
InitializeCriticalSection(cts_SocketQueue[i]);
}
//实例化一个共享内存对象
login_recv_queue = new CShareMemory<NODE_STRUCT> (LOGIN_RECV_KEY);
if(!login_recv_queue->Init(QUEUE_LENGTH)){
MessageBox("初始化通用模块接收共享内存时产生错误!","系统错误",MB_OK|MB_ICONSTOP);
exit(-1);
}
gobang_recv_queue = new CShareMemory<NODE_STRUCT> (GOBANG_RECV_KEY);
if(!gobang_recv_queue->Init(QUEUE_LENGTH)){
MessageBox("初始化五子棋服务接收共享内存时产生错误!","系统错误",MB_OK|MB_ICONSTOP);
exit(-1);
}
login_send_queue = new CShareMemory<NODE_STRUCT> (LOGIN_SEND_KEY);
if(!login_send_queue->Init(QUEUE_LENGTH)){
MessageBox("初始化发送共享内存时产生错误!","系统错误",MB_OK|MB_ICONSTOP);
exit(-1);
}
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
CChessGateDlg::~CChessGateDlg()
{
threadstop = true;
if(login_recv_queue != NULL){
delete login_recv_queue;
}
if(login_send_queue != NULL){
delete login_send_queue;
}
}
void CChessGateDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChessGateDlg)
DDX_Control(pDX, IDC_LIST_MONITOR, m_lstMonitor);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChessGateDlg, CDialog)
//{{AFX_MSG_MAP(CChessGateDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_CLOSE()
ON_WM_QUERYDRAGICON()
ON_COMMAND(ID_MENUITEM_ABOUT, OnMenuitemAbout)
ON_COMMAND(ID_MENUITEM_EXIT, OnMenuitemExit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChessGateDlg message handlers
BOOL CChessGateDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//初始化代码
unsigned int nChess_Port;
::GetPrivateProfileString("LOG","LOGDIR","../../log/GameGate.log",LogFile,sizeof LogFile,"../../ini/chessgate.ini");
AddMsg("五子棋网关初始化开始...");
//初始化SOCKET队列
CString tempstr;
for (int n = 0; n < QUEUE_NUM; n ++){
char num[4];
itoa(n,num,10);
tempstr = "PORT";
tempstr = tempstr+num;
nChess_Port = ::GetPrivateProfileInt(tempstr,"OPEN_PORT",5009,"..\\..\\ini\\chessgate.ini");
// char szHostAddr[] = "60.176.244.76";
// unsigned int nHostPort = nChess_Port;
// sockaddr_in host;
// host.sin_family = AF_INET;
// host.sin_port = htons(nHostPort);
// host.sin_addr.S_un.S_addr=inet_addr(szHostAddr);
//Socket服务监听
pSocketlisten[n] = new CSocketListen(this,n);
//用udp方式进行通信
//pSocketlisten[n]->Create(nChess_Port);
//pSocketlisten[n].Detach()
pSocketlisten[n]->Create(nChess_Port,SOCK_DGRAM);
// pSocketlisten[n]->Listen(15);
}
//创建发送线程
pSendThread = AfxBeginThread(SendThreadFunc,this,THREAD_PRIORITY_NORMAL);
AddMsg("五子棋网关初始化完成!");
m_hStatusWindow = CreateStatusWindow(WS_CHILD|WS_VISIBLE|WS_BORDER,TEXT(" 系统提示:"),GetSafeHwnd(),1);
int pint[4]={72,-1};
::SendMessage(m_hStatusWindow,SB_SETPARTS,2,(LPARAM)pint);
::SendMessage(m_hStatusWindow,SB_SETTEXT,1,(LPARAM)TEXT(" 处理请求数[0]"));
return TRUE; // return TRUE unless you set the focus to a control
}
void CChessGateDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CChessGateDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
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;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CChessGateDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CChessGateDlg::OnCancel()
{
if(MessageBox("请注意,您确定退出五子棋网关程序?","系统提示",MB_OKCANCEL|MB_ICONQUESTION) == IDCANCEL)
return;
//终止线程
if(pSendThread != NULL){
pSendThread->SuspendThread();
TerminateThread(pSendThread->m_hThread,0);
delete pSendThread;
}
//释放客户端Socket
for(int n=0; n< QUEUE_NUM; n++){
pSocketlisten[n]->Close();
delete pSocketlisten[n];
}
CDialog::OnCancel();
}
void CChessGateDlg::OnClose()
{
//写Log文件
WriteLog("ChessGate被用户正常退出.");
CDialog::OnClose();
}
void CChessGateDlg::OnMenuitemAbout()
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
void CChessGateDlg::OnMenuitemExit()
{
this->OnCancel();
}
BOOL CChessGateDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN){
switch(pMsg->wParam){
case VK_RETURN:
return TRUE; //使Enter无效
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
void CChessGateDlg::RefreshStatusBar()
{
CString strMsg;
m_nRequestCount++;
strMsg.Format(" 处理请求数[%d]",m_nRequestCount);
::SendMessage(m_hStatusWindow,SB_SETTEXT,1,(LPARAM)TEXT(LPCTSTR(strMsg)));
}
//功能:写LOG
//参数:sLogFile为Log文件名,sFileName指名哪个文件写LOG
//参数:nLine表明在第X行写LOG,fmt见printf...
BOOL CChessGateDlg::WriteLog(LPCTSTR fmt,...)
{
va_list args;
FILE *fp;
time_t t;
struct tm *ptm;
time(&t);
ptm = localtime(&t);
CSingleLock lock(&loglock);
lock.Lock();
if((fp = fopen(LogFile, "a+t")) == NULL)
return FALSE;
fprintf(fp,"[%04d%02d%02d %02d:%02d:%02d] ",
ptm->tm_year + 1900,
ptm->tm_mon +1,
ptm->tm_mday,
ptm->tm_hour,
ptm->tm_min,
ptm->tm_sec);
va_start(args, fmt);
vfprintf(fp,fmt,args);
fprintf(fp,"\n");
va_end(args);
fclose(fp);
lock.Unlock();
return TRUE;
}
//功能:向监视窗口添加一条信息
//参数:csMsg是要显示的信息
void CChessGateDlg::AddMsg(LPCTSTR fmt , ...)
{
va_list args;
CTime ctTime = CTime::GetCurrentTime();
CString csTime = ctTime.Format("[%Y-%m-%d %H:%M:%S]");
char szBuf[1024];
va_start(args, fmt);
vsprintf(szBuf,fmt,args);
va_end(args);
csTime = csTime + " " + szBuf;
if(m_lstMonitor.GetCount() > MAXSHOWLINES-1){
m_lstMonitor.DeleteMessage(0);
}
int nIndex = m_lstMonitor.AddMessage(csTime);
WriteLog(szBuf);
}
//发送线程处理函数
//取共享内存数据,调用网关API发送消息包
UINT SendThreadFunc(LPVOID pParam)
{
int nNodesCnt;
NODE_STRUCT sendNodes[256];
int queuenum;
CChessGateDlg *pDlg = (CChessGateDlg *)pParam;
//循环发送信息
threadstop = false;
while(!threadstop)
{
nNodesCnt = pDlg->login_send_queue->GetNodes(sendNodes,sizeof(sendNodes)/sizeof(NODE_STRUCT));
for(int i=0;i<nNodesCnt;i++)
{
//通过Socket进行消息发送
queuenum = sendNodes[i].queuenum;
if(queuenum>=0){
//组织socket头(加^用来防止服务器向客户端发送的多次数据被客户端当作一次数据读取
char szBuf[3072]={0};
char szInBuf[3072]={0};
memset(szBuf,0,sizeof szBuf);
memset(szInBuf,0,sizeof szInBuf);
// sprintf(szBuf,"%s|%s|%s|%s^",sendNodes[i].err_code,sendNodes[i].service_id,sendNodes[i].operate_id,sendNodes[i].body);
// sprintf(szBuf,"%s|%s|%s|%s",sendNodes[i].err_code,sendNodes[i].service_id,sendNodes[i].operate_id,sendNodes[i].body);
sprintf(szInBuf,"%s|%s|%s|%s",sendNodes[i].err_code,sendNodes[i].service_id,sendNodes[i].operate_id,sendNodes[i].body);
// strcpy(szInBuf,"反对电风扇发射点");
CodeConvert(szBuf,sizeof(szBuf),szInBuf,"GB2312","UTF-8",true);
// GB2312toUTF8(szBuf,sizeof(szBuf),szInBuf);
//进入临界区,发送Socket
// CSingleLock lock(&pDlg->cts_SocketQueue[queuenum]);
// lock.Lock();
if(pDlg->pSocketlisten[queuenum]->SendData(LPCSTR(szBuf),strlen(szBuf),sendNodes[i].clientport,sendNodes[i].clientip)){
pDlg->AddMsg("发送消息成功!%s,%d",sendNodes[i].clientip,sendNodes[i].clientport);
pDlg->RefreshStatusBar();
}else{
int aaa = pDlg->pSocketlisten[queuenum]->GetLastError();
pDlg->AddMsg("发送消息失败!%d",aaa);
}
pDlg->WriteLog(szBuf);
// lock.Unlock();
}
}
Sleep(100);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -