UserSocket.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #pragma once
  2. #include <stdint.h>
  3. #include <string>
  4. #include <unordered_map>
  5. #include "IOSocket.h"
  6. #include <vector>
  7. #include "../common/comm.h"
  8. template<typename T>
  9. class FNRedirector
  10. {
  11. public:
  12. typedef void (T::* FN)(int8_t*, int16_t Size);
  13. bool Process(T* lhs, int32_t cmd, int8_t*, int16_t);
  14. void Insert(int32_t, FN fn);
  15. private:
  16. std::unordered_map<int32_t, FN> m_fnmap;
  17. };
  18. template<typename T>
  19. bool FNRedirector<T>::Process(T* lhs, int32_t cmd, int8_t* Data, int16_t Size)
  20. {
  21. auto it = m_fnmap.find(cmd);
  22. if (it != m_fnmap.end())
  23. {
  24. (lhs->*it->second)(Data, Size);
  25. return true;
  26. }
  27. return false;
  28. }
  29. template<typename T>
  30. void FNRedirector<T>::Insert(int32_t cmd, FN fn)
  31. {
  32. m_fnmap.insert(std::make_pair(cmd, fn));
  33. }
  34. class CUserSocket :public CIOSocket
  35. {
  36. public:
  37. CUserSocket(HANDLE hCom, SOCKET s);
  38. virtual void OnCreate() override;
  39. virtual void OnRead() override;
  40. void Process(int32_t cmd, int8_t*, int32_t Size);
  41. static void InitFnDirector();
  42. virtual void OnClose() override;
  43. static FNRedirector<CUserSocket> Redirector;
  44. int32_t uid();
  45. RemoNet::UserState state();
  46. CarType car();
  47. int32_t type();
  48. std::string name();
  49. RemoNet::VideoDesc ReqVideo(int32_t peer,int32_t index);
  50. void RepVideo(int32_t peer, int32_t index, RemoNet::VideoDesc desc);
  51. void LeaveVideo(int32_t peer,EgoType type);
  52. void CloseVideo(int32_t peer, int32_t index, EgoType type);
  53. void KickOff();
  54. ULONGLONG GetTimeTick();
  55. void NotifyOffer(int32_t peer, int32_t index, const char* type, const char* sdp);
  56. void NotifyAnswer(int32_t peer, int32_t index, const char* type, const char* sdp);
  57. void NotifyCandidate(int32_t peer, int32_t index, const char* type, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid);
  58. void NotifyMoveBegin(int32_t peer, WorkArea area, int32_t no);
  59. void NotifyMoveEnd(int32_t peer, WorkArea area, int32_t no);
  60. void OnTimeout();
  61. private:
  62. void OnReqVideo(int8_t* Data, int16_t Size);
  63. void OnRepVideo(int8_t* Data, int16_t Size);
  64. //void OnCancelReq(int8_t* Data, int16_t Size);
  65. void OnOffer(int8_t* Data, int16_t Size);
  66. void OnAnswer(int8_t* Data, int16_t Size);
  67. void OnCadidate(int8_t* Data, int16_t Size);
  68. void OnSigin(int8_t* Data, int16_t Size);
  69. void OnAdd(int8_t* Data, int16_t Size);
  70. void OnRobot(int8_t* Data, int16_t Size);
  71. void OnLeave(int8_t* Data, int16_t Size);
  72. //void OnCloseVideo(int8_t* Data,int16_t Size);
  73. void OnKeepAlive(int8_t* Data, int16_t Size);
  74. void OnMoveBegin(int8_t* Data, int16_t Size);
  75. void OnMoveEnd(int8_t* Data, int16_t Size);
  76. void OnChangeState(int8_t* Data, int16_t Size);
  77. void OnSwitchDriver(int8_t* Data, int16_t Size);
  78. void OnMoveRet(int8_t* Data, int16_t Size);
  79. private:
  80. bool _loginsucc;
  81. RemoNet::UserState _state;
  82. int32_t _uid;
  83. EgoType _egoType=EgoType::None;
  84. int32_t _userpeer;
  85. CarType _car = CarType::ZJ;
  86. std::string _name;
  87. ULONGLONG TimeTick;
  88. };