#include "StdAfx.h"
#include "MeetingConnImpl.h"
#include "Poco/Timestamp.h"
#include "Poco/MD5Engine.h"
#include "MeetingFrameImpl.h"
using Poco::MD5Engine;
using Poco::DigestEngine;
using Poco::Timestamp;
#define BUF_LEN 1024*1024
MeetingConnImpl *MeetingConnImpl:: m_instance = NULL;
MeetingConnImpl *MeetingConnImpl::GetInstance(void)
{
if(m_instance == NULL)
{
m_instance = new MeetingConnImpl();
::atexit(ReleaseALL);
}
return m_instance;
}
MeetingConnImpl::MeetingConnImpl(void)
{
m_ulRoomID = 1;
m_mySessionID = 0;
m_userRole = 4;
m_bHasLogined = false;
m_bHasLoginRoom = false;
m_bLogicServerConnected = false;
m_bRawSockConnected = false;
m_TempRecvBuf = new char[BUF_LEN];
memset(m_strHeadImg,0,sizeof(m_strHeadImg));
memset(m_strUserAccount,0,sizeof(m_strUserAccount));
memset(m_strUserName,0,sizeof(m_strUserName));
memset(m_strServer,0,sizeof(m_strServer));
memset(m_TempRecvBuf,0,sizeof(m_TempRecvBuf));
memset(m_strLogin,0,sizeof(m_strLogin));
m_recvLen = 0;
m_pSock = NULL;
m_mySessionID = 0;
//m_pLogFile = new FileStream("c:\\command.txt");
m_bStop = false;
m_pIMeetingEvent = NULL;
}
MeetingConnImpl::~MeetingConnImpl(void)
{
if(m_TempRecvBuf)
{
delete []m_TempRecvBuf;
m_TempRecvBuf = NULL;
}
//释放这个指针的时候要判断线程是否已经结束了
while(thread.isRunning())
{
Thread::sleep(10);
}
if(m_pSock)
{
delete m_pSock;
m_pSock = NULL;
}
}
void MeetingConnImpl::ReConnect()
{
if(thread.isRunning()||m_bRawSockConnected == true || m_bLogicServerConnected == true ||m_bHasLogined == true )
return;
m_pSock = new StreamSocket();
try
{
if(m_pSock)
{
m_pSock->connectNB(SocketAddress(m_strServer));
}
}
catch (ConnectionRefusedException&)
{
m_pIMeetingEvent->OnNetEvent(NET_EVENT_TIME_OUT,"can't connect to login server");
return ;
}
catch(NetException&)
{
m_pIMeetingEvent->OnNetEvent(NET_EVENT_TIME_OUT,"can't connect to login server");
return ;
}
catch(IOException&)
{
m_pIMeetingEvent->OnNetEvent(NET_EVENT_TIME_OUT,"can't connect to login server");
return ;
}
/* 启动信令接收线程 */
m_bStop = false;
thread.start(*this);
}
long filesize(FILE*stream)
{
long curpos,length;
curpos=ftell(stream);
fseek(stream,0L,SEEK_END);
length=ftell(stream);
fseek(stream,curpos,SEEK_SET);
return length;
}
bool MeetingConnImpl::Connect(char* strServer,IMeetingFrameEvent* pIMeetingEvent)
{
if(thread.isRunning())
return false;
m_bRawSockConnected = false;
if(pIMeetingEvent == NULL)
return false;
m_pIMeetingEvent = pIMeetingEvent;
if(NULL != m_pSock)
{
m_pSock->close();
delete m_pSock;
m_pSock = NULL;
}
m_pSock = new StreamSocket();
strcpy(m_strServer,strServer);
try
{
if(m_pSock)
{
m_pSock->connectNB(SocketAddress(strServer));
}
}
catch (ConnectionRefusedException&)
{
m_pIMeetingEvent->OnNetEvent(NET_EVENT_TIME_OUT,"can't connect to login server");
goto OnError;
}
catch(NetException&)
{
m_pIMeetingEvent->OnNetEvent(NET_EVENT_TIME_OUT,"can't connect to login server");
goto OnError;
}
catch(IOException&)
{
m_pIMeetingEvent->OnNetEvent(NET_EVENT_TIME_OUT,"can't connect to login server");
goto OnError;
}
/* 启动信令接收线程 */
m_bStop = false;
thread.start(*this);
return true;
OnError:
m_pSock->close();
delete m_pSock;
m_pSock = NULL;
return false;
}
void MeetingConnImpl::Release()
{
m_bStop = true;
m_bHasLogined = false;
m_bHasLoginRoom = false;
thread.join();
m_bLogicServerConnected = m_bRawSockConnected = false;
}
//用房间号登录,呢称,房间密码,终端类型
void MeetingConnImpl::LoginWithRoomNo(const char* strUserName, const char* strRoomPassword,uint32_t clientType,uint32_t u32RoomID)
{
//md5
m_tryLoginCount = 0;
m_lastLoginTime = Timestamp();
m_loginType = 2;
m_userRole = 2;
m_ulRoomID = u32RoomID;
//MD5Engine engine;
//engine.update(strRoomPassword);
strcpy(m_strUserAccount, strUserName);
strcpy(m_strUserName, strUserName);
strcpy(m_strPassword,strRoomPassword);
m_clientType = clientType;
if(m_bHasLogined == true)
return;
m_bHasLogined = false;
char utfStr[1024]={0};
sprintf(utfStr, "{\"cmd\":\"loginWithRoomNo\",\"roomID\":%u,\"password\":\"%s\",\"client_type\":%u}\r\n",u32RoomID,m_strPassword,clientType);
strcpy(m_strLogin,utfStr);
}
//用户帐号,密码登入
void MeetingConnImpl::Login(const char* strUserName, const char* strPassword,uint32_t clientType,bool bEncrypt)
{
/*
std::string strTest="[18621066138,18979080001]";
std::string strTest2 = strTest;
Parser parser;
Var result;
try
{
result = parser.parse(strTest2);
Poco::JSON::Array::Ptr array =result.extract<Poco::JSON::Array::Ptr>();
for(Poco::Int32 i = 0; i<array->size();i++)
{
Var item = array->get(i);
unsigned long long phone = item;
cout<<phone;
}
}
catch(JSONException& jsone)
{
//printf("%s \n",buffer);
std::cout << jsone.message() << std::endl;
return;
}
*/
//md5
m_loginType = 1;
m_userRole = 2;
m_tryLoginCount = 0;
m_lastLoginTime = Timestamp();
if(bEncrypt)
{
MD5Engine engine;
engine.update(strPassword);
strcpy(m_strUserAccount, strUserName);
strcpy(m_strPassword, DigestEngine::digestToHex(engine.digest()).data());
}
else
{
strcpy(m_strUserAccount, strUserName);
strcpy(m_strPassword,strPassword);
}
m_clientType = clientType;
if(m_bHasLogined == true)
return;
m_bHasLogined = false;
char utfStr[1024]={0};
sprintf(utfStr, "{\"cmd\":\"login\",\"account\":\"%s\",\"password\":\"%s\",\"client_type\":%u}\r\n",m_strUserAccount,m_strPassword,clientType);
strcpy(m_strLogin,utfStr);
}
//登出
void MeetingConnImpl::Logout()
{
char * msg = new char[64];
strcpy(msg,"{\"cmd\":\"logout\"}\r\n");
FastMutex::ScopedLock autoLock(m_lock);
m_sendBufList.push(msg);
}
void MeetingConnImpl::SendTextMsg(uint64_t toSessionID,char * msg,char* fontName,uint32_t fontSize,uint32_t color,uint64_t flag)
{
std::string msg2 = HTTPCookie::escape(msg);
std::string strFont = HTTPCookie::escape(fontName);
char *utfStr = new char[1024*6];
if(fontName == NULL)
fontName = "";
sprintf(utfStr,"{\"cmd\":\"textChat\",\"fromSessionID\":%I64u,\"fromUserName\":\"%s\",\"toSessionID\":%I64u,\"msg\":\"%s\",\"fontSize\":%u,\"color\":%u,\"flag\":%I64u,\"fontName\":\"%s\",\"roomID\":%u}\r\n",
m_mySessionID,m_strUserName,toSessionID,msg2.data(),fontSize,color,flag,strFont.data(),m_ulRoomID);
FastMutex::ScopedLock autoLock(m_lock);
PMsgItem pMsgItem = new MsgItem();
pMsgItem->ulUserID = toSessionID;
pMsgItem->font_size = fontSize;
pMsgItem->color = color;
pMsgItem->fontFlat = flag;
strcpy(pMsgItem->userName,this->m_strUserName);
strcpy(pMsgItem->msg,msg);
strcpy(pMsgItem->familly,fontName);
//插入到列表
m_chatMsgList.push_back(vector<PMsgItem>::value_type(pMsgItem));
m_sendBufList.push(utfStr);
}
//是否连接
bool MeetingConnImpl::IsConnect()
{
return thread.isRunning();
}
//得到房间列表
void MeetingConnImpl::GetRoomList()
{
char *utfStr = new char[64];
sprintf(utfStr, "{\"cmd\":\"getRoomList\"}\r\n");
FastMutex::ScopedLock autoLock(m_lock);
m_sendBufList.push(utfStr);
}
//得到房间用户列表
void MeetingConnImpl::GetRoomUserList(uint32_t roomID)
{
char *utfStr = new char[64];
sprintf(utfStr, "{\"cmd\":\"getRoomUserList\",\"roomID\":%u}\r\n",roomID);
FastMutex::ScopedLock autoLock(m_lock);
m_sendBufList.push(utfStr);
}
//得到房间信息
void MeetingConnImpl::GetRoomInfo(uint32_t roomID)
{
char *utfStr = new char[128];
sprintf(utfStr, "{\"cmd\":\"getRoomInfo\",\"roomID\":%u}\r\n",roomID);
FastMutex::ScopedLock autoLock(m_lock);
m_sendBufList.push(utfStr);
}
//得到聊天历史记录
void MeetingConnImpl::GetChatHistory(uint32_t roomID)
{
char *utfStr = new char[128];
sprintf(utfStr, "{
白话Learning
- 粉丝: 4719
- 资源: 3081
最新资源
- 基于Java实现的MapReduce分布式计算框架设计源码
- Qwen2.5 Technical Report 详细技术报告
- 基于ThinkGms v2.0.1框架的旧快马配送系统设计源码
- 基于Java编程语言的俄罗斯方块游戏设计源码
- 套膜封切机工程图机械结构设计图纸和其它技术资料和技术方案非常好100%好用.zip
- 小麦联合收割机工程图机械结构设计图纸和其它技术资料和技术方案非常好100%好用.zip
- 小型全自动卷烟机构图纸工程图机械结构设计图纸和其它技术资料和技术方案非常好100%好用.zip
- 线体牵引力测试机(含bom)sw17可编辑工程图机械结构设计图纸和其它技术资料和技术方案非常好100%好用.zip
- 前端入门day1的文件记录
- 型钢校正机矫直机工程图机械结构设计图纸和其它技术资料和技术方案非常好100%好用.zip
- 旋转停车系统工程图机械结构设计图纸和其它技术资料和技术方案非常好100%好用.zip
- 数仓构造与多维分析大作业
- 【图像融合】基于matlab结合contourlet与压缩感知图像融合【含Matlab源码 9741期】.zip
- 【坐标转换】基于matlab GUI大地坐标和空间直角坐标相互转换【含Matlab源码 9227期】.zip
- 【迷宫路径规划】基于matlab SARSA和强化学习迷宫路径规划解决迷宫问题【含Matlab源码 8857期】.mp4
- 【语音去噪】基于matlab GUI切比雪夫+椭圆形低通滤波器语音去噪【含Matlab源码 2198期】.mp4
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