From e396824ed2d6514a62302c5e5591cacfbb8cd29b Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 6 Mar 2016 16:36:58 -0800 Subject: Special implementation of dgram socket streams. * socket.c (struct dgram_stream): New struct. (make_dgram_sock_stream, dgram_print, dgram_mark, dgram_destroy, dgram_overflow, dgram_put_byte_callback, dgram_put_string, dgram_put_char, dgram_put_byte, dgram_get_byte_callback, dgram_get_char, dgram_get_byte, dgram_unget_char, dgram_unget_byte, dgram_flush, dgram_close, dgram_get_prop, dgram_get_fd, dgram_get_sock_family, dgram_get_sock_type, dgram_get_sock_peer, dgram_set_sock_peer, dgram_get_error, dgram_get_error_str, dgram_clear_error): New static functions. (dgram_strm_ops): New static structure. (sock_listen): Check against datagram sockets which have a peer; otherwise a no-op for datagram sockets. (sock_accept): Special logic for dgram sockets. (open_sockfd): Function moved here from stream.c, and augmented to open dgram stream for dgram sockets. (open_socket): Function moved here from stream.c. (sock_load_init): Fill in some operations in dgram_strm_ops. * stream.c (generic_get_line, make_sock_stream, errno_to_string): Statics become external. (open_sockfd, open_socket): Functions removed from here, moved to socket.c. * stream.h (generic_get_line, make_sock_stream, errno_to_string): Declared. --- stream.c | 35 +++-------------------------------- 1 file changed, 3 insertions(+), 32 deletions(-) (limited to 'stream.c') diff --git a/stream.c b/stream.c index cbb6821a..df1faa2d 100644 --- a/stream.c +++ b/stream.c @@ -406,7 +406,7 @@ static void stdio_stream_mark(val stream) #endif } -static val errno_to_string(val err) +val errno_to_string(val err) { if (err == zero) return lit("unspecified error"); @@ -608,7 +608,7 @@ static val stdio_get_fd(val stream) return h->f ? num(fileno(h->f)) : nil; } -static val generic_get_line(val stream) +val generic_get_line(val stream) { struct strm_ops *ops = coerce(struct strm_ops *, cobj_ops(stream, stream_s)); const size_t min_size = 512; @@ -1233,7 +1233,7 @@ val make_pipe_stream(FILE *f, val descr) } #if HAVE_SOCKETS -static val make_sock_stream(FILE *f, val family, val type) +val make_sock_stream(FILE *f, val family, val type) { val s = make_stdio_stream_common(f, lit("socket"), &stdio_sock_ops.cobj_ops); struct stdio_handle *h = coerce(struct stdio_handle *, s->co.handle); @@ -3249,35 +3249,6 @@ val open_fileno(val fd, val mode_str) fd, nao))); } -#if HAVE_SOCKETS -val open_sockfd(val fd, val family, val type, val mode_str_in) -{ - struct stdio_mode m; - val mode_str = default_arg(mode_str_in, lit("r+")); - FILE *f = (errno = 0, w_fdopen(c_num(fd), c_str(normalize_mode(&m, mode_str)))); - - if (!f) { - close(c_num(fd)); - uw_throwf(file_error_s, lit("error creating stream for socket ~a: ~d/~s"), - fd, num(errno), string_utf8(strerror(errno)), nao); - } - - if (type == num_fast(SOCK_DGRAM)) - setvbuf(f, (char *) NULL, _IOFBF, 65536); - else - setvbuf(f, (char *) NULL, _IOLBF, 0); - - return set_mode_props(m, make_sock_stream(f, family, type)); -} - -val open_socket(val family, val type, val mode_str) -{ - int fd = socket(c_num(family), c_num(type), 0); - return open_sockfd(num(fd), family, type, mode_str); -} -#endif - - val open_tail(val path, val mode_str, val seek_end_p) { struct stdio_mode m; -- cgit v1.2.3