Control.cpp
资源名称:视频会议系统.rar [点击查看]
上传用户:popouu88
上传日期:2013-02-11
资源大小:2894k
文件大小:6k
源码类别:
IP电话/视频会议
开发平台:
Visual C++
- // Control.cpp : Defines the class behaviors for the application.
- //
- #include "stdafx.h"
- #include "Control.h"
- #include "ControlDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CControlApp
- BEGIN_MESSAGE_MAP(CControlApp, CWinApp)
- //{{AFX_MSG_MAP(CControlApp)
- //}}AFX_MSG
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CControlApp construction
- CControlApp::CControlApp()
- {
- this->appIni = "";
- this->IP = "";
- this->Port = 0;
- this->userName = "";
- this->password = "";
- }
- /////////////////////////////////////////////////////////////////////////////
- // The one and only CControlApp object
- CControlApp theApp;
- /////////////////////////////////////////////////////////////////////////////
- // CControlApp initialization
- BOOL CControlApp::InitInstance()
- { //程序的位置
- ::GetModuleFileName( NULL , this->appIni.GetBufferSetLength( MAX_PATH ) , MAX_PATH );
- this->appIni.ReleaseBuffer( );
- this->appIni = this->appIni.Left( this->appIni.ReverseFind( '\' ) + 1 ) + "control.ini";
- //读取服务器IP
- ::GetPrivateProfileString( "服务器信息" , "服务器IP" , "" , this->IP.GetBufferSetLength( MAX_PATH ) , MAX_PATH , this->appIni );
- this->IP.ReleaseBuffer( );
- //读取端口
- this->Port = ::GetPrivateProfileInt( "服务器信息" , "服务器端口" , 0 , this->appIni );
- //读取用户名
- ::GetPrivateProfileString( "用户信息" , "用户名" , "" , this->userName.GetBufferSetLength( MAX_PATH ) , MAX_PATH , this->appIni );
- this->userName.ReleaseBuffer( );
- //读取用户密码
- ::GetPrivateProfileString( "用户信息" , "密码" , "" , this->password.GetBufferSetLength( MAX_PATH ) , MAX_PATH , this->appIni );
- CControlDlg dlg;
- m_pMainWnd = &dlg;
- dlg.DoModal( );
- theApp.videoTCP.Close( );
- theApp.cmdTCP.Close( );
- return FALSE;
- }
- //自动连接
- void CControlApp::OnConnect( void * pContext )
- {
- CControlDlg * pThis = ( CControlDlg * )pContext;
- PChannelType cType;
- PRegisterRES res;
- //连接服务器
- for( ;; )
- {
- while( ! theApp.cmdTCP.Connect( theApp.IP , theApp.Port ) && ::IsWindow( pThis->GetSafeHwnd( ) ) );
- //如果是连接成功退出的
- if( theApp.cmdTCP.IsConnected( ) )
- { //发送命令通道标志
- cType.assembleData( PChannelType::COMMAND , 0 );
- if( theApp.cmdTCP.Send( cType.buffer ) )
- { //用户注册
- PRegisterREQ req;
- req.assembleData( ( char * )( const char * )theApp.userName , ( char * )( const char * )theApp.password , PRegisterREQ::MONITOR );
- //发送用户注册和接收用户注册回复
- struct timeval tm = { 5 , 0 };
- if( theApp.cmdTCP.Send( req.buffer ) && theApp.cmdTCP.Receive( res.buffer , &tm ) && res.parseData( res.buffer ) && ! res.ireturn )
- break;
- }//循环连接
- theApp.cmdTCP.Close( );
- }
- else //否则是关闭窗口退出的,那么退出连接循环
- return;
- }//解析命令通道回复信息
- if( res.parseData( res.buffer ) && ! res.ireturn )
- { //用户id
- theApp.user_id = res.user_id;
- //连接视频通道
- theApp.videoTCP.Connect( theApp.IP , theApp.Port );
- //发送视频类型
- cType.assembleData( PChannelType::VIDEO , theApp.user_id );
- theApp.videoTCP.Send( cType.buffer );
- PVideoData vd;
- //发送视频头
- vd.assembleData( theApp.user_id , 0 , PVideoData::VIDEO_HEADER , 0 , 0 );
- theApp.videoTCP.Send( vd.buffer );
- //有可能运行失败
- CThread::Run( CControlApp::OnCommand , &theApp );
- //有可能运行失败
- CThread::Run( CControlApp::OnVideo , &theApp );
- }//隐藏自动登陆窗口
- pThis->m_astatic.ShowWindow( SW_HIDE );
- pThis->m_olist.ShowWindow( SW_SHOW );
- pThis->m_viewbtn.ShowWindow( SW_SHOW );
- }
- //命令通道
- void CControlApp::OnCommand( void * pContext )
- { //请求在线用户列表
- POnlineUserREQ req;
- req.assembleData( );
- theApp.cmdTCP.Send( req.buffer );
- CBuffer buffer;
- //2个数据包之间最大的时间间隔是5分钟
- struct timeval tm = { 300 , 0 };
- //接收名命令包
- try
- {
- while( theApp.cmdTCP.Receive( buffer /*, &tm */) )
- {
- if( ! buffer.GetSize( ) )
- break;
- switch( *( int * )( buffer.GetBuffer( ) + INT_SIZE ) )
- {//要求尽快返回
- case POnlineUserRESTAG : theApp.OnlineUser( buffer ); break;
- }
- }
- }
- catch( ... )
- {
- }
- //关闭视频信号
- theApp.videoTCP.Close( );
- //自动登陆
- if( ::IsWindow( theApp.m_pMainWnd->GetSafeHwnd( ) ) )
- theApp.m_pMainWnd->SendMessage( WM_PACKAGE );
- }
- //在线用户
- void CControlApp::OnlineUser( CBuffer & buffer )
- {
- if( ! ::IsWindow( this->m_pMainWnd->GetSafeHwnd( ) ) )
- return;
- POnlineUserRES res;
- res.parseData( buffer );
- this->m_pMainWnd->SendMessage( WM_PACKAGE , ( WPARAM )&res );
- }
- //视频接收
- void CControlApp::OnVideo( void * pContext )
- {
- PVideoData vd;
- INT_PTR_MAP::iterator itr;
- CVideoDlg * dlg = NULL;
- PStopVideoREQ stop;
- try
- {
- while( theApp.videoTCP.Receive( vd.buffer ) && vd.parseData( vd.buffer ) )
- {
- if( ! vd.buffer.GetSize( ) ) continue;
- //找不到显示视频窗口,那么停止发送视频
- if( ( itr = theApp.recv_video_map.find( vd.user_id ) ) == theApp.recv_video_map.end( ) )
- { //停止显示
- stop.assembleData( vd.user_id );
- theApp.cmdTCP.Send( stop.buffer );
- }
- else
- { //视频窗口
- dlg = ( CVideoDlg * )itr->second;
- //窗口句柄无效,停止发送视频
- if( ! ::IsWindow( dlg->GetSafeHwnd( ) ) )
- { //停止显示
- stop.assembleData( vd.user_id );
- theApp.cmdTCP.Send( stop.buffer );
- theApp.recv_video_map.erase( itr );
- }
- else
- { //视频头
- if( vd.type == PVideoData::VIDEO_HEADER )
- { //初始化失败,停止发送视频
- if( ! dlg->video.InitDraw( dlg->m_video.GetSafeHwnd( ) , vd.video , vd.size ) )
- { //停止显示
- stop.assembleData( vd.user_id );
- theApp.cmdTCP.Send( stop.buffer );
- theApp.recv_video_map.erase( itr );
- }
- }//视频数据
- else if( vd.type == PVideoData::VIDEO_DATA )
- dlg->video.Draw( vd.video , vd.size );
- }
- }
- }
- }
- catch( ... )
- {
- }
- theApp.cmdTCP.Close( );
- }