socket_client.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 Remote_port,int32_t Host_port);
  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,int32_t type);
  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. #else
  77. void OnAdd(int8_t* Data, int16_t Size);
  78. void OnNotifyReq(int8_t* Data, int16_t Size);
  79. #endif
  80. void OnNotifyLeave(int8_t* Data, int16_t Size);
  81. void OnNotifyOffer(int8_t* Data, int16_t Size);
  82. void OnNotifyAnswer(int8_t* Data, int16_t Size);
  83. void OnNotifyCandidate(int8_t* Data, int16_t Size);
  84. #ifdef WIN32
  85. void OnRobotRep(int8_t* Data, int16_t Size);
  86. #endif
  87. private:
  88. socket_t sockfd;
  89. std::thread _thread;
  90. bool _run;
  91. bool _connected;
  92. int32_t _uid;
  93. std::string _ip;
  94. std::unordered_map<int16_t, FN> FnMap;
  95. INativeNotify* _notify;
  96. int32_t m_Remote_port;
  97. };