socket_client.h 3.1 KB

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