Skip to content

Commit 33416fd

Browse files
llyzsmfleisz
authored andcommitted
[channels,printer] support printer channel in synchronous mode.
1 parent 938e1ca commit 33416fd

File tree

1 file changed

+79
-47
lines changed

1 file changed

+79
-47
lines changed

channels/printer/client/printer_main.c

+79-47
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ typedef struct
6262
HANDLE thread;
6363
rdpContext* rdpcontext;
6464
char port[64];
65+
BOOL async;
6566
} PRINTER_DEVICE;
6667

6768
typedef enum
@@ -684,8 +685,21 @@ static UINT printer_irp_request(DEVICE* device, IRP* irp)
684685
WINPR_ASSERT(printer_dev);
685686
WINPR_ASSERT(irp);
686687

687-
InterlockedPushEntrySList(printer_dev->pIrpList, &(irp->ItemEntry));
688-
SetEvent(printer_dev->event);
688+
if (printer_dev->async)
689+
{
690+
InterlockedPushEntrySList(printer_dev->pIrpList, &(irp->ItemEntry));
691+
SetEvent(printer_dev->event);
692+
}
693+
else
694+
{
695+
UINT error = printer_process_irp(printer_dev, irp);
696+
if (error)
697+
{
698+
WLog_ERR(TAG, "printer_process_irp failed with error %" PRIu32 "!", error);
699+
return error;
700+
}
701+
}
702+
689703
return CHANNEL_RC_OK;
690704
}
691705

@@ -890,31 +904,34 @@ static UINT printer_free(DEVICE* device)
890904

891905
WINPR_ASSERT(printer_dev);
892906

893-
SetEvent(printer_dev->stopEvent);
894-
895-
if (WaitForSingleObject(printer_dev->thread, INFINITE) == WAIT_FAILED)
907+
if (printer_dev->async)
896908
{
897-
error = GetLastError();
898-
WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
909+
SetEvent(printer_dev->stopEvent);
899910

900-
/* The analyzer is confused by this premature return value.
901-
* Since this case can not be handled gracefully silence the
902-
* analyzer here. */
911+
if (WaitForSingleObject(printer_dev->thread, INFINITE) == WAIT_FAILED)
912+
{
913+
error = GetLastError();
914+
WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
915+
916+
/* The analyzer is confused by this premature return value.
917+
* Since this case can not be handled gracefully silence the
918+
* analyzer here. */
903919
#ifndef __clang_analyzer__
904-
return error;
920+
return error;
905921
#endif
906-
}
922+
}
907923

908-
while ((irp = (IRP*)InterlockedPopEntrySList(printer_dev->pIrpList)) != NULL)
909-
{
910-
WINPR_ASSERT(irp->Discard);
911-
irp->Discard(irp);
912-
}
924+
while ((irp = (IRP*)InterlockedPopEntrySList(printer_dev->pIrpList)) != NULL)
925+
{
926+
WINPR_ASSERT(irp->Discard);
927+
irp->Discard(irp);
928+
}
913929

914-
CloseHandle(printer_dev->thread);
915-
CloseHandle(printer_dev->stopEvent);
916-
CloseHandle(printer_dev->event);
917-
winpr_aligned_free(printer_dev->pIrpList);
930+
CloseHandle(printer_dev->thread);
931+
CloseHandle(printer_dev->stopEvent);
932+
CloseHandle(printer_dev->event);
933+
winpr_aligned_free(printer_dev->pIrpList);
934+
}
918935

919936
if (printer_dev->printer)
920937
{
@@ -961,47 +978,62 @@ static UINT printer_register(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints, rdpPrint
961978
printer_dev->device.Free = printer_free;
962979
printer_dev->rdpcontext = pEntryPoints->rdpcontext;
963980
printer_dev->printer = printer;
964-
printer_dev->pIrpList = (WINPR_PSLIST_HEADER)winpr_aligned_malloc(sizeof(WINPR_SLIST_HEADER),
965-
MEMORY_ALLOCATION_ALIGNMENT);
966981

967-
if (!printer_dev->pIrpList)
968-
{
969-
WLog_ERR(TAG, "_aligned_malloc failed!");
970-
error = CHANNEL_RC_NO_MEMORY;
971-
goto error_out;
972-
}
982+
if (!freerdp_settings_get_bool(pEntryPoints->rdpcontext->settings,
983+
FreeRDP_SynchronousStaticChannels))
984+
printer_dev->async = TRUE;
973985

974986
if (!printer_load_from_config(pEntryPoints->rdpcontext->settings, printer, printer_dev))
975987
goto error_out;
976988

977-
InitializeSListHead(printer_dev->pIrpList);
978-
979-
if (!(printer_dev->event = CreateEvent(NULL, TRUE, FALSE, NULL)))
989+
if (printer_dev->async)
980990
{
981-
WLog_ERR(TAG, "CreateEvent failed!");
982-
error = ERROR_INTERNAL_ERROR;
983-
goto error_out;
984-
}
991+
printer_dev->pIrpList = (WINPR_PSLIST_HEADER)winpr_aligned_malloc(
992+
sizeof(WINPR_SLIST_HEADER), MEMORY_ALLOCATION_ALIGNMENT);
985993

986-
if (!(printer_dev->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
987-
{
988-
WLog_ERR(TAG, "CreateEvent failed!");
989-
error = ERROR_INTERNAL_ERROR;
990-
goto error_out;
994+
if (!printer_dev->pIrpList)
995+
{
996+
WLog_ERR(TAG, "_aligned_malloc failed!");
997+
error = CHANNEL_RC_NO_MEMORY;
998+
goto error_out;
999+
}
1000+
1001+
InitializeSListHead(printer_dev->pIrpList);
1002+
1003+
printer_dev->event = CreateEvent(NULL, TRUE, FALSE, NULL);
1004+
if (!printer_dev->event)
1005+
{
1006+
WLog_ERR(TAG, "CreateEvent failed!");
1007+
error = ERROR_INTERNAL_ERROR;
1008+
goto error_out;
1009+
}
1010+
1011+
printer_dev->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1012+
if (!printer_dev->stopEvent)
1013+
{
1014+
WLog_ERR(TAG, "CreateEvent failed!");
1015+
error = ERROR_INTERNAL_ERROR;
1016+
goto error_out;
1017+
}
9911018
}
9921019

993-
if ((error = pEntryPoints->RegisterDevice(pEntryPoints->devman, &printer_dev->device)))
1020+
error = pEntryPoints->RegisterDevice(pEntryPoints->devman, &printer_dev->device);
1021+
if (error)
9941022
{
9951023
WLog_ERR(TAG, "RegisterDevice failed with error %" PRIu32 "!", error);
9961024
goto error_out;
9971025
}
9981026

999-
if (!(printer_dev->thread =
1000-
CreateThread(NULL, 0, printer_thread_func, (void*)printer_dev, 0, NULL)))
1027+
if (printer_dev->async)
10011028
{
1002-
WLog_ERR(TAG, "CreateThread failed!");
1003-
error = ERROR_INTERNAL_ERROR;
1004-
goto error_out;
1029+
printer_dev->thread =
1030+
CreateThread(NULL, 0, printer_thread_func, (void*)printer_dev, 0, NULL);
1031+
if (!printer_dev->thread)
1032+
{
1033+
WLog_ERR(TAG, "CreateThread failed!");
1034+
error = ERROR_INTERNAL_ERROR;
1035+
goto error_out;
1036+
}
10051037
}
10061038

10071039
WINPR_ASSERT(printer->AddRef);

0 commit comments

Comments
 (0)