📄 ctpdemodlg.cpp
字号:
// CTPDemoDlg.cpp : implementation file
//
// (c) Lev Naumov, CAMEL Laboratory
// E-mail: camellab@mail.ru
// For more information see http://camel.ifmo.ru or
// http://www.codeproject.com/internet/ctp.asp
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "NetBasic.h"
#include "NetReceivers.h"
#include "CTPNet.h"
#include "TCPNet.h"
#include "UDPNet.h"
#include "CTPDemo.h"
#include "CTPStatusDlg.h"
#include "CTPDemoDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CCTPDemoDlg dialog
CCTPDemoDlg::CCTPDemoDlg(CWnd* pParent /*=NULL*/):
CDialog(CCTPDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCTPDemoDlg)
m_sSendText = _T("Some text to be sent.");
m_iProtocol = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
// Initially servers are not created
m_pCTP=NULL;
m_pTCP=NULL;
m_pUDP=NULL;
// Create recievers
m_pCTPReceiver = new CCTPNetReceiver(this);
m_pTCPReceiver = new CTCPNetReceiver(this);
m_pUDPReceiver = new CUDPNetReceiver(this);
// Initialize CTP status dialog pointer with NULL
m_pCTPStatus=NULL;
}
CCTPDemoDlg::~CCTPDemoDlg()
{
// Destroy status dialog
if (m_pCTPStatus) delete m_pCTPStatus;
// Destroy servers
if (m_pCTP) delete m_pCTP;
if (m_pTCP) delete m_pTCP;
if (m_pUDP) delete m_pUDP;
// Destroy recievers
delete m_pCTPReceiver;
delete m_pTCPReceiver;
delete m_pUDPReceiver;
}
void CCTPDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCTPDemoDlg)
DDX_Control(pDX, IDC_IPADDRESS, m_IPAddr);
DDX_Text(pDX, IDC_TEXTSEND, m_sSendText);
DDX_Radio(pDX, IDC_USECTP, m_iProtocol);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCTPDemoDlg, CDialog)
//{{AFX_MSG_MAP(CCTPDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SENDTEXT, OnSendtext)
ON_BN_CLICKED(IDC_SENDFILE, OnSendfile)
ON_BN_CLICKED(IDC_CTPSTATUS, OnCtpstatus)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_USECTP, OnUsectp)
ON_BN_CLICKED(IDC_USETCP, OnUsetcp)
ON_BN_CLICKED(IDC_USEUDP, OnUseudp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCTPDemoDlg message handlers
BOOL CCTPDemoDlg::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);
SetIcon(m_hIcon, FALSE);
// Set default ip-address;
m_IPAddr.SetAddress(127,0,0,1);
// Set richtexts fonts
CHARFORMAT cf;
((CRichEditCtrl*)(GetDlgItem(IDC_TEXTSEND)))->GetDefaultCharFormat(cf);
cf.dwEffects&=~CFE_AUTOCOLOR;
cf.crTextColor=RGB(128,0,0);
strcpy(cf.szFaceName,_T("Courier New"));
((CRichEditCtrl*)(GetDlgItem(IDC_TEXTSEND)))->SetDefaultCharFormat(cf);
((CRichEditCtrl*)(GetDlgItem(IDC_TEXTRECV)))->GetDefaultCharFormat(cf);
cf.dwEffects&=~CFE_AUTOCOLOR;
cf.crTextColor=RGB(0,0,128);
strcpy(cf.szFaceName,_T("Courier New"));
((CRichEditCtrl*)(GetDlgItem(IDC_TEXTRECV)))->SetDefaultCharFormat(cf);
((CRichEditCtrl*)(GetDlgItem(IDC_LOG)))->GetDefaultCharFormat(cf);
cf.dwEffects&=~CFE_AUTOCOLOR;
cf.crTextColor=RGB(0,128,0);
strcpy(cf.szFaceName,_T("Courier New"));
((CRichEditCtrl*)(GetDlgItem(IDC_LOG)))->SetDefaultCharFormat(cf);
// Start WinSocket
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
// Create CTP server
m_pCTP = new CCTPNet(m_pCTPReceiver,1515);
// Server created suspended so it needs to be started manually
m_pCTP->SetSuspended(false);
// Create CTP status dialog
m_pCTPStatus=new CCTPStatusDlg(*m_pCTP,200,this);
m_pCTPStatus->Create(m_pCTPStatus->IDD,this);
// Create TCP server
m_pTCP = new CTCPNet(m_pTCPReceiver,5150,1000);
// Server created suspended so it needs to be started manually
m_pTCP->SetSuspended(false);
// Create UDP server
m_pUDP = new CUDPNet(m_pUDPReceiver,5151,1000);
// Server created suspended so it needs to be started manually
m_pUDP->SetSuspended(false);
SetTimer(1,100,0);
return TRUE;
}
void CCTPDemoDlg::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 CCTPDemoDlg::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 CCTPDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCTPDemoDlg::OnSendtext()
{
// Retrieve entered ip-address
IPAddr to;
m_IPAddr.GetAddress(to.Bytes.b1,to.Bytes.b2,to.Bytes.b3,to.Bytes.b4);
// Retrieve entered text to be sent to m_sSendText variable and selected protocol
UpdateData(TRUE);
NetSender* pns=m_pCTP;
if (m_iProtocol==1) pns=m_pTCP; else
if (m_iProtocol==2) pns=m_pUDP;
unsigned __int8 options=0;
// Copy text to smart buffer
SmartBuffer* sm=new SmartBuffer(m_sSendText.GetLength()+1+(m_iProtocol>0?1:0),true,m_iProtocol>0?0:25,65400);
if (m_iProtocol==0) {
// CTP needs no flag to distinguish text from file
sm->PutData(m_sSendText.GetBuffer(m_sSendText.GetLength()),m_sSendText.GetLength()+1);
// Arrange options
options=ArrangeOptions();
} else {
// TCP and UDP need flag to distinguish text (first byte is zero) from file (first byte is not zero)
int flag=0;
sm->PutDataByte(0);
sm->PutData(m_sSendText.GetBuffer(m_sSendText.GetLength()),m_sSendText.GetLength()+1);
}
// Send command NET_TEXT
bool res=pns->Send(*sm,NET_TEXT,to,options);
// Other necessary variables
CString s;
char ips[16];
char timestamp[22];
// Create log entry
to.GetString(ips);
CCTPErrorInfo::GetTimeStamp(timestamp);
if (res) s.Format("%s %s> Send text to %s\n",timestamp,m_iProtocol==2?"UDP":(m_iProtocol==1?"TCP":"CTP"),ips); else
s.Format("%s %s> Unable to send text to %s\n",timestamp,m_iProtocol==2?"UDP":(m_iProtocol==1?"TCP":"CTP"),ips);
// Prepend text to log
CString text;
GetDlgItem(IDC_LOG)->GetWindowText(text);
GetDlgItem(IDC_LOG)->SetWindowText(s+text);
}
void CCTPDemoDlg::OnSendfile()
{
// Retrieve entered ip-address
IPAddr to;
m_IPAddr.GetAddress(to.Bytes.b1,to.Bytes.b2,to.Bytes.b3,to.Bytes.b4);
// Retrieve selected protocol
UpdateData(TRUE);
NetSender* pns=m_pCTP;
if (m_iProtocol==1) pns=m_pTCP; else
if (m_iProtocol==2) pns=m_pUDP;
unsigned __int8 options=0;
// Ask for filename
CFileDialog fd(TRUE,NULL,NULL,OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_PATHMUSTEXIST,"All files (*.*)|*.*||");
if (fd.DoModal()==IDOK) {
// Copy file to smart buffer
SmartBuffer* sb=new SmartBuffer(fd.GetPathName(),m_iProtocol>0?1:0,true,m_iProtocol>0?0:25,65400);
if (m_iProtocol>0) sb->PutDataByte(1); else {
// Arrange options
options=ArrangeOptions();
}
// Send command NET_FILE
bool res=pns->Send(*sb,NET_FILE,to,options);
// Other necessary variables
CString s;
char ips[16];
char timestamp[22];
// Create log entry
to.GetString(ips);
CCTPErrorInfo::GetTimeStamp(timestamp);
if (res) s.Format("%s %s> Send file to %s\n",timestamp,m_iProtocol==2?"UDP":(m_iProtocol==1?"TCP":"CTP"),ips); else
s.Format("%s %s> Unable to send file to %s\n",timestamp,m_iProtocol==2?"UDP":(m_iProtocol==1?"TCP":"CTP"),ips);
// Prepend text to log
CString text;
GetDlgItem(IDC_LOG)->GetWindowText(text);
GetDlgItem(IDC_LOG)->SetWindowText(s+text);
}
}
void CCTPDemoDlg::OnCtpstatus()
{
if (m_pCTPStatus->IsWindowVisible()) m_pCTPStatus->ShowWindow(SW_HIDE); else m_pCTPStatus->ShowWindow(SW_SHOW);
}
void CCTPDemoDlg::OnTimer(UINT nIDEvent)
{
CheckDlgButton(IDC_CTPSTATUS,m_pCTPStatus->IsWindowVisible());
CDialog::OnTimer(nIDEvent);
}
void CCTPDemoDlg::OnUsectp()
{
EnableCTPControls(TRUE);
}
void CCTPDemoDlg::OnUsetcp()
{
EnableCTPControls(FALSE);
}
void CCTPDemoDlg::OnUseudp()
{
EnableCTPControls(FALSE);
}
void CCTPDemoDlg::EnableCTPControls(BOOL enable)
{
GetDlgItem(IDC_CTPSTATUS)->EnableWindow(enable);
GetDlgItem(IDC_DELAFTERERROR)->EnableWindow(enable);
GetDlgItem(IDC_NORESEND)->EnableWindow(enable);
GetDlgItem(IDC_UNIQUECOMMAND)->EnableWindow(enable);
GetDlgItem(IDC_BROADCAST)->EnableWindow(enable);
}
unsigned __int8 CCTPDemoDlg::ArrangeOptions()
{
unsigned __int8 options=0;
if (IsDlgButtonChecked(IDC_DELAFTERERROR)) options|=CCTPNet::Options::DelAfterError;
if (IsDlgButtonChecked(IDC_NORESEND)) options|=CCTPNet::Options::NoResend;
if (IsDlgButtonChecked(IDC_UNIQUECOMMAND)) options|=CCTPNet::Options::UniqueCommand;
if (IsDlgButtonChecked(IDC_BROADCAST)) options|=CCTPNet::Options::Broadcast;
return options;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -