Server.cpp
资源名称:视频会议系统.rar [点击查看]
上传用户:popouu88
上传日期:2013-02-11
资源大小:2894k
文件大小:5k
源码类别:
IP电话/视频会议
开发平台:
Visual C++
- // Server.cpp: implementation of the CServer class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "Server.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CServer::CServer( )
- {
- }
- CServer::~CServer( )
- {
- }
- bool CServer::Run( const int port )
- {
- if( this->m_server.Create( CServer::OnAccept , this , port ) )
- {
- this->logFile.Write( "中央管理服务器初始化成功,正在监听端口 : %d ...n" , port );
- return true;
- }
- this->logFile.Write( "中央管理服务器初始化失败n" );
- return false;
- }
- void CServer::Stop( void )
- {
- this->logFile.Write( "关闭中央管理服务器监听n" );
- this->m_server.Close( );
- this->logFile.Write( "释放客户端资源n" );
- this->GetMCUInformation( ).Release( );
- }
- void CServer::OnAccept( void * wParam , CTCP &server )
- {
- CServer * pThis = ( CServer * )wParam;
- CTCP * client = new CTCP( );
- client->SetData( ( int )pThis );
- if( server.Accept( client ) )
- {
- pThis->logFile.Write( "服务器接收到一个连接n" );
- CThread::Run( CServer::OnCommand , client );
- }
- else
- {
- client->Close( );
- delete client;
- if( pThis->m_server.IsListen( ) )
- {
- pThis->logFile.Write( "服务器监听失败,再次启动服务器n" );
- }
- }
- }
- void CServer::OnCommand( void * wParam )
- {
- CTCP * client = ( CTCP * )wParam;
- //取出本类指针
- CServer * pThis = ( CServer * )client->GetData( );
- client->SetData( 0 );
- CBuffer buffer;
- pThis->logFile.Write( "服务器为该连接启动一个线程n" );
- while( client->Receive( buffer ) )
- {
- switch( *( int * )( buffer.GetBuffer( ) + INT_SIZE ) )
- {//客户端注册
- case PRegisterREQTAG : pThis->OnRegister( client , buffer ); break;
- //客户端请求在线用户信息
- case POnlineUserREQTAG : pThis->OnOnlineUserREQ( client , buffer ); break;
- //客户端更新在线用户信息
- case POnlineUserRESTAG : pThis->OnOnlineUserRES( client , buffer ); break;
- }
- }//删除该节点
- CMCUNode * node = pThis->GetMCUInformation( ).GetMCUNode( ( int )client );
- pThis->logFile.Write( "MCU服务器 %s 退出了程序,删除该节点的信息n" , node->mcu_name.c_str( ) );
- pThis->GetMCUInformation( ).RemoveMCUNode( ( int )client );
- }
- //子MCU注册
- void CServer::OnRegister( CTCP * client , CBuffer & buffer )
- {
- PRegisterREQ req;
- req.parseData( buffer );
- CMCUNode * node = this->GetMCUInformation( ).AddMCUNode( client , req.userName.c_str( ) );
- //打印信息
- this->logFile.Write( "用户 %s 注册 %s , id号:%dn" , req.userName.c_str( ) , node ? "成功" : "失败" , node ? node->mcu_id : 0 );
- //回复信息
- PRegisterRES res;
- res.assembleData( ! node , node ? node->mcu_id : 0 );
- client->Send( res.buffer );
- }
- //请求在线用户
- void CServer::OnOnlineUserREQ( CTCP * client , CBuffer & buffer )
- {
- CMCUNode * mcuNode = this->GetMCUInformation( ).GetMCUNode( ( int )client );
- if( ! mcuNode )
- {
- return;
- }
- //打印信息
- this->logFile.Write( "MCU服务器 %s 请求其他MCU的在线用户信息n" , mcuNode->mcu_name.c_str( ) );
- //回复在线用户信息
- INT_PTR_MAP * m_map = this->GetMCUInformation( ).LockMap( );
- for( INT_PTR_MAP::iterator itr = m_map->begin( ); itr != m_map->end( ); itr ++ )
- {
- CMCUNode * mcu = ( CMCUNode * )itr->second;
- if( mcu->mcu_id != mcuNode->mcu_id )
- {
- for( INT_PTR_MAP::iterator i = mcu->online_map.begin( ); i != mcu->online_map.end( ); i ++ )
- {
- CClientNode * node = ( CClientNode * )i->second;
- POnlineUserRES res;
- res.assembleData( node->user_id , node->user_name.c_str( ) , node->isCharement , node->data_name.c_str( ) , false );
- client->Send( res.buffer );
- }
- }
- }
- this->GetMCUInformation( ).UnlockMap( );
- }
- //在线用户信息状态的改变
- void CServer::OnOnlineUserRES( CTCP * client , CBuffer & buffer )
- {
- CMCUNode * mcuNode = this->GetMCUInformation( ).GetMCUNode( ( int )client );
- if( ! mcuNode )
- return;
- POnlineUserRES res;
- res.parseData( buffer );
- CClientNode * clientNode = mcuNode->GetClientNode( res.user_id );
- //用户在线
- if( ! res.user_name.empty( ) )
- { //如果找不到该用户,那么新增一个用户
- if( ! clientNode )
- { //打印信息
- this->logFile.Write( "MCU服务器 %s 增加一个用户 %sn", mcuNode->mcu_name.c_str( ) , res.user_name.c_str( ) );
- mcuNode->AddClientNode( res.user_id , res.user_name.c_str( ) , res.data_name.c_str( ) , res.isCharement );
- }//更改用户的状态
- else
- {
- clientNode->data_name = res.data_name;
- clientNode->isCharement = res.isCharement;
- //打印信息
- this->logFile.Write( "MCU服务器 %s 的一个用户 %s 状态 : %sn", mcuNode->mcu_name.c_str( ) , res.user_name.c_str( ) , ! clientNode->data_name.empty( ) ? "开会中" : "空闲" );
- }
- }//用户退出程序
- else if( clientNode )
- { //打印信息
- this->logFile.Write( "MCU服务器 %s 的一个用户 %s 退出了系统n", mcuNode->mcu_name.c_str( ) , clientNode->user_name.c_str( ) );
- mcuNode->RemoveClientNode( clientNode->user_id );
- clientNode = NULL;
- }
- }