📄 hermesdlg.cpp
字号:
// HermesDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Hermes.h"
#include "HermesDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CHermesDlg dialog
CHermesDlg::CHermesDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHermesDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHermesDlg)
// 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);
for (int i = 0; i < BUFNUM; i++)
{
m_pWaveInBuf[i] = NULL;
m_pWaveOutBuf[i] = NULL;
}
m_hWavein = 0x0;
m_hWaveout = 0x0;
}
void CHermesDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHermesDlg)
DDX_Control(pDX, IDC_IPADDRESS, m_IpAddrCtrl);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CHermesDlg, CDialog)
//{{AFX_MSG_MAP(CHermesDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(MM_WIM_OPEN,OnMM_WIM_OPEN)
ON_MESSAGE(MM_WIM_DATA,OnMM_WIM_DATA)
ON_MESSAGE(MM_WOM_OPEN,OnMM_WOM_OPEN)
ON_MESSAGE(MM_WOM_CLOSE,OnMM_WOM_CLOSE)
ON_MESSAGE(MM_WIM_CLOSE,OnMM_WIM_CLOSE)
ON_MESSAGE(WM_DATA_RECEIVED,OnWM_DATA_RECEIVED)
ON_BN_CLICKED(IDC_BUTTON_CALL, OnButtonCall)
ON_BN_CLICKED(IDC_BUTTON_HANGUP, OnButtonHangup)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHermesDlg message handlers
BOOL CHermesDlg::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
m_IpAddrCtrl.SetAddress(128, 1, 1, 63);
m_hWaveoutReady = CreateEvent(NULL, FALSE, FALSE, NULL);
m_hWaveinReady = CreateEvent(NULL, FALSE, FALSE, NULL);
m_hWaveinBufReady = CreateEvent(NULL, FALSE, FALSE, NULL);
m_hWaveoutBufReady = CreateEvent(NULL, FALSE, FALSE, NULL);
CreateThread(NULL, 0, ListenThreadFunc, (LPVOID)this, 0, NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CHermesDlg::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 CHermesDlg::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 CHermesDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
DWORD CHermesDlg::ListenThreadFunc(LPVOID pParam)
{
int ret;
CWaveFormat wf;
((CHermesDlg*)pParam)->m_Listen.Create();
if (((CHermesDlg*)pParam)->m_Listen.m_socket == SOCKET_ERROR)
{
AfxMessageBox("Listen Socket Create Error.");
exit(0);
}
ret = ((CHermesDlg*)pParam)->m_Listen.Bind();
if (ret == SOCKET_ERROR)
{
AfxMessageBox("Listen Socket Bind Error.");
exit(0);
}
ret = ((CHermesDlg*)pParam)->m_Listen.Listen();
if (ret == SOCKET_ERROR)
{
AfxMessageBox("Listen Socket Listen Error.");
exit(0);
}
ret = ((CHermesDlg*)pParam)->m_Listen.Accept();
if (ret == SOCKET_ERROR)
{
AfxMessageBox("Listen Socket Accept Error.");
exit(0);
}
((CHermesDlg*)pParam)->m_Called = CTCPSocket(ret);
ret = waveInOpen(&((CHermesDlg*)pParam)->m_hWavein, WAVE_MAPPER, &wf.m_wfx, (DWORD)((CHermesDlg*)pParam)->m_hWnd, NULL, CALLBACK_WINDOW);
SetEvent( ((CHermesDlg*)pParam)->m_hWaveinReady);
if (ret != 0)
{
AfxMessageBox("ListenThreadFunc waveInOpen Error");
return 0;
}
ret = waveOutOpen( &(((CHermesDlg*)pParam)->m_hWaveout), WAVE_MAPPER, &wf.m_wfx, (DWORD)((CHermesDlg*)pParam)->m_hWnd, NULL, CALLBACK_WINDOW);
SetEvent( ((CHermesDlg*)pParam)->m_hWaveoutReady);
if (ret != 0)
{
AfxMessageBox("ListenThreadFunc waveOutOpen Error");
return 0;
}
((CHermesDlg*)pParam)->m_Role = CALLEE;
((CHermesDlg*)pParam)->m_State = ONLINE;
CreateThread(NULL, 0, RecvThreadFunc, (LPVOID)pParam, 0, NULL);
ret = ((CHermesDlg*)pParam)->m_Listen.Close();
if (ret == SOCKET_ERROR)
{
AfxMessageBox("Listen Socket Close Error.");
}
return 0;
}
DWORD CHermesDlg::RecvThreadFunc(LPVOID pParam)
{
WaitForSingleObject( ((CHermesDlg*)pParam)->m_hWaveoutBufReady,INFINITE);
int ret = 0;
char *pBuf = NULL;
CTCPSocket &sock = ((CHermesDlg*)pParam)->m_Calling;
if ( ((CHermesDlg*)pParam)->m_Role == CALLEE)
{
sock = ((CHermesDlg*)pParam)->m_Called;
}
while ( ((CHermesDlg*)pParam)->m_State == ONLINE)
{
pBuf = new char[CWaveBuffer::BUFSIZE];
while (ret < CWaveBuffer::BUFSIZE && ret != SOCKET_ERROR )
{
ret += sock.Recv(pBuf + ret, CWaveBuffer::BUFSIZE - ret);
}
if (ret == SOCKET_ERROR)
break;
((CHermesDlg*)pParam)->m_pWaveOutBuf[((CHermesDlg*)pParam)->m_WaveOutIndex]->ResetWaveOutBuffer();
((CHermesDlg*)pParam)->m_pWaveOutBuf[((CHermesDlg*)pParam)->m_WaveOutIndex]->UseWaveOutBuffer(pBuf, ret);
ret = ((CHermesDlg*)pParam)->m_pWaveOutBuf[((CHermesDlg*)pParam)->m_WaveOutIndex]->Prepare();
if (ret != 0)
{
AfxMessageBox("Prepare Error in RecvThreadFunc");
}
::PostMessage( ((CHermesDlg*)pParam)->m_hWnd, WM_DATA_RECEIVED, ((CHermesDlg*)pParam)->m_WaveOutIndex, 0);
((CHermesDlg*)pParam)->m_WaveOutIndex = (((CHermesDlg*)pParam)->m_WaveOutIndex + 1) % BUFNUM;
delete []pBuf;
pBuf = NULL;
}
((CHermesDlg*)pParam)->m_State = OFFLINE;
waveOutClose( ((CHermesDlg*)pParam)->m_hWaveout);
waveInClose( ((CHermesDlg*)pParam)->m_hWavein);
return 0;
}
void CHermesDlg::OnMM_WIM_OPEN(UINT wParam,LONG lParam)
{
int ret;
m_WaveInIndex = 0;
WaitForSingleObject(m_hWaveinReady, INFINITE);
for (int i = 0; i < BUFNUM; i++)
{
m_pWaveInBuf[i] = new CWaveBuffer(m_hWavein);
ret = m_pWaveInBuf[i]->Prepare();
if (ret != MMSYSERR_NOERROR)
AfxMessageBox("WAVEIN Prepare Error.");
ret = m_pWaveInBuf[i]->AddToWaveInBuffer();
if (ret != MMSYSERR_NOERROR)
AfxMessageBox("WAVEIN Add Error.");
}
SetEvent(m_hWaveinBufReady);
ret = waveInStart (m_hWavein);
if (ret != MMSYSERR_NOERROR)
AfxMessageBox("waveInStart Error.");
}
void CHermesDlg::OnMM_WIM_DATA(UINT wParam,LONG lParam)
{
UINT ret;
CWaveFormat wf;
if (m_State == OFFLINE)
return;
CTCPSocket &sock = m_Calling;
if (m_Role == CALLEE)
{
sock = m_Called;
}
ret = sock.Send(((WAVEHDR*)lParam)->lpData, ((WAVEHDR*)lParam)->dwBytesRecorded);
if (ret != ((WAVEHDR*)lParam)->dwBytesRecorded)
{
AfxMessageBox("Send Error in OnMM_WIM_DATA.");
waveInClose(m_hWavein);
return;
}
m_pWaveInBuf[m_WaveInIndex]->ResetWaveInBuffer();
ret = m_pWaveInBuf[m_WaveInIndex]->Prepare();
if (ret != 0)
{
AfxMessageBox("Prepare Error in OnMM_WIM_DATA");
return ;
}
ret = m_pWaveInBuf[m_WaveInIndex]->AddToWaveInBuffer();
if (ret != 0)
{
AfxMessageBox("add buffer Error in OnMM_WIM_DATA");
return ;
}
m_WaveInIndex = (m_WaveInIndex+1) % BUFNUM;
}
void CHermesDlg::OnMM_WOM_OPEN(UINT wParam,LONG lParam)
{
m_WaveOutIndex = 0;
WaitForSingleObject(m_hWaveoutReady, INFINITE);
for (int i = 0; i < BUFNUM; i++)
{
m_pWaveOutBuf[i] = new CWaveBuffer(m_hWaveout);
}
SetEvent(m_hWaveoutBufReady);
}
void CHermesDlg::OnMM_WIM_CLOSE(UINT wParam,LONG lParam)
{
for (int i = 0; i < BUFNUM; i++)
{
if (m_pWaveInBuf[i] != NULL)
delete m_pWaveInBuf[i];
m_pWaveInBuf[i] = NULL;
}
}
void CHermesDlg::OnMM_WOM_CLOSE(UINT wParam,LONG lParam)
{
for (int i = 0; i < BUFNUM; i++)
{
if (m_pWaveOutBuf[i] != NULL)
delete m_pWaveOutBuf[i];
m_pWaveOutBuf[i] = NULL;
}
}
void CHermesDlg::OnWM_DATA_RECEIVED(UINT wParam,LONG lParam)
{
if (m_State == OFFLINE)
return;
int ret;
ret = waveOutReset(m_hWaveout);
if (ret != 0)
{
AfxMessageBox("waveOutReset Error in OnWM_DATA_RECEIVED.");
return ;
}
ret = m_pWaveOutBuf[wParam]->Play();
if (ret != 0)
{
AfxMessageBox("Play Error");
return ;
}
return ;
}
void CHermesDlg::OnButtonCall()
{
int ret;
in_addr dwIPaddr;
CWaveFormat wf;
m_Role = CALLER;
m_IpAddrCtrl.GetAddress(dwIPaddr.S_un.S_addr);
ret = m_Calling.Create();
if (ret == SOCKET_ERROR)
{
AfxMessageBox("Create Error in OnButtonCall.");
return;
}
ret = m_Calling.Connect(dwIPaddr.S_un.S_addr);
if (ret == SOCKET_ERROR)
{
AfxMessageBox("Connect Error in OnButtonCall.");
return;
}
ret = waveInGetNumDevs();
ret = waveInOpen(&m_hWavein, WAVE_MAPPER, &wf.m_wfx, (DWORD)this->m_hWnd, NULL, CALLBACK_WINDOW);
SetEvent(m_hWaveinReady);
if (ret != 0)
{
AfxMessageBox("WAVEIN Open Error in OnButtonCall");
return;
}
ret = waveOutOpen( &m_hWaveout, WAVE_MAPPER, &wf.m_wfx, (DWORD)this->m_hWnd, NULL, CALLBACK_WINDOW);
SetEvent(m_hWaveoutReady);
if (ret != 0)
{
AfxMessageBox("WAVEOUT Open Error in OnButtonCall");
return ;
}
m_State = ONLINE;
CreateThread(NULL, 0, RecvThreadFunc, (LPVOID)this, 0, NULL);
}
void CHermesDlg::OnButtonHangup()
{
m_State = OFFLINE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -