AnyServerView.cpp
资源名称:anywhere.zip [点击查看]
上传用户:zhenhuadz
上传日期:2007-01-07
资源大小:1511k
文件大小:7k
源码类别:
远程控制编程
开发平台:
Visual C++
- // AnyServerView.cpp : implementation of the CAnyServerView class
- //
- #include "stdafx.h"
- #include "AnyServer.h"
- #include "AnyServerDoc.h"
- #include "AnyServerView.h"
- #include "Thread.h"
- #include "Blocksock.h"
- #include "RemoteC.h"
- #include "ChatLsnSock.h"
- #include "ChatSock.h"
- #include "ChatClientView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAnyServerView
- IMPLEMENT_DYNCREATE(CAnyServerView, CFormView)
- BEGIN_MESSAGE_MAP(CAnyServerView, CFormView)
- //{{AFX_MSG_MAP(CAnyServerView)
- ON_BN_CLICKED(IDC_STARTBTN, OnStartbtn)
- ON_MESSAGE(WM_SOCKCOMMAND,OnSockCommand)
- ON_MESSAGE(WM_VALID,OnValid)
- ON_MESSAGE(WM_USERNAME,OnUserName)
- ON_MESSAGE(WM_IPADDR,OnIPAddr)
- ON_COMMAND(ID_STARTSERVER, OnStartserver)
- ON_COMMAND(ID_STOPSERVER, OnStopserver)
- ON_BN_CLICKED(IDC_STOPBTN, OnStopbtn)
- ON_UPDATE_COMMAND_UI(ID_STARTSERVER, OnUpdateStartserver)
- ON_UPDATE_COMMAND_UI(ID_STOPSERVER, OnUpdateStopserver)
- ON_BN_CLICKED(IDC_CLOSECHAT, OnClosechat)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CAnyServerView construction/destruction
- CAnyServerView::CAnyServerView()
- : CFormView(CAnyServerView::IDD)
- {
- //{{AFX_DATA_INIT(CAnyServerView)
- //}}AFX_DATA_INIT
- // TODO: add construction code here
- DataBuf.Empty();
- }
- CAnyServerView::~CAnyServerView()
- {
- CAnyServerDoc* pDoc=(CAnyServerDoc*)GetDocument();
- if (pDoc->g_pchatListen)
- if (pDoc->g_pchatListen->m_hSocket!=INVALID_SOCKET)
- {
- pDoc->g_pchatListen->Close();
- delete pDoc->g_pchatListen;
- pDoc->g_pchatListen=NULL;
- }
- }
- void CAnyServerView::DoDataExchange(CDataExchange* pDX)
- {
- CFormView::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAnyServerView)
- DDX_Control(pDX, IDC_IPADDRESS, m_IPAddr);
- DDX_Control(pDX, IDC_CLOSECHAT, m_CloseChat);
- DDX_Control(pDX, IDC_STOPBTN, m_Stopbtn);
- DDX_Control(pDX, IDC_STARTBTN, m_Startbtn);
- //}}AFX_DATA_MAP
- }
- BOOL CAnyServerView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CFormView::PreCreateWindow(cs);
- }
- void CAnyServerView::OnInitialUpdate()
- {
- CFormView::OnInitialUpdate();
- GetParentFrame()->RecalcLayout();
- ResizeParentToFit();
- m_CloseChat.EnableWindow(FALSE);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CAnyServerView diagnostics
- #ifdef _DEBUG
- void CAnyServerView::AssertValid() const
- {
- CFormView::AssertValid();
- }
- void CAnyServerView::Dump(CDumpContext& dc) const
- {
- CFormView::Dump(dc);
- }
- CAnyServerDoc* CAnyServerView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAnyServerDoc)));
- return (CAnyServerDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CAnyServerView message handlers
- CBlockingSocket g_sListen;
- BOOL Started;
- void CAnyServerView::OnStartbtn()
- {
- Started=TRUE;
- m_Startbtn.EnableWindow(!Started);
- try
- {
- CSockAddr saServer(INADDR_ANY,7700);
- g_sListen.Create();
- g_sListen.Bind(saServer);
- g_sListen.Listen();
- ((CAnyServerDoc*)GetDocument())->g_pchatListen=new CChatLsnSock((CAnyServerDoc*)GetDocument());
- CChatLsnSock* g_pchatListen=((CAnyServerDoc*)GetDocument())->g_pchatListen;
- g_pchatListen->Create(9999,SOCK_STREAM);
- g_pchatListen->Listen();
- GetDlgItem(IDC_RESPONSE)->SetWindowText("开始了服务监听!");
- }
- catch(CBlockingSocketException* e)
- {
- g_sListen.Cleanup();
- char error[1024];
- e->GetErrorMessage(error,1024);
- GetDlgItem(IDC_RESPONSE)->SetWindowText(error);
- e->Delete();
- SendMessage(WM_COMMAND,MAKEWPARAM(IDC_STOPBTN,0),0);
- }
- AfxBeginThread(Server,(LPVOID)this);
- }
- LRESULT CAnyServerView::OnSockCommand(WPARAM wParam,LPARAM lParam)
- {
- SCommand command=(SCommand)wParam;
- int nLength=(int)lParam;
- CRemoteC RemoteC(this);
- RemoteC.Command(command,DataBuf,nLength);
- return 0;
- }
- LRESULT CAnyServerView::OnIPAddr(WPARAM wParam,LPARAM lParam)
- {
- BOOL beAssumed=(BOOL)wParam;
- if (beAssumed)
- m_IPAddr.SetAddress(DWORD(lParam));
- else
- m_IPAddr.SetAddress(0);
- UpdateData(FALSE);
- return 0;
- }
- LRESULT CAnyServerView::OnValid(WPARAM wParam,LPARAM lParam)
- {
- BOOL bValid=(BOOL)wParam;
- m_CloseChat.EnableWindow(bValid);
- UpdateData(FALSE);
- return 0;
- }
- LRESULT CAnyServerView::OnUserName(WPARAM wParam,LPARAM lParam)
- {
- BOOL beAssumed=(BOOL)wParam;
- if (beAssumed)
- {
- char *temp=(char*)lParam;
- SetDlgItemText(IDC_USEREDIT,temp);
- }
- else
- SetDlgItemText(IDC_USEREDIT,"");
- return 0;
- }
- void CAnyServerView::OnStartserver()
- {
- CAnyServerView::OnStartbtn();
- }
- void CAnyServerView::OnStopserver()
- {
- CAnyServerView::OnStopbtn();
- }
- extern CBlockingSocket g_sListen;
- extern BOOL beAssumed;
- void CAnyServerView::OnStopbtn()
- {
- // TODO: Add your control notification handler code here
- if (g_sListen.m_hSocket!=NULL) g_sListen.Close();
- if (((CAnyServerDoc*)GetDocument())->g_pchatListen!=NULL&&((CAnyServerDoc*)GetDocument())->g_pchatListen->m_hSocket!=INVALID_SOCKET)
- {
- ((CAnyServerDoc*)GetDocument())->g_pchatListen->Close();
- delete ((CAnyServerDoc*)GetDocument())->g_pchatListen;
- ((CAnyServerDoc*)GetDocument())->g_pchatListen=NULL;
- }
- if (((CAnyServerDoc*)GetDocument())->m_pConnect!=NULL&&((CAnyServerDoc*)GetDocument())->m_pConnect->m_hSocket!=INVALID_SOCKET)
- {
- ((CAnyServerDoc*)GetDocument())->m_pConnect->Close();
- delete ((CAnyServerDoc*)GetDocument())->m_pConnect;
- ((CAnyServerDoc*)GetDocument())->m_pConnect=NULL;
- }
- SendMessage(WM_VALID,(WPARAM)FALSE,0);
- SendMessage(WM_USERNAME,(WPARAM)FALSE,0);
- SendMessage(WM_IPADDR,0,(LPARAM)FALSE);
- Started=FALSE;
- beAssumed=FALSE;
- }
- void CAnyServerView::OnUpdateStartserver(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->Enable(!Started);
- m_Startbtn.EnableWindow(!Started);
- }
- void CAnyServerView::OnUpdateStopserver(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->Enable(Started);
- m_Stopbtn.EnableWindow(Started);
- }
- void CAnyServerView::OnClosechat()
- {
- CAnyServerDoc* pDoc=(CAnyServerDoc*)GetDocument();
- if (pDoc->m_pConnect->m_hSocket!=INVALID_SOCKET)
- {
- pDoc->m_pConnect->Close();
- delete pDoc->m_pConnect;
- pDoc->m_pConnect=NULL;
- }
- SendMessage(WM_VALID,(WPARAM)FALSE,0);
- AfxGetApp()->SaveAllModified();
- POSITION pos = pDoc->GetFirstViewPosition();
- while (pos != NULL)
- {
- CView* pView = pDoc->GetNextView(pos);
- if (pView->IsKindOf(RUNTIME_CLASS(CEditView)))
- {
- pView->EnableWindow(FALSE);
- pView->SetWindowText("");
- }
- }
- }