📄 udtsenddlg.cpp
字号:
// UDTSendDlg.cpp : implementation file
//
#include "stdafx.h"
#include "UDTSend.h"
#include "UDTSendDlg.h"
#include "cmmdef.h"
#include <vector>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static std::vector<SNDPKT> vsnd;
static int minWaiting,minSend;
static int nAckCt,nErrct;
//数据区
static char databuf[1024];
static int datasz;
const int N=20;
void CUDTSendDlg::StartSend()
{
if(vsnd.empty()) return;
minWaiting=minSend=0;
SetTimer(TIMER_SRV_SEND,SRV_SEND_TM,NULL);
nAckCt=nErrct=0;
//SetTimer(TIMER_SRV_ACKWAIT,SRV_ACKWAIT_TM);
//xianshi
CString txt,str; m_info.GetWindowText(txt);
str.Format("PKTs send start..\tn=%d\r\n",vsnd.size());
txt+=str;
m_info.SetWindowText(txt);
}
void CUDTSendDlg::sendPkt(int pktid)
{
CString txt,str; m_info.GetWindowText(txt);
if(pktid<0||pktid>=vsnd.size())
{
str.Format("Error sendPkt:(id=%d)\r\n",pktid);
return ;
}
SNDPKT* pPkt=&vsnd[pktid];
CString send_ip;
BYTE f1,f2,f3,f4;
m_ip.GetAddress(f1,f2,f3,f4);
send_ip.Format("%d.%d.%d.%d",f1,f2,f3,f4);
if(sndSock.SendTo(pPkt,sizeof(SNDPKT),PORT_CLI,send_ip,0)==SOCKET_ERROR)
{
TRACE0("Error on send");
return ;
}
//minSend=pktid;
SetTimer(TIMER_SRV_ACKWAIT,SRV_ACKWAIT_TM,NULL);
//nAckCt=0;
//msg
str.Format("To(%s):PKTid=%d--Send\r\n",send_ip,pPkt->id);
txt+=str;
m_info.SetWindowText(txt); m_info.LineScroll(m_info.GetLineCount());
}
/////////////////////////////////////////////////////////////////////////////
// CUDTSendDlg dialog
CUDTSendDlg::CUDTSendDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUDTSendDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUDTSendDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUDTSendDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUDTSendDlg)
DDX_Control(pDX, IDC_IPADDRESS, m_ip);
DDX_Control(pDX, IDC_EDIT_MSG, m_msgtxt);
DDX_Control(pDX, IDC_EDIT_INFO, m_info);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUDTSendDlg, CDialog)
//{{AFX_MSG_MAP(CUDTSendDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_SEND, OnBtnSend)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUDTSendDlg message handlers
BOOL CUDTSendDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
AfxSocketInit();
m_ip.SetAddress(127,0,0,1);
if(!sndSock.Create(PORT_SRV,SOCK_DGRAM))
{
TRACE0("failed to create sock");
return FALSE;
}
sndSock.SetNotifyWnd(GetSafeHwnd());
sndSock.SetRcvNotifyMsg(SRV_RCV_MSG);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CUDTSendDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CUDTSendDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CUDTSendDlg::OnBtnSend()
{
// TODO: Add your control notification handler code here
//MessageBox("s");
m_msgtxt.GetWindowText(databuf,1024);
vsnd=makepkts(databuf,strlen(databuf)+1);
StartSend();
}
LRESULT CUDTSendDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
if(message==SRV_RCV_MSG)
{
REQPKT rpkt;CString txt,str; m_info.GetWindowText(txt);
if(sndSock.Receive(&rpkt,sizeof(REQPKT))==sizeof(REQPKT)&&rpkt.id>=0)
{
// nErrct=0;
if(rpkt.id==minWaiting)
{
minWaiting++;//确认的成功发送数目+1
//KillTimer(TIMER_SRV_ACKWAIT);
nAckCt=0;//重新计时
str.Format("receive ack:%d\r\n",rpkt.id);txt+=str;
//importatn---------------------------------
if(minWaiting==vsnd.size()) //传输结束
{
KillTimer(TIMER_SRV_SEND);
KillTimer(TIMER_SRV_ACKWAIT);
vsnd.clear();
str="传输结束\r\n"; txt+=str;
}
}
else //丢弃
{
str.Format("receive ack(but ignored!):%d\r\n",rpkt.id);txt+=str;
}
}
m_info.SetWindowText(txt);m_info.LineScroll(m_info.GetLineCount());
}
return CDialog::WindowProc(message, wParam, lParam);
}
void CUDTSendDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==TIMER_SRV_SEND)
{
//发送数据
sendPkt(minSend); if(minSend<vsnd.size()) minSend++;
if(minSend-minWaiting>N) nAckCt=10; //back N!
}
else if(nIDEvent==TIMER_SRV_ACKWAIT)
{//ack超时
if(nAckCt++>10)
{nAckCt=0;
CString txt,str; m_info.GetWindowText(txt);
if(nErrct++>12)
{
str.Format("无法和client取得联系,传输中断!!\r\n");txt+=str;
m_info.SetWindowText(txt);m_info.LineScroll(m_info.GetLineCount());
KillTimer(TIMER_SRV_SEND);
KillTimer(TIMER_SRV_ACKWAIT);
nErrct=0;
}
str.Format("ACK超时!当前发送:%d,倒回%d..\r\n",minSend,minWaiting);
txt+=str;
m_info.SetWindowText(txt);m_info.LineScroll(m_info.GetLineCount());
minSend=minWaiting; //从最后未确认处发送
}
}
CDialog::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -