]> git.vomp.tv Git - vompclient.git/blob - vdpc.h
Server address/port to config. Clean up VConnect
[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() {}
54
55   VDRServer(int tipVersion, const std::string tip, const std::string tname, USHORT tport, ULONG tversion)
56   : ipVersion(tipVersion), ip(tip), name(tname), port(tport), version(tversion) {}
57 };
58
59 class ServerSorter
60 {
61   public:
62     ServerSorter(int tipPreferred) : ipPreferred(tipPreferred) {}
63     bool operator() (const VDRServer& a, const VDRServer& b)
64     {
65       if (a.name == b.name)
66         return a.ipVersion == ipPreferred;
67       else
68         return a.name < b.name;
69     }
70   private:
71     int ipPreferred;
72 };
73
74 class VDPC
75 {
76   private:
77     typedef std::function<void(int, const char*, const char*, USHORT, ULONG)> AddServerCallback;
78
79   // Nested classes
80 #ifdef IPV4
81     class VDPC4
82     {
83       public:
84         bool init(AddServerCallback& taddFunc);
85         void run(); // blocks
86         void stop();
87         void sendRequest();
88
89       private:
90         #ifdef WIN32
91           SOCKET quitPipe;
92         #else
93           int pfdsUDP4[2];
94         #endif
95
96         std::mutex startProtect;
97         std::thread receiveThread;
98         bool stopNow{};
99         void threadMethod();
100
101         UDP4 udp4;
102         AddServerCallback addFunc;
103     };
104 #endif
105
106 #ifdef IPV6
107     class VDPC6
108     {
109       public:
110         bool init(AddServerCallback& taddFunc);
111         void run(); // blocks
112         void stop();
113         void sendRequest();
114
115       private:
116         int pfdsUDP6[2];
117
118         std::mutex startProtect;
119         std::thread receiveThread;
120         bool stopNow{};
121         void threadMethod();
122
123         UDP6 udp6;
124         AddServerCallback addFunc;
125     };
126 #endif
127
128   public:
129     bool init();
130     int go();
131     void stop();
132     ULONG numServers() const;
133     const VDRServer& operator[](ULONG index) const;
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