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

/Conn_intern.h (29ae224447a7296f2f65a44e3564500dd01ecef5) (5262 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;
	void			*blocks[4096]; /* TODO: make it dynamic */
};

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

	/* 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;
	struct Conn_wpool_worker	*ww;
	unsigned short			next; /* next worker to be choosen */
	unsigned short			refs;
};

/*
 * 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;

	char			*ibuf;
	unsigned int		ibuf_size, ibuf_head, ibuf_tail;

	char			*obuf;
	unsigned int		obuf_size, obuf_head, obuf_tail;

	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;

	unsigned long long	bi, bo;

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

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

	int			xerrno;

	time_t			start;

	/* Flags - TODO: remove 'flags' field */
	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;

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

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

	struct Conn_cbs		cbs;		/* Specific callbacks */

	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_* */
};

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? */
	char			*url; /* requested url */
	char			*paras; /* parameters */ /* TODO: should be make NULL after first request? */
	unsigned char		http_protocol; /* 10, 11, 20 etc. */
	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;
};

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


#endif


Mode Type Size Ref File
100644 blob 112 3a048942198455b0035de36927f4655a76284dc6 .exclude
100644 blob 137 bd7f50c983d416799691d785782298dbb96681ef .gitignore
100644 blob 169 c003c095218f64ad33aeb89987f61eb575557d96 .mailmap
100644 blob 1945 fecf0e7a7e8580485101a179685aedc7e00affbb Changelog.pre109
100644 blob 79219 3164c2c99d737ab393620da50e5ec9eddb79e1c7 Conn.c
100644 blob 5781 6eeb57430cb4963cdcafd507bf475b374f86bc4c Conn.h
100644 blob 885 69c6bcbf4b0d490d58d1a480b3b94a36d8918474 Conn.spec.in
100644 blob 747 662c3f3fe8d0a3d23770631d7a0a260719d81e62 Conn_config.h.in
100644 blob 5262 29ae224447a7296f2f65a44e3564500dd01ecef5 Conn_intern.h
100644 blob 10735 75723c05f3567505bbc66d42c26a55b83080ca71 Conn_web.c
100644 blob 93 4754320eef2b558b97b9c75bd01e545f102670b7 Conn_web.h
100644 blob 30 d987fa5df957830331139935d517009e2911b0cf INSTALL
100644 blob 25275 92b8903ff3fea7f49ef5c041b67a087bca21c5ec LICENSE
100644 blob 1177 2a1a14e400ec0949641341c032ace39dc8879628 Makefile.in
100644 blob 192 5b11bdfb23857d8588845465aef993b320596b44 README
100644 blob 19335 1105b5efeb81d0791d097499e4361cad0b821c80 TODO
100755 blob 30 92c4bc48245c00408cd7e1fd89bc1a03058f4ce4 configure
040000 tree - d4c9c4a69c5cfa2a84316967185f1661b6817779 docs
100755 blob 16781 a3216721d79029b2a35e47c6139cdc90cfee498f duilder
100644 blob 1023 3729c7b64fbfa6ce922d282bcec3fee033e6f93a duilder.conf
040000 tree - 8f41d72f3824230bb488f7e8af0f729885970bc3 examples
040000 tree - cc405c053275900a4395d05041eb8e6decae0647 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