socket_client.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. #ifdef WIN32
  3. #ifndef WIN32_LEAN_AND_MEAN
  4. #define WIN32_LEAN_AND_MEAN
  5. #endif
  6. #include <WS2tcpip.h>
  7. #include <WinSock2.h>
  8. #define socketerrno WSAGetLastError()
  9. #define SOCKET_EAGAIN_EINPROGRESS WSAEINPROGRESS
  10. #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK
  11. #ifndef _SSIZE_T_DEFINED
  12. typedef int ssize_t;
  13. #define _SSIZE_T_DEFINED
  14. #endif
  15. #ifndef _SOCKET_T_DEFINED
  16. typedef SOCKET socket_t;
  17. #define _SOCKET_T_DEFINED
  18. #endif
  19. #else
  20. #include <unistd.h>
  21. #include <arpa/inet.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <netinet/tcp.h>
  26. #include <fcntl.h>
  27. #define socketerrno errno
  28. #define SOCKET_EAGAIN_EINPROGRESS EAGAIN
  29. #define SOCKET_EWOULDBLOCK EWOULDBLOCK
  30. #define INVALID_SOCKET -1
  31. #define SOCKET_ERROR -1
  32. #ifndef _SOCKET_T_DEFINED
  33. typedef int socket_t;
  34. #define _SOCKET_T_DEFINED
  35. #endif
  36. #endif
  37. #include <thread>
  38. #include <unordered_map>
  39. #include <functional>
  40. #include "comm.h"
  41. #include "notifier.h"
  42. class CIOBuffer;
  43. class SocketClient
  44. {
  45. public:
  46. //定义成员函数指针的别名 FN,让 FnMap 中的值能够存储指向 SocketClient 类的成员函数的指针
  47. typedef void (SocketClient::* FN)(int8_t* Data, int16_t Size);
  48. SocketClient(INativeNotify* n);
  49. bool Start(const char* ip, int32_t Tcpremote_port, int32_t Tcphost_port);
  50. //bool Start(const char * ip);
  51. void Run();
  52. void Stop();
  53. void Write(CIOBuffer* pBuffer);
  54. public:
  55. #ifdef WIN32
  56. void WriteSign(const char* account, const char* password);
  57. void WriteRobotReq();
  58. void WriteVideoLeave(EgoType type, int32_t peer);
  59. #else
  60. void WriteAddRobot(std::string& serial,std::string& name,std::string url,int32_t type,int32_t car);
  61. #endif
  62. void WriteOffer(int32_t peer,int32_t index, const char* type, const char* sdp);
  63. void WriteAnswer(int32_t peer,int32_t index, const char* type, const char* sdp);
  64. void WriteCandidate(int32_t peer,int32_t index, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid);
  65. //向服务器发送视频请求
  66. void WriteVideoReq(int32_t peer, int32_t index);
  67. void WriteVideoRep(int32_t peer, RemoNet::VideoDesc desc, int32_t index);
  68. void WriteKeepAlive();
  69. //static void MessageCallback(void * user_data,const void * data,const int32_t size);
  70. void OnPeerMessage(ChannelType type,int16_t cmd,int16_t length,const void * data);
  71. private:
  72. //将车辆数据以参数形式传给FnMap中的成员函数并调用成员函数
  73. void NetProcess(int16_t cmd, int8_t* Data, int16_t Size);
  74. #ifdef WIN32
  75. void OnSigin(int8_t* Data, int16_t Size);
  76. void OnNotifyRep(int8_t* Data, int16_t Size);
  77. void OnNotifyAdd(int8_t* Data, int16_t Size);
  78. void OnNotifyDel(int8_t* Data, int16_t Size);
  79. void OnNotifyKick(int8_t* Data, int16_t Size);
  80. void OnNotifyMoveEnd(int8_t* Data, int16_t Size);
  81. void OnNotifyMoveRet(int8_t* Data, int16_t Size);
  82. #else
  83. void OnAdd(int8_t* Data, int16_t Size);
  84. void OnNotifySwitchDriver(int8_t* Data, int16_t Size);
  85. void OnNotifyReq(int8_t* Data, int16_t Size);
  86. void OnNotifyMoveBegin(int8_t* Data, int16_t Size);
  87. #endif
  88. void OnNotifyLeave(int8_t* Data, int16_t Size);
  89. void OnNotifyOffer(int8_t* Data, int16_t Size);
  90. void OnNotifyAnswer(int8_t* Data, int16_t Size);
  91. void OnNotifyCandidate(int8_t* Data, int16_t Size);
  92. void OnNotifyState(int8_t* Data, int16_t Size);
  93. #ifdef WIN32
  94. void OnRobotRep(int8_t* Data, int16_t Size);
  95. #endif
  96. private:
  97. socket_t sockfd;
  98. std::thread _thread;
  99. bool _run;
  100. bool _connected;
  101. int32_t _uid;
  102. std::string _ip;
  103. //声明一个无序映射容器 FnMap,将整型映射到FN成员函数指针类型
  104. std::unordered_map<int16_t, FN> FnMap;
  105. INativeNotify* _notify;
  106. };