📄 display.cpp
字号:
/* */
/* This function will be caled when the client */
/* receives message from server */
/* (called form "mysocket" OnReceive function ) */
/* */
void Display::Receive()
{
char buff[PLAYBUFFER+25],str[PLAYBUFFER+60];
int size=PLAYBUFFER+20,rcount,index,i,j,length;
CString mesg,header,disp,senderip;
UINT senderport;
rcount = sockclt.ReceiveFrom (buff, size, senderip, senderport);
if(rcount==SOCKET_ERROR)
{
log.WriteString("\nError in Receiving the data");
return;
}
//if sent data received back then return
if(senderip.CompareNoCase(sockclt.myaddr)==0)
{
log.WriteString("\n Rejecting data ***********");
return;
}
if(rcount>(PLAYBUFFER+20))
{
log.WriteString("\nMesg size exceeded buffer capacity");
return;
}
buff[rcount]=NULL;
mesg=buff;
sprintf(str,"Received data len= %d , %s ",rcount,&buff[20]);
log.WriteString(str);
index=mesg.Find(':');
//Invalid message Format --must have atleast one tag
if(index==-1)
return;
header=mesg.Left(index);
//Get the legth of buffer
length=0;
sscanf(&buff[15],"%d",&length);
if(length<1 || length>PLAYBUFFER)
return;
if(play->Playing==FALSE)
play->PostThreadMessage(WM_PLAYSOUND_STARTPLAYING,0,0);
sprintf(str,"\n Received data from %s and port = %d ",LPCTSTR(senderip),senderport);
log.WriteString(str);
LPWAVEHDR lpHdr=playhead[curhead];
curhead=(curhead+1)%MAXBUFFER;
for(i=20,j=0;j<length;i++,j++)
lpHdr->lpData[j]=buff[i];
lpHdr->dwBufferLength=length;
lpHdr->dwFlags=0;
play->PostThreadMessage(WM_PLAYSOUND_PLAYBLOCK,0,(LPARAM)lpHdr);
//Save the Voice data from perticular user
if(isSave==TRUE && writeuser.CompareNoCase(header)==0)
{
write->PostThreadMessage(WM_WRITESOUND_WRITEDATA,0,(LPARAM)lpHdr);
log.WriteString("\n Writing to store");
}
log.WriteString("\nPlaying......");
disp=" "+header+" is talking...";
SetDlgItemText(6051,(char*)(LPCTSTR)disp);
}
/* */
/* This function will flash the window whenever */
/* important message has been received...shutdown,logout..etc */
/* */
void Display::showFlash()
{
//flash for 3 times
FlashWindow(TRUE);
Sleep(200);
FlashWindow(TRUE);
}
void Display::OnStartWrite(char *name)
{
isSave=TRUE;
log.WriteString("\n Calling create fun of thread");
write->PostThreadMessage(WM_WRITESOUND_CREATEFILE,0,(LPARAM)name);
}
void Display::OnStopWrite()
{
isSave=FALSE;
write->PostThreadMessage(WM_WRITESOUND_CLOSEFILE,0,0);
}
/* */
/* This function will be called whenever user */
/* presses the start/stop button */
/* */
void Display::OnStart()
{
talk->Play(0,-1,-1);
startRecording();
startPlaying();
start->ShowWindow(FALSE);
stop->ShowWindow(TRUE);
}
/* */
/* This function will be called whenever user */
/* presses the Stop button */
/* */
void Display::OnStop()
{
talk->Stop();
stopRecording();
stopPlaying();
start->ShowWindow(TRUE);
stop->ShowWindow(FALSE);
}
//
//*** Part of this code needs to be removed later***
//
void Display::startRecording()
{
if(record->recording==FALSE)
{
record->PostThreadMessage(WM_RECORDSOUND_STARTRECORDING,0,0);
}
}
/* */
/* This function will stop the recording by sending */
/* stop message to recording thread */
/* */
void Display::stopRecording()
{
if(record->recording==TRUE)
record->PostThreadMessage(WM_RECORDSOUND_STOPRECORDING,0,0);
}
/* */
/* This function will stop the playing by sending */
/* stop message to player thread. */
/* */
void Display::startPlaying()
{
if(play->Playing==FALSE)
play->PostThreadMessage(WM_PLAYSOUND_STARTPLAYING,0,0);
}
/* */
/* This function will stop the playing by sending */
/* stop message to player thread. */
/* */
void Display::stopPlaying()
{
if(play->Playing==TRUE)
play->PostThreadMessage(WM_PLAYSOUND_STOPPLAYING,0,0);
}
/* */
/* This function will send the voice data to the server */
/* (called from the Recorder Thread) */
/* */
void Display::sendMessage(char *mesg,int length)
{
char buflen[15];
char str[PLAYBUFFER+50];
int i,j;
if(mesg==NULL)
{
log.WriteString("\nEmpty buffer received from recordsound");
return;
}
if(length<1 || length>PLAYBUFFER)
{
log.WriteString("\n Send : Length is more than the 2000 or less than 1");
return;
}
mesg=mesg-20;
//First 15 bits contains the destination information
//Next 5 bits contains the length of message
//Remaining part contains the data
for(i=0;i < sockclt.name.GetLength();i++)
mesg[i]=sockclt.name[i];
mesg[i]=':';
sprintf(buflen,"%d",length);
for(j=0,i=15;j<5;j++,i++)
mesg[i]=buflen[j];
//Write to log file
sprintf(str,"\n Sending message datalen = %d , mesg = %s",length,&mesg[20]);
log.WriteString(str);
int ret=sockclt.send.SendTo(mesg,length+20,(SOCKADDR*)&(sockclt.hgroup),sizeof(SOCKADDR),0);
if(ret==SOCKET_ERROR)
log.WriteString("\nData sent Error ");
else
log.WriteString("\nData sent successfully to target host");
}
/* */
/* Display the About Dialog Box */
/* */
void Display::OnAbout()
{
about my(IDD_DIALOG2);
my.DoModal();
}
/* */
/* Display the About Dialog Box */
/* */
void Display::OnSave()
{
if(curuser.IsEmpty())
{
MessageBox("\n First connect to one of the user...");
return;
}
writeuser=curuser;
Save sa(IDD_DIALOG4,this);
sa.DoModal();
}
/* */
/* This function will destroy the client window and */
/* closes all threads and socket connecion */
/* */
void Display::OnCancel()
{
log.WriteString("\n Closing the dialog box");
//Stop the recording and playing threads
if(record->recording==TRUE)
record->PostThreadMessage(WM_RECORDSOUND_STOPRECORDING,0,0);
record->PostThreadMessage(WM_RECORDSOUND_ENDTHREAD,0,0);
if(play->Playing==TRUE)
play->PostThreadMessage(WM_PLAYSOUND_STOPPLAYING,0,0);
play->PostThreadMessage(WM_PLAYSOUND_ENDTHREAD,0,0);
write->PostThreadMessage(WM_WRITESOUND_ENDTHREAD,0,0);
if(sockclt.closeflag==1)
{
showFlash();
MessageBox("Server has shutdown");
}
if(isconnected==TRUE)
sockclt.Close();
CDialog::OnCancel();
}
/* */
/* This function will display the volume setting */
/* dialog box. */
/* */
void Display::OnVolume()
{
Volume vol(IDD_DIALOG3);
vol.DoModal();
}
/* */
/* This function will play the stored sound */
/* */
/* */
void Display::OnPlay()
{
CFileDialog fd(1,0,0,0,"Sound Files(*.wav)|*.wav|All Files|*.*||" );
fd.m_ofn.lpstrTitle="Browse";
if(fd.DoModal()==IDOK)
{
sndPlaySound(fd.GetPathName(),SND_ASYNC);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -