]> git.vomp.tv Git - vompclient.git/blob - inputudp.cc
Log conversion
[vompclient.git] / inputudp.cc
1 /*
2     Copyright 2006-2020 Chris Tallon
3
4     This file is part of VOMP.
5
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.
10
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.
15
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/>.
18 */
19
20 #include <fcntl.h>
21 #ifndef WIN32
22 #include <unistd.h>
23 #endif
24
25 #include "log.h"
26
27 #include "inputudp.h"
28
29 static const char* TAG = "InputUDP";
30
31 const char* InputUDP::myModName = "InputUDP";
32
33 bool InputUDP::init()
34 {
35   if (initted) return false;
36   initted = true;
37   log = LogNT::getInstance();
38   log->debug(TAG, "Starting InputUDP command server");
39
40   if (!udp4.init(2000))
41   {
42     log->debug(TAG, "UDP4 init error");
43     initted = false;
44     return false;
45   }
46
47 #ifdef WIN32
48   quitPipe = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
49   if (quitPipe == INVALID_SOCKET)
50   {
51     log->error(TAG, "Win32 socket fail");
52 #else
53   if (pipe2(pfds, O_NONBLOCK) == -1)
54   {
55     log->error(TAG, "pipe2() fail");
56 #endif
57     udp4.shutdown();
58     initted = false;
59     return false;
60   }
61
62   return true;
63 }
64
65 void InputUDP::shutdown()
66 {
67 #ifdef WIN32
68   CLOSESOCKET(quitPipe);
69 #endif
70
71   udp4.shutdown();
72
73 #ifndef WIN32
74   CLOSESOCKET(pfds[1]);
75   CLOSESOCKET(pfds[0]);
76 #endif
77
78   initted = false;
79 }
80
81 bool InputUDP::start()
82 {
83   threadStartProtect.lock(); // Make sure listenThread is fully initted before start returns
84   listenThread = std::thread( [this]
85   {
86     threadStartProtect.lock();
87     threadStartProtect.unlock();
88     listenLoop();
89   });
90   threadStartProtect.unlock();
91
92   log->debug(TAG, "InputUDP command server started");
93   return true;
94 }
95
96 void InputUDP::stop()
97 {
98   std::lock_guard<std::mutex> lg(threadStartProtect); // Also use it to protect against starting while stopping
99
100   if (!initted) return;
101
102   if (listenThread.joinable())
103   {
104 #ifdef WIN32
105     log->debug(TAG, "Calling CLOSESOCKET on WIN32 quitPipe");
106
107     CLOSESOCKET(quitPipe);
108 #else
109     write(pfds[1], "1", 1); // break the select in listenLoop
110 #endif
111     listenThread.join();
112   }
113 }
114
115 void InputUDP::listenLoop()
116 {
117   int retval;
118   while(1)
119   {
120 #ifdef WIN32
121     retval = udp4.waitforMessage(3, quitPipe);
122 #else
123     retval = udp4.waitforMessage(3, pfds[0]);
124 #endif
125     log->debug(TAG, "Back from waitForMessage");
126
127     if (retval == 2)
128     {
129       processRequest(udp4.getData(), udp4.getDataLength());
130     }
131     else if (retval == 3) // quit
132     {
133       log->debug(TAG, "quit");
134       break;
135     }
136     else
137     {
138       log->crit(TAG, "Wait for packet error");
139       return;
140     }
141   }
142 }
143
144 void InputUDP::processRequest(const void* data, UINT length)
145 {
146   log->debug(TAG, "Got request");
147
148   char* temp = new char[length + 1];
149   memcpy(temp, data, length);
150   temp[length] = '\0';
151   UINT command = static_cast<UINT>(atoi(temp));
152   delete[] temp;
153
154   log->debug(TAG, "Command {} recieved", command);
155   sendInputKey(command);
156 }
157
158 const char* InputUDP::getHardCodedHardwareKeyNamesForVompKey(UCHAR /* vompKey */)
159 {
160   return "";
161 }
162
163 std::string InputUDP::getHardwareKeyName(HWC_TYPE /* hardwareKey */)
164 {
165   std::string retval;
166   return retval;
167 }