medfall

A super great game engine
Log | Files | Refs

platform_network.h (1689B)


      1 #pragma once
      2 
      3 #include "platform.h"
      4 #include "array.h"
      5 #include "ggformat.h"
      6 
      7 #if PLATFORM_WINDOWS
      8 typedef s64 OSSocket;
      9 #elif PLATFORM_UNIX
     10 typedef int OSSocket;
     11 #else
     12 #error new platform
     13 #endif
     14 
     15 enum TransportProtocol { NET_UDP, NET_TCP };
     16 enum IPvX { NET_IPV4, NET_IPV6 };
     17 enum NonblockingBool { NET_BLOCKING, NET_NONBLOCKING };
     18 
     19 enum TCPRecvResult {
     20 	TCP_OK,
     21 	TCP_TIMEOUT,
     22 	TCP_CLOSED,
     23 	TCP_ERROR,
     24 };
     25 
     26 struct UDPSocket {
     27 	OSSocket ipv4, ipv6;
     28 };
     29 
     30 struct TCPSocket {
     31 	OSSocket fd;
     32 };
     33 
     34 struct IPv4 { u8 bytes[ 4 ]; };
     35 struct IPv6 { u8 bytes[ 16 ]; };
     36 
     37 struct NetAddress {
     38         IPvX type;
     39         union {
     40                 IPv4 ipv4;
     41                 IPv6 ipv6;
     42         };
     43         u16 port;
     44 };
     45 
     46 bool operator==( const NetAddress & lhs, const NetAddress & rhs );
     47 bool operator!=( const NetAddress & lhs, const NetAddress & rhs );
     48 
     49 void format( FormatBuffer * fb, const NetAddress & addr, const FormatOpts & opts );
     50 
     51 void net_init();
     52 void net_term();
     53 
     54 UDPSocket net_new_udp( NonblockingBool nonblocking, u16 port = 0 );
     55 void net_send( UDPSocket sock, const void * data, size_t len, const NetAddress & addr );
     56 bool net_tryrecv( UDPSocket sock, void * buf, size_t len, NetAddress * addr, size_t * bytes_received );
     57 void net_destroy( UDPSocket * sock );
     58 
     59 bool net_new_tcp( TCPSocket * sock, const NetAddress & addr, NonblockingBool nonblocking );
     60 bool net_send( TCPSocket sock, const void * data, size_t len );
     61 TCPRecvResult net_recv( TCPSocket sock, void * buf, size_t buf_size, size_t * bytes_read, u32 timeout_ms = 0 );
     62 void net_destroy( TCPSocket * sock );
     63 
     64 
     65 array< NetAddress > dns( const char * host, array< NetAddress > out );
     66 bool dns_first( const char * host, NetAddress * address );