catalinux / Conn (public) (License: LGPLv2) (since 2016-03-01) (hash sha1)
Net library for easy building ipv4/ipv6 network daemons/clients

/Conn_intern.h (95294798236381d591db36ef84ab53040183fccf) (5507 bytes) (mode 100644) (type blob)

#ifndef _Conn_intern_h
#define _Conn_intern_h 1

#include <sys/epoll.h>
#include <string.h>

#include "Conn.h"

/* type */
enum CONN_TYPE {
	CONN_TYPE_UNK = 0,
	CONN_TYPE_MASTER,
	CONN_TYPE_P2P
};

/* state */
enum CONN_STATE {
	CONN_STATE_FREE = 0,
	CONN_STATE_EMPTY,	/* Set in Conn_alloc_prepare */
	CONN_STATE_OPEN,
	CONN_STATE_LISTEN,
	CONN_STATE_CONNECT_0,
	CONN_STATE_CONNECT_a,
	CONN_STATE_CONNECT_b,
	CONN_STATE_ERROR
};

/* error kind */
enum CONN_ERROR {
	CONN_ERROR_NO_ERROR = 0,
	CONN_ERROR_USERREQ,	/* user requested the close */
	CONN_ERROR_POLL,
	CONN_ERROR_RECV,
	CONN_ERROR_SEND,
	CONN_ERROR_SOCKET,
	CONN_ERROR_HANGUP,
	CONN_ERROR_GETADDRINFO,
	CONN_ERROR_EXPIRED,
	CONN_ERROR_ACCEPT,	/* This is free. TODO: check! */
	CONN_ERROR_MEM,
	CONN_ERROR_CONNECT,
	CONN_ERROR_READ_TIMEOUT,
	CONN_ERROR_CONN_TIMEOUT,
	CONN_ERROR_INTERNAL
};

struct Conn_pool
{
	struct Conn		*head, *tail;
	unsigned int		allocated;
	unsigned short		next_block;
	unsigned short		pad;
	void			*blocks[4096]; /* TODO: make it dynamic */
};

struct Conn_wpool_worker
{
	int			epoll_fd;
	int			timer_fd;
	struct epoll_event	events[CONN_EVENTS_SLOTS];
	unsigned short		inited:1;
	unsigned short		pad1:15;
	unsigned short		id; /* Used for logging */
	int			control; /* socket to communicate with parent */
	pid_t			pid;
	int			pad2;
	struct Conn		*conns_head, *conns_tail;

	/* Keep track of free structures */
	struct Conn_pool	free;

	/* stats */
	unsigned long		mem_structs; /* How much bytes are used for conns */
	unsigned long long	in_clients; /* How many clients connected */
};

struct Conn_wpool
{
	int				epoll_fd;
	unsigned short			workers;
	unsigned short			next; /* next worker to be choosen */
	unsigned short			refs;
	unsigned short			pad1;
	unsigned int			pad2;
	struct Conn_wpool_worker	*ww;
};

/*
 * Used to define callbacks
 */
struct Conn_cbs
{
	void (*accept)(struct Conn *C);
	void (*recv)(struct Conn *C);
	void (*send)(struct Conn *C);
	void (*data)(struct Conn *C);
	void (*close)(struct Conn *C);
	void (*trigger)(struct Conn *C);
	void (*error)(struct Conn *C);
	void (*connected)(struct Conn *C);
	void (*accept_error)(struct Conn *C);
};

struct Conn
{
	struct Conn		*next;

	int			fd;

	unsigned char		type;

	unsigned char		state;
	unsigned char		error_state;

	/* Flags */
	unsigned char		auto_reconnect:1;
	unsigned char		close_after_send:1;
	unsigned char		shutdown_after_send:1;
	unsigned char		local_dirty:1;
	unsigned char		remote_dirty:1;
	unsigned char		pad1:3;

	unsigned int		ibuf_size, ibuf_head, ibuf_tail;
	unsigned int		obuf_size, obuf_head, obuf_tail;
	char			*ibuf;
	char			*obuf;

	struct timeval		trecv, tsend;	/* last time we saw an receive/send */
	unsigned int		idle;		/* idle time allowed TODO: ms? */
	unsigned int		read_timeout;	/* Max timeout for receiving an answer (milliseconds) */

	struct timeval		conn_syn;	/* Time when a connection was initiated */
	unsigned int		conn_timeout;	/* Timeout for connection (milliseconds) */

	unsigned int		last_trigger;	/* last trigger was at TODO: make it an offset from start. */
	unsigned int		trigger;	/* Frequency of wakeup a connection */

	int			sock_domain;
	int			sock_type;
	int			sock_protocol;

	char			addr[40], bind_addr[40];
	int			port, bind_port;

	struct Conn_cbs		cbs;		/* Specific callbacks */

	unsigned long long	bi, bo;

	void			*private;	/* private use by user */

	time_t			start;

	unsigned long long	id;		/* the id of a connection */

	int			xerrno;

