📄 sdgstestdlg.cpp
字号:
// sdgstestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "sdgstest.h"
#include "sdgstestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define SOCKTEXT( a ) ( CString( a ) + ( m_bTelnetMode ? CString( "\r\n" ) : CString( "\r" ) ) )
#define ENDLINE ( m_bTelnetMode ? "\r\n" : "\r" )
#define ENDLINELEN ( m_bTelnetMode ? 2 : 1 )
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CSdgstestDlg dialog
CSdgstestDlg::CSdgstestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSdgstestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSdgstestDlg)
// 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 CSdgstestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSdgstestDlg)
DDX_Control(pDX, IDC_HOST, m_oHost);
DDX_Control(pDX, IDC_LOG, m_oLog);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSdgstestDlg, CDialog)
//{{AFX_MSG_MAP(CSdgstestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSdgstestDlg message handlers
BOOL CSdgstestDlg::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
m_oLog.InsertColumn( 0, "Log" );
WSAData w;
int n = WSAStartup( MAKEWORD( 2, 0 ), &w );
if( n != 0 )
{
CString str;
str.Format( "Sockets not initialised (%d)", n );
Log( str );
}
m_oHost.SetWindowText( "127.0.0.1" );
GetDlgItem( IDC_PORT )->SetWindowText( "5001" );
return TRUE; // return TRUE unless you set the focus to a control
}
void CSdgstestDlg::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 CSdgstestDlg::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 CSdgstestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSdgstestDlg::OnButton1()
{
CSocket sock;
bool bAllOk = true;
m_bTelnetMode = static_cast< CButton* >( GetDlgItem( IDC_TELNET ) )->GetCheck() != 0;
try
{
if( !sock.Create() )
{
CString err;
err.Format( "Failed to create socket (%d)", sock.GetLastError() );
throw err;
}
CString strHost;
m_oHost.GetWindowText( strHost );
CString strPort;
GetDlgItem( IDC_PORT )->GetWindowText( strPort );
unsigned uPort = atoi( strPort );
if( !sock.Connect( strHost, uPort ) )
{
CString strError;
strError.Format( "Failed to connect (%d)", sock.GetLastError() );
throw strError;
}
Send( sock, SOCKTEXT( "RS_PACKET_STREAM_CLIENT" ) );
Get( sock, SOCKTEXT( "RS_PACKET_STREAM_SERVER" ) );
Send( sock, SOCKTEXT( "RS_PACKET_STREAM_VERSION 1" ) );
Get( sock, SOCKTEXT( "OK" ) );
CString strSC;
GetDlgItem( IDC_SERVICECOMPONENT )->GetWindowText( strSC );
CString strPW;
GetDlgItem( IDC_PASSWORD )->GetWindowText( strPW );
Send( sock, CString( SOCKTEXT( "SERVICE_COMPONENT " + strSC ) ) );
Send( sock, CString( SOCKTEXT( "PASSWORD " + strPW ) ) );
Get( sock, SOCKTEXT( "OK" ) );
int nSize = 0;
const int MAX_DG = 200;
char dgbuf[ MAX_DG + 1 ];
do
{
Get( sock, SOCKTEXT( "NEW_DATA_GROUP_REQUIRED" ) );
++nSize;
Send( sock, SOCKTEXT( "NEW_DATA_GROUP" ) );
CString strLen;
strLen.Format( "LENGTH %d", nSize );
Send( sock, SOCKTEXT( strLen ) );
for( int i=0; i < nSize; i++ )
{
dgbuf[ i ] = ( i % 26 ) + 'A';
}
dgbuf[ nSize ] = NULL;
CString data = dgbuf;
Send( sock, data );
Get( sock, SOCKTEXT( "OK" ) );
}
while( nSize < MAX_DG );
Log( "End of test." );
}
catch( CString& rE )
{
Log( rE );
bAllOk = false;
}
if( bAllOk )
{
Log( "No problems reported." );
}
Log( "---End---" );
}
void CSdgstestDlg::Log( const CString& rstrLog )
{
static int nMaxCol = 0;
m_oLog.InsertItem( 0, rstrLog );
if( rstrLog.GetLength() > nMaxCol )
{
nMaxCol = rstrLog.GetLength();
m_oLog.SetColumnWidth( 0, LVSCW_AUTOSIZE );
}
}
void CSdgstestDlg::Send( CSocket& rSock, const CString& rstrSend )
{
CString strLog;
strLog.Format( "SND> %s", rstrSend );
Log( strLog );
if( rSock.Send( rstrSend, rstrSend.GetLength() ) == SOCKET_ERROR )
{
CString err;
err.Format( "ERROR during send (%d)", rSock.GetLastError() );
throw err;
}
}
bool CSdgstestDlg::Receive( CSocket& rSock, CString& rstrBuf )
{
bool bGotLine = false;
while( !bGotLine )
{
int nRPos = m_strRecvBuf.Find( ENDLINE );
if( nRPos >= 0 )
{
rstrBuf = m_strRecvBuf.Left( nRPos + ENDLINELEN );
m_strRecvBuf = m_strRecvBuf.Right( m_strRecvBuf.GetLength() - nRPos - ENDLINELEN );
bGotLine = true;
}
else
{
const int BUF_SIZE = 10000;
char buf[ BUF_SIZE + 1 ];
int nRead = rSock.Receive( buf, BUF_SIZE );
if( nRead == 0 )
{
throw CString( "ERROR during receive: Socket closed" );
}
else if( nRead == SOCKET_ERROR )
{
CString err;
err.Format( "ERROR during receive (%d)", rSock.GetLastError() );
throw err;
}
buf[ nRead ] = NULL;
m_strRecvBuf += buf;
CString strLog;
strLog.Format( "RCV< %s", buf );
Log( strLog );
}
}
return true;
}
bool CSdgstestDlg::Get( CSocket& rSock, const CString& rstrExpect )
{
CString strBuf;
Receive( rSock, strBuf );
if( strBuf != rstrExpect )
{
CString err;
err.Format( "ERROR during receive: Expected %s got %s", rstrExpect, strBuf );
throw err;
}
return true;
}
void CSdgstestDlg::OnButton2()
{
m_oLog.DeleteAllItems();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -