?? filesenddlg.cpp
字號:
// FileSendDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FileSendDlg.h"
#include "DSPFile.h"
#include "DSPSpider.h"
#include "SpiderWnd.h"
#include "ListenType.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// FileSendDlg dialog
FileSendDlg::FileSendDlg(CWnd* pParent /*=NULL*/)
: CDialog(FileSendDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(FileSendDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
this->m_phListenThreadParam =NULL;
this->m_sockConnect =INVALID_SOCKET;
}
void FileSendDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(FileSendDlg)
DDX_Control(pDX, IDC_STATIC_FILESIZE, m_strFilesize);
DDX_Control(pDX, IDC_STATIC_FILENAME, m_strFilename);
DDX_Control(pDX, IDC_STATIC_CURRLEN, m_strCurrLen);
DDX_Control(pDX, IDC_PROGRESS1, m_progress);
DDX_Control(pDX, IDC_COMBOIP, m_comboIP);
DDX_Control(pDX, IDC_CHECKNET, m_checkNet);
DDX_Control(pDX, IDC_CHECKCRC, m_checkCrc);
DDX_Control(pDX, IDC_BTNSEND, m_btnSend);
DDX_Control(pDX, IDC_BTNLISTEN, m_btnListen);
DDX_Control(pDX, IDC_BTNCONNECT, m_btnConnect);
DDX_Control(pDX, IDC_BTNCLOSE, m_btnClose);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(FileSendDlg, CDialog)
//{{AFX_MSG_MAP(FileSendDlg)
ON_BN_CLICKED(IDC_BTNCLOSE, OnBtnclose)
ON_BN_CLICKED(IDC_BTNCONNECT, OnBtnconnect)
ON_BN_CLICKED(IDC_BTNLISTEN, OnBtnlisten)
ON_BN_CLICKED(IDC_BTNSEND, OnBtnsend)
ON_BN_CLICKED(IDC_BTNSPIDERRECV, OnBtnspiderrecv)
ON_BN_CLICKED(IDC_BTNJOIN, OnBtnjoin)
ON_BN_CLICKED(IDC_BTNHELP, OnBtnhelp)
//}}AFX_MSG_MAP
ON_MESSAGE(DSPFile::MSG_SENDSECT,OnSendSect)
ON_MESSAGE(DSPFile::MSG_RECVSECT,OnRecvSect)
ON_MESSAGE(DSPFile::MSG_SENDCOMPLETE,OnSendComplete)
ON_MESSAGE(DSPFile::MSG_SETFILENAMESIZE,OnSetFilenameSize)
ON_MESSAGE(DSPSpider::MSG_GETFILENAME,OnGetFilename)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// FileSendDlg message handlers
BOOL FileSendDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
this->m_btnClose.EnableWindow(FALSE);
this->m_btnSend.EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void FileSendDlg::OnBtnclose()
{
// TODO: Add your control notification handler code here
if(this->m_bRun)
{
this->m_bRun =false;
//禁止放棄、傳送按鈕
this->m_btnClose.EnableWindow(FALSE);
this->m_btnSend.EnableWindow(FALSE);
//允許連接按鈕、IP選擇和校驗(yàn)選擇
this->m_btnConnect.EnableWindow();
this->m_comboIP.EnableWindow();
if(this->m_btnListen.IsWindowEnabled())
{
//如果沒有按下監(jiān)聽按鈕,則允許連接類型選擇
this->m_checkNet.EnableWindow();
}
::closesocket(this->m_sockConnect);
this->m_sockConnect =INVALID_SOCKET;
}
}
void FileSendDlg::OnBtnconnect()
{
// TODO: Add your control notification handler code here
if(this->m_sockConnect!=INVALID_SOCKET)
{
::AfxMessageBox("連接已創(chuàng)建,必需先斷開后,才能再次連接。");
return;
}
CString str;
int nSel =this->m_comboIP.GetCurSel();
if(nSel<0)
{
//如果沒有選擇
this->m_comboIP.GetWindowText(str);
if(str=="")
{
//如果輸入了IP
::AfxMessageBox("請首先選擇一個(gè)IP");
return;
}
}
else
{
//如果選擇了一個(gè)IP
this->m_comboIP.GetLBText(nSel,str);
}
if(str=="")
{
::AfxMessageBox("請首行輸入連接方的IP!");
}
else
{
//連接指定的IP
SOCKET sock =DSP::ConnectTo(str,LISTENPORT,(this->m_checkNet.GetCheck())?false:true);
if(sock!=INVALID_SOCKET)
{
if(nSel<0)
{
//如果沒有選擇一個(gè)IP,則將這個(gè)成功的IP加入下拉列表中
this->m_comboIP.AddString(str);
}
this->m_bRun =true;
this->m_bSendType =true;
this->m_sockConnect =sock;
//禁止選擇連接類型
this->m_checkNet.EnableWindow(FALSE);
//禁止連接按鈕
this->m_btnConnect.EnableWindow(FALSE);
//禁止選擇IP
this->m_comboIP.EnableWindow(FALSE);
//允許傳送、放棄按鈕
this->m_btnSend.EnableWindow();
this->m_btnClose.EnableWindow();
}
else
{
::AfxMessageBox("連接失敗,請檢查IP后重試!");
}
}
}
void FileSendDlg::OnBtnsend()
{
// TODO: Add your control notification handler code here
//禁止傳送按鈕和校驗(yàn)選擇
::AfxBeginThread(FileSendDlg::SendFileThread,(LPVOID)this);
}
UINT FileSendDlg::SendFileThread(LPVOID lpDlg)
{
FileSendDlg* pDlg =(FileSendDlg*)lpDlg;
//是否需要CRC校驗(yàn)
bool bCrc =(pDlg->m_checkCrc.GetCheck())?false:true;
if(pDlg->m_sockConnect!=INVALID_SOCKET)
{
//設(shè)置等待時(shí)間為1分鐘
DSP::_DSPPACKET packet(pDlg->m_bRun);
packet.Timeout.tv_sec =60;
packet.SetParameter(false,false);
//允許傳送
pDlg->m_bRun =true;
if(pDlg->m_bSendType)
{
//設(shè)置希望監(jiān)聽方SOCKET所執(zhí)行的動作
ListenType lt;
lt.m_dwID =0;
lt.m_wType =ListenType::OTHER;
//傳送ListenType對象
if(packet.WriteObject(pDlg->m_sockConnect,lt)==false)
{
//如果傳送對象失敗,則關(guān)閉連接
::closesocket(pDlg->m_sockConnect);
pDlg->m_sockConnect =INVALID_SOCKET;
//允許CRC校驗(yàn),禁止斷開按鈕
pDlg->m_checkCrc.EnableWindow();
pDlg->m_btnClose.EnableWindow(FALSE);
//允許選擇連接類型
pDlg->m_checkNet.EnableWindow();
//允許連接按鈕
pDlg->m_btnConnect.EnableWindow();
pDlg->m_bRun =false;
}
//不需要再傳送類型
pDlg->m_bSendType =false;
}
if(pDlg->m_bRun)
{
//創(chuàng)建一個(gè)對象
DSPFile dspFile;
if(dspFile.ReadySendFile(pDlg->m_bRun,pDlg->m_sockConnect,bCrc,packet,pDlg->m_hWnd))
{
//如果傳送成功,則允許傳送按鈕和校驗(yàn)選擇
pDlg->m_btnSend.EnableWindow();
pDlg->m_checkCrc.EnableWindow();
}
}
}
::AfxEndThread(0);
return 0;
}
void FileSendDlg::OnBtnlisten()
{
// TODO: Add your control notification handler code here
//禁止重新選擇連接類型
this->m_checkNet.EnableWindow(FALSE);
//禁止監(jiān)聽按鈕
this->m_btnListen.EnableWindow(FALSE);
this->m_phListenThreadParam =new DSPSpider::_SENDTHREADPARAM();
this->m_phListenThreadParam->pCWnd =this;
this->m_phListenThreadParam->hwnd =this->m_hWnd;
this->m_phListenThreadParam->bInternet =(this->m_checkNet.GetCheck())?false:true;
this->m_phListenThreadParam->pFunc =FileSendDlg::ListenProcessFunc;
::AfxBeginThread(DSPSpider::ListenThread,(LPVOID)this->m_phListenThreadParam);
}
LRESULT FileSendDlg::OnSendComplete(WPARAM wParam, LPARAM lParam)
{
//允許重新選擇是否校驗(yàn)
this->m_checkCrc.EnableWindow();
CString str;
if(DSPFile::GetCompleteInfo(lParam,str))
{
//如果成功
::AfxMessageBox(str);
//允許傳送按鈕
this->m_btnConnect.EnableWindow();
}
else
{
//如果失敗
::AfxMessageBox(str);
this->OnBtnclose();
}
return 0;
};
LRESULT FileSendDlg::OnRecvSect(WPARAM wParam, LPARAM lParam)
{
this->m_progress.StepIt();
DSPFile::_FILEDETAIL* lphfd =(DSPFile::_FILEDETAIL*)wParam;
CString str;
str.Format("(%dK) %d",lphfd->nCurrLen/1024,lphfd->nCurrLen);
this->m_strCurrLen.SetWindowText(str);
return 0;
};
LRESULT FileSendDlg::OnSendSect(WPARAM wParam, LPARAM lParam)
{
this->m_progress.StepIt();
DSPFile::_FILEDETAIL* lphfd =(DSPFile::_FILEDETAIL*)wParam;
CString str;
str.Format("(%dK) %d",lphfd->nCurrLen/1024,lphfd->nCurrLen);
this->m_strCurrLen.SetWindowText(str);
return 0;
}
LRESULT FileSendDlg::OnSetFilenameSize(WPARAM wParam, LPARAM lParam)
{
//設(shè)置文件的名字和大小
DSPFile::_FILEDETAIL* lphfd =(DSPFile::_FILEDETAIL*)wParam;
if(lphfd->nCurrLen>0)
{
//顯示文件名
this->m_strFilename.SetWindowText(lphfd->strPathname);
//顯示總的長度
CString str;
str.Format("/ %d (%dK)",lphfd->nFilesize,lphfd->nFilesize/1024);
this->m_strFilesize.SetWindowText(str);
//初始化當(dāng)前接收的長度
this->m_strCurrLen.SetWindowText("0K");
//初始化進(jìn)度顯示
this->m_progress.SetRange32(0,lphfd->nFilesize);
this->m_progress.SetStep(lphfd->nCurrLen);
this->m_progress.SetPos(0);
//禁止傳送按鈕和校驗(yàn)選擇
this->m_btnSend.EnableWindow(FALSE);
this->m_checkCrc.EnableWindow(FALSE);
}
else
{
//顯示文件名
this->m_strFilename.SetWindowText(lphfd->strPathname);
}
return 0;
}
LRESULT FileSendDlg::OnGetFilename(WPARAM wParam, LPARAM lParam)
{
//打開對話框,選擇需要被傳送的文件名
DSPSpider::_FILEDETAIL& fd =*(DSPSpider::_FILEDETAIL*)wParam;
CFileDialog fileDialog(TRUE,NULL,fd.strFilename,OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_FILEMUSTEXIST);
if(fileDialog.DoModal()==IDOK)
{
//得到被選擇的文件名
CString str =fileDialog.GetPathName();
DSPSpider::SetFilename(wParam,str);
fd.dwFilesize =DSP::GetFileSize(fd.strPathname);
}
return 0;
};
void FileSendDlg::OnBtnspiderrecv()
{
// TODO: Add your control notification handler code here
POINT point;
CWnd* pWnd;
int row =15;
int delta =SpiderWnd::WND_HEIGHT+5;
point.x =850;
for(;row<500;row+=delta)
{
point.y =row;
pWnd =CWnd::WindowFromPoint(point);
if(pWnd!=NULL)
{
if(pWnd->GetParent()==this)
continue;
}
//如果找到了一個(gè)空的位置
SpiderWnd* bs =new SpiderWnd;
SpiderWnd::pLastWnd =bs;
bs->Create(800,row,170,this);
bs->ShowWindow(SW_SHOW);
bs->m_spider.m_bInternet =(this->m_checkNet.GetCheck())?false:true;
//打開對話框,選擇需要下載的文件名
CFileDialog fileDialog(FALSE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST);
if(fileDialog.DoModal()==IDOK)
{
//得到被選擇的文件名
bs->m_spider.RunSpider(fileDialog.GetFileName(),fileDialog.GetPathName(),bs->m_hWnd,3);
}
break;
}
}
bool FileSendDlg::ListenProcessFunc(DSPSpider::_SENDTHREADPARAM *phParam,DSP::_DSPPACKET& packet)
{
FileSendDlg* pDlg =(FileSendDlg*)phParam->pCWnd;
//準(zhǔn)備接收文件
//允許斷開按鈕
pDlg->m_btnClose.EnableWindow();
//禁止連接按鈕
pDlg->m_btnConnect.EnableWindow(FALSE);
//保存連接的SOCKET
pDlg->m_sockConnect =phParam->sock;
DSPFile dspFile;
dspFile.ReadyRecvFile(pDlg->m_bRun,phParam->sock,packet,pDlg->m_hWnd);
return true;
};
void FileSendDlg::OnBtnjoin()
{
// TODO: Add your control notification handler code here
if(SpiderWnd::pLastWnd!=NULL)
{
CString str;
this->m_comboIP.GetWindowText(str);
if(str=="")
{
::AfxMessageBox("請首先選擇一個(gè)IP或者輸入一個(gè)新的IP,然后再點(diǎn)擊加入按鈕。");
return;
}
SpiderWnd::pLastWnd->m_spider.AddIP(str,34567);
}
}
void FileSendDlg::OnBtnhelp()
{
// TODO: Add your control notification handler code here
CString str;
str.Format("在點(diǎn)擊[多線程接收]接鈕后,必需要選擇或輸入一個(gè)IP,然后點(diǎn)擊[加入多線程IP]按鈕,加入一個(gè)IP,程序會自動與新加入的IP建立連接,并開始下載。\r\n傳送方將根據(jù)接收方傳送的文件名,選擇一個(gè)文件準(zhǔn)備傳送。\r\n可以選擇多個(gè)IP加入,但是當(dāng)IP超過設(shè)置的線程個(gè)數(shù)時(shí),則將不起作用。");
::AfxMessageBox(str);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -