]> git.vomp.tv Git - vompclient.git/blob - vdpc.h
Windows fixes
[vompclient.git] / vdpc.h
1 /*
2     Copyright 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 #ifndef VDPC_H
21 #define VDPC_H
22
23 // Vomp Discovery Protocol Client
24
25 #include <string>
26 #include <vector>
27 #include <mutex>
28 #include <thread>
29 #include <condition_variable>
30 #include <functional>
31
32 #ifdef WIN32
33   #include <WinSock2.h>
34 #endif
35
36 #include "defines.h"
37
38 #ifdef IPV4
39 #include "udp4.h"
40 #endif
41 #ifdef IPV6
42 #include "udp6.h"
43 #endif
44
45 struct VDRServer
46 {
47   int ipVersion;
48   std::string ip;
49   std::string name;
50   USHORT port;
51   ULONG version;
52
53   VDRServer(int tipVersion, const std::string tip, const std::string tname, USHORT tport, ULONG tversion)
54   : ipVersion(tipVersion), ip(tip), name(tname), port(tport), version(tversion) {}
55 };
56
57 class ServerSorter
58 {
59   public:
60     ServerSorter(int tipPreferred) : ipPreferred(tipPreferred) {}
61     bool operator() (const VDRServer& a, const VDRServer& b)
62     {
63       if (a.name == b.name)
64         return a.ipVersion == ipPreferred;
65       else
66         return a.name < b.name;
67     }
68   private:
69     int ipPreferred;
70 };
71
72 class VDPC
73 {
74   private:
75     typedef std::function<void(int, const char*, const char*, USHORT, ULONG)> AddServerCallback;
76
77   // Nested classes
78 #ifdef IPV4
79     class VDPC4
80     {
81       public:
82         bool init(AddServerCallback& taddFunc);
83         void run(); // blocks
84         void stop();
85         void sendRequest();
86
87       private:
88         #ifdef WIN32
89           SOCKET quitPipe;
90         #else
91           int pfdsUDP4[2];
92         #endif
93
94         std::mutex startProtect;
95         std::thread receiveThread;
96         bool stopNow{};
97         void threadMethod();
98
99         UDP4 udp4;
100         AddServerCallback addFunc;
101     };
102 #endif
103
104 #ifdef IPV6
105     class VDPC6
106     {
107       public:
108         bool init(AddServerCallback& taddFunc);
109         void run(); // blocks
110         void stop();
111         void sendRequest();
112
113       private:
114         int pfdsUDP6[2];
115
116         std::mutex startProtect;
117         std::thread receiveThread;
118         bool stopNow{};
119         void threadMethod();
120
121         UDP6 udp6;
122         AddServerCallback addFunc;
123     };
124 #endif
125
126   public:
127     bool init();
128     int go();
129     void stop();
130     ULONG numServers() const;
131     const VDRServer& operator[](ULONG index) const;
132
133     void TEMPaddCLIServer(const std::string&); // FIXME NCONFIG
134
135   private:
136     std::vector<VDRServer> servers;
137     std::mutex serversLock;
138
139     std::condition_variable waitCond;
140     std::mutex waitMutex;
141     bool stopNow{};
142
143     // FIXME retrieve from new config system NCONFIG
144     bool dedupServers{true};
145     int preferIPV{4};
146
147 #ifdef IPV4
148     VDPC4 vdpc4;
149 #endif
150 #ifdef IPV6
151     VDPC6 vdpc6;
152 #endif
153 };
154
155 #endif