Skip to content

Commit

Permalink
Fixes #1109 (#1112)
Browse files Browse the repository at this point in the history
Updated test functions in main() to newer syntax
Changed NamedPipeTransact to be Async
Added Timeout to NamedPipeTransact

Former-commit-id: 348683f107e64ac154b3ab0177f880ebc34af9ed [formerly 28f2f1db09972d34e70442758dd5083c483e52f6] [formerly 19fda3526383af55ad71b2b9562d12be7a1a32fa [formerly c0b2278617b55b346460057a16d16fbe3765ec88]] [formerly e14f226b45fd904fa3144f9d57be595656a9f5b3 [formerly 08d64f89bbd6a2833383bd96c4c2bf1777aa5ae5] [formerly acd621ec5fcf861a203fb0e7a058d4aa78124d96 [formerly 610571fead75751ef33e005bf0b8c3f19f91b2ff]]]
Former-commit-id: 00d9f732082ed375c5ac5c3f53df2aa8234a7730 [formerly acfd49f4928efc272f87dd439611544838c67c9a] [formerly 6a9a6a632f6bd8341ae965751a5483139adc4f85 [formerly 275d5165f1dfa7aa710e21188a9e89c419e4c9c6]]
Former-commit-id: 714a836d9944c21e9f6ff5fe7eed466cd55db555 [formerly 5eca6f2ccea787dfc96c4d1b59d91bb9ae25727e]
Former-commit-id: 1501e64d27a83484a1b85f8a18cf19fd1999218a
  • Loading branch information
dedmen authored Sep 19, 2016
1 parent 52bfa51 commit a621b37
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
10 changes: 6 additions & 4 deletions arma3/@task_force_radio/task_force_radio_pipe/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "stdafx.h"

HANDLE pipe = INVALID_HANDLE_VALUE;
HANDLE waitForDataEvent = INVALID_HANDLE_VALUE;//#Dedmen i dont know why these are defined here. They are not needed here.

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
Expand Down Expand Up @@ -36,10 +37,11 @@ int main(int argc, char * argv [])
DWORD ticks = GetTickCount();
for (int q = 0; q < 1000; q++)
{
RVExtension(output, TEST_OUTPUT_SIZE, "POS@[TF]Nkey@0.5@9.3@123.5003@236");
RVExtension(output, TEST_OUTPUT_SIZE, "POS@[TF]Nkey2@5@-0.7@123.5003@236");
RVExtension(output, TEST_OUTPUT_SIZE, "TANGENT@PRESSED");
RVExtension(output, TEST_OUTPUT_SIZE, "TANGENT@RELEASED");
RVExtension(output, TEST_OUTPUT_SIZE, "VERSION\ttest\test_channel\ttest_password");
RVExtension(output, TEST_OUTPUT_SIZE, "POS\t[TF]Nkey\t0.5\t9.3\t123.5003\t236");
RVExtension(output, TEST_OUTPUT_SIZE, "POS\t[TF]Nkey2\t5\t-0.7\t123.5003\t236");
RVExtension(output, TEST_OUTPUT_SIZE, "TANGENT\tPRESSED");
RVExtension(output, TEST_OUTPUT_SIZE, "TANGENT\tRELEASED");
}
printf("%f\n", (float)(GetTickCount() - ticks) / 4000.0);
//Sleep(200);
Expand Down
2 changes: 2 additions & 0 deletions arma3/@task_force_radio/task_force_radio_pipe/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ using namespace std;
#define PIPE_NAME L"\\\\.\\pipe\\task_force_radio_pipe"
#define DEBUG_PIPE_NAME L"\\\\.\\pipe\\task_force_radio_pipe_debug"
#define DEBUG_PARAMETER L"-tfdebug"
#define PIPE_TIMEOUT 2000
extern HANDLE pipe;
extern HANDLE waitForDataEvent;

extern "C"
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ DWORD dwMessageMode = PIPE_READMODE_MESSAGE;
void closePipe()
{
CloseHandle(pipe);
CloseHandle(waitForDataEvent);
pipe = INVALID_HANDLE_VALUE;
waitForDataEvent = INVALID_HANDLE_VALUE;
}

bool isDebugArmaInstance()
Expand Down Expand Up @@ -40,50 +42,72 @@ void openPipe()
pipe = CreateNamedPipe(
pipeName, // name of the pipe
PIPE_ACCESS_DUPLEX| FILE_FLAG_OVERLAPPED, // 1-way pipe -- send only
PIPE_TYPE_MESSAGE, // send data as message
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, // send data as message
1, // only allow 1 instance of this pipe
0, // no outbound buffer
0, // no inbound buffer
0, // use default wait time
&SA // use default security attributes
);

waitForDataEvent = CreateEvent(
&SA, // default security attribute
TRUE, // manual-reset event
TRUE, // initial state = signaled
NULL); // unnamed event object
}

void __stdcall RVExtension(char *output, int outputSize, const char *input)
{
if(input[0] == '\0')
return;

if (SetNamedPipeHandleState(
pipe, // pipe handle
&dwMessageMode, // new pipe mode
NULL, // don't set maximum bytes
NULL) // don't set maximum time fSuccess
)
{
DWORD written = 0;
if (!TransactNamedPipe(pipe, (void*) input, strlen(input), output, outputSize, &written, NULL))
DWORD written = 0;
OVERLAPPED pipeOverlap;
pipeOverlap.hEvent = waitForDataEvent;
DWORD errorCode = ERROR_SUCCESS;
if (!TransactNamedPipe(pipe, (void*) input, strlen(input), output, outputSize, &written, NULL)) {
errorCode = GetLastError();
if (errorCode == ERROR_IO_PENDING)//Handle overlapped datatransfer
{
DWORD errorCode = GetLastError();
if (errorCode != ERROR_PIPE_LISTENING) openPipe();
switch (errorCode)
{
DWORD waitResult = WaitForSingleObject(waitForDataEvent, PIPE_TIMEOUT);
if (!waitResult) {
errorCode = WAIT_TIMEOUT;
MessageBoxA(NULL, "timeout", "stuff", MB_ICONERROR | MB_OK);
} else {

GetOverlappedResult(
pipe, // handle to pipe
&pipeOverlap, // OVERLAPPED structure
&written, // bytes transferred
FALSE); // do not wait

if (written == 0)
errorCode = ERROR_NO_DATA;
else
return; //successful read. We dont need to handle any errors
}
}

//When WAIT_TIMEOUT happens teamspeak is likely unresponsive or crashed so we still reopen the pipe
if (errorCode != ERROR_PIPE_LISTENING) openPipe();
printf("ERROR: %d\n", errorCode);
switch (errorCode) {
case ERROR_NO_DATA:
case WAIT_TIMEOUT:
MessageBoxA(NULL, "timeout or nodata", "stuff", MB_ICONERROR | MB_OK);
strncpy_s(output, outputSize, "Pipe was closed by TS", _TRUNCATE);
break;
case ERROR_PIPE_LISTENING:
strncpy_s(output, outputSize, "Pipe not opened from TS plugin", _TRUNCATE);
break;
case 230:
case ERROR_BAD_PIPE:
strncpy_s(output, outputSize, "Not connected to TeamSpeak", _TRUNCATE);
break;
default:
char unknownError[16];
sprintf_s(unknownError, strlen(unknownError), "Pipe error %i", errorCode);
sprintf_s(unknownError, 16, "Pipe error %i", errorCode);
strncpy_s(output, outputSize, unknownError, _TRUNCATE);
break;
}
}
}
}

0 comments on commit a621b37

Please sign in to comment.