📄 netserverdlg.cpp
字号:
// NetServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "NetServer.h"
#include "NetServerDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CNetServerDlg dialog
CNetServerDlg::CNetServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CNetServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CNetServerDlg)
m_strAdd = _T("");
m_dwPort = 0;
m_dwPackageSize = DEFAULT_PACKAGE_SIZE;
m_strContent = _T("I LOVE this game !End");
m_nSocketType = 0;
m_nISAddMember = FALSE;
m_bIsLoopBack = FALSE;
m_nSndBuff = 10;
m_bPortChanged = FALSE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CNetServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNetServerDlg)
DDX_Control(pDX, IDC_IPLIST, m_IPLIST);
DDX_Control(pDX, IDC_COMBO1, m_COMTTL);
DDX_Control(pDX, IDC_COMBO2, m_Comb2);
DDX_Text(pDX, IDC_ADD, m_strAdd);
DDX_Text(pDX, IDC_PORT, m_dwPort);
DDV_MinMaxDWord(pDX, m_dwPort, 1000, 60000);
DDX_Text(pDX, IDC_PACKAGESIZE, m_dwPackageSize);
DDV_MinMaxDWord(pDX, m_dwPackageSize, 100, 65500);
DDX_Text(pDX, IDC_STRVALUE, m_strContent);
DDV_MaxChars(pDX, m_strContent, 120);
DDX_Radio(pDX, IDC_RADIO1, m_nSocketType);
DDX_Check(pDX, IDC_CHECK1, m_nISAddMember);
DDX_Check(pDX, IDC_CHECK2, m_bIsLoopBack);
DDX_Text(pDX, IDC_SNDBUF, m_nSndBuff);
DDV_MinMaxInt(pDX, m_nSndBuff, -1, 128);
DDX_Check(pDX, IDC_CHECK3, m_bPortChanged);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CNetServerDlg, CDialog)
//{{AFX_MSG_MAP(CNetServerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SETBITRATE, OnSetbitrate)
ON_BN_CLICKED(IDC_START, OnStart)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_CBN_SELCHANGE(IDC_COMBO2, OnSelchangeCombo2)
ON_BN_CLICKED(IDC_SETPACKAGESIZE, OnSetpackagesize)
ON_BN_CLICKED(IDC_LOADVALUE, OnLoadvalue)
ON_BN_CLICKED(IDC_CLOSESOCKET, OnClosesocket)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNetServerDlg message handlers
BOOL CNetServerDlg::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
InitPara();
CheckStatus();
return TRUE; // return TRUE unless you set the focus to a control
}
void CNetServerDlg::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 CNetServerDlg::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 CNetServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CNetServerDlg::Init()
{ char szbuff[64];
DWORD dwBitrate;
UpdateData(TRUE);
sprintf(szbuff,"%s",m_strAdd);
dwBitrate=fBitrateArray[m_Comb2.GetCurSel()]*1024*1024;
g_nTTL=nTTLArray[m_COMTTL.GetCurSel()];
// sprintf(szbuff,"%d",g_nTTL);
//AfxMessageBox(szbuff);
g_bIsAddMember=m_nISAddMember;
g_bPortIschanged=m_bPortChanged;
g_bIsLoopBack=m_bIsLoopBack;
g_nSndBuf=m_nSndBuff;
FillValue();
m_Stream.SetPackageSize(m_dwPackageSize);
m_Stream.SetBitrate(dwBitrate);
//m_strIpArray[m_IPLIST.GetCurSel()]
m_Stream.Init(szbuff,m_strIpArray[m_IPLIST.GetCurSel()],m_dwPort,
m_nSocketType);
UpdateSetValue();
}
void CNetServerDlg::OnSetbitrate()
{
DWORD dwBitrate;
UpdateData(TRUE);
dwBitrate=fBitrateArray[m_Comb2.GetCurSel()]*1024*1024;
m_Stream.SetBitrate(dwBitrate);
UpdateSetValue();
}
void CNetServerDlg::OnStart()
{
if(m_Stream.GetStreamStatus()==S_S_Idle)
{
Init();
Sleep(100);
}
m_Stream.Start();
Sleep(10);
CheckStatus();
}
void CNetServerDlg::OnCancel()
{
m_Stream.Close();
// EndWSA();
CDialog::OnCancel();
}
void CNetServerDlg::InitPara()
{
// m_comb1.SetCurSel(0);
GetHostAddrLst();
m_Comb2.SetCurSel(3);
m_COMTTL.SetCurSel(0);
m_strAdd=_T("226.8.8.5");
m_dwPort=1900;
g_hMonWnd=this->GetSafeHwnd();
UpdateData(FALSE);
m_IPLIST.SetCurSel(1);
}
void CNetServerDlg::OnStop()
{
m_Stream.Stop();
Sleep(10);
CheckStatus();
}
LRESULT CNetServerDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
switch(message){
case MSG_SEND_PROGRESS:
UpdateInfo(wParam,lParam);
break;
default:
break;
}// end switch
return CDialog::WindowProc(message, wParam, lParam);
}
void CNetServerDlg::UpdateInfo(WPARAM wParam, LPARAM lParam)
{ char szbuff[64];
DWORD dwBlockIndex=wParam;
DWORD dwBitrate=lParam;
//TRACE("dss blockindex=%d bitrate=%d\n",dwBlockIndex,dwBitrate);
sprintf(szbuff,"%d",dwBlockIndex);
SetDlgItemText(IDC_BLOCKINDEX,szbuff);
PubSwapToDotString(szbuff,dwBitrate);
strcat(szbuff," bps");
//sprintf(szbuff,"%d",dwBitrate);
SetDlgItemText(IDC_BITRATE,szbuff);
}
void CNetServerDlg::OnSelchangeCombo2()
{
// TODO: Add your control notification handler code here
DWORD dwBitrate;
dwBitrate=fBitrateArray[m_Comb2.GetCurSel()]*1024*1024;
TRACE("index=%d ff=%.3f bitrate=%d\n",m_Comb2.GetCurSel(),fBitrateArray[m_Comb2.GetCurSel()],
dwBitrate);
}
void CNetServerDlg::OnSetpackagesize()
{
UpdateData(TRUE);
if(m_dwPackageSize<1000||m_dwPackageSize>65501)
return;
m_Stream.SetPackageSize(m_dwPackageSize);
UpdateSetValue();
}
void CNetServerDlg::GetHostAddrLst()
{
char szbuff[64];
int i=0;
hostent *p;
DWORD dwadd;
gethostname(szbuff,64);
// hlittle
p=gethostbyname(szbuff);
// dwadd=p->h_addr_list[0];
sprintf(m_strIpArray[0],"127.0.0.1");
m_IPLIST.InsertString(-1,m_strIpArray[0]);
while(1){
if(!p->h_addr_list[i])
break;
memcpy(&dwadd,p->h_addr_list[i],4);
PubGetIPAddrString(dwadd,szbuff);
m_IPLIST.InsertString(-1,szbuff);
sprintf(m_strIpArray[i+1],"%s",szbuff);
TRACE("add =%s\n",m_strIpArray[i]);
i++;
}
}
void CNetServerDlg::OnLoadvalue()
{
m_Stream.Stop();
//AfxMessageBox("stop");
Sleep(100);
FillValue();
//AfxMessageBox("Start");
m_Stream.Start();
}
void CNetServerDlg::FillValue()
{
char szbuff[64];
char szContentp[128];
DWORD dwBitrate;
UpdateData(TRUE);
sprintf(szContentp,"%s",m_strContent);
if(strlen(szContentp)<=0)
{ AfxMessageBox("内容长度不能为0");
return;
}
m_Stream.FillBlock(szContentp);
}
void CNetServerDlg::UpdateSetValue()
{
char szbuff[32];
m_dwPackageSize=m_Stream.GetPackageSize();
sprintf(szbuff,"%d ms",m_Stream.GetSpanTime());
SetDlgItemText(IDC_STR_SPAN_TIME,szbuff);
sprintf(szbuff,"%d",m_Stream.GetCountEach());
SetDlgItemText(IDC_STR_COUNT_EACH,szbuff);
sprintf(szbuff,"%d B",g_nGetSndBuf);
SetDlgItemText(IDC_GETSNDBUFFER,szbuff);
UpdateData(FALSE);
}
void CNetServerDlg::OnClosesocket()
{
m_Stream.Close();
Sleep(10);
CheckStatus();
}
void CNetServerDlg::OnClose()
{
m_Stream.Close();
// AfxMessageBox("d");
CDialog::OnClose();
}
void CNetServerDlg::CheckStatus()
{ Stream_Status s=m_Stream.GetStreamStatus();
switch(s){
case S_S_Idle:
GetDlgItem(IDC_START)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP)->EnableWindow(FALSE);
GetDlgItem(IDC_CLOSESOCKET)->EnableWindow(FALSE);
GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
SetDlgItemText(IDC_BLOCKINDEX,"0");
SetDlgItemText(IDC_BITRATE,"0 bps");
break;
case S_S_Running:
GetDlgItem(IDC_START)->EnableWindow(FALSE);
GetDlgItem(IDC_STOP)->EnableWindow(TRUE);
GetDlgItem(IDC_CLOSESOCKET)->EnableWindow(TRUE);
GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
break;
case S_S_Stoped:
GetDlgItem(IDC_START)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP)->EnableWindow(FALSE);
GetDlgItem(IDC_CLOSESOCKET)->EnableWindow(TRUE);
GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
//SetDlgItemText(IDC_BITRATE,"0 bps");
break;
default:
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -