From e8b3babd44971d1d4b25186f88bdbea23f7bf394 Mon Sep 17 00:00:00 2001 From: Matthias Vogelgesang Date: Wed, 17 Feb 2016 15:52:25 +0100 Subject: Implement outstanding write request --- uca-net-camera.c | 30 ++++++++++++++++++++++++++++++ uca-net-protocol.h | 7 +++++++ ucad.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/uca-net-camera.c b/uca-net-camera.c index ad3606d..f2d5638 100644 --- a/uca-net-camera.c +++ b/uca-net-camera.c @@ -157,7 +157,37 @@ uca_net_camera_write (UcaCamera *camera, gsize size, GError **error) { + UcaNetCameraPrivate *priv; + GOutputStream *output; + gssize bytes_left; + gchar *buffer; + UcaNetMessageWriteRequest request = { .type = UCA_NET_MESSAGE_WRITE }; + g_return_if_fail (UCA_IS_NET_CAMERA (camera)); + + priv = UCA_NET_CAMERA_GET_PRIVATE (camera); + output = g_io_stream_get_output_stream (G_IO_STREAM (priv->connection)); + request.size = size; + strncpy (request.name, name, sizeof (request.name)); + + if (!g_output_stream_write_all (output, &request, sizeof (request), NULL, NULL, error)) + return; + + bytes_left = size; + buffer = (gchar *) data; + + while (bytes_left > 0) { + gssize written; + + written = g_output_stream_write (output, &buffer[size - bytes_left], bytes_left, NULL, error); + + if (written < 0) + return; + + bytes_left -= written; + } + + handle_default_reply (priv->connection, UCA_NET_MESSAGE_WRITE, error); } static gboolean diff --git a/uca-net-protocol.h b/uca-net-protocol.h index 9ea6699..653a26d 100644 --- a/uca-net-protocol.h +++ b/uca-net-protocol.h @@ -13,6 +13,7 @@ typedef enum { UCA_NET_MESSAGE_STOP_READOUT, UCA_NET_MESSAGE_TRIGGER, UCA_NET_MESSAGE_GRAB, + UCA_NET_MESSAGE_WRITE, UCA_NET_MESSAGE_CLOSE_CONNECTION, } UcaNetMessageType; @@ -53,4 +54,10 @@ typedef struct { gsize size; } UcaNetMessageGrabRequest; +typedef struct { + UcaNetMessageType type; + gsize size; + gchar name[128]; +} UcaNetMessageWriteRequest; + #endif diff --git a/ucad.c b/ucad.c index 6421c21..3c83f74 100644 --- a/ucad.c +++ b/ucad.c @@ -234,6 +234,42 @@ handle_grab_request (GSocketConnection *connection, UcaCamera *camera, gpointer } } +static void +handle_write_request (GSocketConnection *connection, UcaCamera *camera, gpointer message, GError **stream_error) +{ + GInputStream *input; + UcaNetMessageWriteRequest *request; + UcaNetDefaultReply reply = { .type = UCA_NET_MESSAGE_WRITE }; + gchar *buffer; + gsize bytes_left; + GError *error = NULL; + + input = g_io_stream_get_input_stream (G_IO_STREAM (connection)); + request = (UcaNetMessageWriteRequest *) message; + buffer = g_malloc0 (request->size); + bytes_left = request->size; + + while (bytes_left > 0) { + gssize read; + gchar *buffer; + + read = g_input_stream_read (input, &buffer[request->size - bytes_left], bytes_left, NULL, stream_error); + + if (read < 0) + goto handle_write_request_cleanup; + + bytes_left -= read; + } + + uca_camera_write (camera, request->name, buffer, request->size, &error); + + prepare_error_reply (error, &reply.error); + send_reply (connection, &reply, sizeof (reply), stream_error); + +handle_write_request_cleanup: + g_free (buffer); +} + static void serve_connection (GSocketConnection *connection, UcaCamera *camera) { @@ -250,6 +286,7 @@ serve_connection (GSocketConnection *connection, UcaCamera *camera) { UCA_NET_MESSAGE_STOP_READOUT, handle_stop_readout_request }, { UCA_NET_MESSAGE_TRIGGER, handle_trigger_request }, { UCA_NET_MESSAGE_GRAB, handle_grab_request }, + { UCA_NET_MESSAGE_WRITE, handle_write_request }, { UCA_NET_MESSAGE_INVALID, NULL } }; -- cgit v1.2.3