📄 vodserverdlg.cpp
字号:
// VODServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "VODServer.h"
#include "VODServerDlg.h"
#include "UNetwork.h"
#include "CAddFileDlg.h"
#include "CMediaInfo.h"
#include "defines.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CVODServerDlg dialog
CVODServerDlg::CVODServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CVODServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CVODServerDlg)
// 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);
mClientIP = 0;
mClientCmdPort = 0;
}
void CVODServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CVODServerDlg)
DDX_Control(pDX, IDC_LIST_PROGRAMS, mProgramListCtrl);
DDX_Control(pDX, IDC_EDIT_HOST_INFO, mEditHostInfo);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CVODServerDlg, CDialog)
//{{AFX_MSG_MAP(CVODServerDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON_EDIT, OnButtonEdit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVODServerDlg message handlers
BOOL CVODServerDlg::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
char hostName[100];
char hostIP[50];
if (UNetwork::GetHostInfo(hostIP, hostName))
{
CString info;
info.Format("Host: %s IP: %s Command Port (UDP) : %d",
hostName, hostIP, SERVER_UDP_PORT);
mEditHostInfo.SetWindowText(info);
}
mProgramListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
mProgramListCtrl.InsertColumn(0, "File", LVCFMT_CENTER, 120);
mProgramListCtrl.InsertColumn(1, "Type", LVCFMT_CENTER, 80);
mProgramListCtrl.InsertColumn(2, "Size", LVCFMT_CENTER, 80);
mProgramListCtrl.InsertColumn(3, "PID", LVCFMT_CENTER, 60);
LoadProgramList();
// Set up command listening
mCommandManager.AddMsgReceiver(this);
mCommandManager.SetLocalPort(SERVER_UDP_PORT);
if (mCommandManager.CreateReceiver())
{
mCommandManager.StartReceiving();
}
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 CVODServerDlg::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 CVODServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CVODServerDlg::OnDestroy()
{
SaveProgramList();
CDialog::OnDestroy();
}
void CVODServerDlg::SaveProgramList(void)
{
CFile file;
if (file.Open("C:\\hqvod.lst", CFile::modeCreate | CFile::modeWrite))
{
CArchive ar(&file, CArchive::store);
// Save program count first
long count = mPrgrmList.GetCount();
ar << count;
// Then save all program details
POSITION pos = mPrgrmList.GetHeadPosition();
while (pos)
{
CMediaInfo info = mPrgrmList.GetNext(pos);
info.Serialize(ar);
}
}
}
void CVODServerDlg::LoadProgramList(void)
{
mPrgrmList.RemoveAll();
mProgramListCtrl.DeleteAllItems();
CFile file;
if (file.Open("C:\\hqvod.lst", CFile::modeRead))
{
CArchive ar(&file, CArchive::load);
// Get the program count first
long count = 0;
ar >> count;
// Then get all program details
for (long i = 0; i < count; i++)
{
CMediaInfo info;
info.Serialize(ar);
AddProgramItem(info);
}
}
}
void CVODServerDlg::AddProgramItem(CMediaInfo & inMedia)
{
int count = mProgramListCtrl.GetItemCount();
mProgramListCtrl.InsertItem(count, inMedia.mFilePath);
// Determine file type
CString str;
switch (inMedia.mFileType)
{
case FT_MPEG1:
str = "MPEG1";
break;
case FT_MPEG2:
str = "MPEG2";
break;
case FT_AVI:
str = "AVI";
break;
case FT_MP3:
str = "MP3";
break;
default:
str = "Unknown";
break;
}
mProgramListCtrl.SetItemText(count, 1, str);
str.Format("%.3fM", inMedia.mFileSize / 1024. / 1024.);
mProgramListCtrl.SetItemText(count, 2, str);
str.Format("%d", inMedia.mProgramId);
mProgramListCtrl.SetItemText(count, 3, str);
// Save to the list too
mPrgrmList.AddTail(inMedia);
}
void CVODServerDlg::OnButtonAdd()
{
CMediaInfo media;
CAddFileDlg dlg;
dlg.SetMediaInfo(&media);
if (dlg.DoModal() == IDOK)
{
AddProgramItem(media);
}
}
void CVODServerDlg::OnButtonDelete()
{
int index = mProgramListCtrl.GetNextItem(-1, LVNI_SELECTED);
if (index >= 0)
{
// Get media file name
CString filename = mProgramListCtrl.GetItemText(index, 0);
POSITION pos = mPrgrmList.GetHeadPosition();
while (pos)
{
POSITION previous = pos;
CMediaInfo media = mPrgrmList.GetNext(pos);
if (filename.CompareNoCase(media.mFilePath) == 0)
{
mPrgrmList.RemoveAt(previous);
break;
}
}
mProgramListCtrl.DeleteItem(index);
}
}
// Edit the current selected program
void CVODServerDlg::OnButtonEdit()
{
int index = mProgramListCtrl.GetNextItem(-1, LVNI_SELECTED);
if (index >= 0)
{
// Get media file name
CString filename = mProgramListCtrl.GetItemText(index, 0);
CMediaInfo media;
POSITION pos = mPrgrmList.GetHeadPosition();
while (pos)
{
media = mPrgrmList.GetNext(pos);
if (filename.CompareNoCase(media.mFilePath) == 0)
{
break;
}
}
CAddFileDlg dlg;
dlg.SetMediaInfo(&media);
dlg.SetEditMode(TRUE);
if (dlg.DoModal() == IDOK)
{
// Update the modified program
CString str;
str.Format("%d", media.mProgramId);
mProgramListCtrl.SetItemText(index, 3, str);
// Update to the list
pos = mPrgrmList.GetHeadPosition();
while (pos)
{
POSITION previous = pos;
CMediaInfo media2 = mPrgrmList.GetNext(pos);
if (filename.CompareNoCase(media2.mFilePath) == 0)
{
mPrgrmList.SetAt(previous, media);
break;
}
}
}
}
}
bool CVODServerDlg::ReceiveMessage(MessageT inMessage,
void * ioParam,
void * ioParam2)
{
if (inMessage == msg_CommandReceived)
{
ProcessCommand(ioParam);
return true;
}
return CMsgReceiver::ReceiveMessage(inMessage, ioParam, ioParam2);
}
// Process commands received from the client
void CVODServerDlg::ProcessCommand(void * inCommand)
{
// First, get the command id
long * pCmd = (long *) inCommand;
switch (ntohl(*pCmd))
{
case Cmd_RequestTCPConnect: // Must be first received!!!
{
pCmd++;
Request_TCP_Connect * pData = (Request_TCP_Connect *) pCmd;
pData->my_ntoh();
mClientIP = pData->client_ip;
mClientCmdPort = pData->client_udp;
mNetSender.ConnectTo(pData->client_ip, pData->client_tcp);
}
break;
case Cmd_RequestTCPDisconnect:
{
mNetSender.Detach();
mNetSender.StopSending();
}
break;
case Cmd_RequestProgramList: // Send program list to the client
SendProgramList();
break;
case Cmd_RequestPlay:
{
pCmd++;
Media_Control * pData = (Media_Control *) pCmd;
pData->my_ntoh();
// Find the program according to the ID
POSITION pos = mPrgrmList.GetHeadPosition();
while (pos)
{
CMediaInfo info = mPrgrmList.GetNext(pos);
if (info.mProgramId == pData->program_id)
{
mNetSender.SetSource(info);
mNetSender.SetPaused(FALSE);
mNetSender.StartSending();
break;
}
}
}
break;
case Cmd_RequestPause:
mNetSender.SetPaused(TRUE);
break;
case Cmd_RequestStop:
mNetSender.SetPaused(FALSE);
mNetSender.StopSending();
break;
default:
break;
}
}
// Send program list to the client
void CVODServerDlg::SendProgramList(void)
{
mCommandManager.SetTargetIP(mClientIP);
mCommandManager.SetTargetPort(mClientCmdPort);
if (mCommandManager.CreateSender())
{
char buffer[MAX_COMMAND_SIZE];
long len = sizeof(Net_Command) + sizeof(Program_List);
// Specify command id first
long * pCmd = (long *) buffer;
*pCmd = Cmd_ProgramList;
*pCmd = htonl(*pCmd);
pCmd++;
Program_List * pData = (Program_List *) pCmd;
POSITION pos = mPrgrmList.GetHeadPosition();
while (pos)
{
// Fill data after command id and send them to the client
CMediaInfo info = mPrgrmList.GetNext(pos);
info.CopyTo(pData);
pData->my_hton();
mCommandManager.Send(buffer, len);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -