2 Copyright 2006-2020 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP. If not, see <https://www.gnu.org/licenses/>.
30 static const char* TAG = "InputUDP";
32 const char* InputUDP::myModName = "InputUDP";
36 if (initted) return false;
38 log = LogNT::getInstance();
40 Config::getInstance()->getInt("input_udp", "port", port);
42 log->debug(TAG, "Starting InputUDP command server on port {}", port);
44 if (!udp6.init(static_cast<USHORT>(port)))
46 log->debug(TAG, "UDP6 init error");
52 quitPipe = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
53 if (quitPipe == INVALID_SOCKET)
55 log->error(TAG, "Win32 socket fail");
57 if (pipe2(pfds, O_NONBLOCK) == -1)
59 log->error(TAG, "pipe2() fail");
69 void InputUDP::shutdown()
72 CLOSESOCKET(quitPipe);
85 bool InputUDP::start()
87 threadStartProtect.lock(); // Make sure listenThread is fully initted before start returns
88 listenThread = std::thread( [this]
90 threadStartProtect.lock();
91 threadStartProtect.unlock();
94 threadStartProtect.unlock();
96 log->debug(TAG, "InputUDP command server started");
100 void InputUDP::stop()
102 std::lock_guard<std::mutex> lg(threadStartProtect); // Also use it to protect against starting while stopping
104 if (!initted) return;
106 if (listenThread.joinable())
109 log->debug(TAG, "Calling CLOSESOCKET on WIN32 quitPipe");
111 CLOSESOCKET(quitPipe);
113 write(pfds[1], "1", 1); // break the select in listenLoop
119 void InputUDP::listenLoop()
125 retval = udp6.waitforMessage(3, quitPipe);
127 retval = udp6.waitforMessage(3, pfds[0]);
129 log->debug(TAG, "Back from waitForMessage");
133 processRequest(udp6.getData(), udp6.getDataLength());
135 else if (retval == 3) // quit
137 log->debug(TAG, "quit");
142 log->crit(TAG, "Wait for packet error");
148 void InputUDP::processRequest(const void* data, UINT length)
150 log->debug(TAG, "Got request");
152 char* temp = new char[length + 1];
153 memcpy(temp, data, length);
155 UINT command = static_cast<UINT>(atoi(temp));
158 log->debug(TAG, "Command {} recieved", command);
159 sendInputKey(command);
162 const char* InputUDP::getHardCodedHardwareKeyNamesForVompKey(UCHAR /* vompKey */)
167 std::string InputUDP::getHardwareKeyName(HWC_TYPE /* hardwareKey */)