www.pudn.com > dataCS.rar > SuperServerView.cpp


// SuperServerView.cpp : implementation of the CSuperServerView class 
// 
 
#include "stdafx.h" 
#include "SuperServer.h" 
 
#include "SuperServerSet.h" 
#include "SuperServerDoc.h" 
#include "SuperServerView.h" 
#include "Finddlg2.h" 
#include "adddlg.h" 
#include "portdlg.h" 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CSuperServerView 
 
IMPLEMENT_DYNCREATE(CSuperServerView, CDaoRecordView) 
 
BEGIN_MESSAGE_MAP(CSuperServerView, CDaoRecordView) 
	//{{AFX_MSG_MAP(CSuperServerView) 
	ON_COMMAND(ID_START_SERVER, OnStartServer) 
	ON_COMMAND(ID_STOP_SERVER, OnStopServer) 
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd) 
	ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel) 
	ON_BN_CLICKED(IDC_BUTTON_MODIFY, OnButtonModify) 
	ON_BN_CLICKED(IDC_BUTTON_FIND, OnButtonFind) 
	ON_BN_CLICKED(IDC_BUTTON_B, OnButtonBack) 
	ON_BN_CLICKED(IDC_BUTTON_F, OnButtonForward) 
	ON_COMMAND(ID_SET_PORT, OnSetPort) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSuperServerView construction/destruction 
 
CSuperServerView::CSuperServerView() 
	: CDaoRecordView(CSuperServerView::IDD) 
{ 
	//{{AFX_DATA_INIT(CSuperServerView) 
	m_pSet = NULL; 
	m_iPort=1234; 
	//}}AFX_DATA_INIT 
	// TODO: add construction code here 
 
} 
 
CSuperServerView::~CSuperServerView() 
{ 
} 
 
void CSuperServerView::DoDataExchange(CDataExchange* pDX) 
{ 
	CDaoRecordView::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CSuperServerView) 
	DDX_Control(pDX, IDC_STATIC_STATE, m_staState); 
	DDX_FieldText(pDX, IDC_EDIT1, m_pSet->m_No, m_pSet); 
	DDX_FieldText(pDX, IDC_EDIT2, m_pSet->m_Name, m_pSet); 
	DDX_FieldText(pDX, IDC_EDIT3, m_pSet->m_Password, m_pSet); 
	DDX_FieldText(pDX, IDC_EDIT5, m_pSet->m_G_, m_pSet); 
	//}}AFX_DATA_MAP 
} 
 
BOOL CSuperServerView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CDaoRecordView::PreCreateWindow(cs); 
} 
 
void CSuperServerView::OnInitialUpdate() 
{ 
	m_pSet = &GetDocument()->m_superServerSet; 
	CDaoRecordView::OnInitialUpdate(); 
	GetParentFrame()->RecalcLayout(); 
	ResizeParentToFit(); 
 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CSuperServerView diagnostics 
 
#ifdef _DEBUG 
void CSuperServerView::AssertValid() const 
{ 
	CDaoRecordView::AssertValid(); 
} 
 
void CSuperServerView::Dump(CDumpContext& dc) const 
{ 
	CDaoRecordView::Dump(dc); 
} 
 
CSuperServerDoc* CSuperServerView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSuperServerDoc))); 
	return (CSuperServerDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CSuperServerView database support 
CDaoRecordset* CSuperServerView::OnGetRecordset() 
{ 
	return m_pSet; 
} 
 
void CSuperServerView::MyAccept() 
{ 
    for(int i=0;iAccept (m_SocketClient[i]); 
             m_SocketClient[i].GetView (this); 
			 break; 
		} 
	} 
     
} 
 
void CSuperServerView::MyClose(CSocketClient *pClient) 
{ 
        pClient->Close(); 
	    pClient->m_bBusy=false; 
} 
 
