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

/Conn_intern.h (7bc5f77db036d7714b28600ffd90ab4c5ee080e2) (5546 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);
	void (*worker_start)(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 162 30a78e3a392ae33217d139ce27c4e1ebd04aa6e0 .gitignore
100644 blob 169 c003c095218f64ad33aeb89987f61eb575557d96 .mailmap
100644 blob 1945 fecf0e7a7e8580485101a179685aedc7e00affbb Changelog.pre109
100644 blob 81468 04121fc5d4ad5c21b6e12114219185324515d768 Conn.c
100644 blob 6098 eeed3b0c1d148829337ae265d8fe5d7f8b01e99a Conn.h
100644 blob 905 884fa11125512f99984f40735af6c380330f871c Conn.spec.in
100644 blob 747 662c3f3fe8d0a3d23770631d7a0a260719d81e62 Conn_config.h.in
100644 blob 5546 7bc5f77db036d7714b28600ffd90ab4c5ee080e2 Conn_intern.h
100644 blob 12764 d7f5dfa1fc74f110cd31ed7433fbe4237d47cc84 Conn_web.c
100644 blob 93 4754320eef2b558b97b9c75bd01e545f102670b7 Conn_web.h
100644 blob 30 d987fa5df957830331139935d517009e2911b0cf INSTALL
100644 blob 25275 92b8903ff3fea7f49ef5c041b67a087bca21c5ec LICENSE
100644 blob 1261 fb7abee03563c44f3c579a4ada10e0826d5f3ebe Makefile.in
100644 blob 29 e214257f87a28e8fb0413b627cf7ee76ade2e94c Makefile.include.in
100644 blob 200 02d1f3eebc03e84013c23a35db5fe3b77f12f0cc README
100644 blob 19668 bf0f19113acc021dcdc92af4be32b2c737b362a7 TODO
100755 blob 30 92c4bc48245c00408cd7e1fd89bc1a03058f4ce4 configure
040000 tree - d4c9c4a69c5cfa2a84316967185f1661b6817779 docs
100755 blob 18252 e2438615edba7066a730ed6a796a5302263f1f37 duilder
100644 blob 1414 54b2baeaf7fbb58783b5588ab07ea8d547aef645 duilder.conf
040000 tree - b255bf8fe16832bbbb513181b1dc6ae9420deed1 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