src/server/io.c

changeset 8
f4d56bf9de40
parent 4
998844b5ed25
child 9
30e51941a673
equal deleted inserted replaced
7:3c2ed7a7a5fd 8:f4d56bf9de40
36 IOStream native_io_funcs = { 36 IOStream native_io_funcs = {
37 system_write, 37 system_write,
38 system_read 38 system_read
39 }; 39 };
40 40
41 IOStream net_io_funcs = {
42 net_stream_write,
43 net_stream_read
44 };
45
41 46
42 IOStream* stream_new_from_fd(int fd) { 47 IOStream* stream_new_from_fd(int fd) {
43 SystemIOStream *st = malloc(sizeof(SystemIOStream)); 48 SystemIOStream *st = malloc(sizeof(SystemIOStream));
44 st->st = native_io_funcs; 49 st->st = native_io_funcs;
45 st->fd = fd; 50 st->fd = fd;
50 return write(((SystemIOStream*)st)->fd, buf, nbytes); 55 return write(((SystemIOStream*)st)->fd, buf, nbytes);
51 } 56 }
52 57
53 ssize_t system_read(IOStream *st, void *buf, size_t nbytes) { 58 ssize_t system_read(IOStream *st, void *buf, size_t nbytes) {
54 return read(((SystemIOStream*)st)->fd, buf, nbytes); 59 return read(((SystemIOStream*)st)->fd, buf, nbytes);
60 }
61
62
63 IOStream* net_stream_from_fd(int fd) {
64 NetIOStream *st = malloc(sizeof(NetIOStream));
65 st->st = net_io_funcs;
66 st->fd = fd;
67 st->max_read = 0;
68 st->rd = 0;
69 }
70
71 ssize_t net_stream_write(IOStream *st, void *buf, size_t nbytes) {
72 // TODO: implement
73 }
74
75 ssize_t net_stream_read(IOStream *st, void *buf, size_t nbytes) {
76 NetIOStream *n = (NetIOStream*)st;
77 if(n->max_read != 0 && n->rd >= n->max_read) {
78 return 0;
79 }
80 ssize_t r = read(n->fd, buf, nbytes);
81 n->rd += r;
82 return r;
83 }
84
85
86 ssize_t net_read(SYS_NETFD fd, void *buf, size_t nbytes) {
87 ssize_t r = ((IOStream*)fd)->read(fd, buf, nbytes);
88 if(r == 0) {
89 return IO_EOF;
90 }
91 return r;
55 } 92 }
56 93
57 94
58 /* iovec buffer */ 95 /* iovec buffer */
59 iovec_buf_t *iovec_buf_create(pool_handle_t *pool) { 96 iovec_buf_t *iovec_buf_create(pool_handle_t *pool) {

mercurial