void CSuperServerView::MyReceive(CSocketClient *pClient) 
{ 
	m_pSF=new CSocketFile(pClient); 
	m_pArIn=new CArchive(m_pSF,CArchive::load); 
    msg.Serialize (*m_pArIn); 
	int i;	 
	bool bOK=false; 
	switch(msg.m_eType ) 
	{ 
	case LOGIN: 
		m_pSF=new CSocketFile(pClient); 
		m_pArOut=new CArchive(m_pSF,CArchive::store); 
		msg.m_eType =LOGINResponse; 
		if(CheckLogin(pClient)) 
		{ 
			if(!pClient->m_bBoss) 
			{ 
				msg.m_strMsg="GOOD|欢迎!"; 
			    bOK=true; 
			} 
			else 
			{ 
		        msg.m_strMsg="GOOD|BOSS"; 
			    bOK=true; 
			} 
		} 
		else 
		{ 
			 msg.m_strMsg="FAILED|验证失败!"; 
		}			 
		msg.Serialize (*m_pArOut); 
		m_pArOut->Flush (); 
		if(bOK) SendUserList(); 
		break; 
	case CHATTING: 
        for(i=0;im_strName+"|"+Decode(2,msg.m_strMsg); 
		          msg.Serialize (*m_pArOut); 
				  m_pArOut->Flush (); 
			      break; 
			   } 
		}         
        break; 
	case NOTICE: 
		msg.m_strMsg=pClient->m_strName+"|"+Decode(2,msg.m_strMsg); 
		for(i=0;im_strName) 
		   { 
				  m_pSF=new CSocketFile(&m_SocketClient[i]); 
		          m_pArOut=new CArchive(m_pSF,CArchive::store); 
		          msg.Serialize (*m_pArOut); 
				  m_pArOut->Flush (); 
		   } 
		}         
		break; 
 
	} 
} 
 
 
bool CSuperServerView::CheckLogin(CSocketClient *pClient) 
{ 
/*	LPCTSTR  lpszPathName; 
	lpszPathName = "data2.mdb";		 
    CDaoDatabase* tmpDB = new CDaoDatabase; 
	try{	 
		tmpDB->Open(lpszPathName); 
	} 
	catch (CDaoException* e) 
	{ 
		delete tmpDB; 		 
		e->Delete(); 
		return FALSE; 
	} 
	/*if (m_pDB)	{ 
		if (m_pDB->IsOpen()) 
	        m_pDB->Close(); 
		delete m_pDB; 
	}*/ 
    /*m_pDB = tmpDB; 
	CDaoRecordset rs(m_pDB); 
    try 
	{ 
	   CString Name=Decode(1,msg.m_strMsg); 
	   CString Password=Decode(2,msg.m_strMsg); 
	   CString strSQL(_T(" Select * From [User] where Name='"+Name+"' and Password='"+Password+"'")); 
	   rs.Open(dbOpenDynaset,strSQL); 
	   if(!rs.IsEOF()) 
	   {		  
		   pClient->m_strName=Name; 
		   pClient->m_bBusy =true; 
		   return true; 
	   } 
	   else 
	   { 
		  pClient->Close (); 
		  return false; 
	   } 
	} 
    catch (CDaoException* e) 
	{ 
		e->Delete(); 
		return false; 
	} 
	return false;*/ 
	CString Name=Decode(1,msg.m_strMsg); 
	CString Password=Decode(2,msg.m_strMsg); 
	m_pSet->MoveFirst (); 
	while(!m_pSet->IsEOF ()) 
	{ 
		if(m_pSet->m_Name==Name&& m_pSet->m_Password==Password) 
		{ 
		   pClient->m_strName=Name; 
		   pClient->m_bBusy =true; 
		   if(m_pSet->m_G_==0) pClient->m_bBoss=true; 
		   return true; 
		} 
	    m_pSet->MoveNext (); 
	} 
	return false; 
} 
 
