int http_serve_template ( http_request *h, gchar *file, GHashTable *data ) { gchar *form; guint r, n; form = parse_template( file, data ); n = strlen(form); http_add_header( h, "Content-Type", "text/html" ); http_send_header( h, 200, "OK" ); r = g_io_channel_write( h->sock, form, n, &n ); g_free( form ); if ( r != G_IO_ERROR_NONE ) { g_warning( "Serving template to %s failed: %m", h->peer_ip ); return 0; } return 1; }
static gboolean control_channel_output(GIOChannel *channel, GIOCondition cond, gpointer user_data) { ControlConnectionOutput *output = (ControlConnectionOutput *) user_data; GIOError error; gsize bytes_written; /* NOTE: write is deprecated as it bypasses the output buffer. But using * buffered I/O is incompatible with using nonblocking i/o and we don't * need buffering anyway */ error = g_io_channel_write(channel, output->buffer->str + output->pos, output->buffer->len - output->pos, &bytes_written); if (error == G_IO_ERROR_AGAIN) return TRUE; else if (error != G_IO_ERROR_NONE) { msg_error("Error writing control channel", evt_tag_errno("error", errno), NULL); return FALSE; } /* normal, some characters were written */ output->pos += bytes_written; if (output->pos == output->buffer->len) { /**/ g_string_free(output->buffer, TRUE); g_free(output); g_io_channel_set_flags(channel, 0, NULL); g_io_add_watch(channel, G_IO_IN, control_channel_input, NULL); return FALSE; } return TRUE; }
static GIOError gst_avdtp_sink_audioservice_send( GstAvdtpSink *self, const bt_audio_msg_header_t *msg) { GIOError error; gsize written; const char *type, *name; uint16_t length; length = msg->length ? msg->length : BT_SUGGESTED_BUFFER_SIZE; error = g_io_channel_write(self->server, (const gchar *) msg, length, &written); if (error != G_IO_ERROR_NONE) GST_ERROR_OBJECT(self, "Error sending data to audio service:" " %s(%d)", strerror(errno), errno); type = bt_audio_strtype(msg->type); name = bt_audio_strname(msg->name); GST_DEBUG_OBJECT(self, "sent: %s -> %s", type, name); return error; }
static gboolean session_event(GIOChannel *chan, GIOCondition cond, gpointer data) { struct dun_server *server = data; unsigned char buf[672]; gsize len, written; GIOError err; if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) goto disconnected; err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len); if (err == G_IO_ERROR_AGAIN) return TRUE; g_io_channel_write(chan, (const gchar *) buf, len, &written); return TRUE; disconnected: server->client_id = 0; disconnect(server); return FALSE; }
/* This callback is called for (IN|ERR) or OUT. We add the watch for (IN|ERR) when the client connects in async_server_iofunc(). We remove it if the connection is closed (i.e. we read 0 bytes) or if there's an error. We add the watch for OUT when we have something to write and remove it when we're done writing or when the connection is closed. */ static gboolean async_client_iofunc(GIOChannel *iochannel, GIOCondition c, gpointer data) { client_state *cs = (client_state *) data; g_assert(cs != NULL); /* Check for socket error */ if (c & G_IO_ERR) { fprintf(stderr, "Client socket error!\n"); goto error; } /* Check for data to be read (or if the socket was closed) */ if (c & G_IO_IN) { GIOError e; gsize bytes_read; /* Read the data into our buffer */ e = g_io_channel_read(iochannel, &cs->buffer[cs->n], sizeof(cs->buffer) - cs->n, &bytes_read); /* Check for socket error */ if (e != G_IO_ERROR_NONE) { fprintf(stderr, "Client read error: %d\n", e); goto error; } else if (bytes_read == 0) { /* Check if we read 0 bytes, which means the connection is closed */ goto error; } else { g_assert(bytes_read > 0); /* If there isn't an out_watch, add one */ if (cs->out_watch == 0) { cs->out_watch = g_io_add_watch(iochannel, G_IO_OUT, async_client_iofunc, cs); } if (fwrite(&cs->buffer[cs->n], bytes_read, 1, stdout) != 1) { fprintf (stderr, "Error: fwrite to stdout failed: %s\n", g_strerror (errno)); } cs->n += bytes_read; } } if (c & G_IO_OUT) { GIOError e; gsize bytes_written; /* Write the data out */ e = g_io_channel_write(iochannel, cs->buffer, cs->n, &bytes_written); if (e != G_IO_ERROR_NONE) { fprintf(stderr, "Client write error: %d\n", e); clientstate_delete(cs); return FALSE; } else if (bytes_written > 0) { /* Move the memory down some (you wouldn't do this in a performance server because it's slow) */ g_memmove(cs->buffer, &cs->buffer[bytes_written], bytes_written); cs->n -= bytes_written; } /* Check if done */ if (cs->n == 0) { cs->out_watch = 0; return FALSE; } } return TRUE; error: clientstate_delete(cs); return FALSE; }