socket_client.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. typedef void (SocketClient::* FN)(int8_t* Data, int16_t Size);
  47. SocketClient(INativeNotify* n);
  48. bool Start(const char* ip, int32_t Tcpremote_port, int32_t Tcphost_port);
  49. //bool Start(const char * ip);
  50. void Run();
  51. void Stop();
  52. void Write(CIOBuffer* pBuffer);
  53. public:
  54. #ifdef WIN32
  55. void WriteSign(const char* account, const char* password);
  56. void WriteRobotReq();
  57. void WriteVideoLeave(EgoType type, int32_t peer);
  58. #else
  59. void WriteAddRobot(std::string& serial,std::string& name,std::string url,int32_t type,int32_t car);
  60. #endif
  61. void WriteOffer(int32_t peer,int32_t index, const char* type, const char* sdp);
  62. void WriteAnswer(int32_t peer,int32_t index, const char* type, const char* sdp);
  63. void WriteCandidate(int32_t peer,int32_t index, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid);
  64. void WriteVideoReq(int32_t peer, int32_t index);
  65. void WriteVideoRep(int32_t peer, RemoNet::VideoDesc desc, int32_t index);
  66. void WriteKeepAlive();
  67. //static void MessageCallback(void * user_data,const void * data,const int32_t size);
  68. void OnPeerMessage(ChannelType type,int16_t cmd,int16_t length,const void * data);
  69. private:
  70. void NetProcess(int16_t cmd, int8_t* Data, int16_t Size);
  71. #ifdef WIN32
  72. void OnSigin(int8_t* Data, int16_t Size);
  73. void OnNotifyRep(int8_t* Data, int16_t Size);
  74. void OnNotifyAdd(int8_t* Data, int16_t Size);
  75. void OnNotifyDel(int8_t* Data, int16_t Size);
  76. void OnNotifyKick(int8_t* Data, int16_t Size);
  77. void OnNotifyMoveEnd(int8_t* Data, int16_t Size);
  78. void OnNotifyMoveRet(int8_t* Data, int16_t Size);
  79. #else
  80. void OnAdd(int8_t* Data, int16_t Size);
  81. void OnNotifySwitchDriver(int8_t* Data, int16_t Size);
  82. void OnNotifyReq(int8_t* Data, int16_t Size);
  83. void OnNotifyMoveBegin(int8_t* Data, int16_t Size);
  84. #endif
  85. void OnNotifyLeave(int8_t* Data, int16_t Size);
  86. void OnNotifyOffer(int8_t* Data, int16_t Size);
  87. void OnNotifyAnswer(int8_t* Data, int16_t Size);
  88. void OnNotifyCandidate(int8_t* Data, int16_t Size);
  89. void OnNotifyState(int8_t* Data, int16_t Size);
  90. #ifdef WIN32
  91. void OnRobotRep(int8_t* Data, int16_t Size);
  92. #endif
  93. private:
  94. socket_t sockfd;
  95. std::thread _thread;
  96. bool _run;
  97. bool _connected;
  98. int32_t _uid;
  99. std::string _ip;
  100. std::unordered_map<int16_t, FN> FnMap;
  101. INativeNotify* _notify;
  102. };