From 8a5c78bde62ca26cf1a6b06e031a3016d8e98671 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Thu, 31 Aug 2017 09:06:59 +0200 Subject: [PATCH] Unified dynamic channel cleanup code. --- channels/drdynvc/client/drdynvc_main.c | 78 ++++++++++++++++------------------ 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/channels/drdynvc/client/drdynvc_main.c b/channels/drdynvc/client/drdynvc_main.c index 4c49dbb..2e53b78 100644 --- a/channels/drdynvc/client/drdynvc_main.c +++ b/channels/drdynvc/client/drdynvc_main.c @@ -297,27 +297,43 @@ static DVCMAN_CHANNEL* dvcman_channel_new(IWTSVirtualChannelManager* static void dvcman_channel_free(void* arg) { DVCMAN_CHANNEL* channel = (DVCMAN_CHANNEL*) arg; + UINT error = CHANNEL_RC_OK; - if (channel->channel_callback) + if (channel) { - channel->channel_callback->OnClose(channel->channel_callback); - channel->channel_callback = NULL; - } + if (channel->channel_callback) + { + IFCALL(channel->channel_callback->OnClose, + channel->channel_callback); + } - if (channel->dvc_data) - { - Stream_Release(channel->dvc_data); - channel->dvc_data = NULL; - } + if (channel->status == CHANNEL_RC_OK) + { + IWTSVirtualChannel* ichannel = (IWTSVirtualChannel*) channel; + + if (channel->dvcman && channel->dvcman->drdynvc) + { + DrdynvcClientContext* context = channel->dvcman->drdynvc->context; + if (context) + { + IFCALLRET(context->OnChannelDisconnected, error, + context, channel->channel_name, + channel->pInterface); + } + } - DeleteCriticalSection(&(channel->lock)); + error = IFCALLRESULT(CHANNEL_RC_OK, ichannel->Close, ichannel); + if (error != CHANNEL_RC_OK) + WLog_ERR(TAG, "Close failed with error %"PRIu32"!", error); + } + + if (channel->dvc_data) + Stream_Release(channel->dvc_data); + + DeleteCriticalSection(&(channel->lock)); - if (channel->channel_name) - { free(channel->channel_name); - channel->channel_name = NULL; } - free(channel); } @@ -408,6 +424,7 @@ static UINT dvcman_write_channel(IWTSVirtualChannel* pChannel, ULONG cbSize, */ static UINT dvcman_close_channel_iface(IWTSVirtualChannel* pChannel) { + DVCMAN_CHANNEL* channel = (DVCMAN_CHANNEL*) pChannel; if (!channel) @@ -440,7 +457,7 @@ static UINT dvcman_create_channel(IWTSVirtualChannelManager* pChannelMgr, return CHANNEL_RC_NO_MEMORY; } - channel->status = 1; + channel->status = ERROR_NOT_CONNECTED; ArrayList_Add(dvcman->channels, channel); for (i = 0; i < dvcman->num_listeners; i++) @@ -461,7 +478,7 @@ static UINT dvcman_create_channel(IWTSVirtualChannelManager* pChannelMgr, { WLog_DBG(TAG, "listener %s created new channel %"PRIu32"", listener->channel_name, channel->channel_id); - channel->status = 0; + channel->status = CHANNEL_RC_OK; channel->channel_callback = pCallback; channel->pInterface = listener->iface.pInterface; context = dvcman->drdynvc->context; @@ -536,10 +553,9 @@ static UINT dvcman_close_channel(IWTSVirtualChannelManager* pChannelMgr, UINT32 ChannelId) { DVCMAN_CHANNEL* channel; - IWTSVirtualChannel* ichannel; - DrdynvcClientContext* context; - DVCMAN* dvcman = (DVCMAN*) pChannelMgr; UINT error = CHANNEL_RC_OK; + DVCMAN* dvcman = (DVCMAN*) pChannelMgr; + channel = (DVCMAN_CHANNEL*) dvcman_find_channel_by_id(pChannelMgr, ChannelId); if (!channel) @@ -552,30 +568,8 @@ static UINT dvcman_close_channel(IWTSVirtualChannelManager* pChannelMgr, return CHANNEL_RC_OK; } - if (channel->status == CHANNEL_RC_OK) - { - context = dvcman->drdynvc->context; - IFCALLRET(context->OnChannelDisconnected, error, context, channel->channel_name, - channel->pInterface); - - if (error) - { - WLog_ERR(TAG, "OnChannelDisconnected returned with error %"PRIu32"!", error); - return error; - } - - WLog_DBG(TAG, "dvcman_close_channel: channel %"PRIu32" closed", ChannelId); - ichannel = (IWTSVirtualChannel*) channel; - - if ((ichannel->Close) && (error = ichannel->Close(ichannel))) - { - WLog_ERR(TAG, "Close failed with error %"PRIu32"!", error); - return error; - } - } - ArrayList_Remove(dvcman->channels, channel); - return CHANNEL_RC_OK; + return error; } /** -- 2.7.4