123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- #pragma once
- #ifdef WIN32
- #ifndef WIN32_LEAN_AND_MEAN
- #define WIN32_LEAN_AND_MEAN
- #endif
- #include <WS2tcpip.h>
- #include <WinSock2.h>
- #define socketerrno WSAGetLastError()
- #define SOCKET_EAGAIN_EINPROGRESS WSAEINPROGRESS
- #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK
- #ifndef _SSIZE_T_DEFINED
- typedef int ssize_t;
- #define _SSIZE_T_DEFINED
- #endif
- #ifndef _SOCKET_T_DEFINED
- typedef SOCKET socket_t;
- #define _SOCKET_T_DEFINED
- #endif
- #else
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netinet/tcp.h>
- #include <fcntl.h>
- #define socketerrno errno
- #define SOCKET_EAGAIN_EINPROGRESS EAGAIN
- #define SOCKET_EWOULDBLOCK EWOULDBLOCK
- #define INVALID_SOCKET -1
- #define SOCKET_ERROR -1
- #ifndef _SOCKET_T_DEFINED
- typedef int socket_t;
- #define _SOCKET_T_DEFINED
- #endif
- #endif
- #include <thread>
- #include <unordered_map>
- #include <functional>
- #include "comm.h"
- #include "notifier.h"
- class CIOBuffer;
- class SocketClient
- {
- public:
- //定义成员函数指针的别名 FN,让 FnMap 中的值能够存储指向 SocketClient 类的成员函数的指针
- typedef void (SocketClient::* FN)(int8_t* Data, int16_t Size);
- SocketClient(INativeNotify* n);
- bool Start(const char* ip, int32_t Tcpremote_port, int32_t Tcphost_port);
- //bool Start(const char * ip);
- void Run();
- void Stop();
- void Write(CIOBuffer* pBuffer);
- public:
- #ifdef WIN32
- void WriteSign(const char* account, const char* password);
- void WriteRobotReq();
- void WriteVideoLeave(EgoType type, int32_t peer);
-
- #else
- void WriteAddRobot(std::string& serial,std::string& name,std::string url,int32_t type,int32_t car);
- #endif
- void WriteOffer(int32_t peer,int32_t index, const char* type, const char* sdp);
- void WriteAnswer(int32_t peer,int32_t index, const char* type, const char* sdp);
- void WriteCandidate(int32_t peer,int32_t index, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid);
-
- //向服务器发送视频请求
- void WriteVideoReq(int32_t peer, int32_t index);
-
- void WriteVideoRep(int32_t peer, RemoNet::VideoDesc desc, int32_t index);
- void WriteKeepAlive();
- //static void MessageCallback(void * user_data,const void * data,const int32_t size);
-
- void OnPeerMessage(ChannelType type,int16_t cmd,int16_t length,const void * data);
-
- private:
-
- //将车辆数据以参数形式传给FnMap中的成员函数并调用成员函数
- void NetProcess(int16_t cmd, int8_t* Data, int16_t Size);
- #ifdef WIN32
- void OnSigin(int8_t* Data, int16_t Size);
-
- void OnNotifyRep(int8_t* Data, int16_t Size);
- void OnNotifyAdd(int8_t* Data, int16_t Size);
- void OnNotifyDel(int8_t* Data, int16_t Size);
- void OnNotifyKick(int8_t* Data, int16_t Size);
- void OnNotifyMoveEnd(int8_t* Data, int16_t Size);
- void OnNotifyMoveRet(int8_t* Data, int16_t Size);
- #else
- void OnAdd(int8_t* Data, int16_t Size);
- void OnNotifySwitchDriver(int8_t* Data, int16_t Size);
- void OnNotifyReq(int8_t* Data, int16_t Size);
- void OnNotifyMoveBegin(int8_t* Data, int16_t Size);
- #endif
- void OnNotifyLeave(int8_t* Data, int16_t Size);
- void OnNotifyOffer(int8_t* Data, int16_t Size);
- void OnNotifyAnswer(int8_t* Data, int16_t Size);
- void OnNotifyCandidate(int8_t* Data, int16_t Size);
- void OnNotifyState(int8_t* Data, int16_t Size);
-
- #ifdef WIN32
-
- void OnRobotRep(int8_t* Data, int16_t Size);
- #endif
-
- private:
- socket_t sockfd;
- std::thread _thread;
- bool _run;
- bool _connected;
- int32_t _uid;
- std::string _ip;
-
- //声明一个无序映射容器 FnMap,将整型映射到FN成员函数指针类型
- std::unordered_map<int16_t, FN> FnMap;
- INativeNotify* _notify;
- };
|