better texture binding
[voxelands-alt.git] / inc / net.h
blobb6803eaef7a5bd9c76d0ebf525df16db55a1148c
1 #ifndef _NET_H_
2 #define _NET_H_
4 #include "common.h"
5 #include "array.h"
6 #include "file.h"
8 #ifdef WIN32
9 /* the next few lines mean we need winXP or better,
10 * better being non-windows, really */
11 #ifdef _WIN32_WINNT
12 #undef _WIN32_WINNT
13 #endif
14 #define _WIN32_WINNT 0x0501
15 #include <winsock2.h>
16 #include <ws2tcpip.h>
17 #else
18 #include <sys/socket.h>
19 #include <sys/select.h>
20 #include <netdb.h>
21 #include <netinet/in.h>
22 #endif
24 #define NETSTATE_UNUSED 0
25 #define NETSTATE_OPEN 1
26 #define NETSTATE_CLOSED 2
28 #define NETTYPE_TCP 1
29 #define NETTYPE_UDP 2
30 #define NETTYPE_TCP_HOST 3
31 #define NETTYPE_UDP_HOST 4
33 #ifndef _HAVE_NETTCP_BUFFER_TYPE
34 #define _HAVE_NETTCP_BUFFER_TYPE
35 typedef struct net_tcp_buffer_s {
36 unsigned char buff[1024];
37 int start;
38 int end;
39 } net_tcp_buffer_t;
40 #endif
42 #ifndef _HAVE_NETUDP_BUFFER_TYPE
43 #define _HAVE_NETUDP_BUFFER_TYPE
44 typedef struct net_udp_buffer_s {
45 file_t *unsorted;
46 file_t *sorted;
47 } net_udp_buffer_t;
48 #endif
50 #ifndef _HAVE_NET_TYPE
51 #define _HAVE_NET_TYPE
52 typedef struct net_connection_s {
53 struct net_connection_s *prev;
54 struct net_connection_s *next;
55 struct addrinfo hints;
56 struct addrinfo *addr;
57 struct sockaddr_storage remote_addr;
58 socklen_t remote_addr_len;
59 int id;
60 int type;
61 int fd;
62 int state;
63 int tries;
64 char* host;
65 char* port;
66 union {
67 net_tcp_buffer_t tcp;
68 net_udp_buffer_t udp;
69 } buff;
70 array_t *peers;
71 } net_connection_t;
72 #endif
74 /* defined in net.c */
75 int net_init(void);
76 void net_exit(void);
77 net_connection_t *net_connection(void);
78 net_connection_t *net_fetch(int id);
79 int net_state(net_connection_t *n);
80 void net_disconnect(net_connection_t *n);
81 void net_close(net_connection_t *n);
82 array_t *net_select(unsigned int msec, int reconnect, net_connection_t *n);
83 array_t *net_select_array(unsigned int msec, int reconnect, array_t *a);
84 int net_write(net_connection_t *n, void *buff, unsigned int size);
85 int net_write_string(net_connection_t *n, char* fmt, ...);
86 int net_read(net_connection_t *n, void *buff, unsigned int size);
87 int net_readline(net_connection_t *n, void *buff, unsigned int size);
88 int net_broadcast(net_connection_t *n, void *buff, unsigned int size);
89 int net_accept(net_connection_t *n);
91 /* defined in net_udp.c */
92 file_t *udp_next(net_connection_t *n);
94 #ifdef _NET_LOCAL
95 /* defined in net_tcp.c */
96 net_connection_t *tcp_client_connect(char* host, char* port);
97 int tcp_client_reconnect(net_connection_t *n);
98 net_connection_t *tcp_host_connect(char* host, char* port);
99 int tcp_host_reconnect(net_connection_t *n);
100 int tcp_write(net_connection_t *n, void *buff, unsigned int size);
101 int tcp_pending(net_connection_t *n);
102 int tcp_read(net_connection_t *n, void *buff, unsigned int size);
103 int tcp_readline(net_connection_t *n, void *buf, unsigned int size);
104 int tcp_broadcast(net_connection_t *n, void *buff, unsigned int size);
105 int tcp_accept(net_connection_t *n);
107 /* defined in net_udp.c */
108 net_connection_t *udp_client_connect(char* host, char* port);
109 int udp_client_reconnect(net_connection_t *n);
110 net_connection_t *udp_host_connect(char* host, char* port);
111 int udp_host_reconnect(net_connection_t *n);
112 int udp_write(net_connection_t *n, void *buff, unsigned int size);
113 int udp_pending(net_connection_t *n);
114 int udp_read(net_connection_t *n, void *buff, unsigned int size);
115 int udp_readline(net_connection_t *n, void *buf, unsigned int size);
116 int udp_broadcast(net_connection_t *n, void *buff, unsigned int size);
117 int udp_accept(net_connection_t *n);
118 #endif
120 #endif