user_handler.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #pragma once
  2. #include <unordered_map>
  3. #include "../common/comm.h"
  4. #include "remote_def.h"
  5. class CIOBuffer;
  6. template<typename T>
  7. class FNRedirector
  8. {
  9. public:
  10. typedef void (T::* FN)(int8_t*, int16_t Size);
  11. bool Process(T* lhs, int32_t cmd, int8_t*, int16_t);
  12. void Insert(int32_t, FN fn);
  13. private:
  14. std::unordered_map<int32_t, FN> m_fnmap;
  15. };
  16. template<typename T>
  17. bool FNRedirector<T>::Process(T* lhs, int32_t cmd, int8_t* Data, int16_t Size)
  18. {
  19. auto it = m_fnmap.find(cmd);
  20. if (it != m_fnmap.end())
  21. {
  22. (lhs->*it->second)(Data, Size);
  23. return true;
  24. }
  25. return false;
  26. }
  27. template<typename T>
  28. void FNRedirector<T>::Insert(int32_t cmd, FN fn)
  29. {
  30. m_fnmap.insert(std::make_pair(cmd, fn));
  31. }
  32. class CUserHandler
  33. {
  34. public:
  35. CUserHandler(int32_t fd);
  36. static void InitFnDirector();
  37. long long GetTimeTick();
  38. void OnTimeout();
  39. int32_t uid();
  40. int32_t Read();
  41. int32_t Fd();
  42. std::string& name();
  43. UserState state();
  44. void OnClose();
  45. void Write(CIOBuffer * pBuffer);
  46. EgoType type();
  47. void NotifyOffer(int32_t peer, int32_t index, const char* type, const char* sdp);
  48. void NotifyAnswer(int32_t peer, int32_t index, const char* type, const char* sdp);
  49. void NotifyCandidate(int32_t peer, int32_t index, const char* type, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid);
  50. void RepVideo(int32_t peer, int32_t index,remote::VideoDesc desc);
  51. remote::VideoDesc ReqVideo(int32_t peer, int32_t index);
  52. void LeaveVideo(int32_t peer,EgoType type);
  53. private:
  54. void Process(int32_t cmd, int8_t*, int32_t Size);
  55. void OnOffer(int8_t* Data, int16_t Size);
  56. void OnAnswer(int8_t* Data, int16_t Size);
  57. void OnCadidate(int8_t* Data, int16_t Size);
  58. void OnKeepAlive(int8_t* Data, int16_t Size);
  59. void OnAdd(int8_t* Data, int16_t Size);
  60. void OnRobot(int8_t* Data, int16_t Size);
  61. void OnLeave(int8_t* Data, int16_t Size);
  62. void OnReqVideo(int8_t* Data, int16_t Size);
  63. void OnRepVideo(int8_t* Data, int16_t Size);
  64. private:
  65. int32_t _fd;
  66. CIOBuffer* _readBuffer;
  67. long long _tick;
  68. int32_t _uid;
  69. static FNRedirector<CUserHandler> Redirector;
  70. bool _loginsucc=false;
  71. std::string _name;
  72. EgoType _egoType;
  73. UserState _state;
  74. int32_t _peer;
  75. };