WebHandler.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #define WIN32_LEAN_AND_MEAN
  2. #include <Windows.h>
  3. #include "rapidjson/rapidjson.h"
  4. #include "rapidjson/document.h"
  5. #include "rapidjson/istreamwrapper.h"
  6. #include "rapidjson/stringbuffer.h"
  7. #include "rapidjson/writer.h"
  8. #include "DBConnect.h"
  9. #include "scoped_ptr.h"
  10. #include "IOBuffer.h"
  11. #include "WebHandler.h"
  12. #include "WebServer.h"
  13. #include "UserManager.h"
  14. CWebHandler::CWebHandler(CWebServer* s, int32_t u, int32_t cid, websocketpp::connection_hdl hdl) :server(s), connect_hdl(hdl)
  15. {
  16. bWait = true;
  17. bRun = false;
  18. uid = u;
  19. companyid = cid;
  20. video_peer = -1;
  21. beat_tick = GetTickCount64();
  22. }
  23. CWebHandler::~CWebHandler()
  24. {
  25. }
  26. bool CWebHandler::Write(CIOBuffer* pBuffer)
  27. {
  28. try {
  29. server->Write(connect_hdl, (char*)pBuffer->Buffer);
  30. return true;
  31. }
  32. catch (std::exception& e)
  33. {
  34. }
  35. return false;
  36. }
  37. void CWebHandler::OnClose()
  38. {
  39. if (video_peer != -1)
  40. {
  41. CWebUserManager::GetInstance().LeavePeerVideo(video_peer, uid);
  42. }
  43. }
  44. void CWebHandler::OnWebReqVideo(int32_t peer, int32_t width, int32_t height, int32_t fps)
  45. {
  46. if (peer == uid) return;
  47. if (video_peer == -1)
  48. {
  49. auto state = CWebUserManager::GetInstance().ConnectPeerVideo(peer, uid, width, height, fps);
  50. if (state == VideoDesc::OK)
  51. video_peer = peer;
  52. else
  53. {
  54. rapidjson::StringBuffer strBuf;
  55. rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
  56. root.StartObject();
  57. root.Key("type");
  58. root.String(kRepVideo);
  59. root.Key("peer");
  60. root.Int(peer);
  61. root.Key("ret");
  62. if (state == VideoDesc::Busy)
  63. root.String("busy");
  64. else if (state == VideoDesc::NoFound)
  65. root.String("nofound");
  66. else if (state = VideoDesc::Reject)
  67. root.String("Reject");
  68. root.EndObject();
  69. CIOBuffer* pBuffer = CIOBuffer::Alloc();
  70. strcpy((char*)pBuffer->Buffer, strBuf.GetString());
  71. pBuffer->Length = strBuf.GetLength() + 1;
  72. Write(pBuffer);
  73. pBuffer->Release();
  74. }
  75. }
  76. else
  77. {
  78. rapidjson::StringBuffer strBuf;
  79. rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
  80. root.StartObject();
  81. root.Key("type");
  82. root.String(kRepVideo);
  83. root.Key("peer");
  84. root.Int(peer);
  85. root.Key("ret");
  86. root.String("IsVideoing");
  87. root.EndObject();
  88. CIOBuffer* pBuffer = CIOBuffer::Alloc();
  89. strcpy((char*)pBuffer->Buffer, strBuf.GetString());
  90. pBuffer->Length = strBuf.GetLength() + 1;
  91. Write(pBuffer);
  92. pBuffer->Release();
  93. }
  94. }
  95. void CWebHandler::OnWebRepVideo(int32_t peer, std::string& desc, int32_t width, int32_t height, int32_t fps)
  96. {
  97. VideoDesc type = VideoDesc::OK;
  98. if (desc == "OK")
  99. type = VideoDesc::OK;
  100. else if (desc == "busy")
  101. type = VideoDesc::Busy;
  102. else if (desc == "nofound")
  103. type = VideoDesc::NoFound;
  104. else if (desc == "IsVideoing")
  105. type = VideoDesc::IsVideoing;
  106. if (type == VideoDesc::OK)
  107. {
  108. video_peer = peer;
  109. }
  110. else
  111. {
  112. video_peer = -1;
  113. }
  114. CWebUserManager::GetInstance().ReplyPeerVideo(peer, uid, type, width, height, fps);
  115. }
  116. void CWebHandler::KickOff()
  117. {
  118. rapidjson::StringBuffer strBuf;
  119. rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
  120. root.StartObject();
  121. root.Key(kSessionDescriptionTypeName);
  122. root.String(kKickOff);
  123. root.EndObject();
  124. CIOBuffer* pBuffer = CIOBuffer::Alloc();
  125. strcpy((char*)pBuffer->Buffer, strBuf.GetString());
  126. pBuffer->Length = strBuf.GetLength() + 1;
  127. Write(pBuffer);
  128. pBuffer->Release();
  129. }
  130. websocketpp::connection_hdl CWebHandler::GetConnectHandle()
  131. {
  132. return connect_hdl;
  133. }
  134. void CWebHandler::OnWebLeave(int32_t peer)
  135. {
  136. video_peer = -1;
  137. CWebUserManager::GetInstance().LeavePeerVideo(peer, uid);
  138. }
  139. void CWebHandler::OnWebCancelReq(int32_t did)
  140. {
  141. if (video_peer != did) return;
  142. video_peer = -1;
  143. CWebUserManager::GetInstance().CancelReq(did, uid);
  144. }
  145. VideoDesc CWebHandler::ReqVideo(int32_t uid, int32_t width, int32_t height, int32_t fps)
  146. {
  147. if (video_peer != -1 && video_peer != uid)
  148. {
  149. return VideoDesc::Busy;
  150. }
  151. rapidjson::StringBuffer strBuf;
  152. rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
  153. root.StartObject();
  154. root.Key("type");
  155. root.String(kReqVideo);
  156. root.Key("peer");
  157. root.Int(uid);
  158. root.Key("width");
  159. root.Int(width);
  160. root.Key("height");
  161. root.Int(height);
  162. root.Key("fps");
  163. root.Int(fps);
  164. root.EndObject();
  165. CIOBuffer* pBuffer = CIOBuffer::Alloc();
  166. strcpy((char*)pBuffer->Buffer, strBuf.GetString());
  167. pBuffer->Length = strBuf.GetLength() + 1;
  168. Write(pBuffer);
  169. pBuffer->Release();
  170. return VideoDesc::OK;
  171. }
  172. void CWebHandler::RepVideo(int32_t peer, VideoDesc ret, int32_t width, int32_t height, int32_t fps)
  173. {
  174. if (ret != VideoDesc::OK)
  175. {
  176. video_peer = -1;
  177. }
  178. rapidjson::StringBuffer strBuf;
  179. rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
  180. root.StartObject();
  181. root.Key("type");
  182. root.String(kRepVideo);
  183. root.Key("peer");
  184. root.Int(peer);
  185. root.Key("width");
  186. root.Int(width);
  187. root.Key("height");
  188. root.Int(height);
  189. root.Key("fps");
  190. root.Int(fps);
  191. root.Key("ret");
  192. if (ret == VideoDesc::OK)
  193. root.String("OK");
  194. else if (ret == VideoDesc::Busy)
  195. root.String("busy");
  196. else if (ret == VideoDesc::Reject)
  197. root.String("reject");
  198. root.EndObject();
  199. CIOBuffer* pBuffer = CIOBuffer::Alloc();
  200. strcpy((char*)pBuffer->Buffer, strBuf.GetString());
  201. pBuffer->Length = strBuf.GetLength() + 1;
  202. Write(pBuffer);
  203. pBuffer->Release();
  204. }
  205. void CWebHandler::LeaveVideo(int32_t peer)
  206. {
  207. if (video_peer != peer) return;
  208. video_peer = -1;
  209. rapidjson::StringBuffer strBuf;
  210. rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
  211. root.StartObject();
  212. root.Key("type");
  213. root.String(kLeave);
  214. root.Key("peer");
  215. root.Int(uid);
  216. root.EndObject();
  217. CIOBuffer* pBuffer = CIOBuffer::Alloc();
  218. strcpy((char*)pBuffer->Buffer, strBuf.GetString());
  219. pBuffer->Length = strBuf.GetLength() + 1;
  220. Write(pBuffer);
  221. pBuffer->Release();
  222. }
  223. void CWebHandler::CancelReq(int32_t peer)
  224. {
  225. if (video_peer != peer) return;
  226. video_peer = -1;
  227. rapidjson::StringBuffer strBuf;
  228. rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
  229. root.StartObject();
  230. root.Key("type");
  231. root.String(kCancelReq);
  232. root.Key("peer");
  233. root.Int(uid);
  234. root.EndObject();
  235. CIOBuffer* pBuffer = CIOBuffer::Alloc();
  236. strcpy((char*)pBuffer->Buffer, strBuf.GetString());
  237. pBuffer->Length = strBuf.GetLength() + 1;
  238. Write(pBuffer);
  239. pBuffer->Release();
  240. }
  241. void CWebHandler::MessageThread()
  242. {
  243. CQPtr<CConnectionPtr<sql::Connection>> Conn = CDBConnectPool::GetInstance().QueryConnect();
  244. scoped_ptr<sql::Statement> stmt = (*Conn.get())->createStatement();
  245. char sql[1024];
  246. sprintf_s(sql, "select id, uid,content,create_time from user_message where uid=%d and processed=0", uid);
  247. std::vector<int32_t> ids;
  248. scoped_ptr<sql::ResultSet> resultSet = stmt->executeQuery(sql);
  249. while (resultSet->next())
  250. {
  251. int32_t id = resultSet->getInt(1);
  252. int32_t uid = resultSet->getInt(2);
  253. std::string content = resultSet->getString(3).c_str();
  254. std::string datetime = resultSet->getString(4).c_str();
  255. rapidjson::StringBuffer strBuf;
  256. rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
  257. root.StartObject();
  258. root.Key("type");
  259. root.String("message");
  260. root.Key("content");
  261. root.String(content.c_str());
  262. root.Key("date");
  263. root.String(datetime.c_str());
  264. root.EndObject();
  265. CIOBuffer* pBuffer = CIOBuffer::Alloc();
  266. strcpy((char*)pBuffer->Buffer, strBuf.GetString());
  267. pBuffer->Length = strBuf.GetLength() + 1;
  268. bool bret = Write(pBuffer);
  269. pBuffer->Release();
  270. ids.push_back(id);
  271. Sleep(10);
  272. }
  273. for (auto id : ids)
  274. {
  275. sprintf_s(sql, "update user_message set processed=1 where id=%d", id);
  276. stmt->execute(sql);
  277. }
  278. }
  279. void CWebHandler::OnWebHeartbeat()
  280. {
  281. beat_tick = GetTickCount64();
  282. }
  283. bool CWebHandler::Notify(const char* content)
  284. {
  285. // rapidjson::StringBuffer strBuf;
  286. // rapidjson::Writer<rapidjson::StringBuffer> root(strBuf);
  287. // root.StartObject();
  288. // root.Key("type");
  289. // root.String(kNotify);
  290. // root.Key("content");
  291. // root.String(content);
  292. // root.EndObject();
  293. CIOBuffer* pBuffer = CIOBuffer::Alloc();
  294. strcpy((char*)pBuffer->Buffer, content);
  295. pBuffer->Length = strlen(content) + 1;
  296. bool bret = Write(pBuffer);
  297. pBuffer->Release();
  298. return bret;
  299. }