Skip to content

Commit

Permalink
Increased robustness after long idle time.
Browse files Browse the repository at this point in the history
  • Loading branch information
usertoroot committed Jun 19, 2022
1 parent 1bdd791 commit 6b34203
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 30 deletions.
12 changes: 12 additions & 0 deletions NetInput.Capture/NetInput.Capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ SOCKET sock;
struct sockaddr_in addr;
XINPUT_STATE lastSentInputStates[XUSER_MAX_COUNT];

void SendResetControllers()
{
uint8_t packet[] = { 0xFFu };
if (sendto(sock, (const char*)&packet, sizeof(packet), 0, (struct sockaddr*)&addr, sizeof(addr)) != sizeof(packet))
printf("Failed to send reset message.\n");
}

void PollControllers()
{
XINPUT_STATE state;
Expand Down Expand Up @@ -94,6 +101,10 @@ int main()
addr.sin_family = AF_INET;
addr.sin_port = htons(4313);

printf("Sending reset...\n");
SendResetControllers();
printf("Done.\n");

printf("Waiting for gamepad input, press ESC to exit...\n");
memset(lastSentInputStates, 0, sizeof(lastSentInputStates));
while (true)
Expand All @@ -105,6 +116,7 @@ int main()
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}

SendResetControllers();
closesocket(sock);
WSACleanup();
CoUninitialize();
Expand Down
74 changes: 44 additions & 30 deletions NetInput.Player/NetInput.Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@

SOCKET sock;
PVIGEM_TARGET pads[XUSER_MAX_COUNT];
PVIGEM_CLIENT client;

void ResetGamepads()
{
for (int i = 0; i < XUSER_MAX_COUNT; i++)
{
auto pad = pads[i];
pads[i] = nullptr;

if (pad == nullptr)
continue;

vigem_target_remove(client, pad);
vigem_target_free(pad);
}
}

int main()
{
Expand Down Expand Up @@ -49,7 +65,7 @@ int main()
printf("Done.\n");

printf("Setting up ViGEmClient...\n");
const auto client = vigem_alloc();
client = vigem_alloc();
if (client == nullptr)
{
printf("Failed to setup ViGEmClient.\n");
Expand All @@ -71,7 +87,7 @@ int main()

printf("Done.\n");

printf("Waiting for data, press ESC to exit...\n");
printf("Waiting for data...\n");

struct sockaddr_in clientAddr;
uint8_t packet[sizeof(XINPUT_STATE) + 1];
Expand All @@ -83,42 +99,40 @@ int main()
memset(&clientAddr, 0, sizeof(clientAddr));
int addrLen = sizeof(clientAddr);
int bytesReceived = recvfrom(sock, (char*)&packet, sizeof(packet), 0, (struct sockaddr*)&clientAddr, &addrLen);
if (bytesReceived != sizeof(packet))
continue;

uint32_t i = (uint32_t)packet[0];
XINPUT_STATE* state = (XINPUT_STATE*)(packet + 1);

if (pads[i] == nullptr)
if (bytesReceived == 1 && packet[0] == (uint8_t)0xFFu)
{
auto pad = vigem_target_x360_alloc();
const auto targetAddResult = vigem_target_add(client, pad);
if (!VIGEM_SUCCESS(targetAddResult))
printf("Reset signal received, gamepads will be reset.\n");
ResetGamepads();
}
else if (bytesReceived == sizeof(packet) && packet[0] >= 0 && packet[0] < XUSER_MAX_COUNT)
{
uint32_t i = (uint32_t)packet[0];
XINPUT_STATE* state = (XINPUT_STATE*)(packet + 1);

if (pads[i] == nullptr)
{
vigem_target_free(pad);
printf("Failed to add pad %u with error code 0x%08X.\n", i, targetAddResult);
return -6;
auto pad = vigem_target_x360_alloc();
const auto targetAddResult = vigem_target_add(client, pad);
if (VIGEM_SUCCESS(targetAddResult))
{
printf("Connected new gamepad %u.\n", i);
pads[i] = pad;
}
else
{
vigem_target_free(pad);
printf("Failed to add pad %u with error code 0x%08X.\n", i, targetAddResult);
}
}

printf("Connected new gamepad %u.\n", i);
pads[i] = pad;
auto pad = pads[i];
if (pad != nullptr)
vigem_target_x360_update(client, pad, *reinterpret_cast<XUSB_REPORT*>(&state->Gamepad));
}

auto pad = pads[i];
if (pad != nullptr)
vigem_target_x360_update(client, pad, *reinterpret_cast<XUSB_REPORT*>(&state->Gamepad));
}

for (int i = 0; i < XUSER_MAX_COUNT; i++)
{
auto pad = pads[i];
if (pad == nullptr)
continue;

vigem_target_remove(client, pad);
vigem_target_free(pad);
}

ResetGamepads();
vigem_disconnect(client);
vigem_free(client);
closesocket(sock);
Expand Down

0 comments on commit 6b34203

Please sign in to comment.