📄 filetransferclientview.cpp
字号:
// FileTransferClientView.cpp : implementation of the CFileTransferClientView class
//
#include "stdafx.h"
#include "FileTransferClient.h"
#include "FileTransferClientDoc.h"
#include "FileTransferClientView.h"
#include <stdlib.h> // for rand and srand functions; used to simulate mismatch events
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
const UINT UWM_GETFILEEVENT = ::RegisterWindowMessage(
_T( "UWM_GETFILEEVENT_F6A100CA_692D_478d_B28C_8E87A1991EA1" ) );
/////////////////////////////////////////////////////////////////////////////
// CFileTransferClientView
IMPLEMENT_DYNCREATE(CFileTransferClientView, CFormView)
BEGIN_MESSAGE_MAP(CFileTransferClientView, CFormView)
//{{AFX_MSG_MAP(CFileTransferClientView)
ON_NOTIFY(IPN_FIELDCHANGED, IDC_IPADDRESS, OnFieldChangedIPAddress)
ON_BN_CLICKED(IDC_BUTTON_GETFILE, OnButtonGetFile)
ON_CBN_SELCHANGE(IDC_COMBOIPADDRESSES, OnSelChangeComboIPAddresses)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
ON_REGISTERED_MESSAGE( UWM_GETFILEEVENT, OnGetFileEvent )
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileTransferClientView construction/destruction
CFileTransferClientView::CFileTransferClientView()
: CFormView(CFileTransferClientView::IDD)
{
//{{AFX_DATA_INIT(CFileTransferClientView)
//}}AFX_DATA_INIT
// TODO: add construction code here
m_iNumMismatches = 0;
m_pThread = NULL;
}
CFileTransferClientView::~CFileTransferClientView()
{
delete m_pThread; // NULL deletes are OK
}
void CFileTransferClientView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFileTransferClientView)
DDX_Control(pDX, IDC_CHECK_SIMULATION, m_ctlSimulate);
DDX_Control(pDX, IDC_STATIC_STATUS, m_ctlStatus);
DDX_Control(pDX, IDC_STATIC_NUMMISMATCHES, m_ctlNumMismatches);
DDX_Control(pDX, IDC_PROGRESS_RECV, m_ctlProgressGet);
DDX_Control(pDX, IDC_EDIT_LOCALFILENAME, m_ctlFileName);
DDX_Control(pDX, IDC_COMBOIPADDRESSES, m_cbIPAddresses);
DDX_Control(pDX, IDC_IPADDRESS, m_ctlTargetIP);
DDX_Control(pDX, IDC_BUTTON_GETFILE, m_btnGetFile);
//}}AFX_DATA_MAP
}
BOOL CFileTransferClientView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CFileTransferClientView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
// initialize controls based on .INI file settings
CWinApp* pApp = AfxGetApp();
int nDex = pApp->GetProfileInt( "Dialog Settings", "Combo Box", -1 );
m_cbIPAddresses.SetCurSel( nDex );
CString strIP = pApp->GetProfileString( "Dialog Settings", "IP Address", NULL );
DWORD dwIP = ntohl( inet_addr( strIP.GetBuffer( MAX_PATH ) ) );
m_ctlTargetIP.SetAddress( dwIP );
CString fName = pApp->GetProfileString( "Dialog Settings", "Last File Name", NULL );
m_ctlFileName.SetWindowText( fName );
BOOL bCheck = (BOOL)pApp->GetProfileInt( "Dialog Settings", "Inject Mismatch Events", 0 );
m_ctlSimulate.SetCheck( bCheck );
// setup auxiliary controls
CString msg;
msg.Empty();
msg.Format( "Status: Idle" );
m_ctlStatus.SetWindowText( msg );
msg.Format( "Receive mismatch events = %d", m_iNumMismatches );
m_ctlNumMismatches.SetWindowText( msg );
m_ctlProgressGet.SetRange( 0, 128 );
m_ctlProgressGet.SetPos( 0 );
}
#define GFE_THREADSTART 0x00000001
#define GFE_THREADCOMPLETE 0x00000002
#define GFE_UPDATECONTROLS 0x00000004
#define GFE_STATUSCONNECTED 0x00000008
afx_msg LRESULT CFileTransferClientView::OnGetFileEvent(WPARAM wParam, LPARAM lParam)
{
switch ( wParam )
{
case GFE_THREADSTART:
m_btnGetFile.EnableWindow( FALSE );
m_ctlFileName.EnableWindow( FALSE );
m_cbIPAddresses.EnableWindow( FALSE );
m_ctlTargetIP.EnableWindow( FALSE );
m_ctlSimulate.EnableWindow( FALSE );
m_ctlStatus.SetWindowText( "Trying to connect ..." );
break;
case GFE_THREADCOMPLETE:
m_btnGetFile.EnableWindow( TRUE );
m_ctlFileName.EnableWindow( TRUE );
m_cbIPAddresses.EnableWindow( TRUE );
m_ctlTargetIP.EnableWindow( TRUE );
m_ctlSimulate.EnableWindow( TRUE );
m_ctlStatus.SetWindowText( "Status: Idle" );
m_ctlProgressGet.SetPos( 0 );
m_pThread = NULL;
break;
case GFE_UPDATECONTROLS:
{
CString msg;
msg.Format( "Receive mismatch events = %d", m_iNumMismatches );
m_ctlNumMismatches.SetWindowText( msg );
}
break;
case GFE_STATUSCONNECTED:
m_ctlStatus.SetWindowText( "Connection established -- receiving file ..." );
break;
default:
ASSERT( FALSE ); // shouldn't get here
}
return 0L;
}
void CFileTransferClientView::OnDestroy()
{
// save settings in .INI file
CWinApp* pApp = AfxGetApp();
pApp->WriteProfileInt( "Dialog Settings", "Combo Box", m_cbIPAddresses.GetCurSel() );
IN_ADDR ia;
DWORD dwIP;
m_ctlTargetIP.GetAddress( dwIP );
ia.S_un.S_addr = htonl( dwIP );
pApp->WriteProfileString( "Dialog Settings", "IP Address", inet_ntoa( ia ) );
CString fName;
m_ctlFileName.GetWindowText( fName );
pApp->WriteProfileString( "Dialog Settings", "Last File Name", fName );
pApp->WriteProfileInt( "Dialog Settings", "Inject Mismatch Events", (BOOL)m_ctlSimulate.GetCheck() );
CFormView::OnDestroy();
}
/////////////////////////////////////////////////////////////////////////////
// CFileTransferClientView printing
BOOL CFileTransferClientView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CFileTransferClientView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CFileTransferClientView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CFileTransferClientView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CFileTransferClientView diagnostics
#ifdef _DEBUG
void CFileTransferClientView::AssertValid() const
{
CFormView::AssertValid();
}
void CFileTransferClientView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CFileTransferClientDoc* CFileTransferClientView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFileTransferClientDoc)));
return (CFileTransferClientDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CFileTransferClientView message handlers
void CFileTransferClientView::OnFieldChangedIPAddress(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}
void CFileTransferClientView::OnButtonGetFile()
{
if ( m_pThread != NULL ) // make certain thread is not already running
return;
DWORD dwIP = 0;
IN_ADDR ia;
CString strLocalFileName;
CString strTargetIP;
// get IP address
m_ctlTargetIP.GetAddress( dwIP );
ia.S_un.S_addr = htonl( dwIP );
strTargetIP = inet_ntoa( ia );
// get file name and check for invalid characters
m_ctlFileName.GetWindowText( strLocalFileName );
if ( -1 != strLocalFileName.FindOneOf( ";*?\"<>|" ) )
{
CString sMessage;
sMessage.Format( "File name contains invalid characters (;*?\"<>| are not permitted)\n"
"%s\nEnter another name", strLocalFileName );
::MessageBox( NULL, sMessage, "Invalid File Name", MB_OK|MB_ICONEXCLAMATION );
return;
}
// now get the file
/// BOOL bRet = GetFileFromRemoteSender( strTargetIP, strLocalFileName ); // now done in separate thread, below
m_strFileName = strLocalFileName;
m_strTargetIP = strTargetIP;
m_bSimulateEvents = m_ctlSimulate.GetCheck();
m_pThread = ::AfxBeginThread( ThreadedGetFileFromRemoteSender, (LPVOID) this, THREAD_PRIORITY_BELOW_NORMAL );
}
void CFileTransferClientView::OnSelChangeComboIPAddresses()
{
CString strIP, strTemp;
strIP.Empty();
m_cbIPAddresses.GetLBText( m_cbIPAddresses.GetCurSel(), strIP );
DWORD dwIP = ntohl( inet_addr( strIP.GetBuffer( MAX_PATH ) ) );
m_ctlTargetIP.SetAddress( dwIP );
/*******************
use inet_addr function (above) instead of this code
********************/
/*
int ii, nDex;
BOOL bErr = TRUE;
BYTE b[4];
for ( ii=0; ii<3; ii++ )
{
nDex = strIP.Find( '.', 0 );
if ( nDex == -1 )
{
bErr = FALSE;
break;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -