#ifndef IOSTREAM_H
#define IOSTREAM_H
#include <openssl/ssl.h>
#include "../public/nsapi.h"
#include "systems.h"
#ifdef _WIN32
#include <windows.h>
#include <winsock2.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef XP_UNIX
#define SYS_SOCKET int
#elif defined(
XP_WIN32)
#define SYS_SOCKET SOCKET
#endif
#define IO_MODE_BLOCKING 0
#define IO_MODE_NONBLOCKING 1
#define IO_POLL_NONE 0
#define IO_POLL_IN 1
#define IO_POLL_OUT 2
typedef struct IOStream IOStream;
typedef struct SysStream SysStream;
typedef struct HttpStream HttpStream;
typedef ssize_t(*io_write_f)(IOStream *,
void *,
size_t);
typedef ssize_t(*io_writev_f)(IOStream *,
struct iovec *,
int);
typedef ssize_t(*io_read_f)(IOStream *,
void *,
size_t);
typedef ssize_t(*io_sendfile_f)(IOStream *, sendfiledata *);
typedef void(*io_close_f)(IOStream *);
typedef void(*io_finish_f)(IOStream *);
typedef void(*io_setmode_f)(IOStream *,
int);
typedef int(*io_poll_f)(IOStream *, EventHandler *,
int, Event *);
struct IOStream {
io_write_f write;
io_writev_f writev;
io_read_f read;
io_sendfile_f sendfile;
io_close_f close;
io_finish_f finish;
io_setmode_f setmode;
io_poll_f poll;
int io_errno;
};
struct SysStream {
IOStream st;
#ifdef XP_UNIX
int fd;
#elif defined(
XP_WIN32)
SOCKET fd;
#endif
};
struct HttpStream {
IOStream st;
IOStream *fd;
uint64_t max_read;
uint64_t read;
WSBool chunked_enc;
WSBool buffered;
};
typedef struct SSLStream {
IOStream st;
SSL *ssl;
int error;
} SSLStream;
IOStream* sysstream_new(
pool_handle_t *pool,
SYS_SOCKET fd);
ssize_t net_sys_write(SysStream *st,
void *buf,
size_t nbytes);
ssize_t net_sys_writev(SysStream *st,
struct iovec *iovec,
int iovcnt);
ssize_t net_sys_read(SysStream *st,
void *buf,
size_t nbytes);
ssize_t net_sys_sendfile(SysStream *st, sendfiledata *sfd);
void net_sys_close(SysStream *st);
void net_sys_setmode(SysStream *st,
int mode);
int net_sys_poll(SysStream *st, EventHandler *ev,
int events, Event *cb);
IOStream* httpstream_new(
pool_handle_t *pool, IOStream *fd);
ssize_t net_http_write(HttpStream *st,
void *buf,
size_t nbytes);
ssize_t net_http_writev(HttpStream *st,
struct iovec *iovec,
int iovcnt);
ssize_t net_http_read(HttpStream *st,
void *buf,
size_t nbytes);
ssize_t net_http_sendfile(HttpStream *st, sendfiledata *sfd);
void net_http_close(HttpStream *st);
void net_http_finish(HttpStream *st);
void net_http_setmode(HttpStream *st,
int mode);
int net_http_poll(HttpStream *st, EventHandler *ev,
int events, Event *cb);
IOStream* sslstream_new(
pool_handle_t *pool,
SSL *ssl);
ssize_t net_ssl_write(SSLStream *st,
void *buf,
size_t nbytes);
ssize_t net_ssl_writev(SSLStream *st,
struct iovec *iovec,
int iovcnt);
ssize_t net_ssl_read(SSLStream *st,
void *buf,
size_t nbytes);
void net_ssl_close(SSLStream *st);
void net_ssl_finish(SSLStream *st);
void net_ssl_setmode(SSLStream *st,
int mode);
int net_ssl_poll(SSLStream *st, EventHandler *ev,
int events, Event *cb);
ssize_t net_fallback_sendfile(IOStream *fd, sendfiledata *sfd);
void net_finish(
SYS_NETFD fd);
#ifdef __cplusplus
}
#endif
#endif