/* ==========================================================================
 * libevnet/src/bufio-socket.h - Network server library for libevent.
 * --------------------------------------------------------------------------
 * Copyright (c) 2006  Barracuda Networks, Inc.
 * Copyright (c) 2006  William Ahern
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to permit
 * persons to whom the Software is furnished to do so, subject to the
 * following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 * ==========================================================================
 */
#ifndef EVNET_BUFIO_SOCKET_H
#define EVNET_BUFIO_SOCKET_H

#include <sys/time.h>
#include <sys/types.h>
#include <event.h>	/* struct event */

struct tls;


#define BUFIO_SOCKET_FD_WAIT	(-2)


struct bufio_socket_options {
	size_t page_size;
}; /* struct bufio_socket_options */


struct bufio_socket_frame {
	struct bufio_socket **sp;

#if defined SLIST_ENTRY && !_WIN32
	SLIST_ENTRY(bufio_socket_frame) sle;
#else
	struct { struct bufio_socket_frame *sle_next; } sle;
#endif
}; /* struct bufio_socket_frame */


struct bufio_socket_event {
	short want;

	void (*notify)(int, short, void *);

	struct {
		struct event ev_op;

		struct bufio_socket *ev_so;

		struct timeval timeout;
	} timer;
}; /* struct bufio_socket_event */


struct bufio_socket {
	int fd;

	struct bufio_socket_options opts;

	struct event_base *ev_base;
	struct event ev_op;
	short ev_pending;

	const struct arena_prototype *ap;

	struct tls *tls;

	struct {
		struct bufio_socket_event event;

		enum bufio_errno lasterr;

		struct {
			bufio_source_poll_cb fn;

			void *arg;

			enum bufio_errno retval;
		} cb;

		struct {
#if defined SLIST_HEAD
			SLIST_HEAD(, bufio_socket_frame) stack;
#else
			struct { struct bufio_socket_frame *slh_first; } stack;
#endif

			unsigned int ncalls;
		} poll;
	} source;

	struct {
		struct bufio_socket_event event;

		enum bufio_errno lasterr;

		struct {
			bufio_sink_poll_cb fn;

			void *arg;

			enum bufio_errno retval;
		} cb;

		struct {
#if defined SLIST_HEAD
			SLIST_HEAD(, bufio_socket_frame) stack;
#else
			struct { struct bufio_socket_frame *slh_first; } stack;
#endif

			unsigned int ncalls;
		} poll;
	} sink;

	BUFIO_IMPLEMENTS(bufio_socket, bufio_source);
	BUFIO_IMPLEMENTS(bufio_socket, bufio_sink);
}; /* struct bufio_socket */

#define bufio_socket_initialized(s)	((s)->fd != -1)

extern const struct bufio_socket bufio_socket_initializer;


extern const struct bufio_socket_options bufio_socket_defaults;


struct bufio_socket *bufio_socket_init(struct bufio_socket *, int, const struct bufio_socket_options *, struct event_base *, const struct arena_prototype *, enum bufio_errno *);

void bufio_socket_destroy(struct bufio_socket *);

struct bufio_socket *bufio_socket_open(int, const struct bufio_socket_options *, struct event_base *, const struct arena_prototype *, enum bufio_errno *);

void bufio_socket_close(struct bufio_socket *);

void bufio_socket_switch_tls(struct bufio_socket *s, struct tls *tls);

void bufio_socket_set_fd(struct bufio_socket *s, int fd);

struct bufio_source *bufio_socket_to_source(struct bufio_socket *);

struct bufio_sink *bufio_socket_to_sink(struct bufio_socket *);


#endif /* EVNET_BUFIO_SOCKET_H */


syntax highlighted by Code2HTML, v. 0.9.1