void CSuperServerView::SendUserList() 
{ 
	msg.m_eType =USERList;	 
	msg.m_strMsg="END"; 
	for(int i=0;iFlush ();  
		} 
	}     
} 
 
CString CSuperServerView::Decode(int n,CString strMsg) 
{ 
	   int pos; 
	   CString str; 
	   for(int i=1;i<=n;i++) 
	   { 
	       pos=strMsg.Find ("|",0); 
	       if(pos<0) 
			   str=strMsg; 
		   else 
		       str=strMsg.Left (pos); 
		   strMsg=strMsg.Right(strMsg.GetLength ()-pos-1); 
	   } 
	   return str; 
} 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CSuperServerView message handlers 
 
void CSuperServerView::OnStartServer()  
{ 
    m_pSocket=new CSocketListen(this); 
	m_pSocket->Create (m_iPort,SOCK_STREAM); 
	m_pSocket->Listen (); 
	m_staState.SetWindowText("正在监听......"); 
} 
 
void CSuperServerView::OnStopServer()  
{ 
	if(m_pSocket) m_pSocket->Close (); 
	for(int i=0;iAddNew (); 
			 m_pSet->m_Name=dlg.m_strName; 
			 m_pSet->m_Password=dlg.m_strPwd; 
			 m_pSet->m_G_=dlg.m_iG; 
			 m_pSet->Update (); 
			 UpdateData(FALSE); 
		} 
 
	}	 
	 
} 
 
void CSuperServerView::OnButtonDel()     //删除帐户 
{ 
 	m_pSet->Delete ();  
	m_pSet->MoveNext (); 
	if(m_pSet->IsEOF ()) 
	   m_pSet->MoveFirst(); 
	UpdateData(FALSE); 
	 
} 
 
void CSuperServerView::OnButtonModify()  //修改帐户信息 
{ 
	CAddDlg dlg; 
	dlg.m_strName=m_pSet->m_Name; 
	dlg.m_strPwd=m_pSet->m_Password; 
	dlg.m_iG=m_pSet->m_G_; 
	if(dlg.DoModal()==IDOK) 
	{  
        if(dlg.m_strName!="") 
		{ 
			 m_pSet->Edit (); 
			 m_pSet->m_Name=dlg.m_strName; 
			 m_pSet->m_Password=dlg.m_strPwd; 
			 m_pSet->m_G_=dlg.m_iG; 
			 m_pSet->Update (); 
			 UpdateData(FALSE); 
		} 
	} 
 
} 
 
void CSuperServerView::OnButtonFind() // 查找帐户 
{ 
 	CFindDLG dlg; 
	if(dlg.DoModal()==IDOK) 
	{  
        if(dlg.m_strKey!="") 
		{ 
	 		CString m_strName; 
		    UpdateData(TRUE); 
	    	m_strName=dlg.m_strKey ; 
		    if(m_pSet->IsOpen ()) 
			   m_pSet->Close (); 
		    m_pSet->Open(AFX_DAO_USE_DEFAULT_TYPE,"SELECT * FROM user where Name='"+m_strName+"'"); 
		    UpdateData(FALSE); 
		} 
	} 
 
} 
 
void CSuperServerView::OnButtonBack() 
{ 
    m_pSet->MovePrev(); 
	if(m_pSet->IsBOF ()) 
	   m_pSet->MoveLast(); 
	UpdateData(FALSE); 
} 
 
void CSuperServerView::OnButtonForward()  
{ 
   	m_pSet->MoveNext (); 
	if(m_pSet->IsEOF ()) 
		m_pSet->MoveFirst(); 
	UpdateData(FALSE); 
} 
 
void CSuperServerView::OnSetPort()  
{ 
	CPortDlg dlg; 
	dlg.m_iPort =m_iPort; 
	if(dlg.DoModal ()==IDOK) 
	{ 
		m_iPort=dlg.m_iPort ; 
	} 
 
	 
}