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