platform_network.h (1008B)
1 #pragma once 2 3 #include "platform.h" 4 #include "common.h" 5 6 #if PLATFORM_WINDOWS 7 #include <winsock2.h> 8 typedef SOCKET PlatformSocket; 9 #elif PLATFORM_UNIX 10 typedef int PlatformSocket; 11 #else 12 #error new platform 13 #endif 14 15 enum IPvX { NET_IPV4, NET_IPV6 }; 16 17 enum TCPRecvResult { 18 TCP_OK, 19 TCP_CLOSED, 20 TCP_ERROR, 21 }; 22 23 struct TCPSocket { 24 PlatformSocket fd; 25 }; 26 27 struct IPv4 { u8 bytes[ 4 ]; }; 28 struct IPv6 { u8 bytes[ 16 ]; }; 29 30 struct NetAddress { 31 IPvX type; 32 union { 33 IPv4 ipv4; 34 IPv6 ipv6; 35 }; 36 u16 port; 37 }; 38 39 bool operator==( const NetAddress & lhs, const NetAddress & rhs ); 40 bool operator!=( const NetAddress & lhs, const NetAddress & rhs ); 41 42 void net_init(); 43 void net_term(); 44 45 bool net_new_tcp( TCPSocket * sock, const NetAddress & addr, const char ** err ); 46 bool net_send( TCPSocket sock, const void * data, size_t len ); 47 TCPRecvResult net_recv( TCPSocket sock, void * buf, size_t buf_size, size_t * bytes_read ); 48 void net_destroy( TCPSocket * sock ); 49 50 bool dns_first( const char * host, NetAddress * address );