src/server/util/io.c

changeset 14
b8bf95b39952
parent 9
30e51941a673
child 20
7b235fa88008
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/server/util/io.c	Sat Jan 14 13:53:44 2012 +0100
@@ -0,0 +1,130 @@
+/*
+ * 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 net_io_funcs = {
+    net_stream_write,
+    net_stream_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);
+}
+
+
+IOStream* net_stream_from_fd(int fd) {
+    NetIOStream *st = malloc(sizeof(NetIOStream));
+    st->st = net_io_funcs;
+    st->fd = fd;
+    st->max_read = 0;
+    st->rd = 0;
+}
+
+ssize_t net_stream_write(IOStream *st, void *buf, size_t nbytes) {
+    // TODO: implement
+}
+
+ssize_t net_stream_read(IOStream *st, void *buf, size_t nbytes) {
+    NetIOStream *n = (NetIOStream*)st;
+    if(n->max_read != 0 && n->rd >= n->max_read) {
+        return 0;
+    }
+    ssize_t r = read(n->fd, buf, nbytes);
+    n->rd += r;
+    return r;
+}
+
+
+ssize_t net_read(SYS_NETFD fd, void *buf, size_t nbytes) {
+    ssize_t r = ((IOStream*)fd)->read(fd, buf, nbytes);
+    if(r == 0) {
+        return IO_EOF;
+    }
+    return r;
+}
+
+ssize_t net_write(SYS_NETFD fd, void *buf, size_t nbytes) {
+    ssize_t r = ((IOStream*)fd)->write(fd, buf, nbytes);
+    if(r < 0) {
+        return IO_ERROR;
+    }
+    return r;
+}
+
+
+/* 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);
+}

mercurial