#ifndef INCLUDED_CHAN
#define INCLUDED_CHAN 1

typedef struct HEAD {
	int ispost, content_len; 
	int ismsie;
	int keepalive;
	char content_type[BFSZ];
	char *ref_query;
	char ref_script[BFSZ];
	char ref_full[BFSZ];
	char ref_file[BFSZ];
} Head;
enum {C_BAD,C_HTTP,C_NNTP,C_TOMASTER,C_TOCHILD,C_USER,C_LISTEN};
typedef struct CHAN {
	SOCKET sk; 
	char *out;
	char *in;
	int inat;
	int serverpush;
	int pushgid;
	int inget;
	int outat,outlen;
	int type;
	int code; /* login code for passwords */
	int sending;
	int gothead;
	int keepalive;
	int canclose;
	void *p1;
	void (*fn_read)(struct CHAN *this, void *p1); 
	void (*fn_write)(struct CHAN *this, void *p1);
	void (*fn_close)(struct CHAN *this, void *p1);
	struct CHAN *next;
	Head *head;
	int hostid;
	int isslave;
	struct UINFO *uinfo;
	int iscompress;
#ifdef DOCOMPRESS
	z_stream z_str;
#endif
	double rawbytes,compressedbytes;
} Chan;
int tcp_waitchans(Chan *c);
void chan_listen_read(Chan *c);
void chan_free(Chan *c);
int chan_close(Chan *c);
int chan_init(void);
void chan_open_udp(Chan *c, char *to,  char *bind);
int chan_setinitem(int t);

int chan_write(Chan *c, char *bf, int len);
void chan_run(void);
int chan_isopen(Chan *c);
int chan_getcode(Chan *c);
int chan_read(Chan *c, char *bf, int len);
void chan_setcode(Chan *c, int r);
void chan_idle(void);
int chan_ingetc(Chan *c);
void chan_instart(Chan *c);
void chan_setread(Chan *c, void (*fn_read)(Chan *c,void *p1),void *p2);
void chan_loop(void);
void chan_setwrite(Chan *c, void (*fn_read)(Chan *c,void *p1),void *p2);
void chan_open(Chan *c, char *to, char *bind);
Chan *chan_getlist(void);
void chan_exit(void);
Chan *chan_new(void);
void chan_add(Chan *c);
int chan_setdelay(Chan *c, int t);


Chan *chan_findhost(int hostid);
void chan_sethost(Chan *c, int h);
void chan_setsending(Chan *c, int x);
int chan_write_s(Chan *c, char *s);
void chan_release(Chan *c);
int chan_isreallyopen(Chan *c);
#endif