diff options
author | Matthias Vogelgesang <matthias.vogelgesang@kit.edu> | 2016-02-17 15:52:25 +0100 |
---|---|---|
committer | Matthias Vogelgesang <matthias.vogelgesang@kit.edu> | 2016-02-17 15:52:25 +0100 |
commit | e8b3babd44971d1d4b25186f88bdbea23f7bf394 (patch) | |
tree | 6f59998cbd157df14dc444f66f302f66b85c6ec0 | |
parent | 9da4e6a2c7ad73a0eb4f377533179eb606a14285 (diff) | |
download | uca-net-e8b3babd44971d1d4b25186f88bdbea23f7bf394.tar.gz uca-net-e8b3babd44971d1d4b25186f88bdbea23f7bf394.tar.bz2 uca-net-e8b3babd44971d1d4b25186f88bdbea23f7bf394.tar.xz uca-net-e8b3babd44971d1d4b25186f88bdbea23f7bf394.zip |
Implement outstanding write request
-rw-r--r-- | uca-net-camera.c | 30 | ||||
-rw-r--r-- | uca-net-protocol.h | 7 | ||||
-rw-r--r-- | ucad.c | 37 |
3 files changed, 74 insertions, 0 deletions
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 @@ -235,6 +235,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) { GInputStream *input; @@ -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 } }; |