Skip to content

Instantly share code, notes, and snippets.

@grobian
Last active July 11, 2018 19:05
Show Gist options
  • Save grobian/3b85f74f038415cfb90bfcf178a20b54 to your computer and use it in GitHub Desktop.
Save grobian/3b85f74f038415cfb90bfcf178a20b54 to your computer and use it in GitHub Desktop.
switch to buffered writes from stdio
diff --git a/server.c b/server.c
index 6530e69..57f59a1 100644
--- a/server.c
+++ b/server.c
@@ -111,20 +111,19 @@ typedef struct _z_strm {
static inline ssize_t
sockwrite(void *strm, const void *buf, size_t sze)
{
- return write(*((int *)strm), buf, sze);
+ return fwrite(buf, sze, 1, (FILE *)strm) * sze;
}
static inline int
sockflush(void *strm)
{
- /* noop, we don't use a stream in the normal case */
- return 0;
+ return fflush((FILE *)strm);
}
static inline int
sockclose(void *strm)
{
- return close(*((int *)strm));
+ return fclose((FILE *)strm);
}
static inline const char *
@@ -615,6 +614,15 @@ server_queuereader(void *d)
}
#endif
+ if (self->transport == W_PLAIN) {
+ self->strm = fdopen(self->fd, "w");
+ if (self->strm == NULL) {
+ logerr("failed to open stream: %s\n", strerror(errno));
+ close(self->fd);
+ self->fd = -1;
+ continue;
+ }
+ }
#ifdef HAVE_GZIP
if (self->transport == W_GZIP) {
self->strm = gzdopen(self->fd, "w");
@@ -813,7 +821,6 @@ server_new(
}
ret->fd = -1;
if (transport == W_PLAIN) {
- ret->strm = &(ret->fd);
ret->strmwrite = &sockwrite;
ret->strmflush = &sockflush;
ret->strmclose = &sockclose;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment