From 315627dd43585bd2ccd139e8fcc96ea3104a0903 Mon Sep 17 00:00:00 2001 From: Vic Lee Date: Sun, 10 Jul 2011 10:00:16 +0800 Subject: [PATCH] libfreerdp-utils/svc_plugin: reassemble data chunks using stream. --- include/freerdp/utils/stream.h | 1 + include/freerdp/utils/svc_plugin.h | 3 ++- libfreerdp-utils/svc_plugin.c | 38 ++++++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/include/freerdp/utils/stream.h b/include/freerdp/utils/stream.h index b62bde9e8..641fe657e 100644 --- a/include/freerdp/utils/stream.h +++ b/include/freerdp/utils/stream.h @@ -50,6 +50,7 @@ stream_extend(STREAM * stream); #define stream_get_head(_s) _s->data #define stream_get_tail(_s) _s->p #define stream_get_length(_s) (_s->p - _s->data) +#define stream_get_size(_s) (_s->size) #define stream_read_uint8(_s, _v) do { _v = *_s->p++; } while (0) #define stream_read_uint16(_s, _v) do { _v = \ diff --git a/include/freerdp/utils/svc_plugin.h b/include/freerdp/utils/svc_plugin.h index 041204743..149e2091f 100644 --- a/include/freerdp/utils/svc_plugin.h +++ b/include/freerdp/utils/svc_plugin.h @@ -24,6 +24,7 @@ /* static channel plugin base implementation */ #include +#include typedef struct rdp_svc_plugin_private rdpSvcPluginPrivate; typedef struct rdp_svc_plugin rdpSvcPlugin; @@ -33,7 +34,7 @@ struct rdp_svc_plugin CHANNEL_DEF channel_def; void (*connect_callback)(rdpSvcPlugin* plugin); - void (*receive_callback)(rdpSvcPlugin* plugin, uint8* data, int size); + void (*receive_callback)(rdpSvcPlugin* plugin, STREAM* data_in); void (*terminate_callback)(rdpSvcPlugin* plugin); rdpSvcPluginPrivate* priv; diff --git a/libfreerdp-utils/svc_plugin.c b/libfreerdp-utils/svc_plugin.c index 0d36be2ae..e2344f78d 100644 --- a/libfreerdp-utils/svc_plugin.c +++ b/libfreerdp-utils/svc_plugin.c @@ -22,9 +22,11 @@ #include #include #include +#include #include #include #include +#include #include #ifdef WITH_DEBUG_SVC @@ -50,6 +52,7 @@ struct rdp_svc_plugin_private { void* init_handle; uint32 open_handle; + STREAM* data_in; }; static rdpSvcPlugin* svc_plugin_find_by_init_handle(void* init_handle) @@ -116,7 +119,29 @@ static void svc_plugin_remove(rdpSvcPlugin* plugin) static void svc_plugin_process_received(rdpSvcPlugin* plugin, void* pData, uint32 dataLength, uint32 totalLength, uint32 dataFlags) { - plugin->receive_callback(plugin, (uint8*)pData, dataLength); + STREAM* data_in; + + if (dataFlags & CHANNEL_FLAG_FIRST) + { + if (plugin->priv->data_in != NULL) + stream_free(plugin->priv->data_in); + plugin->priv->data_in = stream_new(totalLength); + } + + data_in = plugin->priv->data_in; + stream_check_size(data_in, dataLength); + stream_write(data_in, pData, dataLength); + + if (dataFlags & CHANNEL_FLAG_LAST) + { + if (stream_get_size(data_in) != stream_get_length(data_in)) + { + printf("svc_plugin_process_received: read error\n"); + } + /* the stream ownership is passed to the callback who is responsible for freeing it. */ + plugin->priv->data_in = NULL; + plugin->receive_callback(plugin, data_in); + } } static void svc_plugin_open_event(uint32 openHandle, uint32 event, void* pData, uint32 dataLength, @@ -124,7 +149,8 @@ static void svc_plugin_open_event(uint32 openHandle, uint32 event, void* pData, { rdpSvcPlugin* plugin; - DEBUG_SVC("event %d dataLength %d", event, dataLength); + DEBUG_SVC("openHandle %d event %d dataLength %d totalLength %d dataFlags %d", + openHandle, event, dataLength, totalLength, dataFlags); plugin = (rdpSvcPlugin*)svc_plugin_find_by_open_handle(openHandle); if (plugin == NULL) @@ -160,9 +186,17 @@ static void svc_plugin_process_connected(rdpSvcPlugin* plugin, void* pData, uint static void svc_plugin_process_terminated(rdpSvcPlugin* plugin) { plugin->channel_entry_points.pVirtualChannelClose(plugin->priv->open_handle); + svc_plugin_remove(plugin); + + if (plugin->priv->data_in != NULL) + { + stream_free(plugin->priv->data_in); + plugin->priv->data_in = NULL; + } xfree(plugin->priv); plugin->priv = NULL; + plugin->terminate_callback(plugin); }