📄 target_generic_network.h
字号:
#define TARGET_NATIVE_NETWORK_SOCKET_BIND(socketDescriptor,address,port,result) \ do { \ struct sockaddr_in __socketAddress; \ \ memset(&__socketAddress,0,sizeof(__socketAddress)); \ __socketAddress.sin_family = AF_INET; \ __socketAddress.sin_addr.s_addr = htonl(address); \ __socketAddress.sin_port = htons(((short)port)); \ \ result=(bind(socketDescriptor,(struct sockaddr*)&__socketAddress,sizeof(__socketAddress))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_SOCKET_LISTEN* Purpose : listen socket* Input : socketDescriptor - socket descriptor* maxQueueLength - max. number of pending connections* Output : result - TARGET_NATIVE_OK if no error occurred, * TARGET_NATIVE_ERROR otherwise* Return : -* Side-effect: unknown* Notes : -\***********************************************************************//* XXX ??? address in network byte order? */#ifndef TARGET_NATIVE_NETWORK_SOCKET_LISTEN #include <sys/socket.h> #define TARGET_NATIVE_NETWORK_SOCKET_LISTEN(socketDescriptor,maxQueueLength,result) \ do { \ result=(listen(socketDescriptor,maxQueueLength)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_SOCKET_ACCEPT* Purpose : accept socket* Input : socketDescriptor - socket descriptor* Output : result - TARGET_NATIVE_OK if no error occurred, * TARGET_NATIVE_ERROR otherwise* Return : -* Side-effect: unknown* Notes : -\***********************************************************************//* XXX ??? address in network byte order? */#ifndef TARGET_NATIVE_NETWORK_SOCKET_ACCEPT #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define TARGET_NATIVE_NETWORK_SOCKET_ACCEPT(socketDescriptor,newSocketDescriptor,result) \ do { \ struct sockaddr_in __socketAddress; \ socklen_t __socketAddressLength; \ \ memset(&__socketAddress,0,sizeof(__socketAddress)); \ __socketAddressLength=sizeof(__socketAddress); \ newSocketDescriptor=accept(socketDescriptor,(struct sockaddr*)&__socketAddress,&__socketAddressLength); \ result=(newSocketDescriptor!=-1)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_SOCKET_GET_LOCAL_INFO* Purpose : get local socket data info* Input : socketDescriptor - socket descriptor* Output : localAddress - local address (NOT in network byte order!)* localPort - local port number (NOT in network byte order!)* result - TARGET_NATIVE_OK if no error occurred, * TARGET_NATIVE_ERROR otherwise* Return : -* Side-effect: unknown* Notes : -\***********************************************************************/#ifndef TARGET_NATIVE_NETWORK_SOCKET_GET_LOCAL_INFO #include <sys/socket.h> #include <netinet/in.h> #define TARGET_NATIVE_NETWORK_SOCKET_GET_LOCAL_INFO(socketDescriptor,localAddress,localPort,result) \ do { \ struct sockaddr_in __socketAddress; \ socklen_t __socketAddressLength; \ \ localAddress=0; \ localPort =0; \ \ __socketAddressLength=sizeof(__socketAddress); \ result=(getsockname(socketDescriptor,(struct sockaddr*)&__socketAddress,&__socketAddressLength)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \ if (result==TARGET_NATIVE_OK) \ { \ localAddress=ntohl(__socketAddress.sin_addr.s_addr); \ localPort =ntohs(__socketAddress.sin_port); \ } \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_SOCKET_GET_REMOTE_INFO* Purpose : get remote socket data info* Input : socketDescriptor - socket descriptor* Output : remoteAddress - remote address (NOT in network byte order!)* remotePort - remote port number (NOT in network byte order!)* : result - TARGET_NATIVE_OK if no error occurred, * TARGET_NATIVE_ERROR otherwise* Return : -* Side-effect: unknown* Notes : -\***********************************************************************/#ifndef TARGET_NATIVE_NETWORK_SOCKET_GET_REMOTE_INFO #include <sys/socket.h> #include <netinet/in.h> #define TARGET_NATIVE_NETWORK_SOCKET_GET_REMOTE_INFO(socketDescriptor,remoteAddress,remotePort,result) \ do { \ struct sockaddr_in __socketAddress; \ socklen_t __socketAddressLength; \ \ remoteAddress=0; \ remotePort =0; \ \ __socketAddressLength=sizeof(__socketAddress); \ result=(getpeername(socketDescriptor,(struct sockaddr*)&__socketAddress,&__socketAddressLength)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \ if (result==TARGET_NATIVE_OK) \ { \ remoteAddress=ntohl(__socketAddress.sin_addr.s_addr); \ remotePort =ntohs(__socketAddress.sin_port); \ } \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_AVAILABLE* Purpose : get number of available bytes for receive* Input : socketDescriptor - socket descriptor* Output : bytesAvailable - available bytes for receive* : result - TARGET_NATIVE_OK if no error occurred, * TARGET_NATIVE_ERROR otherwise* Return : -* Side-effect: unknown* Notes : -\***********************************************************************/#ifndef TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_AVAILABLE #include <sys/ioctl.h> #define TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_AVAILABLE(socketDescriptor,bytesAvailable,result) \ do { \ int __value; \ \ bytesAvailable=0; \ \ result=(ioctl(socketDescriptor,FIONREAD,&__value)==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \ if (result==TARGET_NATIVE_OK) \ { \ bytesAvailable=__value; \ } \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_SOCKET_RECEIVE* Purpose : receive data from socket* Input : socketDescriptor - socket descriptor* maxLength - max. size of bfufer* Output : buffer - received data* bytesReceive - length of received data* Return : -* Side-effect: unknown* Notes : -\***********************************************************************/#ifndef TARGET_NATIVE_NETWORK_SOCKET_RECEIVE #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define TARGET_NATIVE_NETWORK_SOCKET_RECEIVE(socketDescriptor,buffer,maxLength,bytesReceived) \ do { \ struct sockaddr_in __socketAddress; \ socklen_t __socketAddressLength; \ \ memset(&__socketAddress,0,sizeof(__socketAddress)); \ __socketAddressLength=sizeof(__socketAddress); \ bytesReceived=recv(socketDescriptor,buffer,maxLength,0); \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_WITH_ADDRESS_PORT* Purpose : receive data from socket* Input : socketDescriptor - socket descriptor* maxLength - max. size of bfufer* Output : buffer - received data* address - from address (NOT in network byte order!)* port - from port (NOT in network byte order!)* bytesReceive - length of received data* Return : -* Side-effect: unknown* Notes : -\***********************************************************************/#ifndef TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_WITH_ADDRESS_PORT #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define TARGET_NATIVE_NETWORK_SOCKET_RECEIVE_WITH_ADDRESS_PORT(socketDescriptor,buffer,maxLength,address,port,bytesReceived) \ do { \ struct sockaddr_in __socketAddress; \ socklen_t __socketAddressLength; \ \ port=0; \ \ memset(&__socketAddress,0,sizeof(__socketAddress)); \ __socketAddressLength=sizeof(__socketAddress); \ bytesReceived=recvfrom(socketDescriptor,buffer,maxLength,0,(struct sockaddr*)&__socketAddress,&__socketAddressLength); \ if (__socketAddressLength==sizeof(__socketAddress)) \ { \ address=ntohl(__socketAddress.sin_addr.s_addr); \ port =ntohs(__socketAddress.sin_port); \ } \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_SOCKET_SEND* Purpose : send data to socket* Input : socketDescriptor - socket descriptor* : buffer - data to send* length - length of data to send* Output : bytesSent - number of bytes sent, -1 otherwise* Side-effect: unknown* Notes : -\***********************************************************************/#ifndef TARGET_NATIVE_NETWORK_SOCKET_SEND #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define TARGET_NATIVE_NETWORK_SOCKET_SEND(socketDescriptor,buffer,length,bytesSent) \ do { \ bytesSent=send(socketDescriptor,buffer,length,0); \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_SOCKET_SEND_WITH_ADDRESS_PORT* Purpose : send data to socket* Input : socketDescriptor - socket descriptor* : buffer - data to send* length - length of data to send* Address - to address (NOT in network byte order!)* Port - to port (NOT in network byte order!)* Output : bytesSent - number of bytes sent, -1 otherwise* Side-effect: unknown* Notes : -\***********************************************************************/#ifndef TARGET_NATIVE_NETWORK_SOCKET_SEND_WITH_ADDRESS_PORT #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define TARGET_NATIVE_NETWORK_SOCKET_SEND_WITH_ADDRESS_PORT(socketDescriptor,buffer,length,address,port,bytesSent) \ do { \ struct sockaddr_in __socketAddress; \ \ memset(&__socketAddress,0,sizeof(__socketAddress)); \ __socketAddress.sin_family = AF_INET; \ __socketAddress.sin_addr.s_addr = htonl(address); \ __socketAddress.sin_port = htons((short)port); \ bytesSent=sendto(socketDescriptor,buffer,length,0,(struct sockaddr*)&__socketAddress,sizeof(__socketAddress)); \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_TCP_NODELAY* Purpose : set socket option TCP_NODELAY* Input : socketDescriptor - socket descriptor* flag - 1 or 0* Output : result - TARGET_NATIVE_OK if no error occurred, * TARGET_NATIVE_ERROR otherwise* Return : -* Side-effect: unknown* Notes : -\***********************************************************************/#ifndef TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_TCP_NODELAY #include <sys/types.h> #include <sys/socket.h> #include <netinet/tcp.h> #define TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_TCP_NODELAY(socketDescriptor,flag,result) \ do { \ int __value; \ \ __value=flag; \ result=(setsockopt(socketDescriptor,IPPROTO_TCP,TCP_NODELAY,&__value,sizeof(__value))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR; \ } while (0)#endif/***********************************************************************\* Name : TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_LINGER* Purpose : set socket option SO_LINGER* Input : socketDescriptor - socket descriptor* flag - 1 or 0* value - linger value* Output : result - TARGET_NATIVE_OK if no error occurred, * TARGET_NATIVE_ERROR otherwise* Return : -* Side-effect: unknown* Notes : -\***********************************************************************/#ifndef TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_LINGER #include <sys/types.h> #include <sys/socket.h> #define TARGET_NATIVE_NETWORK_SOCKET_SET_OPTION_SO_LINGER(socketDescriptor,flag,value,result) \ do { \ struct linger __linger; \ \ memset(&__linger,0,sizeof(__linger)); \ if (flag) \ { \ __linger.l_onoff=0; \ } \ else \ { \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -