ConnToServer.rar

  • 风雨后的山顶
    了解作者
  • C++ Builder
    开发工具
  • 3KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 4
    下载次数
  • 2011-12-22 10:55
    上传日期
连接媒体服务器,并从远程数据库中获取设备信息。并建立转发socket,进行视频流转发
ConnToServer.rar
  • ConnToServer
  • vssver.scc
    64B
  • ConnToServer.h
    1.4KB
  • ConnToServer.cpp
    7.6KB
内容介绍
//--------------------------------------------------------------------------- #pragma hdrstop #include "ConnToServer.h" #include "HCNetSDK.h" //--------------------------------------------------------------------------- #include "dataModule.h" #include "readconfig.h" #include "videoChannels.h" #include "somedef.h" #include "deviceMgr.h" #include "decodeDeviceMgr.h" #pragma package(smart_init) CLoginServer* CLoginServer::SingleInstance() { static CLoginServer inst; return &inst; } CLoginServer::CLoginServer() { m_pTmr = new TTimer(0); m_pTmr->Interval = 1000; m_pTmr->OnTimer = OnTimer; } //1、连接流媒体服务器 //2、连接数据库,获取每个视频通道对应的转发服务器Ip //3、这个步骤应该在加载了本地XML数据之后进行 bool CLoginServer::Initialize() { WSADATA wsaData; WSAStartup(MAKEWORD(2,2),&wsaData); m_log.Initialize(2,"./log/ConnToServer/"); __connectStreamServer(); return True; } void CLoginServer::__connectStreamServer() { try { DM->SocketConnection->Host = CONFIG.GetSvrAddr().c_str(); DM->SocketConnection->Connected = True; g_pVideoChannels->LoadData(); m_bConnected2Svr = true; } catch(...) { m_bConnected2Svr = false; m_pTmr->Enabled = True; } } void __fastcall CLoginServer::OnTimer(TObject*) { if(!m_bConnected2Svr) { __connectStreamServer(); } else m_pTmr->Enabled = false; } bool CLoginServer::Connected() const { return m_bConnected2Svr; } /*连接至视频通道对应的转发服务器*/ SOCKET CLoginServer::__connectToTransfer(SA::LPCHANNEL_INFO pChannelInfo) { TMapTransferSocket::iterator it = m_mapTransferSocket.find(pChannelInfo->m_strProxyIp); if(m_mapTransferSocket.end()!=it) return it->second; else { SOCKET recvSckt = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(INVALID_SOCKET == recvSckt) return INVALID_SOCKET; unsigned long ulMaxTimeout(2000); setsockopt(recvSckt, SOL_SOCKET, SO_SNDTIMEO, (char*)&ulMaxTimeout, sizeof(unsigned long)); SOCKADDR_IN svrSocketAddr; memset(&svrSocketAddr, 0, sizeof(svrSocketAddr)); svrSocketAddr.sin_family = AF_INET; svrSocketAddr.sin_port = htons(pChannelInfo->m_nProxyPort); svrSocketAddr.sin_addr.s_addr = inet_addr(pChannelInfo->m_strProxyIp.c_str()); if(INVALID_SOCKET == connect(recvSckt,(SOCKADDR*)&svrSocketAddr,sizeof(svrSocketAddr))) { closesocket(recvSckt); return INVALID_SOCKET; } //连接成功 m_mapTransferSocket[pChannelInfo->m_strProxyIp] = recvSckt; return recvSckt; } } bool CLoginServer::StartRecvStream(SA::LPCHANNEL_INFO pChannelInfo, SA::LPVIDEO_DEVICE_INFO pDecoderDevice, ULONG ulBncNum) { SOCKET recvSckt = __connectToTransfer(pChannelInfo); if(INVALID_SOCKET == recvSckt) { m_log.WriteError("StartRecvStream::__connectToTransfer failed"); return false; } tag_SEC_MSG_RECV_MEDIASTREAM_REQ_EX2 msg; msg.m_header.m_ucReserve = 0; msg.m_header.m_usTransNo = 0; msg.m_header.m_ucMsgType = SEC_MSG_TYPE_RECV_MEDIASTREAM_REQ_EX2; msg.m_header.m_usVerNo = 0x1; msg.m_header.m_ucPacketLen = sizeof(tag_SEC_MSG_RECV_MEDIASTREAM_REQ_EX2); msg.m_MediaType = 1; msg.m_RecvMode = 1; msg.m_VideoSvrType = VSS_HKDS8000X; msg.m_ChannelNo = pChannelInfo->m_ulChannelID; msg.m_ucDNSEnabled = 0; msg.m_ucStreamType = pChannelInfo->m_eChannelMode; SA::LPVIDEO_DEVICE_INFO pDeviceInfo = g_pDeviceInfo->getDeviceInfoByID(pChannelInfo->m_ulDeviceID); __getIpFromString(pDeviceInfo->m_strEncoderIp.c_str(),msg.m_VideoSrcIP); //这里未填转发服务器Ip msg.m_TransSvrIP; if(SOCKET_ERROR == send(recvSckt,(char*)&msg,sizeof(tag_SEC_MSG_RECV_MEDIASTREAM_REQ_EX2),0)) return false; fd_set fds; struct timeval timeout = {5, 0}; FD_ZERO( &fds ); FD_SET( recvSckt,&fds ); if( select(0, &fds, NULL, NULL, &timeout) == 0 ) //等待超时 return false; if(FD_ISSET( recvSckt, &fds )) { //收到数据 SEC_MSG_RECV_MEDIASTREAM_RES RecvMsg; int recvLen=recv(recvSckt, (char *)&RecvMsg, sizeof(SEC_MSG_RECV_MEDIASTREAM_RES), 0); if(recvLen < sizeof(SEC_MSG_RECV_MEDIASTREAM_RES)) return false; if(RecvMsg.m_accept == 1) { SOCKET streamRecvSckt = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(INVALID_SOCKET == streamRecvSckt) return false; SOCKADDR_IN streamSendAddr; streamSendAddr.sin_family = AF_INET; streamSendAddr.sin_port = htons(RecvMsg.m_port); streamSendAddr.sin_addr.s_addr = inet_addr(pChannelInfo->m_strProxyIp.c_str()); if(SOCKET_ERROR == connect(streamRecvSckt, (SOCKADDR*)&streamSendAddr, sizeof(streamSendAddr))) { closesocket(streamRecvSckt); return false; } LPDECODER_CHANNEL_INFO pDecoderChannel = g_pDecodeDeviceInfo->GetDecoderChannelInfo( pDecoderDevice->m_ulDeviceID, ulBncNum); if(NULL == pDecoderChannel) return false; pDecoderChannel->m_recvSckt = streamRecvSckt; return __createRecvThread(pDecoderChannel); } } return false; } bool CLoginServer::StopRecvStream(SA::LPVIDEO_DEVICE_INFO pDecoderDevice, ULONG ulBncNum) { return true; } void CLoginServer::__getIpFromString( const char* strIP, unsigned char *szIP ) { AnsiString strTmp( strIP ); TStringList *slIpAddr( new TStringList ); slIpAddr->Delimiter = '.'; slIpAddr->DelimitedText = strIP; if(slIpAddr->Count==4) { szIP[0] = (unsigned char)slIpAddr->Strings[0].ToInt(); szIP[1] = (unsigned char)slIpAddr->Strings[1].ToInt(); szIP[2] = (unsigned char)slIpAddr->Strings[2].ToInt(); szIP[3] = (unsigned char)slIpAddr->Strings[3].ToInt(); } delete slIpAddr; } bool CLoginServer::__createRecvThread(LPDECODER_CHANNEL_INFO pDecoderChannel) { if(NULL == pDecoderChannel->m_hEventRecvThreadExit) { HANDLE hEvent = CreateEvent(NULL,TRUE,FALSE,NULL); if(NULL == hEvent) return false; pDecoderChannel->m_hEventRecvThreadExit = hEvent; } if(NULL == pDecoderChannel->m_hRecvThread) { HANDLE hThread = CreateThread(0,0,__recvStreamThread,(void*)pDecoderChannel,0,0); if(NULL == hThread) return false; pDecoderChannel->m_hRecvThread = hThread; } return true; } DWORD __stdcall CLoginServer::__recvStreamThread(LPVOID param) { LPDECODER_CHANNEL_INFO pDecoderChannel = (LPDECODER_CHANNEL_INFO)param; fd_set readfds; struct timeval timeout = {5, 0}; while(WAIT_OBJECT_0 != WaitForSingleObject(pDecoderChannel->m_hEventRecvThreadExit,0)) { FD_ZERO(&readfds); FD_SET(pDecoderChannel->m_recvSckt,&readfds); if(SOCKET_ERROR == select(0, &readfds, NULL, NULL, &timeout)) { //Sleep(40); } if(FD_ISSET(pDecoderChannel->m_recvSckt, &readfds)) { int iRecvLen = recv(pDecoderChannel->m_recvSckt, pDecoderChannel->m_szRecvBuf, MAX_RECV_SIZE, 0); if(iRecvLen > 0) { NET_DVR_MatrixSendData(pDecoderChannel->m_lPassiveHandle, pDecoderChannel->m_szRecvBuf, iRecvLen); } } } return 0; }
评论
    相关推荐
    • PeerCast流媒体服务器
      流媒体服务器,支持HLS,C++,很有参考价值。
    • vlc 流媒体服务器
      vlc 流媒体服务器 一个操作非常简单的流媒体服务器,支持多种音频视频格式(MPEG-1, MPEG-2, MPEG-4, DivX, mp3, ogg...)和流媒体协议。最具特色的功能是可以边下载边观看Divx媒体文件,并可以播放不完全的AVI文件...
    • 流媒体服务器
      流媒体服务器
    • 流媒体服务器架设工具
      流媒体服务器,是本人整合网络资源之后,自己增加一部分代码以及动态库文件组成,保证简便可以。是视频开发者的必备利器。是不是自己看吧!
    • RED5流媒体服务器
      RED5流媒体服务器的简单搭建,只需要简单配置环境变量,然后执行批处理文件,就可以运行RED5流媒体服务器了,非常方便简单
    • 流媒体服务器spook
      流媒体服务器spook源代码,RTSP,RTP支持,流媒体服务器spook源代码
    • QuickTime 流媒体服务器模块
      描述的是为开放源码的Darwin流媒体服务器创建QuickTime流媒体服务器(QTSS)模块所需要的编程接口,版本为4.0。QTSS编程接口为开发者提供了一种易于使用的开发方式,用于为流媒体服务器添加新的功能。这个版本的编程...
    • 流媒体服务器live555
      live555是著名的流媒体服务器开源项目,很多服务器都是以此为蓝本,这是完整的代码。 BasicUsageEnvironment groupsock liveMedia mediaServer nbproject testProgs UsageEnvironment WindowsAudioInputDevice ……
    • rtspl流媒体服务器
      rtsp流媒体服务器,支持MP3,MP4,h264等视频格式
    • 流媒体服务器
      流媒体服务器相关资源,点播、直播分发简介,hls rtsp对比,网络电台功能模块。