📄 communication.h~
字号:
#ifndef H_COMMUNICATION_#define H_COMMUNICATION_/************************************************************************************************************************************************ communication.cpp and .h for CCommunication.************************************************************************************************************************************************/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <termios.h>#include <errno.h>#include <string.h>#include <ctype.h>#include <netdb.h>#include <error.h>#include <sys/socket.h>#include <arpa/inet.h>#include <sys/time.h>#include <sys/select.h>extern void g_errorlog(char *msg);extern char * g_errorget();extern void error_log(char *message);typedef enum enuCHECKTYPE{ N = 0, O = 1, E = 2}paritytype_t;class CComdev{ public: CComdev(); CComdev(int comPort, speed_t initSpeed, int databit, int stopbit, paritytype_t parityType, unsigned int delay); ~CComdev(); static ssize_t ReadData(int comfd, void *buffer, size_t num); ssize_t ReadData(void *buffer, size_t num); static ssize_t WriteData(int comfd, void *buffer, size_t num); ssize_t WriteData(void *buffer, size_t num); static int OpenCom(int comPort, speed_t initSpeed, int dataBit, int stopBit, paritytype_t parityType, unsigned int delay); static int OpenCom(char *com_filename, speed_t initSpeed, int dataBit, int stopBit, paritytype_t parityType, unsigned int delay); bool Open(int comPort, speed_t initSpeed, int dataBit, int stopBit, paritytype_t parityType, unsigned int delay); bool Open(char *com_filename, speed_t initSpeed, int dataBit, int stopBit, paritytype_t parityType, unsigned int delay); static bool SetRaw(int comfd, bool isRaw); bool SetRaw(bool isRaw); static bool SetTime(int comfd, unsigned int delay); bool SetTime(unsigned int delay); static bool SetMin(int comfd, unsigned int min); bool SetMin(unsigned int min); static bool SetSpeed(int comfd, speed_t speed); bool SetSpeed(speed_t speed); static bool SetDatabit(int comfd, int dataBit); bool SetDatabit(int dataBit); static bool SetStopbit(int comfd, int stopBit); bool SetStopbit(int stopBit); static bool SetParity(int comfd, paritytype_t type); bool SetParity(paritytype_t type); static bool SetOption(int comfd, struct termios *p_option); bool SetOption(struct termios *p_option); static bool GetOption(int comfd, struct termios *p_option); bool GetOption(struct termios *p_option); static bool CloseCom(int& comfd); bool CloseCom(); private: int com_fd;};typedef enum enuHAFORMAT{ NUMDOTADDR, DOMAINADDR}HAFormat;extern int errno; extern int net_error;extern int net_error_log(const char *error_msg1 = NULL, const char *error_msg2 = NULL, const int error_code = 0);typedef int int_socket;class CSocket{ public: typedef enum enuErrorCode { NOT_OPENED_CODE = 0x0001, OPENED_CODE = 0x0002 }enuErrorCode_t; typedef enum enuProtocol { TCP = SOCK_STREAM, UDP = SOCK_DGRAM, RAW = SOCK_RAW }enuProtocol_t; typedef enum enuFlags { OOB = MSG_OOB, DONTROUTE = MSG_DONTROUTE, DONTWAIT = MSG_DONTWAIT, NOSIGNAL = MSG_NOSIGNAL, PEEK = MSG_PEEK, WAITALL = MSG_WAITALL, TRUNC = MSG_TRUNC }enuFlags_t; typedef enum enuShutdown { READ = SHUT_RD, WRITE = SHUT_WR, READ_WRITE = SHUT_RDWR }enuShutdown_t; CSocket(); CSocket(int_socket sockfd); explicit CSocket(enuProtocol protocol); ~CSocket(); static int_socket OpenSocket(enuProtocol protocol); bool Open(enuProtocol protocol); static void Close(int_socket& mysocket); void Close(); int_socket GetDescriptor() { return socketfd;} static bool IsOpened(int_socket mysock); bool IsOpened(); static bool Bind(int_socket mysock, const sockaddr_in& addr); bool Bind(const sockaddr_in& addr); static bool Connect(int_socket mysock, const sockaddr_in& addr); bool Connect(const sockaddr_in& addr); static ssize_t Send(int_socket mysock, const void * data, size_t size, unsigned int timeout, int flags = 0); ssize_t Send(const void * data, size_t size, unsigned int timeout, int flags = 0); static ssize_t Sendto(int_socket mysock, const sockaddr_in& addr, const void * data, size_t size, unsigned int timeout, int flags = 0); ssize_t Sendto(const sockaddr_in& addr, const void * data, size_t size, unsigned int timeout, int flags = 0); static ssize_t Recv(int_socket mysock, void *data, size_t size, unsigned int timeout, int flags = 0); ssize_t Recv(void * data, size_t size, unsigned int timeout, int flags = 0); static ssize_t Recvfrom(int_socket mysock, sockaddr_in& addr, void * data, size_t size, unsigned int timeout, int flags = 0); ssize_t Recvfrom(sockaddr_in& addr, void * data, size_t size, unsigned int timeout, int flags = 0); static bool Listen(int_socket mysock, int queuelen); bool Listen(int queuelen); static int_socket Accept(int_socket mysock, struct sockaddr_in* addr); int_socket Accept(struct sockaddr_in* addr); static bool GetSockname(int_socket mysock, struct sockaddr_in *localaddr, socklen_t *addrlen); bool GetSockname(struct sockaddr_in *localaddr, socklen_t *addrlen); static bool GetPeername(int_socket mysock, struct sockaddr_in *peeraddr, socklen_t *addrlen); bool GetPeername(struct sockaddr_in *peeraddr, socklen_t *addrlen); static void Shutdown(int_socket mysock, enuShutdown how); void Shutdown(enuShutdown how); static bool Wait(int_socket mysock, bool & read, bool &write, bool &exception, int seconds = 0, int useconds = 0); bool Wait(bool& read, bool& write, bool& exception, int seconds = 0, int useconds = 0); private: int_socket socketfd;};class CClient{ public: CClient(CSocket::enuProtocol_t initProtocol, HAFormat initFormat, const char *initServername, int initServerport); CClient(int_socket initSocketfd); ~CClient(); void Start(char *servername = NULL); void Shutdown(); void Close(); bool SetServer(HAFormat initFormat, const char *initServername, int initServerport); ssize_t Send(const void * data, size_t size, unsigned int timeout, int flag = 0); ssize_t Recv(void *data, size_t size, unsigned int timeout, int flag = 0); int_socket GetSocketfd(); bool IsTCP() {return is_tcp;} private: CSocket my_sock; struct sockaddr_in server_addr; struct sockaddr_in my_addr; bool is_tcp;};class CServer{ public: CServer(CSocket::enuProtocol_t initProtocol, int initServerPort, int initQueueLen); ~CServer(); bool IsTCP() {return this->is_tcp;} bool Start(); bool Close(); ssize_t Send(const void * data, size_t size, unsigned int timeout, int flag = 0); ssize_t Recv(void *data, size_t size, unsigned int timeout, int flag = 0); int_socket Accept(); private: CSocket server_sock; struct sockaddr_in server_addr; struct sockaddr_in client_addr; bool is_tcp;};extern void communi_error(char *msg);typedef enum tagCOMMUNICATIONTYPE{ NIL = 0, NET = 4, NETCLIENT = 2, NETSERVER = 3, COM = 1}communication_t;typedef union uniREALDEVTYPE{ CSocket *p_net; CClient *p_netclient; CServer *p_netserver; CComdev *p_com;}realdev_t;class CCommunication{ public: CCommunication(); CCommunication(communication_t iType, int realdev_fd); CCommunication(communication_t iType, int iPortno, speed_t iSpeed, unsigned iDataBits, unsigned int iStopBit, paritytype_t iParity, char *iMyAddress, CSocket::enuProtocol_t iProtocol, char *iServerAddress, int iServerPort/*, bool iIsClient*/); ~CCommunication(); bool Open(communication_t iType, int iPortno, speed_t iSpeed, unsigned iDataBits, unsigned int iStopBit, paritytype_t iParity, char *iMyAddress, CSocket::enuProtocol_t iProtocol, char *iServerAddress, int iServerPort/*, bool iIsClient*/); ssize_t RecvData(void *buffer, size_t size, unsigned int timeout); bool RecvData(void *buffer, int& p_size, unsigned int timeout); ssize_t SendData(void *buffer, size_t size, unsigned int timeout); bool SendData(void *buffer, int&p_size, unsigned int timeout); CCommunication * Accept(); communication_t GetType(); bool Close(); private: communication_t type; realdev_t realdev; };#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -