2008-10-02 12:29:19 +00:00
|
|
|
#ifndef ENGINE_NETWORK_H
|
|
|
|
#define ENGINE_NETWORK_H
|
2007-11-25 19:42:40 +00:00
|
|
|
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2008-10-02 12:29:19 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
typedef struct
|
2007-07-13 13:40:04 +00:00
|
|
|
{
|
2007-08-22 07:52:33 +00:00
|
|
|
/* -1 means that it's a stateless packet */
|
|
|
|
/* 0 on the client means the server */
|
2007-07-13 13:40:04 +00:00
|
|
|
int client_id;
|
2008-07-06 11:21:21 +00:00
|
|
|
NETADDR address; /* only used when client_id == -1 */
|
2007-07-13 13:40:04 +00:00
|
|
|
int flags;
|
|
|
|
int data_size;
|
|
|
|
const void *data;
|
2008-04-05 14:50:43 +00:00
|
|
|
} NETCHUNK;
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2008-10-02 12:29:19 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
NETADDR addr;
|
|
|
|
int type;
|
|
|
|
int expires;
|
|
|
|
} NETBANINFO;
|
|
|
|
|
2008-04-05 14:50:43 +00:00
|
|
|
/*typedef struct
|
2007-07-13 13:40:04 +00:00
|
|
|
{
|
|
|
|
int send_bytes;
|
|
|
|
int recv_bytes;
|
|
|
|
int send_packets;
|
|
|
|
int recv_packets;
|
|
|
|
|
|
|
|
int resend_packets;
|
|
|
|
int resend_bytes;
|
2008-04-05 14:50:43 +00:00
|
|
|
} NETSTATS;*/
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2008-08-31 19:29:09 +00:00
|
|
|
typedef struct NETSERVER NETSERVER;
|
|
|
|
typedef struct NETCLIENT NETCLIENT;
|
2007-07-13 13:40:04 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
NETFLAG_ALLOWSTATELESS=1,
|
2008-04-05 14:50:43 +00:00
|
|
|
NETSENDFLAG_VITAL=1,
|
|
|
|
NETSENDFLAG_CONNLESS=2,
|
|
|
|
NETSENDFLAG_FLUSH=4,
|
2007-07-13 13:40:04 +00:00
|
|
|
|
|
|
|
NETSTATE_OFFLINE=0,
|
|
|
|
NETSTATE_CONNECTING,
|
2008-10-02 12:29:19 +00:00
|
|
|
NETSTATE_ONLINE,
|
|
|
|
|
|
|
|
NETBANTYPE_SOFT=1,
|
|
|
|
NETBANTYPE_DROP=2
|
2007-07-13 13:40:04 +00:00
|
|
|
};
|
|
|
|
|
2007-08-14 18:37:16 +00:00
|
|
|
typedef int (*NETFUNC_DELCLIENT)(int cid, void *user);
|
|
|
|
typedef int (*NETFUNC_NEWCLIENT)(int cid, void *user);
|
|
|
|
|
2008-04-05 14:50:43 +00:00
|
|
|
/* both */
|
|
|
|
void netcommon_openlog(const char *filename);
|
|
|
|
void netcommon_init();
|
2008-10-14 12:11:42 +00:00
|
|
|
int netcommon_compress(const void *data, int data_size, void *output, int output_size);
|
|
|
|
int netcommon_decompress(const void *data, int data_size, void *output, int output_size);
|
2008-04-05 14:50:43 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
/* server side */
|
2008-07-06 11:21:21 +00:00
|
|
|
NETSERVER *netserver_open(NETADDR bindaddr, int max_clients, int flags);
|
2007-08-22 07:52:33 +00:00
|
|
|
int netserver_set_callbacks(NETSERVER *s, NETFUNC_NEWCLIENT new_client, NETFUNC_DELCLIENT del_client, void *user);
|
2008-04-05 14:50:43 +00:00
|
|
|
int netserver_recv(NETSERVER *s, NETCHUNK *chunk);
|
|
|
|
int netserver_send(NETSERVER *s, NETCHUNK *chunk);
|
2007-08-22 07:52:33 +00:00
|
|
|
int netserver_close(NETSERVER *s);
|
|
|
|
int netserver_update(NETSERVER *s);
|
2008-02-10 15:32:30 +00:00
|
|
|
NETSOCKET netserver_socket(NETSERVER *s);
|
2007-08-22 07:52:33 +00:00
|
|
|
int netserver_drop(NETSERVER *s, int client_id, const char *reason);
|
2008-07-06 11:21:21 +00:00
|
|
|
int netserver_client_addr(NETSERVER *s, int client_id, NETADDR *addr);
|
2007-08-22 07:52:33 +00:00
|
|
|
int netserver_max_clients(NETSERVER *s);
|
2008-10-02 12:29:19 +00:00
|
|
|
|
|
|
|
int netserver_ban_add(NETSERVER *s, NETADDR addr, int type, int seconds);
|
|
|
|
int netserver_ban_remove(NETSERVER *s, NETADDR addr);
|
|
|
|
int netserver_ban_num(NETSERVER *s); /* caution, slow */
|
|
|
|
int netserver_ban_get(NETSERVER *s, int index, NETBANINFO *info); /* caution, slow */
|
|
|
|
|
2008-04-05 14:50:43 +00:00
|
|
|
/*void netserver_stats(NETSERVER *s, NETSTATS *stats);*/
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
/* client side */
|
2008-07-06 11:21:21 +00:00
|
|
|
NETCLIENT *netclient_open(NETADDR bindaddr, int flags);
|
2007-08-22 07:52:33 +00:00
|
|
|
int netclient_disconnect(NETCLIENT *c, const char *reason);
|
2008-07-06 11:21:21 +00:00
|
|
|
int netclient_connect(NETCLIENT *c, NETADDR *addr);
|
2008-04-05 14:50:43 +00:00
|
|
|
int netclient_recv(NETCLIENT *c, NETCHUNK *chunk);
|
|
|
|
int netclient_send(NETCLIENT *c, NETCHUNK *chunk);
|
2007-08-22 07:52:33 +00:00
|
|
|
int netclient_close(NETCLIENT *c);
|
|
|
|
int netclient_update(NETCLIENT *c);
|
|
|
|
int netclient_state(NETCLIENT *c);
|
2008-04-06 11:52:00 +00:00
|
|
|
int netclient_flush(NETCLIENT *c);
|
2007-12-11 21:55:52 +00:00
|
|
|
int netclient_gotproblems(NETCLIENT *c);
|
2008-04-05 14:50:43 +00:00
|
|
|
/*void netclient_stats(NETCLIENT *c, NETSTATS *stats);*/
|
2007-08-22 07:52:33 +00:00
|
|
|
const char *netclient_error_string(NETCLIENT *c);
|
2007-07-13 13:40:04 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
class net_server
|
|
|
|
{
|
|
|
|
NETSERVER *ptr;
|
|
|
|
public:
|
|
|
|
net_server() : ptr(0) {}
|
|
|
|
~net_server() { close(); }
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int open(NETADDR bindaddr, int max, int flags) { ptr = netserver_open(bindaddr, max, flags); return ptr != 0; }
|
2007-08-22 07:52:33 +00:00
|
|
|
int close() { int r = netserver_close(ptr); ptr = 0; return r; }
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2007-08-14 18:37:16 +00:00
|
|
|
int set_callbacks(NETFUNC_NEWCLIENT new_client, NETFUNC_DELCLIENT del_client, void *user)
|
2007-08-22 07:52:33 +00:00
|
|
|
{ return netserver_set_callbacks(ptr, new_client, del_client, user); }
|
2007-08-14 18:37:16 +00:00
|
|
|
|
2008-04-05 14:50:43 +00:00
|
|
|
int recv(NETCHUNK *chunk) { return netserver_recv(ptr, chunk); }
|
|
|
|
int send(NETCHUNK *chunk) { return netserver_send(ptr, chunk); }
|
2007-08-22 07:52:33 +00:00
|
|
|
int update() { return netserver_update(ptr); }
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
int drop(int client_id, const char *reason) { return netserver_drop(ptr, client_id, reason); }
|
|
|
|
|
|
|
|
int max_clients() { return netserver_max_clients(ptr); }
|
2008-04-05 14:50:43 +00:00
|
|
|
/*void stats(NETSTATS *stats) { netserver_stats(ptr, stats); }*/
|
2007-07-13 13:40:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class net_client
|
|
|
|
{
|
|
|
|
NETCLIENT *ptr;
|
|
|
|
public:
|
|
|
|
net_client() : ptr(0) {}
|
|
|
|
~net_client() { close(); }
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int open(NETADDR bindaddr, int flags) { ptr = netclient_open(bindaddr, flags); return ptr != 0; }
|
2007-08-22 07:52:33 +00:00
|
|
|
int close() { int r = netclient_close(ptr); ptr = 0; return r; }
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int connect(NETADDR *addr) { return netclient_connect(ptr, addr); }
|
2007-08-22 07:52:33 +00:00
|
|
|
int disconnect(const char *reason) { return netclient_disconnect(ptr, reason); }
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2008-04-05 14:50:43 +00:00
|
|
|
int recv(NETCHUNK *chunk) { return netclient_recv(ptr, chunk); }
|
|
|
|
int send(NETCHUNK *chunk) { return netclient_send(ptr, chunk); }
|
2007-08-22 07:52:33 +00:00
|
|
|
int update() { return netclient_update(ptr); }
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
const char *error_string() { return netclient_error_string(ptr); }
|
2007-08-04 18:23:26 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
int state() { return netclient_state(ptr); }
|
2008-04-05 14:50:43 +00:00
|
|
|
/*void stats(NETSTATS *stats) { netclient_stats(ptr, stats); }*/
|
2007-07-13 13:40:04 +00:00
|
|
|
};
|
|
|
|
#endif
|
2008-04-05 14:50:43 +00:00
|
|
|
|
2008-10-02 12:29:19 +00:00
|
|
|
|
|
|
|
#endif
|