📄 client.cpp
字号:
ASSERT_KINDOF( CClientView, pView );
int nLine = pMsg->m_nCount;
pView->SetSelection( CPoint( 0, nLine ),
CPoint( pDoc->m_TextBuffer.GetLineLength( nLine ), nLine ) );
Message( pMsg->m_msgList.RemoveHead() );
int id;
::sscanf( (LPCTSTR)pMsg->m_msgList.GetHead(), "%d", &id );
switch( id )
{
case OUT_TYPE_ERROR:
{
CSelDoc* pDoc = ( CSelDoc* )m_SelDocList.RemoveHead();
ASSERT_VALID( pDoc );
ASSERT_KINDOF( CSelDoc, pDoc );
pDoc->OnCloseDocument();
}
break;
default:
;
}
}
break;
case SEL_START:
{
POSITION pos = GetFirstDocTemplatePosition();
CDocTemplate* pTemplate;
while( pos )
{
pTemplate = GetNextDocTemplate( pos );
CString str;
pTemplate->GetDocString( str, CDocTemplate::docName );
if( str == _T( "SEL" ) )
{
pTemplate->OpenDocumentFile(NULL);
break;
}
}
pos = pTemplate->GetFirstDocPosition();
CSelDoc* pDoc;
while( pos )
pDoc = ( CSelDoc* )pTemplate->GetNextDoc( pos );
ASSERT_VALID( pDoc );
ASSERT_KINDOF( CSelDoc, pDoc );
m_SelDocList.AddTail( pDoc );
Message( pMsg->m_msgList.GetHead() );
}
break;
case SEL_COL:
{
CSelDoc* pDoc = ( CSelDoc* )m_SelDocList.GetHead();
ASSERT_VALID( pDoc );
ASSERT_KINDOF( CSelDoc, pDoc );
POSITION pos = pDoc->GetFirstViewPosition();
CSelView* pView = ( CSelView* )pDoc->GetNextView( pos );
ASSERT_VALID( pView );
ASSERT_KINDOF( CSelView, pView );
pView->InsertCol( *pMsg );
}
break;
case SEL_ITEM:
{
CSelDoc* pDoc = ( CSelDoc* )m_SelDocList.GetHead();
ASSERT_VALID( pDoc );
ASSERT_KINDOF( CSelDoc, pDoc );
POSITION pos = pDoc->GetFirstViewPosition();
CSelView* pView = ( CSelView* )pDoc->GetNextView( pos );
ASSERT_VALID( pView );
ASSERT_KINDOF( CSelView, pView );
pView->InsertItem( *pMsg );
}
break;
case SEL_SUCCESSFUL:
{
m_SelDocList.RemoveHead();
Message( pMsg->m_msgList.GetHead() );
}
break;
case COMPLETED:
{
m_ClientDocList.RemoveHead();
}
break;
case TABLE_CREATED:
{
CString tname = pMsg->m_msgList.GetHead();
CMainFrame* pMainFrm = ( CMainFrame* )AfxGetMainWnd();
pMainFrm->m_FileBar.insert_tree( tname, pMainFrm->m_FileBar.hitem0 );
pMainFrm->m_FileBar.ExpandTree();
Message( "Successful : Created table \"" + tname + "\"\n" );
}
break;
case INDEX_CREATED:
{
CString tname = pMsg->m_msgList.RemoveHead();
CString iname = pMsg->m_msgList.RemoveHead();
CMainFrame* pMainFrm = ( CMainFrame* )AfxGetMainWnd();
pMainFrm->m_FileBar.InsertIndex( tname, iname );
Message( "Successful : Created index \"" + iname + ".bpt\" on table \"" + tname + "\"\n" );
}
break;
case TABLE_DROP:
{
CString tname = pMsg->m_msgList.GetHead();
CMainFrame* pMainFrm = ( CMainFrame* )AfxGetMainWnd();
pMainFrm->m_FileBar.DelFile( tname );
Message( "Successful : Dropped table \"" + tname +"\"\n" );
}
break;
case INDEX_DROP:
{
CString tname = pMsg->m_msgList.RemoveHead();
CString iname = pMsg->m_msgList.RemoveHead();
CMainFrame* pMainFrm = ( CMainFrame* )AfxGetMainWnd();
pMainFrm->m_FileBar.DropIndex( tname, iname );
Message( "Successful : Dropped index \"" + iname + ".bpt\" on table\"" + tname + "\"\n" );
}
break;
case SUCCESSFUL:
{
Message( pMsg->m_msgList.GetHead() );
}
break;
default:
;
}
}
while( !m_pArchiveIn->IsBufferEmpty() );
}
void CClientApp::SendMsg(CMsg &msg)
{
if( m_pArchiveOut )
{
TRY
{
msg.Serialize( *m_pArchiveOut );
m_pArchiveOut->Flush();
}
CATCH( CFileException, e )
{
m_pArchiveOut->Abort();
delete m_pArchiveOut;
m_pArchiveOut = NULL;
Message( "Server has reset the connection!\n" );
}
END_CATCH
}
}
BOOL CClientApp::ConnectSocket( LPCTSTR name, LPCTSTR code, LPCTSTR address, UINT port )
{
m_pSocket = new CRecSocket();
if( !m_pSocket->Create() )
{
delete m_pSocket;
m_pSocket = NULL;
AfxMessageBox( "Socket creating failed!" );
return FALSE;
}
while( !m_pSocket->Connect( address, port + 700 ) )
{
if( AfxMessageBox( "无法连接,要重试吗?",
MB_ICONQUESTION | MB_YESNO ) == IDNO )
{
delete m_pSocket;
m_pSocket = NULL;
return FALSE;
}
}
m_pFile = new CSocketFile( m_pSocket );
m_pArchiveIn = new CArchive( m_pFile, CArchive::load );
m_pArchiveOut = new CArchive( m_pFile, CArchive::store );
m_name = name;
m_address = address;
m_port = port;
CMsg msg( LOGON );
msg.m_msgList.AddTail( name );
msg.m_msgList.AddTail( code );
SendMsg( msg );
return TRUE;
}
void CClientApp::Drop()
{
if( (m_pSocket != NULL) && (m_pFile != NULL) && (m_pArchiveOut != NULL) )
{
CMsg msg( CLIENT_CLOSE );
msg.m_bClose = TRUE;
msg.m_msgList.AddTail( m_name );
msg.Serialize( *m_pArchiveOut );
m_pArchiveOut->Flush();
}
delete m_pArchiveIn;
m_pArchiveIn = NULL;
delete m_pArchiveOut;
m_pArchiveOut = NULL;
delete m_pFile;
m_pFile = NULL;
if( m_pSocket )
{
BYTE buffer[ 50 ];
m_pSocket->ShutDown();
while( m_pSocket->Receive( buffer, 50 ) > 0 );
}
delete m_pSocket;
m_bConnection = FALSE;
m_pSocket = NULL;
m_address = _T("");
m_port = 0;
m_name = _T("");
m_type = 0;
}
BOOL CClientApp::Logon()
{
CLogDlg dlg;
dlg.m_address = m_address;
dlg.m_port = m_port;
dlg.m_name = m_name;
if( dlg.DoModal() == IDOK )
{
if( m_pSocket && (dlg.m_address == m_address) && (dlg.m_port == m_port) )
{
CMsg msg( LOGON );
msg.m_msgList.AddTail( dlg.m_name );
msg.m_msgList.AddTail( dlg.m_code );
SendMsg( msg );
return TRUE;
}
else if( m_pSocket )
{
Drop();
if( ConnectSocket( dlg.m_name, dlg.m_code, dlg.m_address, dlg.m_port ) )
return TRUE;
}
else
{
if( ConnectSocket( dlg.m_name, dlg.m_code, dlg.m_address, dlg.m_port ) )
return TRUE;
}
if ( AfxMessageBox( "要不要更改IP地址?",MB_ICONQUESTION | MB_YESNO) == IDYES )
return Logon();
else
return FALSE;
}
else
CWinApp::OnAppExit();
return FALSE;
}
int CClientApp::ExitInstance()
{
Drop();
return CWinApp::ExitInstance();
}
void CClientApp::OnConnect()
{
Logon();
}
void CClientApp::OnUpdateConnect(CCmdUI* pCmdUI)
{
pCmdUI->Enable( !m_bConnection );
}
void CClientApp::OnDisconnect()
{
if( AfxMessageBox( "这将断开与服务器的连接,\n要继续吗?",
MB_ICONQUESTION | MB_YESNO ) == IDYES )
Drop();
}
void CClientApp::OnUpdateDisconnect(CCmdUI* pCmdUI)
{
pCmdUI->Enable( m_bConnection );
}
void CClientApp::OnHelpMe()
{
CString path( theApp.dir );
path += "\\help.hlp";
OpenDocumentFile( path );
}
void CClientApp::OnFileNew()
{
POSITION curTemplatePos = GetFirstDocTemplatePosition();
while( curTemplatePos != NULL )
{
CDocTemplate* curTemplate =
GetNextDocTemplate( curTemplatePos );
CString str;
curTemplate->GetDocString( str, CDocTemplate::docName );
if( str == _T( "Client" ) )
{
curTemplate->OpenDocumentFile(NULL);
return;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -