⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mediaserverdlg.cpp

📁 Mpeg流的网络客户端实时播放
💻 CPP
字号:
// MediaServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include "MediaServer.h"
#include "MediaServerDlg.h"

#include "CListenSocket.h"
#include "CMediaSocketServer.h"
#include "CLocalMachine.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMediaServerDlg dialog

CMediaServerDlg::CMediaServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(IDD_MEDIASERVER_DIALOG, pParent)
{
	//{{AFX_DATA_INIT(CMediaServerDlg)
		// 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);
	m_pListener     = NULL;
	m_pWorkerSock   = NULL;
	m_strSourceFile = "";
}

void CMediaServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMediaServerDlg)
	DDX_Control(pDX, IDC_STATIC_HOSTIP, m_hostIP);
	DDX_Control(pDX, IDC_STATIC_HOST, m_host);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMediaServerDlg, CDialog)
	//{{AFX_MSG_MAP(CMediaServerDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_SELECT, OnButtonSelect)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_NEW_SOCKET, ListenOnConnection)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMediaServerDlg message handlers

BOOL CMediaServerDlg::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
	
	char  host[255], hostIP[255];
	CLocalMachine  myhost;
	myhost.GetIPAddress(hostIP, host);
	m_host.SetWindowText(host);
	m_hostIP.SetWindowText(hostIP);

	// Create a server listen socket....
	bool  pass = true;
	m_pListener = new CListenSocket();
	pass = m_pListener->Create(BASE_SOCKET_PORT);
	if (pass)
	{
		pass = m_pListener->StartListen();
	}

	// Select an init mpeg file
	if (pass)
	{
		OnButtonSelect();
	}
	
	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 CMediaServerDlg::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 CMediaServerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMediaServerDlg::OnDestroy() 
{
	if (m_pListener != NULL)
	{
		delete m_pListener;
		m_pListener = NULL;
	}

	if (m_pWorkerSock != NULL)
	{
		delete m_pWorkerSock;
		m_pWorkerSock = NULL;
	}

	CDialog::OnDestroy();	
}

// Select a source mpeg file
void CMediaServerDlg::OnButtonSelect() 
{
	CFileDialog  dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
			"Mpeg Files (*.mpg;*.mpeg)|*.mpg; *.mpeg|", NULL);
	if (dlg.DoModal() == IDOK)
	{
		m_strSourceFile = dlg.GetPathName();
		ListenOnConnection();
	}
}

void CMediaServerDlg::ListenOnConnection()
{
	// Check if has some socket connection
	if (m_pListener)
	{
		if (m_pListener->m_ClientList.GetCount())
		{
			SOCKET pSock = (SOCKET) m_pListener->m_ClientList.GetHead();
			if (m_pWorkerSock == NULL)
				m_pWorkerSock = new CMediaSocketServer();
			m_pWorkerSock->Attach(pSock);

			((CMediaSocketServer *)m_pWorkerSock)->SetSourceFile(m_strSourceFile);
			m_pWorkerSock->StartReceiving();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -