⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesenddlg.cpp

📁 a dsp dll and sample
💻 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选择和校验选择
		this->m_btnConnect.EnableWindow();
		this->m_comboIP.EnableWindow();

		if(this->m_btnListen.IsWindowEnabled())
		{
			//如果没有按下监听按钮,则允许连接类型选择
			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("连接已创建,必需先断开后,才能再次连接。");
		return;
	}

	CString str;
	int	nSel	=this->m_comboIP.GetCurSel();
	if(nSel<0)
	{
		//如果没有选择
		this->m_comboIP.GetWindowText(str);
		if(str=="")
		{
			//如果输入了IP
			::AfxMessageBox("请首先选择一个IP");
			return;
		}
	}
	else
	{
		//如果选择了一个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)
			{
				//如果没有选择一个IP,则将这个成功的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
	//禁止传送按钮和校验选择
	::AfxBeginThread(FileSendDlg::SendFileThread,(LPVOID)this);
}

UINT FileSendDlg::SendFileThread(LPVOID lpDlg)
{
	FileSendDlg* pDlg	=(FileSendDlg*)lpDlg;
	
	//是否需要CRC校验
	bool	bCrc		=(pDlg->m_checkCrc.GetCheck())?false:true;

	if(pDlg->m_sockConnect!=INVALID_SOCKET)
	{
		//设置等待时间为1分钟
		DSP::_DSPPACKET packet(pDlg->m_bRun);
		packet.Timeout.tv_sec	=60;
		packet.SetParameter(false,false);
		
		//允许传送
		pDlg->m_bRun	=true;

		if(pDlg->m_bSendType)
		{
			//设置希望监听方SOCKET所执行的动作
			ListenType	lt;
			lt.m_dwID	=0;
			lt.m_wType	=ListenType::OTHER;

			//传送ListenType对象
			if(packet.WriteObject(pDlg->m_sockConnect,lt)==false)
			{
				//如果传送对象失败,则关闭连接
				::closesocket(pDlg->m_sockConnect);
				pDlg->m_sockConnect	=INVALID_SOCKET;

				//允许CRC校验,禁止断开按钮
				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)
		{
			//创建一个对象
			DSPFile	dspFile;
			if(dspFile.ReadySendFile(pDlg->m_bRun,pDlg->m_sockConnect,bCrc,packet,pDlg->m_hWnd))
			{
				//如果传送成功,则允许传送按钮和校验选择
				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);

	//禁止监听按钮
	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)
{
	//允许重新选择是否校验
	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)
{
	//设置文件的名字和大小
	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);

		//初始化当前接收的长度
		this->m_strCurrLen.SetWindowText("0K");

		//初始化进度显示
		this->m_progress.SetRange32(0,lphfd->nFilesize);
		this->m_progress.SetStep(lphfd->nCurrLen);
		this->m_progress.SetPos(0);

		//禁止传送按钮和校验选择
		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;
		}

		//如果找到了一个空的位置
		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;
	
	//准备接收文件
	//允许断开按钮
	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("请首先选择一个IP或者输入一个新的IP,然后再点击加入按钮。");
			return;
		}
		SpiderWnd::pLastWnd->m_spider.AddIP(str,34567);
	}
}

void FileSendDlg::OnBtnhelp() 
{
	// TODO: Add your control notification handler code here
	CString str;
	str.Format("在点击[多线程接收]接钮后,必需要选择或输入一个IP,然后点击[加入多线程IP]按钮,加入一个IP,程序会自动与新加入的IP建立连接,并开始下载。\r\n传送方将根据接收方传送的文件名,选择一个文件准备传送。\r\n可以选择多个IP加入,但是当IP超过设置的线程个数时,则将不起作用。");
	::AfxMessageBox(str);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -