src/server/util/io.h

Sat, 13 Jan 2018 18:48:19 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 13 Jan 2018 18:48:19 +0100
branch
aio
changeset 192
6a145e13d933
parent 172
5580517faafc
child 193
aa8393527b1e
permissions
-rw-r--r--

replaces eventfd with pipe and closes aio branch

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#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;



/* system stream */
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);

/* http stream */
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);

/* ssl stream */
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);

/* net_ functions */
ssize_t net_fallback_sendfile(IOStream *fd, sendfiledata *sfd);
void    net_finish(SYS_NETFD fd);

#ifdef	__cplusplus
}
#endif

#endif	/* IOSTREAM_H */

mercurial