	/* reconnect stuff */
	unsigned int		retries;
	unsigned int		delay; /* delay between reconnects */
	unsigned int		tryat; /* when we go to CONNECT_a state */

	/* bandwidth stuff */
	unsigned int		band_tokens;	/* 1 token -> 1000 bytes */
	unsigned int		band_factor;	/* tokens cannot go past f * w */
	struct timeval		band_lasttime;	/* last time tokens were added */
	unsigned int		band_width;	/* in 1000b increments */

	unsigned int		pad2;
	struct timeval		time_open; 	/* When a connect succeded */ /* TODO: why not use start? */

	/* wpool */
	struct Conn_wpool	*wp;		/* Worker pool associated with this conn */
	struct Conn_wpool_worker
				*worker;	/* used to link C in active list of a worker */

	/* web server */
	struct Conn_web		*web;		/* everything needed for web (urls etc.) - common for all childs */
	struct Conn_web_request	*web_req;	/* per child */
};

#ifndef SO_REUSEPORT
	#define SO_REUSEPORT 15
#endif

/* For web server */
#define CONN_WEB_TYPE_PATH	0
#define CONN_WEB_TYPE_SCRIPT	1

#define CONN_WEB_REQ_WS		0
#define CONN_WEB_REQ_GET	1
#define CONN_WEB_REQ_POST	2

struct Conn_web_url
{
	struct Conn_web_url	*next;
	char			*url;
	char			*path;
	void			(*cb)(struct Conn *);
	unsigned char		type:1; /* CONN_WEB_TYPE_* */
	unsigned char		pad1:7;
	unsigned char		pad2[7];
};

struct Conn_web
{
	struct Conn_web_url	*urls;
};

struct Conn_web_request
{
	unsigned char		req_type:2; /* CONN_WEB_REQ_* */ /* TODO: replace with string representation to support more types? */
	unsigned char		pad1:6;
	unsigned char		http_protocol; /* 10, 11, 20 etc. */
	unsigned char		pad2[6];
	char			*url; /* requested url */
	char			*paras; /* parameters */ /* TODO: should be make NULL after first request? */
	char			*header;
	struct Conn_web_url	*u; /* used for websocket */ /* TODO: really used? */
};

/* For parsing */
struct Conn_split_cell
{
	struct Conn_split_cell	*next;
	char			*left;
	char			*right;
	unsigned int		right_len;
	unsigned int		pad;
};

struct Conn_split
{
	struct Conn_split_cell	*head, *tail;
	char			*line;
};


#endif


Mode Type Size Ref File
100644 blob 129 38f2534580e0aace0e6a5b49d79ada2c2ca162be .exclude
100644 blob 155 f46bab40e9d32df6e6ef7ab643931d4e1019d9bf .gitignore
100644 blob 169 c003c095218f64ad33aeb89987f61eb575557d96 .mailmap
100644 blob 1945 fecf0e7a7e8580485101a179685aedc7e00affbb Changelog.pre109
100644 blob 79498 7ad881b5be242d5fdac3ceb550ab4d140344db49 Conn.c
100644 blob 5849 c20cfbd2cfe0b094ed52e61b428b92da19b5daae Conn.h
100644 blob 917 5423bbceb9236d56d8ee827877d8b6d65986c490 Conn.spec.in
100644 blob 747 662c3f3fe8d0a3d23770631d7a0a260719d81e62 Conn_config.h.in
100644 blob 5507 95294798236381d591db36ef84ab53040183fccf Conn_intern.h
100644 blob 10966 95be3dead1c6fee0917051220e74aef99f49daed Conn_web.c
100644 blob 93 4754320eef2b558b97b9c75bd01e545f102670b7 Conn_web.h
100644 blob 30 d987fa5df957830331139935d517009e2911b0cf INSTALL
100644 blob 25275 92b8903ff3fea7f49ef5c041b67a087bca21c5ec LICENSE
100644 blob 1215 0518f7d179939cbc33d13f6626fc45f94db1bf68 Makefile.in
100644 blob 29 e214257f87a28e8fb0413b627cf7ee76ade2e94c Makefile.include.in
100644 blob 192 5b11bdfb23857d8588845465aef993b320596b44 README
100644 blob 19376 5fbc10d9b77d79cd5f62c5f972cc4f7171a4c4f5 TODO
100755 blob 30 92c4bc48245c00408cd7e1fd89bc1a03058f4ce4 configure
040000 tree - d4c9c4a69c5cfa2a84316967185f1661b6817779 docs
100755 blob 16779 274273c95ecfd0d46b63e1e3e8fbd24c204586c9 duilder
100644 blob 1274 25314f6d244d5e701d7c2a22826f00f2cf242651 duilder.conf
040000 tree - 8373602f32aa7000178a7d17fac694f480c246cd examples
040000 tree - 5643f06c34660e576e6c5d0dee5ac74a2bf34f51 tests
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/catalinux/Conn

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/catalinux/Conn

Clone this repository using git:
git clone git://git.rocketgit.com/user/catalinux/Conn

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main