#ifndef SESSIONHANDLER_H
#define SESSIONHANDLER_H
#include "../util/thrpool.h"
#include "../public/nsapi.h"
#include "../util/io.h"
#include "event.h"
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct _session_handler SessionHandler;
typedef struct _connection Connection;
typedef union ConnectionAddr ConnectionAddr;
union ConnectionAddr {
struct sockaddr_in address_v4;
struct sockaddr_in6 address_v6;
};
enum ConnectionAddrType {
CONN_ADDR_IPV4 =
0,
CONN_ADDR_IPV6
};
typedef enum ConnectionAddrType ConnectionAddrType;
struct _connection {
uint64_t id;
int fd;
ConnectionAddr address;
ConnectionAddrType addr_type;
HttpListener *listener;
SessionHandler *session_handler;
SSL *ssl;
WSBool ssl_accepted;
int ssl_error;
int (*read)(Connection *conn,
void *buf,
int len);
int (*write)(Connection *conn,
const void *buf,
int len);
void (*close)(Connection *conn);
};
typedef void(*enqueue_connection_f)(SessionHandler*, Connection*);
typedef void(*keep_alive_f)(SessionHandler*, Connection*);
struct _session_handler {
void(*enqueue_connection)(SessionHandler *sh, Connection *conn);
void(*keep_alive)(SessionHandler*, Connection *conn);
IOStream*(*create_iostream)(SessionHandler *sh, Connection *conn,
pool_handle_t *pool, WSBool *ssl);
};
typedef struct _basic_session_handler {
SessionHandler sh;
threadpool_t *threadpool;
} BasicSessionHandler;
typedef struct _event_session_handler {
SessionHandler sh;
EVHandler *eventhandler;
} EventSessionHandler;
typedef struct EventHttpIO EventHttpIO;
int connection_read(Connection *conn,
void *buf,
int len);
int connection_write(Connection *conn,
const void *buf,
int len);
void connection_close(Connection *conn);
int connection_ssl_read(Connection *conn,
void *buf,
int len);
int connection_ssl_write(Connection *conn,
const void *buf,
int len);
void connection_ssl_close(Connection *conn);
void connection_destroy(Connection *conn);
IOStream* create_connection_iostream(
SessionHandler *sh,
Connection *conn,
pool_handle_t *pool,
WSBool *ssl);
SessionHandler* create_basic_session_handler(
pool_handle_t *pool);
void basic_enq_conn(SessionHandler *handler, Connection *conn);
void* basic_run_session(
void *data);
void basic_keep_alive(SessionHandler *handler, Connection *conn);
SessionHandler* create_event_session_handler(
pool_handle_t *pool);
void evt_request_timeout(EventHandler *h, EVWatchList *item);
int evt_add_request(EventHandler *h, Event *event);
void evt_enq_conn(SessionHandler *handler, Connection *conn);
EventHttpIO* evt_req_init(SessionHandler *handler, Connection *conn);
int evt_request_ssl_accept(EventHandler *handler, Event *event);
int evt_request_input(EventHandler *h, Event *event);
int evt_request_finish(EventHandler *h, Event *event);
int evt_request_error(EventHandler *h, Event *event);
void evt_keep_alive(SessionHandler *handler, Connection *conn);
int evt_keep_alive_enqueue(EventHandler *h, Event *event);
int evt_keep_alive_input_event(EventHandler *h, Event *event);
void evt_keep_alive_destroy(EventHandler *h, EVWatchList *item);
#ifdef __cplusplus
}
#endif
#endif