📄 arqdlg.cpp
字号:
// ARQDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ARQ.h"
#include "ARQDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CARQDlg dialog
CARQDlg::CARQDlg(CWnd* pParent /*=NULL*/)
: CDialog(CARQDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CARQDlg)
m_eveSend = _T("");
m_eveRecv = _T("");
m_buff = _T("");
m_size = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CARQDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CARQDlg)
DDX_Control(pDX, IDC_SETTING, m_setting);
DDX_Control(pDX, IDC_SEND, m_send);
DDX_Text(pDX, IDC_EVE_SEND, m_eveSend);
DDX_Text(pDX, IDC_EVE_RECV, m_eveRecv);
DDX_Text(pDX, IDC_BUFF, m_buff);
DDX_Text(pDX, IDC_TEXT, m_size);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CARQDlg, CDialog)
//{{AFX_MSG_MAP(CARQDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SETTING, OnSetting)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_SEND, OnSend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CARQDlg message handlers
BOOL CARQDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
setting.DoModal();
SYSTEMTIME t;
GetSystemTime(&t);
srand(t.wMilliseconds);
vs=0;
vr=0;
time=0;
curr=0;
queue=0;
return TRUE; // return TRUE unless you set the focus to a control
}
void CARQDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CARQDlg::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 CARQDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CARQDlg::OnSetting()
{
// TODO: Add your control notification handler code here
setting.DoModal();
}
void CARQDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
KillTimer(nIDEvent);
switch(nIDEvent) {
case WM_TIMEOUT:
OnTimeOut();
break;
case WM_TOBUFFER:
ToBuffer();
break;
case WM_SENDFRAME:
SendFrame();
break;
case WM_RECEIVE:
OnRecv();
break;
case WM_ONACK:
OnACK();
break;
case WM_ONNAK:
OnNAK();
break;
case WM_SENDACK:
SendACK();
break;
case WM_SENDNAK:
SendNAK();
break;
default:
break;
}
CDialog::OnTimer(nIDEvent);
}
void CARQDlg::OnSend()
{
// TODO: Add your control notification handler code here
UpdateData(true);
CString text;
text.Format("%d个帧被送入发送队列\r\n",m_size);
m_eveSend=text;
queue=m_size;
text.Format("现在队列中有%d个帧\r\n",queue);
m_eveSend+=text;
m_eveRecv="";
vs=vr=0;
curr=0;
time=0;
ToBuffer();
UpdateData(false);
}
void CARQDlg::ToBuffer()//将帧读入缓冲区
{
if (queue==0)
{
CString text;
text.Format("发送完成\r\n");
m_eveSend+=text;
text.Format("发送完成,共发%d个帧,用时%dms\r\n",curr,time);
m_buff=text;
MessageBox(text);
text.Format("平均用时%fms/帧",(float)time/(float)curr);
m_buff+=text;
MessageBox(text);
UpdateData(false);
time=0;
curr=0;
}
else
{
curr++;
queue--;
CString text;
text.Format("当前缓冲区中为第%d帧,还有%d帧要发\r\n",curr,queue);
m_buff=text;
UpdateData(false);
SendFrame();
}
}
void CARQDlg::SendFrame()
{
CString text;
text.Format("发送第%d帧,帧序号为%d\r\n",curr,vs);
m_eveSend+=text;
UpdateData(false);
SetTimer(WM_TIMEOUT,setting.m_timeout,NULL);
int p=rand();
if (p%100>setting.m_missing) {
time+=setting.m_tl;
SetTimer(WM_RECEIVE,setting.m_tl,NULL);
}
}
void CARQDlg::OnACK()
{
CString text;
text.Format("接收到ACK帧,准备发下一帧\r\n");
m_eveSend+=text;
time+=setting.m_tr;
vs=(vs+1)%2;
KillTimer(WM_TIMEOUT);
UpdateData(false);
SetTimer(WM_TOBUFFER,setting.m_tr,NULL);
}
void CARQDlg::OnNAK()
{
CString text;
text.Format("接收到NAK帧,准备重发\r\n");
m_eveSend+=text;
time+=setting.m_tr;
SetTimer(WM_SENDFRAME,setting.m_tr,NULL);
KillTimer(WM_TIMEOUT);
SendFrame();
UpdateData(false);
}
void CARQDlg::OnTimeOut()
{
CString text;
text.Format("计时器超时,准备重发\r\n");
m_eveSend+=text;
SendFrame();
UpdateData(false);
}
void CARQDlg::OnRecv()
{
CString text;
text.Format("接收到第%d帧,帧序号为%d\r\n",curr,vs);
m_eveRecv+=text;
int p=rand();
if (p%100>setting.m_error&&vs==vr) //so luck,no damage!
{
text.Format("很幸运,%d帧完好无损,被送往网络层\r\n",curr);
m_eveRecv+=text;
time+=setting.m_tp;
vr=(vr+1)%2;
SetTimer(WM_SENDACK,setting.m_tp,NULL);
}
else
{
if (vs==vr) {
text.Format("第%d帧损坏 \r\n",curr);
m_eveRecv+=text;
time+=setting.m_tp;
SetTimer(WM_SENDNAK,setting.m_tp,NULL);
}
else
{
text.Format("错误的序列号%d,该帧被丢弃\r\n",vs);
m_eveRecv+=text;
time+=setting.m_tp;
SetTimer(WM_SENDACK,setting.m_tp,NULL);
}
}
UpdateData(false);
}
void CARQDlg::SendACK()
{
CString text;
text.Format("发送ACK\r\n");
m_eveRecv+=text;
time+=setting.m_ts;
int p=rand();
if (p%100>setting.m_missing) {
time+=setting.m_tl;
SetTimer(WM_ONACK,setting.m_tl,NULL);
}
else
{
text.Format("ACK不幸丢失\r\n");
m_eveRecv+=text;
}
UpdateData(false);
}
void CARQDlg::SendNAK()
{
CString text;
text.Format("发送NAK\r\n");
m_eveRecv+=text;
int p=rand();
if (p%100>setting.m_missing) {
time+=setting.m_tl;
SetTimer(WM_ONNAK,setting.m_tl,NULL);
}
else
{
text.Format("NAK不幸丢失\r\\n");
m_eveRecv+=text;
}
UpdateData(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -