📄 clientdlg.cpp
字号:
{
wsprintf(sShowText, "%s: in talking with %s", m_MySock.m_sUserName, \
m_lpPeerName);
iIndex = m_ComBoTalker.FindString(0, m_lpPeerName);
m_ComBoTalker.SetCurSel(iIndex);
}
else
{
if(lstrlen(m_MySock.m_sUserName) > 0)
wsprintf(sShowText, "%s: free", m_MySock.m_sUserName);
else
wsprintf(sShowText, "Voice Talk Client");
}
SetWindowText(sShowText);
}
void CClientDlg::ParseMsgData(void)
{
CString strRecv;
switch(m_vtRecvMsgBuf.m_ucMsgType)
{
case VT_WAVE1M08: // Voice message
case VT_WAVE1S08:
case VT_WAVE1M16:
case VT_WAVE1S16:
case VT_WAVE2M08:
case VT_WAVE2S08:
case VT_WAVE2M16:
case VT_WAVE2S16:
case VT_WAVE4M08:
case VT_WAVE4S08:
case VT_WAVE4M16:
case VT_WAVE4S16:
m_progctrlRecv.SetPos(1);
// Get wave data buffer size
m_uiBufLen = m_vtRecvMsgBuf.m_lLength - SIZEVTMSGHDR;
// Copy wave data buffer
memcpy((void*)m_pWaveData, (void*)m_vtRecvMsgBuf.m_pData,m_uiBufLen);
if(!m_bVoiceMsg)
{
m_bVoiceMsg = TRUE;
m_vtPlay.SetPlayData(m_pWaveData, m_uiBufLen);
_vtPlay.StartPlay();
}
return;
}
}
LONG CClientDlg::OnVTDataReady(WPARAM wparam, LPARAM lparam)
{
// If the server connection was closed Clean the temporary buffers
char sPeerName[16];
char sShowText[128];
int iIndex;
int iCount;
memset((void*)sPeerName, 0, 16);
memset((void*)sShowText, 0, 128);
if(LOWORD(lparam) == FD_CLOSE)
{
AfxMessageBox("Server Connection Dropped!");
DoConnection(FALSE);
return 1L;
}
memset((void*)&m_vtRecvMsgBuf, 0, SIZEVTMSG);
// Read the incoming message data
if(!RecvVTMessage(&m_MySock, &m_vtRecvMsgBuf))
{
return 0L;
}
switch(m_vtRecvMsgBuf.m_ucCmd)
{
case VTCMD_REGNAME:
// Add the Clients name to "Talk With" combobox
memcpy((void*)sPeerName, (void*)m_vtRecvMsgBuf.m_pData, 16*sizeof(char));
iCount = m_ComBoTalker.GetCount();
m_ComBoTalker.InsertString(iCount, sPeerName);
break;
case VTCMD_DEREGNAME:
// Remove name from list box
memcpy((void*)sPeerName, (void*)m_vtRecvMsgBuf.m_pData,16*sizeof(char));
iIndex = m_ComBoTalker.FindString(-1, sPeerName);
if(iIndex != LB_ERR)
{
m_ComBoTalker.DeleteString(iIndex);
}
break;
case VTCMD_REQSESSION:
// Someone is asking for a chat session
lstrcpy(sShowText, (char*)m_vtRecvMsgBuf.m_pData);
lstrcat(sShowText, " requests to talk.");
if(AfxMessageBox(sShowText, MB_OKCANCEL) == IDOK)
{
lstrcpy(m_lpPeerName, (char*)m_vtRecvMsgBuf.m_pData);
memset(&m_vtSendMsgBuf, 0, SIZEVTMSG);
m_vtSendMsgBuf.m_ucIdentity = VT_IDENTITY;
m_vtSendMsgBuf.m_ucCmd = VTCMD_SESSIONREQRESP;
m_vtSendMsgBuf.m_ucMsgType = VT_TEXT;
m_vtSendMsgBuf.m_lLength = SIZEVTMSGHDR + 2;
m_vtSendMsgBuf.m_pData[0] = 1;
if(SendVTMessage(m_MySock.m_hSock, &m_vtSendMsgBuf))
EnableTalking(TRUE);
else
AfxMessageBox("Can't send message to server");
return 1L;
}
else
{
// Rejected the session request,// send the denial message
memset(&m_vtSendMsgBuf, 0, SIZEVTMSG);
m_vtSendMsgBuf.m_ucIdentity = VT_IDENTITY;
m_vtSendMsgBuf.m_ucCmd = VTCMD_SESSIONREQRESP;
m_vtSendMsgBuf.m_ucMsgType = VT_TEXT;
m_vtSendMsgBuf.m_lLength = SIZEVTMSGHDR + 2;
m_vtSendMsgBuf.m_pData[0] = 0;
if(SendVTMessage(m_MySock.m_hSock, &m_vtSendMsgBuf))
EnableTalking(FALSE);
}
break;
// Response to request
case VTCMD_SESSIONREQRESP:
if(m_MySock.m_iStatus != VTSOCKET_REQSESSION)
{
return 0L;
}
if(*m_vtRecvMsgBuf.m_pData == 1)
{
EnableTalking(TRUE);
}
else
{
EnableTalking(FALSE);
}
break;
case VTCMD_SESSIONCLOSE:
EnableTalking(FALSE);
m_MySock.m_iStatus = VTSOCKET_AVAILABLE;
wsprintf(sShowText, "%s: available for talking", m_MySock.m_sUserName);
SetWindowText(sShowText);
if(m_bVoiceMsg)
m_vtPlay.StopPlay();
if(m_bVoiceTalk)
m_vtRecord.StopRecord();
return FALSE;
case VTCMD_MSGDATA:
// Parse the message and display it
ParseMsgData();
break;
default:
return 0L;
}
return 1L;
}
LONG CClientDlg::OnVTReadyForWrite(WPARAM wparam, LPARAM lparam)
{
memset((void*)m_MySock.m_sUserName, 0, 16*sizeof(char));
strncpy(m_MySock.m_sUserName,m_strUserName, m_strUserName.GetLength()+1);
memset(&m_vtSendMsgBuf, 0, SIZEVTMSG);
m_vtSendMsgBuf.m_ucIdentity = VT_IDENTITY;
m_vtSendMsgBuf.m_ucCmd = VTCMD_REGNAME;
m_vtSendMsgBuf.m_ucMsgType = VT_TEXT;
m_vtSendMsgBuf.m_lLength = SIZEVTMSGHDR + REALLEN(m_MySock.m_sUserName);
memcpy((void*)m_vtSendMsgBuf.m_pData, (void*)m_MySock.m_sUserName,
REALLEN(m_MySock.m_sUserName));
if(SendVTMessage(m_MySock.m_hSock, &m_vtSendMsgBuf))
{
DoConnection(TRUE);
}
return 1L;
}
LONG CClientDlg::OnRecordEvent(WPARAM wparam, LPARAM lparam)
{
if(!m_bVoiceTalk)
return 1L;
m_progctrlSend.SetPos(1);
WAVEHDR* lpwhdr = (WAVEHDR*)lparam;
// Reset the voice message packet
memset(&m_vtSendMsgBuf, 0, SIZEVTMSG);
m_vtSendMsgBuf.m_ucIdentity = VT_IDENTITY;
m_vtSendMsgBuf.m_ucCmd = VTCMD_MSGDATA;
m_vtSendMsgBuf.m_ucMsgType = m_iWaveFormat;
m_vtSendMsgBuf.m_lLength = SIZEVTMSGHDR + lpwhdr->dwBufferLength;
memcpy((void*)m_vtSendMsgBuf.m_pData, (void*)lpwhdr->lpData,
lpwhdr->dwBufferLength);
m_vtRecord.ResetRecord();
// Send the voice message packet
SendVTMessage(m_MySock.m_hSock, &m_vtSendMsgBuf);
m_progctrlSend.SetPos(0);
return 1L;
}
LONG CClientDlg::OnPlayEvent(WPARAM wparam, LPARAM lparam)
{
if(!m_bVoiceMsg)
return 0L;
m_progctrlRecv.SetPos(0);
m_vtPlay.ResetPlay();
m_vtPlay.SetPlayData(m_pWaveData, m_uiBufLen);
m_vtPlay.RestartPlay();
return 1L;
}
void CClientDlg::OnChangeEditname()
{
m_editUserName.GetWindowText(m_strUserName);
}
void CClientDlg::OnChangeEditserver()
{
m_editServer.GetWindowText(m_strServer);
}
void CClientDlg::OnSelchangeCombprot()
{
int iIndex;
iIndex = m_ComBoProts.GetCurSel();
if(iIndex == LB_ERR)
{
AfxMessageBox("An error ocurred in selecting protocols!");
return;
}
switch(m_lpProtBuf[m_iWorkProts[iIndex]].iAddressFamily)
{
case AF_INET:
m_editServer.EnableWindow(TRUE);
break;
default: // Nothing
m_editServer.SetWindowText("");
m_editServer.EnableWindow(FALSE);
break;
}
}
void CClientDlg::OnSelchangeCombchatter()
{
// Add control notification handler code here
int iIndex;
int iRev;
char sNewPeer[16];
memset(sNewPeer, 0, 16);
iIndex = m_ComBoTalker.GetCurSel();
if(iIndex == LB_ERR)
{
AfxMessageBox("An error occurred in selecting peer talker");
return;
}
if(iIndex == 0)
{
if(m_bTalking)
{
memset(&m_vtSendMsgBuf, 0, SIZEVTMSG);
m_vtSendMsgBuf.m_ucIdentity = VT_IDENTITY;
m_vtSendMsgBuf.m_ucCmd = VTCMD_SESSIONCLOSE;
m_vtSendMsgBuf.m_ucMsgType = VT_TEXT;
m_vtSendMsgBuf.m_lLength = SIZEVTMSGHDR;
if(SendVTMessage(m_MySock.m_hSock, &m_vtSendMsgBuf))
{
if(WSAAsyncSelect(m_MySock.m_hSock, GetSafeHwnd(), 0, != SOCKET_ERROR)
{
EnableTalking(FALSE);
return;
}
}
}
return;
}
else
{
iRev = m_ComBoTalker.GetLBText(iIndex, sNewPeer);
if(iRev == CB_ERR)
{
AfxMessageBox("An error occurred in getting new talker's name");
return;
}
if(lstrcmp(sNewPeer, m_lpPeerName) == 0)
{
return;
}
lstrcpy(m_lpPeerName, sNewPeer);
m_MySock.m_iStatus = VTSOCKET_REQSESSION;
memset(&m_vtSendMsgBuf, 0, SIZEVTMSG);
m_vtSendMsgBuf.m_ucIdentity = VT_IDENTITY;
m_vtSendMsgBuf.m_ucCmd = VTCMD_REQSESSION;
m_vtSendMsgBuf.m_ucMsgType = VT_TEXT;
m_vtSendMsgBuf.m_lLength = SIZEVTMSGHDR + REALLEN(sNewPeer);
memcpy((void*)m_vtSendMsgBuf.m_pData, (void*)sNewPeer,
REALLEN(sNewPeer));
if(SendVTMessage(m_MySock.m_hSock, &m_vtSendMsgBuf))
{
if(WSAAsyncSelect(m_MySock.m_hSock, GetSafeHwnd(), VTM_DATAREADY, FD_READ
FD_CLOSE) != SOCKET_ERROR)
{
EnableTalking(TRUE);
return;
}
}
}
}
void CClientDlg::OnBtnconnect()
{
int iIndex;
iIndex = m_ComBoProts.GetCurSel();
if(iIndex == LB_ERR)
{
AfxMessageBox("Did not select a protocol!");
return;
}
if(m_strUserName.IsEmpty())
{
AfxMessageBox("Did not input a user name");
return;
}
if(m_lpProtBuf[m_iWorkProts[iIndex]].iAddressFamily
== AF_INET && m_strServer.IsEmpty())
{
AfxMessageBox("Did not input a server name");
return;
}
if(!InitSockets(m_iWorkProts[iIndex]))
{
AfxMessageBox("Can't build connection with server!");
}
}
void CClientDlg::OnBtndisconnect()
{
if(m_bConnect)
DoConnection(FALSE);
}
void CClientDlg::OnChkvoice()
{
if(!m_bConnect)
return;
if(!m_bVoiceTalk)
{
m_bVoiceTalk = TRUE;
m_chkVoice.SetCheck(1);
m_vtRecord.StartRecord();
}
else // If checked previously
{
m_bVoiceTalk = FALSE;
m_chkVoice.SetCheck(0);
m_vtRecord.StopRecord();
}
}
void CClientDlg::OnTimer(UINT nIDEvent)
{
CDialog::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -