123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- #define WIN32_LEAN_AND_MEAN
- #include <Windows.h>
- #include "rapidjson/rapidjson.h"
- #include "rapidjson/document.h"
- #include "rapidjson/istreamwrapper.h"
- #include "rapidjson/stringbuffer.h"
- #include "rapidjson/writer.h"
- #include "DBConnect.h"
- #include "scoped_ptr.h"
- #include "IOBuffer.h"
- #include "WebHandler.h"
- #include "WebServer.h"
- #include "UserManager.h"
- CWebHandler::CWebHandler(CWebServer* s, int32_t u, int32_t cid, websocketpp::connection_hdl hdl) :server(s), connect_hdl(hdl)
- {
- bWait = true;
- bRun = false;
- uid = u;
- companyid = cid;
- video_peer = -1;
- beat_tick = GetTickCount64();
- }
- CWebHandler::~CWebHandler()
- {
- }
- bool CWebHandler::Write(CIOBuffer* pBuffer)
- {
- try {
- server->Write(connect_hdl, (char*)pBuffer->Buffer);
- return true;
- }
- catch (std::exception& e)
- {
- }
- return false;
- }
- void CWebHandler::OnClose()
- {
- if (video_peer != -1)
- {
- CWebUserManager::GetInstance().LeavePeerVideo(video_peer, uid);
- }
- }
- void CWebHandler::OnWebReqVideo(int32_t peer, int32_t width, int32_t height, int32_t fps)
- {
- if (peer == uid) return;
- if (video_peer == -1)
- {
- auto state = CWebUserManager::GetInstance().ConnectPeerVideo(peer, uid, width, height, fps);
- if (state == VideoDesc::OK)
- video_peer = peer;
- else
- {
- rapidjson::StringBuffer strBuf;
- rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
- root.StartObject();
- root.Key("type");
- root.String(kRepVideo);
- root.Key("peer");
- root.Int(peer);
- root.Key("ret");
- if (state == VideoDesc::Busy)
- root.String("busy");
- else if (state == VideoDesc::NoFound)
- root.String("nofound");
- else if (state = VideoDesc::Reject)
- root.String("Reject");
- root.EndObject();
- CIOBuffer* pBuffer = CIOBuffer::Alloc();
- strcpy((char*)pBuffer->Buffer, strBuf.GetString());
- pBuffer->Length = strBuf.GetLength() + 1;
- Write(pBuffer);
- pBuffer->Release();
- }
- }
- else
- {
- rapidjson::StringBuffer strBuf;
- rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
- root.StartObject();
- root.Key("type");
- root.String(kRepVideo);
- root.Key("peer");
- root.Int(peer);
- root.Key("ret");
- root.String("IsVideoing");
- root.EndObject();
- CIOBuffer* pBuffer = CIOBuffer::Alloc();
- strcpy((char*)pBuffer->Buffer, strBuf.GetString());
- pBuffer->Length = strBuf.GetLength() + 1;
- Write(pBuffer);
- pBuffer->Release();
- }
- }
- void CWebHandler::OnWebRepVideo(int32_t peer, std::string& desc, int32_t width, int32_t height, int32_t fps)
- {
- VideoDesc type = VideoDesc::OK;
- if (desc == "OK")
- type = VideoDesc::OK;
- else if (desc == "busy")
- type = VideoDesc::Busy;
- else if (desc == "nofound")
- type = VideoDesc::NoFound;
- else if (desc == "IsVideoing")
- type = VideoDesc::IsVideoing;
- if (type == VideoDesc::OK)
- {
- video_peer = peer;
- }
- else
- {
- video_peer = -1;
- }
- CWebUserManager::GetInstance().ReplyPeerVideo(peer, uid, type, width, height, fps);
- }
- void CWebHandler::KickOff()
- {
- rapidjson::StringBuffer strBuf;
- rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
- root.StartObject();
- root.Key(kSessionDescriptionTypeName);
- root.String(kKickOff);
- root.EndObject();
- CIOBuffer* pBuffer = CIOBuffer::Alloc();
- strcpy((char*)pBuffer->Buffer, strBuf.GetString());
- pBuffer->Length = strBuf.GetLength() + 1;
- Write(pBuffer);
- pBuffer->Release();
- }
- websocketpp::connection_hdl CWebHandler::GetConnectHandle()
- {
- return connect_hdl;
- }
- void CWebHandler::OnWebLeave(int32_t peer)
- {
- video_peer = -1;
- CWebUserManager::GetInstance().LeavePeerVideo(peer, uid);
- }
- void CWebHandler::OnWebCancelReq(int32_t did)
- {
- if (video_peer != did) return;
- video_peer = -1;
- CWebUserManager::GetInstance().CancelReq(did, uid);
- }
- VideoDesc CWebHandler::ReqVideo(int32_t uid, int32_t width, int32_t height, int32_t fps)
- {
- if (video_peer != -1 && video_peer != uid)
- {
- return VideoDesc::Busy;
- }
- rapidjson::StringBuffer strBuf;
- rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
- root.StartObject();
- root.Key("type");
- root.String(kReqVideo);
- root.Key("peer");
- root.Int(uid);
- root.Key("width");
- root.Int(width);
- root.Key("height");
- root.Int(height);
- root.Key("fps");
- root.Int(fps);
- root.EndObject();
- CIOBuffer* pBuffer = CIOBuffer::Alloc();
- strcpy((char*)pBuffer->Buffer, strBuf.GetString());
- pBuffer->Length = strBuf.GetLength() + 1;
- Write(pBuffer);
- pBuffer->Release();
- return VideoDesc::OK;
- }
- void CWebHandler::RepVideo(int32_t peer, VideoDesc ret, int32_t width, int32_t height, int32_t fps)
- {
- if (ret != VideoDesc::OK)
- {
- video_peer = -1;
- }
- rapidjson::StringBuffer strBuf;
- rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
- root.StartObject();
- root.Key("type");
- root.String(kRepVideo);
- root.Key("peer");
- root.Int(peer);
- root.Key("width");
- root.Int(width);
- root.Key("height");
- root.Int(height);
- root.Key("fps");
- root.Int(fps);
- root.Key("ret");
- if (ret == VideoDesc::OK)
- root.String("OK");
- else if (ret == VideoDesc::Busy)
- root.String("busy");
- else if (ret == VideoDesc::Reject)
- root.String("reject");
- root.EndObject();
- CIOBuffer* pBuffer = CIOBuffer::Alloc();
- strcpy((char*)pBuffer->Buffer, strBuf.GetString());
- pBuffer->Length = strBuf.GetLength() + 1;
- Write(pBuffer);
- pBuffer->Release();
- }
- void CWebHandler::LeaveVideo(int32_t peer)
- {
- if (video_peer != peer) return;
- video_peer = -1;
- rapidjson::StringBuffer strBuf;
- rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
- root.StartObject();
- root.Key("type");
- root.String(kLeave);
- root.Key("peer");
- root.Int(uid);
- root.EndObject();
- CIOBuffer* pBuffer = CIOBuffer::Alloc();
- strcpy((char*)pBuffer->Buffer, strBuf.GetString());
- pBuffer->Length = strBuf.GetLength() + 1;
- Write(pBuffer);
- pBuffer->Release();
- }
- void CWebHandler::CancelReq(int32_t peer)
- {
- if (video_peer != peer) return;
- video_peer = -1;
- rapidjson::StringBuffer strBuf;
- rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
- root.StartObject();
- root.Key("type");
- root.String(kCancelReq);
- root.Key("peer");
- root.Int(uid);
- root.EndObject();
- CIOBuffer* pBuffer = CIOBuffer::Alloc();
- strcpy((char*)pBuffer->Buffer, strBuf.GetString());
- pBuffer->Length = strBuf.GetLength() + 1;
- Write(pBuffer);
- pBuffer->Release();
- }
-
- void CWebHandler::MessageThread()
- {
- CQPtr<CConnectionPtr<sql::Connection>> Conn = CDBConnectPool::GetInstance().QueryConnect();
- scoped_ptr<sql::Statement> stmt = (*Conn.get())->createStatement();
- char sql[1024];
- sprintf_s(sql, "select id, uid,content,create_time from user_message where uid=%d and processed=0", uid);
- std::vector<int32_t> ids;
- scoped_ptr<sql::ResultSet> resultSet = stmt->executeQuery(sql);
- while (resultSet->next())
- {
- int32_t id = resultSet->getInt(1);
- int32_t uid = resultSet->getInt(2);
- std::string content = resultSet->getString(3).c_str();
- std::string datetime = resultSet->getString(4).c_str();
- rapidjson::StringBuffer strBuf;
- rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
- root.StartObject();
- root.Key("type");
- root.String("message");
- root.Key("content");
- root.String(content.c_str());
- root.Key("date");
- root.String(datetime.c_str());
- root.EndObject();
- CIOBuffer* pBuffer = CIOBuffer::Alloc();
- strcpy((char*)pBuffer->Buffer, strBuf.GetString());
- pBuffer->Length = strBuf.GetLength() + 1;
- bool bret = Write(pBuffer);
- pBuffer->Release();
- ids.push_back(id);
- Sleep(10);
- }
- for (auto id : ids)
- {
- sprintf_s(sql, "update user_message set processed=1 where id=%d", id);
- stmt->execute(sql);
- }
- }
- void CWebHandler::OnWebHeartbeat()
- {
- beat_tick = GetTickCount64();
- }
- bool CWebHandler::Notify(const char* content)
- {
- // rapidjson::StringBuffer strBuf;
- // rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
- // root.StartObject();
- // root.Key("type");
- // root.String(kNotify);
- // root.Key("content");
- // root.String(content);
- // root.EndObject();
- CIOBuffer* pBuffer = CIOBuffer::Alloc();
- strcpy((char*)pBuffer->Buffer, content);
- pBuffer->Length = strlen(content) + 1;
- bool bret = Write(pBuffer);
- pBuffer->Release();
- return bret;
- }
-
|