freerdp: remove deprecated svc_plugin.c utils

This commit is contained in:
Marc-André Moreau 2014-11-12 13:34:07 -05:00
parent 0e7c95c421
commit 986dae429b
5 changed files with 281 additions and 446 deletions

View File

@ -21,10 +21,6 @@
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winpr/crt.h>
#include <winpr/stream.h>
@ -57,6 +53,29 @@ static int drdynvc_write_variable_uint(wStream* s, UINT32 val)
return cb;
}
int drdynvc_send(drdynvcPlugin* drdynvc, wStream* s)
{
UINT32 status = 0;
if (!drdynvc)
{
status = CHANNEL_RC_BAD_INIT_HANDLE;
}
else
{
status = drdynvc->channelEntryPoints.pVirtualChannelWrite(drdynvc->OpenHandle,
Stream_Buffer(s), (UINT32) Stream_GetPosition(s), s);
}
if (status != CHANNEL_RC_OK)
{
Stream_Free(s, TRUE);
WLog_ERR(TAG, "drdynvc_send: VirtualChannelWrite failed %d", status);
}
return status;
}
int drdynvc_write_data(drdynvcPlugin* drdynvc, UINT32 ChannelId, BYTE* data, UINT32 dataSize)
{
wStream* data_out;
@ -81,7 +100,8 @@ int drdynvc_write_data(drdynvcPlugin* drdynvc, UINT32 ChannelId, BYTE* data, UIN
Stream_SetPosition(data_out, 0);
Stream_Write_UINT8(data_out, 0x40 | cbChId);
Stream_SetPosition(data_out, pos);
status = svc_plugin_send((rdpSvcPlugin*) drdynvc, data_out);
status = drdynvc_send(drdynvc, data_out);
}
else if (dataSize <= CHANNEL_CHUNK_LENGTH - pos)
{
@ -90,7 +110,8 @@ int drdynvc_write_data(drdynvcPlugin* drdynvc, UINT32 ChannelId, BYTE* data, UIN
Stream_Write_UINT8(data_out, 0x30 | cbChId);
Stream_SetPosition(data_out, pos);
Stream_Write(data_out, data, dataSize);
status = svc_plugin_send((rdpSvcPlugin*) drdynvc, data_out);
status = drdynvc_send(drdynvc, data_out);
}
else
{
@ -100,11 +121,15 @@ int drdynvc_write_data(drdynvcPlugin* drdynvc, UINT32 ChannelId, BYTE* data, UIN
Stream_SetPosition(data_out, 0);
Stream_Write_UINT8(data_out, 0x20 | cbChId | (cbLen << 2));
Stream_SetPosition(data_out, pos);
chunkLength = CHANNEL_CHUNK_LENGTH - pos;
Stream_Write(data_out, data, chunkLength);
data += chunkLength;
dataSize -= chunkLength;
status = svc_plugin_send((rdpSvcPlugin*) drdynvc, data_out);
status = drdynvc_send(drdynvc, data_out);
while (status == CHANNEL_RC_OK && dataSize > 0)
{
@ -118,12 +143,16 @@ int drdynvc_write_data(drdynvcPlugin* drdynvc, UINT32 ChannelId, BYTE* data, UIN
Stream_SetPosition(data_out, pos);
chunkLength = dataSize;
if (chunkLength > CHANNEL_CHUNK_LENGTH - pos)
chunkLength = CHANNEL_CHUNK_LENGTH - pos;
Stream_Write(data_out, data, chunkLength);
data += chunkLength;
dataSize -= chunkLength;
status = svc_plugin_send((rdpSvcPlugin*)drdynvc, data_out);
status = drdynvc_send(drdynvc, data_out);
}
}
@ -146,7 +175,7 @@ static int drdynvc_send_capability_response(drdynvcPlugin* drdynvc)
Stream_Write_UINT16(s, 0x0050); /* Cmd+Sp+cbChId+Pad. Note: MSTSC sends 0x005c */
Stream_Write_UINT16(s, drdynvc->version);
status = svc_plugin_send((rdpSvcPlugin*) drdynvc, s);
status = drdynvc_send(drdynvc, s);
if (status != CHANNEL_RC_OK)
{
@ -251,7 +280,7 @@ static int drdynvc_process_create_request(drdynvcPlugin* drdynvc, int Sp, int cb
Stream_Write_UINT32(data_out, (UINT32)(-1));
}
status = svc_plugin_send((rdpSvcPlugin*) drdynvc, data_out);
status = drdynvc_send(drdynvc, data_out);
if (status != CHANNEL_RC_OK)
{
@ -297,10 +326,10 @@ static int drdynvc_process_data(drdynvcPlugin* drdynvc, int Sp, int cbChId, wStr
static int drdynvc_process_close_request(drdynvcPlugin* drdynvc, int Sp, int cbChId, wStream* s)
{
UINT32 ChannelId;
wStream* data_out;
int value;
int error;
UINT32 ChannelId;
wStream* data_out;
ChannelId = drdynvc_read_variable_uint(s, cbChId);
DEBUG_DVC("ChannelId=%d", ChannelId);
@ -308,9 +337,11 @@ static int drdynvc_process_close_request(drdynvcPlugin* drdynvc, int Sp, int cbC
data_out = Stream_New(NULL, 4);
value = (CLOSE_REQUEST_PDU << 4) | (cbChId & 0x03);
Stream_Write_UINT8(data_out, value);
drdynvc_write_variable_uint(data_out, ChannelId);
error = svc_plugin_send((rdpSvcPlugin*) drdynvc, data_out);
error = drdynvc_send(drdynvc, data_out);
if (error != CHANNEL_RC_OK)
{
@ -323,15 +354,15 @@ static int drdynvc_process_close_request(drdynvcPlugin* drdynvc, int Sp, int cbC
return 0;
}
static void drdynvc_process_receive(rdpSvcPlugin* plugin, wStream* s)
static void drdynvc_order_recv(drdynvcPlugin* drdynvc, wStream* s)
{
int value;
int Cmd;
int Sp;
int cbChId;
drdynvcPlugin* drdynvc = (drdynvcPlugin*) plugin;
Stream_Read_UINT8(s, value);
Cmd = (value & 0xf0) >> 4;
Sp = (value & 0x0c) >> 2;
cbChId = (value & 0x03) >> 0;
@ -366,19 +397,172 @@ static void drdynvc_process_receive(rdpSvcPlugin* plugin, wStream* s)
}
}
static void drdynvc_process_connect(rdpSvcPlugin* plugin)
/****************************************************************************************/
static wListDictionary* g_InitHandles;
static wListDictionary* g_OpenHandles;
void drdynvc_add_init_handle_data(void* pInitHandle, void* pUserData)
{
if (!g_InitHandles)
g_InitHandles = ListDictionary_New(TRUE);
ListDictionary_Add(g_InitHandles, pInitHandle, pUserData);
}
void* drdynvc_get_init_handle_data(void* pInitHandle)
{
void* pUserData = NULL;
pUserData = ListDictionary_GetItemValue(g_InitHandles, pInitHandle);
return pUserData;
}
void drdynvc_remove_init_handle_data(void* pInitHandle)
{
ListDictionary_Remove(g_InitHandles, pInitHandle);
}
void drdynvc_add_open_handle_data(DWORD openHandle, void* pUserData)
{
void* pOpenHandle = (void*) (size_t) openHandle;
if (!g_OpenHandles)
g_OpenHandles = ListDictionary_New(TRUE);
ListDictionary_Add(g_OpenHandles, pOpenHandle, pUserData);
}
void* drdynvc_get_open_handle_data(DWORD openHandle)
{
void* pUserData = NULL;
void* pOpenHandle = (void*) (size_t) openHandle;
pUserData = ListDictionary_GetItemValue(g_OpenHandles, pOpenHandle);
return pUserData;
}
void drdynvc_remove_open_handle_data(DWORD openHandle)
{
void* pOpenHandle = (void*) (size_t) openHandle;
ListDictionary_Remove(g_OpenHandles, pOpenHandle);
}
static void drdynvc_virtual_channel_event_data_received(drdynvcPlugin* drdynvc,
void* pData, UINT32 dataLength, UINT32 totalLength, UINT32 dataFlags)
{
wStream* data_in;
if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
{
return;
}
if (dataFlags & CHANNEL_FLAG_FIRST)
{
if (drdynvc->data_in)
Stream_Free(drdynvc->data_in, TRUE);
drdynvc->data_in = Stream_New(NULL, totalLength);
}
data_in = drdynvc->data_in;
Stream_EnsureRemainingCapacity(data_in, (int) dataLength);
Stream_Write(data_in, pData, dataLength);
if (dataFlags & CHANNEL_FLAG_LAST)
{
if (Stream_Capacity(data_in) != Stream_GetPosition(data_in))
{
WLog_ERR(TAG, "drdynvc_plugin_process_received: read error");
}
drdynvc->data_in = NULL;
Stream_SealLength(data_in);
Stream_SetPosition(data_in, 0);
MessageQueue_Post(drdynvc->MsgPipe->In, NULL, 0, (void*) data_in, NULL);
}
}
static VOID VCAPITYPE drdynvc_virtual_channel_open_event(DWORD openHandle, UINT event,
LPVOID pData, UINT32 dataLength, UINT32 totalLength, UINT32 dataFlags)
{
drdynvcPlugin* drdynvc;
drdynvc = (drdynvcPlugin*) drdynvc_get_open_handle_data(openHandle);
if (!drdynvc)
{
WLog_ERR(TAG, "drdynvc_virtual_channel_open_event: error no match");
return;
}
switch (event)
{
case CHANNEL_EVENT_DATA_RECEIVED:
drdynvc_virtual_channel_event_data_received(drdynvc, pData, dataLength, totalLength, dataFlags);
break;
case CHANNEL_EVENT_WRITE_COMPLETE:
Stream_Free((wStream*) pData, TRUE);
break;
case CHANNEL_EVENT_USER:
break;
}
}
static void* drdynvc_virtual_channel_client_thread(void* arg)
{
wStream* data;
wMessage message;
drdynvcPlugin* drdynvc = (drdynvcPlugin*) arg;
while (1)
{
if (!MessageQueue_Wait(drdynvc->MsgPipe->In))
break;
if (MessageQueue_Peek(drdynvc->MsgPipe->In, &message, TRUE))
{
if (message.id == WMQ_QUIT)
break;
if (message.id == 0)
{
data = (wStream*) message.wParam;
drdynvc_order_recv(drdynvc, data);
}
}
}
ExitThread(0);
return NULL;
}
static void drdynvc_virtual_channel_event_connected(drdynvcPlugin* drdynvc, LPVOID pData, UINT32 dataLength)
{
UINT32 status;
UINT32 index;
ADDIN_ARGV* args;
rdpSettings* settings;
drdynvcPlugin* drdynvc = (drdynvcPlugin*)plugin;
DEBUG_DVC("connecting");
status = drdynvc->channelEntryPoints.pVirtualChannelOpen(drdynvc->InitHandle,
&drdynvc->OpenHandle, drdynvc->channelDef.name, drdynvc_virtual_channel_open_event);
drdynvc_add_open_handle_data(drdynvc->OpenHandle, drdynvc);
if (status != CHANNEL_RC_OK)
{
WLog_ERR(TAG, "drdynvc_virtual_channel_event_connected: open failed: status: %d", status);
return;
}
drdynvc->MsgPipe = MessagePipe_New();
drdynvc->channel_mgr = dvcman_new(drdynvc);
drdynvc->channel_error = 0;
settings = (rdpSettings*) ((rdpSvcPlugin*) plugin)->channel_entry_points.pExtendedData;
settings = (rdpSettings*) drdynvc->channelEntryPoints.pExtendedData;
for (index = 0; index < settings->DynamicChannelCount; index++)
{
@ -389,16 +573,26 @@ static void drdynvc_process_connect(rdpSvcPlugin* plugin)
dvcman_init(drdynvc->channel_mgr);
drdynvc->state = DRDYNVC_STATE_CAPABILITIES;
drdynvc->thread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE) drdynvc_virtual_channel_client_thread, (void*) drdynvc, 0, NULL);
}
static void drdynvc_process_terminate(rdpSvcPlugin* plugin)
static void drdynvc_virtual_channel_event_terminated(drdynvcPlugin* drdynvc)
{
drdynvcPlugin* drdynvc = (drdynvcPlugin*) plugin;
MessagePipe_PostQuit(drdynvc->MsgPipe, 0);
WaitForSingleObject(drdynvc->thread, INFINITE);
DEBUG_DVC("terminating");
MessagePipe_Free(drdynvc->MsgPipe);
CloseHandle(drdynvc->thread);
if (!drdynvc)
return;
drdynvc->channelEntryPoints.pVirtualChannelClose(drdynvc->OpenHandle);
if (drdynvc->data_in)
{
Stream_Free(drdynvc->data_in, TRUE);
drdynvc->data_in = NULL;
}
if (drdynvc->channel_mgr)
{
@ -406,9 +600,35 @@ static void drdynvc_process_terminate(rdpSvcPlugin* plugin)
drdynvc->channel_mgr = NULL;
}
svc_plugin_terminate(plugin);
drdynvc_remove_open_handle_data(drdynvc->OpenHandle);
drdynvc_remove_init_handle_data(drdynvc->InitHandle);
}
free(drdynvc);
static VOID VCAPITYPE drdynvc_virtual_channel_init_event(LPVOID pInitHandle, UINT event, LPVOID pData, UINT dataLength)
{
drdynvcPlugin* drdynvc;
drdynvc = (drdynvcPlugin*) drdynvc_get_init_handle_data(pInitHandle);
if (!drdynvc)
{
WLog_ERR(TAG, "drdynvc_virtual_channel_init_event: error no match");
return;
}
switch (event)
{
case CHANNEL_EVENT_CONNECTED:
drdynvc_virtual_channel_event_connected(drdynvc, pData, dataLength);
break;
case CHANNEL_EVENT_DISCONNECTED:
break;
case CHANNEL_EVENT_TERMINATED:
drdynvc_virtual_channel_event_terminated(drdynvc);
break;
}
}
/**
@ -426,27 +646,23 @@ int drdynvc_get_version(DrdynvcClientContext* context)
BOOL VCAPITYPE VirtualChannelEntry(PCHANNEL_ENTRY_POINTS pEntryPoints)
{
drdynvcPlugin* _p;
drdynvcPlugin* drdynvc;
DrdynvcClientContext* context;
CHANNEL_ENTRY_POINTS_FREERDP* pEntryPointsEx;
_p = (drdynvcPlugin*) calloc(1, sizeof(drdynvcPlugin));
drdynvc = (drdynvcPlugin*) calloc(1, sizeof(drdynvcPlugin));
if (!_p)
if (!drdynvc)
return FALSE;
_p->plugin.channel_def.options =
drdynvc->channelDef.options =
CHANNEL_OPTION_INITIALIZED |
CHANNEL_OPTION_ENCRYPT_RDP |
CHANNEL_OPTION_COMPRESS_RDP;
strcpy(_p->plugin.channel_def.name, "drdynvc");
strcpy(drdynvc->channelDef.name, "drdynvc");
_p->state = DRDYNVC_STATE_INITIAL;
_p->plugin.connect_callback = drdynvc_process_connect;
_p->plugin.receive_callback = drdynvc_process_receive;
_p->plugin.terminate_callback = drdynvc_process_terminate;
drdynvc->state = DRDYNVC_STATE_INITIAL;
pEntryPointsEx = (CHANNEL_ENTRY_POINTS_FREERDP*) pEntryPoints;
@ -458,15 +674,29 @@ BOOL VCAPITYPE VirtualChannelEntry(PCHANNEL_ENTRY_POINTS pEntryPoints)
if (!context)
return -1;
context->handle = (void*) _p;
_p->context = context;
context->handle = (void*) drdynvc;
context->custom = NULL;
drdynvc->context = context;
context->GetVersion = drdynvc_get_version;
*(pEntryPointsEx->ppInterface) = (void*) context;
}
svc_plugin_init((rdpSvcPlugin*) _p, pEntryPoints);
drdynvc->log = WLog_Get("com.freerdp.channels.drdynvc.client");
WLog_Print(drdynvc->log, WLOG_DEBUG, "VirtualChannelEntry");
CopyMemory(&(drdynvc->channelEntryPoints), pEntryPoints, sizeof(CHANNEL_ENTRY_POINTS_FREERDP));
drdynvc->channelEntryPoints.pVirtualChannelInit(&drdynvc->InitHandle,
&drdynvc->channelDef, 1, VIRTUAL_CHANNEL_VERSION_WIN2000, drdynvc_virtual_channel_init_event);
drdynvc->channelEntryPoints.pInterface = *(drdynvc->channelEntryPoints.ppInterface);
drdynvc->channelEntryPoints.ppInterface = &(drdynvc->channelEntryPoints.pInterface);
drdynvc_add_init_handle_data(drdynvc->InitHandle, (void*) drdynvc);
return 1;
}

View File

@ -20,11 +20,12 @@
#ifndef __DRDYNVC_MAIN_H
#define __DRDYNVC_MAIN_H
#include <winpr/wlog.h>
#include <freerdp/api.h>
#include <freerdp/svc.h>
#include <freerdp/addin.h>
#include <freerdp/client/drdynvc.h>
#include <freerdp/utils/svc_plugin.h>
enum _DRDYNVC_STATE
{
@ -43,11 +44,17 @@ typedef enum _DRDYNVC_STATE DRDYNVC_STATE;
#define CLOSE_REQUEST_PDU 0x04
#define CAPABILITY_REQUEST_PDU 0x05
typedef struct drdynvc_plugin drdynvcPlugin;
struct drdynvc_plugin
{
rdpSvcPlugin plugin;
CHANNEL_DEF channelDef;
CHANNEL_ENTRY_POINTS_FREERDP channelEntryPoints;
wLog* log;
HANDLE thread;
wStream* data_in;
void* InitHandle;
DWORD OpenHandle;
wMessagePipe* MsgPipe;
DRDYNVC_STATE state;
DrdynvcClientContext* context;
@ -61,6 +68,7 @@ struct drdynvc_plugin
IWTSVirtualChannelManager* channel_mgr;
};
typedef struct drdynvc_plugin drdynvcPlugin;
int drdynvc_write_data(drdynvcPlugin* plugin, UINT32 ChannelId, BYTE* data, UINT32 data_size);

View File

@ -1,79 +0,0 @@
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* Static Virtual Channel Interface
*
* Copyright 2009-2011 Jay Sorg
* Copyright 2010-2011 Vic Lee
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FREERDP_UTILS_SVC_PLUGIN_H
#define FREERDP_UTILS_SVC_PLUGIN_H
/* static channel plugin base implementation */
#include <freerdp/api.h>
#include <freerdp/svc.h>
#include <freerdp/addin.h>
#include <winpr/crt.h>
#include <winpr/synch.h>
#include <winpr/thread.h>
#include <winpr/stream.h>
#include <winpr/collections.h>
#include <freerdp/svc.h>
#include <freerdp/log.h>
typedef struct rdp_svc_plugin rdpSvcPlugin;
struct rdp_svc_plugin
{
CHANNEL_ENTRY_POINTS_FREERDP channel_entry_points;
CHANNEL_DEF channel_def;
void (*connect_callback)(rdpSvcPlugin* plugin);
void (*receive_callback)(rdpSvcPlugin* plugin, wStream* data_in);
void (*terminate_callback)(rdpSvcPlugin* plugin);
HANDLE thread;
HANDLE started;
wStream* data_in;
void* InitHandle;
DWORD OpenHandle;
wMessagePipe* MsgPipe;
wStreamPool *pool;
};
#ifdef __cplusplus
extern "C" {
#endif
FREERDP_API void svc_plugin_init(rdpSvcPlugin* plugin, CHANNEL_ENTRY_POINTS* pEntryPoints);
FREERDP_API void svc_plugin_terminate(rdpSvcPlugin* plugin);
FREERDP_API int svc_plugin_send(rdpSvcPlugin* plugin, wStream* data_out);
#ifdef __cplusplus
}
#endif
#define SVC_TAG FREERDP_TAG("svc")
#ifdef WITH_DEBUG_SVC
#define DEBUG_SVC(fmt, ...) WLog_DBG(SVC_TAG, fmt, ## __VA_ARGS__)
#else
#define DEBUG_SVC(fmt, ...)
#endif
#endif /* FREERDP_UTILS_SVC_PLUGIN_H */

View File

@ -25,7 +25,6 @@ set(${MODULE_PREFIX}_SRCS
ringbuffer.c
signal.c
stopwatch.c
svc_plugin.c
tcp.c
time.c
uds.c)

View File

@ -1,323 +0,0 @@
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* Static Virtual Channel Interface
*
* Copyright 2009-2011 Jay Sorg
* Copyright 2010-2011 Vic Lee
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <assert.h>
#include <winpr/crt.h>
#include <winpr/synch.h>
#include <winpr/stream.h>
#include <winpr/collections.h>
#include <freerdp/log.h>
#include <freerdp/constants.h>
#include <freerdp/utils/svc_plugin.h>
#define TAG FREERDP_TAG("utils")
static wListDictionary* g_InitHandles;
static wListDictionary* g_OpenHandles;
void svc_plugin_add_init_handle_data(void* pInitHandle, void* pUserData)
{
if (!g_InitHandles)
g_InitHandles = ListDictionary_New(TRUE);
ListDictionary_Add(g_InitHandles, pInitHandle, pUserData);
}
void* svc_plugin_get_init_handle_data(void* pInitHandle)
{
void* pUserData = NULL;
pUserData = ListDictionary_GetItemValue(g_InitHandles, pInitHandle);
return pUserData;
}
void svc_plugin_remove_init_handle_data(void* pInitHandle)
{
ListDictionary_Remove(g_InitHandles, pInitHandle);
}
void svc_plugin_add_open_handle_data(DWORD openHandle, void* pUserData)
{
void* pOpenHandle = (void*) (size_t) openHandle;
if (!g_OpenHandles)
g_OpenHandles = ListDictionary_New(TRUE);
ListDictionary_Add(g_OpenHandles, pOpenHandle, pUserData);
}
void* svc_plugin_get_open_handle_data(DWORD openHandle)
{
void* pUserData = NULL;
void* pOpenHandle = (void*) (size_t) openHandle;
pUserData = ListDictionary_GetItemValue(g_OpenHandles, pOpenHandle);
return pUserData;
}
void svc_plugin_remove_open_handle_data(DWORD openHandle)
{
void* pOpenHandle = (void*) (size_t) openHandle;
ListDictionary_Remove(g_OpenHandles, pOpenHandle);
}
static void svc_plugin_process_received(rdpSvcPlugin* plugin, void* pData, UINT32 dataLength,
UINT32 totalLength, UINT32 dataFlags)
{
wStream* s;
if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
{
/*
* According to MS-RDPBCGR 2.2.6.1, "All virtual channel traffic MUST be suspended.
* This flag is only valid in server-to-client virtual channel traffic. It MUST be
* ignored in client-to-server data." Thus it would be best practice to cease data
* transmission. However, simply returning here avoids a crash.
*/
return;
}
if (dataFlags & CHANNEL_FLAG_FIRST)
{
if (plugin->data_in)
Stream_Release(plugin->data_in);
plugin->data_in = StreamPool_Take(plugin->pool, totalLength);
}
s = plugin->data_in;
Stream_EnsureRemainingCapacity(s, (int) dataLength);
Stream_Write(s, pData, dataLength);
if (dataFlags & CHANNEL_FLAG_LAST)
{
plugin->data_in = NULL;
Stream_SealLength(s);
Stream_SetPosition(s, 0);
MessageQueue_Post(plugin->MsgPipe->In, NULL, 0, (void*) s, NULL);
}
}
static VOID VCAPITYPE svc_plugin_open_event(DWORD openHandle, UINT event, LPVOID pData, UINT32 dataLength,
UINT32 totalLength, UINT32 dataFlags)
{
rdpSvcPlugin* plugin;
DEBUG_SVC("openHandle %d event %d dataLength %d totalLength %d dataFlags %d",
openHandle, event, dataLength, totalLength, dataFlags);
plugin = (rdpSvcPlugin*) svc_plugin_get_open_handle_data(openHandle);
if (!plugin)
{
WLog_ERR(TAG, "svc_plugin_open_event: error no match");
return;
}
switch (event)
{
case CHANNEL_EVENT_DATA_RECEIVED:
svc_plugin_process_received(plugin, pData, dataLength, totalLength, dataFlags);
break;
case CHANNEL_EVENT_WRITE_COMPLETE:
Stream_Free((wStream*) pData, TRUE);
break;
}
}
static void* svc_plugin_thread_func(void* arg)
{
wStream* data;
wMessage message;
rdpSvcPlugin* plugin = (rdpSvcPlugin*) arg;
DEBUG_SVC("in");
assert(NULL != plugin);
IFCALL(plugin->connect_callback, plugin);
SetEvent(plugin->started);
while (1)
{
if (!MessageQueue_Wait(plugin->MsgPipe->In))
break;
if (MessageQueue_Peek(plugin->MsgPipe->In, &message, TRUE))
{
if (message.id == WMQ_QUIT)
break;
if (message.id == 0)
{
data = (wStream*) message.wParam;
IFCALL(plugin->receive_callback, plugin, data);
Stream_Release(data);
}
}
}
DEBUG_SVC("out");
ExitThread(0);
return 0;
}
static void svc_plugin_process_connected(rdpSvcPlugin* plugin, LPVOID pData, UINT32 dataLength)
{
UINT32 status;
status = plugin->channel_entry_points.pVirtualChannelOpen(plugin->InitHandle,
&(plugin->OpenHandle), plugin->channel_def.name, svc_plugin_open_event);
if (status != CHANNEL_RC_OK)
{
WLog_ERR(TAG, "svc_plugin_process_connected: open failed: status: %d", status);
return;
}
svc_plugin_add_open_handle_data(plugin->OpenHandle, plugin);
plugin->MsgPipe = MessagePipe_New();
plugin->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) svc_plugin_thread_func, (void*) plugin, 0, NULL);
WaitForSingleObject(plugin->started,INFINITE);
}
static void svc_plugin_process_terminated(rdpSvcPlugin* plugin)
{
MessagePipe_PostQuit(plugin->MsgPipe, 0);
WaitForSingleObject(plugin->thread, INFINITE);
MessagePipe_Free(plugin->MsgPipe);
CloseHandle(plugin->thread);
if (plugin->started)
{
CloseHandle(plugin->started);
plugin->started = NULL;
}
plugin->channel_entry_points.pVirtualChannelClose(plugin->OpenHandle);
if (plugin->data_in)
{
Stream_Release(plugin->data_in);
plugin->data_in = NULL;
}
svc_plugin_remove_open_handle_data(plugin->OpenHandle);
svc_plugin_remove_init_handle_data(plugin->InitHandle);
IFCALL(plugin->terminate_callback, plugin);
}
static VOID VCAPITYPE svc_plugin_init_event(LPVOID pInitHandle, UINT event, LPVOID pData, UINT dataLength)
{
rdpSvcPlugin* plugin;
DEBUG_SVC("event %d", event);
plugin = (rdpSvcPlugin*) svc_plugin_get_init_handle_data(pInitHandle);
if (!plugin)
{
WLog_ERR(TAG, "svc_plugin_init_event: error no match");
return;
}
switch (event)
{
case CHANNEL_EVENT_CONNECTED:
svc_plugin_process_connected(plugin, pData, dataLength);
break;
case CHANNEL_EVENT_DISCONNECTED:
break;
case CHANNEL_EVENT_TERMINATED:
svc_plugin_process_terminated(plugin);
break;
}
}
void svc_plugin_init(rdpSvcPlugin* plugin, CHANNEL_ENTRY_POINTS* pEntryPoints)
{
/**
* The channel manager will guarantee only one thread can call
* VirtualChannelInit at a time. So this should be safe.
*/
CopyMemory(&(plugin->channel_entry_points), pEntryPoints, sizeof(CHANNEL_ENTRY_POINTS_FREERDP));
plugin->channel_entry_points.pVirtualChannelInit(&(plugin->InitHandle),
&(plugin->channel_def), 1, VIRTUAL_CHANNEL_VERSION_WIN2000, svc_plugin_init_event);
plugin->channel_entry_points.pInterface = *(plugin->channel_entry_points.ppInterface);
plugin->channel_entry_points.ppInterface = &(plugin->channel_entry_points.pInterface);
plugin->started = CreateEvent(NULL, TRUE, FALSE, NULL);
plugin->pool = StreamPool_New(TRUE, 10);
svc_plugin_add_init_handle_data(plugin->InitHandle, plugin);
}
void svc_plugin_terminate(rdpSvcPlugin* plugin)
{
if (plugin->pool)
{
StreamPool_Free(plugin->pool);
plugin->pool = NULL;
}
if (plugin->started)
{
CloseHandle(plugin->started);
plugin->started = NULL;
}
}
int svc_plugin_send(rdpSvcPlugin* plugin, wStream* data_out)
{
UINT32 status = 0;
DEBUG_SVC("length %d", (int) Stream_GetPosition(data_out));
if (!plugin)
status = CHANNEL_RC_BAD_INIT_HANDLE;
else
status = plugin->channel_entry_points.pVirtualChannelWrite(plugin->OpenHandle,
Stream_Buffer(data_out), Stream_GetPosition(data_out), data_out);
if (status != CHANNEL_RC_OK)
{
Stream_Free(data_out, TRUE);
WLog_ERR(TAG, "svc_plugin_send: VirtualChannelWrite failed %d", status);
}
return status;
}