Sun, 13 Nov 2011 13:43:01 +0100
Added some protocol functions
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2011 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. */ #include <unistd.h> #include <stdlib.h> #include <sys/uio.h> #include "io.h" #include "pool.h" IOStream native_io_funcs = { system_write, system_read }; IOStream* stream_new_from_fd(int fd) { SystemIOStream *st = malloc(sizeof(SystemIOStream)); st->st = native_io_funcs; st->fd = fd; return (IOStream*)st; } ssize_t system_write(IOStream *st, void *buf, size_t nbytes) { return write(((SystemIOStream*)st)->fd, buf, nbytes); } ssize_t system_read(IOStream *st, void *buf, size_t nbytes) { return read(((SystemIOStream*)st)->fd, buf, nbytes); } /* iovec buffer */ iovec_buf_t *iovec_buf_create(pool_handle_t *pool) { iovec_buf_t *buf = pool_malloc(pool, sizeof(iovec_buf_t)); buf->pool = pool; buf->iov = pool_calloc(pool, 32, sizeof(struct iovec)); buf->maxiovec = 32; buf->iovctn = 0; return buf; } void iovec_buf_write(iovec_buf_t *io, void *buf, size_t nbyte) { if(io->iovctn >= io->maxiovec) { io->iov = pool_realloc( io->pool, io->iov, (io->maxiovec + 16) * sizeof(struct iovec)); } io->iov[io->iovctn].iov_base = buf; io->iov[io->iovctn].iov_len = nbyte; io->iovctn++; } ssize_t iovec_buf_flush(iovec_buf_t *io, int fd) { return writev(fd, io->iov, io->iovctn); }