1 /******************************************************************************\
2  * Copyright (c) 2004-2020
3  *
4  * Author(s):
5  *  Volker Fischer
6  *
7  ******************************************************************************
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License as published by the Free Software
11  * Foundation; either version 2 of the License, or (at your option) any later
12  * version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
22  *
23 \******************************************************************************/
24 
25 #pragma once
26 
27 #include <QMutex>
28 #include <QTimer>
29 #include <QDateTime>
30 #include <list>
31 #include <cmath>
32 #include "global.h"
33 #include "util.h"
34 
35 /* Definitions ****************************************************************/
36 // protocol message IDs
37 #define PROTMESSID_ILLEGAL                  0  // illegal ID
38 #define PROTMESSID_ACKN                     1  // acknowledge
39 #define PROTMESSID_JITT_BUF_SIZE            10 // jitter buffer size
40 #define PROTMESSID_REQ_JITT_BUF_SIZE        11 // request jitter buffer size
41 #define PROTMESSID_NET_BLSI_FACTOR          12 // OLD (not used anymore)
42 #define PROTMESSID_CHANNEL_GAIN             13 // set channel gain for mix
43 #define PROTMESSID_CONN_CLIENTS_LIST_NAME   14 // OLD (not used anymore)
44 #define PROTMESSID_SERVER_FULL              15 // OLD (not used anymore)
45 #define PROTMESSID_REQ_CONN_CLIENTS_LIST    16 // request connected client list
46 #define PROTMESSID_CHANNEL_NAME             17 // OLD (not used anymore)
47 #define PROTMESSID_CHAT_TEXT                18 // contains a chat text
48 #define PROTMESSID_PING_MS                  19 // OLD (not used anymore)
49 #define PROTMESSID_NETW_TRANSPORT_PROPS     20 // properties for network transport
50 #define PROTMESSID_REQ_NETW_TRANSPORT_PROPS 21 // request properties for network transport
51 #define PROTMESSID_DISCONNECTION            22 // OLD (not used anymore)
52 #define PROTMESSID_REQ_CHANNEL_INFOS        23 // request channel infos for fader tag
53 #define PROTMESSID_CONN_CLIENTS_LIST        24 // channel infos for connected clients
54 #define PROTMESSID_CHANNEL_INFOS            25 // set channel infos
55 #define PROTMESSID_OPUS_SUPPORTED           26 // tells that OPUS codec is supported
56 #define PROTMESSID_LICENCE_REQUIRED         27 // licence required
57 #define PROTMESSID_REQ_CHANNEL_LEVEL_LIST   28 // OLD (not used anymore) // TODO needed for compatibility to old servers >= 3.4.6 and <= 3.5.12
58 #define PROTMESSID_VERSION_AND_OS           29 // version number and operating system
59 #define PROTMESSID_CHANNEL_PAN              30 // set channel pan for mix
60 #define PROTMESSID_MUTE_STATE_CHANGED       31 // mute state of your signal at another client has changed
61 #define PROTMESSID_CLIENT_ID                32 // current user ID and server status
62 #define PROTMESSID_RECORDER_STATE           33 // contains the state of the jam recorder (ERecorderState)
63 #define PROTMESSID_REQ_SPLIT_MESS_SUPPORT   34 // request support for split messages
64 #define PROTMESSID_SPLIT_MESS_SUPPORTED     35 // split messages are supported
65 
66 // message IDs of connection less messages (CLM)
67 // DEFINITION -> start at 1000, end at 1999, see IsConnectionLessMessageID
68 #define PROTMESSID_CLM_PING_MS                1001 // for measuring ping time
69 #define PROTMESSID_CLM_PING_MS_WITHNUMCLIENTS 1002 // for ping time and num. of clients info
70 #define PROTMESSID_CLM_SERVER_FULL            1003 // server full message
71 #define PROTMESSID_CLM_REGISTER_SERVER        1004 // register server
72 #define PROTMESSID_CLM_UNREGISTER_SERVER      1005 // unregister server
73 #define PROTMESSID_CLM_SERVER_LIST            1006 // server list
74 #define PROTMESSID_CLM_REQ_SERVER_LIST        1007 // request server list
75 #define PROTMESSID_CLM_SEND_EMPTY_MESSAGE     1008 // an empty message shall be send
76 #define PROTMESSID_CLM_EMPTY_MESSAGE          1009 // empty message
77 #define PROTMESSID_CLM_DISCONNECTION          1010 // disconnection
78 #define PROTMESSID_CLM_VERSION_AND_OS         1011 // version number and operating system
79 #define PROTMESSID_CLM_REQ_VERSION_AND_OS     1012 // request version number and operating system
80 #define PROTMESSID_CLM_CONN_CLIENTS_LIST      1013 // channel infos for connected clients
81 #define PROTMESSID_CLM_REQ_CONN_CLIENTS_LIST  1014 // request the connected clients list
82 #define PROTMESSID_CLM_CHANNEL_LEVEL_LIST     1015 // channel level list
83 #define PROTMESSID_CLM_REGISTER_SERVER_RESP   1016 // status of server registration request
84 #define PROTMESSID_CLM_REGISTER_SERVER_EX     1017 // register server with extended information
85 #define PROTMESSID_CLM_RED_SERVER_LIST        1018 // reduced server list
86 
87 // special IDs
88 #define PROTMESSID_SPECIAL_SPLIT_MESSAGE 2001 // a container for split messages
89 
90 // lengths of message as defined in protocol.cpp file
91 #define MESS_HEADER_LENGTH_BYTE    7 // TAG (2), ID (2), cnt (1), length (2)
92 #define MESS_LEN_WITHOUT_DATA_BYTE ( MESS_HEADER_LENGTH_BYTE + 2 /* CRC (2) */ )
93 
94 // time out for message re-send if no acknowledgement was received
95 #define SEND_MESS_TIMEOUT_MS 400 // ms
96 
97 // message split parameters
98 #define MESS_SPLIT_PART_SIZE_BYTES 550
99 #define MAX_NUM_MESS_SPLIT_PARTS   ( MAX_SIZE_BYTES_NETW_BUF / MESS_SPLIT_PART_SIZE_BYTES )
100 
101 /* Classes ********************************************************************/
102 class CProtocol : public QObject
103 {
104     Q_OBJECT
105 
106 public:
107     CProtocol();
108 
109     void Reset();
SetSplitMessageSupported(const bool bIn)110     void SetSplitMessageSupported ( const bool bIn ) { bSplitMessageSupported = bIn; }
111 
112     void CreateJitBufMes ( const int iJitBufSize );
113     void CreateReqJitBufMes();
114     void CreateClientIDMes ( const int iChanID );
115     void CreateChanGainMes ( const int iChanID, const float fGain );
116     void CreateChanPanMes ( const int iChanID, const float fPan );
117     void CreateMuteStateHasChangedMes ( const int iChanID, const bool bIsMuted );
118     void CreateConClientListMes ( const CVector<CChannelInfo>& vecChanInfo );
119     void CreateReqConnClientsList();
120     void CreateChanInfoMes ( const CChannelCoreInfo ChanInfo );
121     void CreateReqChanInfoMes();
122     void CreateChatTextMes ( const QString strChatText );
123     void CreateNetwTranspPropsMes ( const CNetworkTransportProps& NetTrProps );
124     void CreateReqNetwTranspPropsMes();
125     void CreateReqSplitMessSupportMes();
126     void CreateSplitMessSupportedMes();
127     void CreateLicenceRequiredMes ( const ELicenceType eLicenceType );
128     void CreateOpusSupportedMes();
129 
130     // clang-format off
131 // TODO needed for compatibility to old servers >= 3.4.6 and <= 3.5.12
132 void CreateReqChannelLevelListMes();
133     // clang-format on
134 
135     void CreateVersionAndOSMes();
136     void CreateRecorderStateMes ( const ERecorderState eRecorderState );
137 
138     void CreateCLPingMes ( const CHostAddress& InetAddr, const int iMs );
139     void CreateCLPingWithNumClientsMes ( const CHostAddress& InetAddr, const int iMs, const int iNumClients );
140     void CreateCLServerFullMes ( const CHostAddress& InetAddr );
141     void CreateCLRegisterServerMes ( const CHostAddress& InetAddr, const CHostAddress& LInetAddr, const CServerCoreInfo& ServerInfo );
142     void CreateCLRegisterServerExMes ( const CHostAddress& InetAddr, const CHostAddress& LInetAddr, const CServerCoreInfo& ServerInfo );
143     void CreateCLUnregisterServerMes ( const CHostAddress& InetAddr );
144     void CreateCLServerListMes ( const CHostAddress& InetAddr, const CVector<CServerInfo> vecServerInfo );
145     void CreateCLRedServerListMes ( const CHostAddress& InetAddr, const CVector<CServerInfo> vecServerInfo );
146     void CreateCLReqServerListMes ( const CHostAddress& InetAddr );
147     void CreateCLSendEmptyMesMes ( const CHostAddress& InetAddr, const CHostAddress& TargetInetAddr );
148     void CreateCLEmptyMes ( const CHostAddress& InetAddr );
149     void CreateCLDisconnection ( const CHostAddress& InetAddr );
150     void CreateCLVersionAndOSMes ( const CHostAddress& InetAddr );
151     void CreateCLReqVersionAndOSMes ( const CHostAddress& InetAddr );
152     void CreateCLConnClientsListMes ( const CHostAddress& InetAddr, const CVector<CChannelInfo>& vecChanInfo );
153     void CreateCLReqConnClientsListMes ( const CHostAddress& InetAddr );
154     void CreateCLChannelLevelListMes ( const CHostAddress& InetAddr, const CVector<uint16_t>& vecLevelList, const int iNumClients );
155     void CreateCLRegisterServerResp ( const CHostAddress& InetAddr, const ESvrRegResult eResult );
156 
157     static bool ParseMessageFrame ( const CVector<uint8_t>& vecbyData,
158                                     const int               iNumBytesIn,
159                                     CVector<uint8_t>&       vecbyMesBodyData,
160                                     int&                    iRecCounter,
161                                     int&                    iRecID );
162 
163     void ParseMessageBody ( const CVector<uint8_t>& vecbyMesBodyData, const int iRecCounter, const int iRecID );
164 
165     void ParseConnectionLessMessageBody ( const CVector<uint8_t>& vecbyMesBodyData, const int iRecID, const CHostAddress& InetAddr );
166 
IsConnectionLessMessageID(const int iID)167     static bool IsConnectionLessMessageID ( const int iID ) { return ( iID >= 1000 ) && ( iID < 2000 ); }
168 
169     // this function is public because we need it in the test bench
170     void CreateAndImmSendAcknMess ( const int& iID, const int& iCnt );
171 
172 protected:
173     class CSendMessage
174     {
175     public:
CSendMessage()176         CSendMessage() : vecMessage ( 0 ), iID ( PROTMESSID_ILLEGAL ), iCnt ( 0 ) {}
CSendMessage(const CVector<uint8_t> & nMess,const int iNCnt,const int iNID)177         CSendMessage ( const CVector<uint8_t>& nMess, const int iNCnt, const int iNID ) : vecMessage ( nMess ), iID ( iNID ), iCnt ( iNCnt ) {}
178 
179         CSendMessage& operator= ( const CSendMessage& NewSendMess )
180         {
181             vecMessage.Init ( NewSendMess.vecMessage.Size() );
182             vecMessage = NewSendMess.vecMessage;
183 
184             iID  = NewSendMess.iID;
185             iCnt = NewSendMess.iCnt;
186             return *this;
187         }
188 
189         CVector<uint8_t> vecMessage;
190         int              iID, iCnt;
191     };
192 
193     void EnqueueMessage ( CVector<uint8_t>& vecMessage, const int iCnt, const int iID );
194 
195     void GenMessageFrame ( CVector<uint8_t>& vecOut, const int iCnt, const int iID, const CVector<uint8_t>& vecData );
196 
197     void GenSplitMessageContainer ( CVector<uint8_t>&       vecOut,
198                                     const int               iID,
199                                     const int               iNumParts,
200                                     const int               iSplitCnt,
201                                     const CVector<uint8_t>& vecData,
202                                     const int               iStartIndexInData,
203                                     const int               iLengthOfDataPart );
204 
205     bool ParseSplitMessageContainer ( const CVector<uint8_t>& vecbyData,
206                                       CVector<uint8_t>&       vecbyMesBodyData,
207                                       const int               iSplitMessageDataIndex,
208                                       int&                    iID,
209                                       int&                    iNumParts,
210                                       int&                    iSplitCnt,
211                                       int&                    iCurPartSize );
212 
213     void PutValOnStream ( CVector<uint8_t>& vecIn, int& iPos, const uint32_t iVal, const int iNumOfBytes );
214 
215     void PutStringUTF8OnStream ( CVector<uint8_t>& vecIn,
216                                  int&              iPos,
217                                  const QByteArray& sStringUTF8,
218                                  const int         iNumberOfBytsLen = 2 ); // default is 2 bytes length indicator
219 
220     static uint32_t GetValFromStream ( const CVector<uint8_t>& vecIn, int& iPos, const int iNumOfBytes );
221 
222     bool GetStringFromStream ( const CVector<uint8_t>& vecIn,
223                                int&                    iPos,
224                                const int               iMaxStringLen,
225                                QString&                strOut,
226                                const int               iNumberOfBytsLen = 2 ); // default is 2 bytes length indicator
227 
228     void SendMessage();
229 
230     void CreateAndSendMessage ( const int iID, const CVector<uint8_t>& vecData );
231 
232     void CreateAndImmSendConLessMessage ( const int iID, const CVector<uint8_t>& vecData, const CHostAddress& InetAddr );
233 
234     bool EvaluateJitBufMes ( const CVector<uint8_t>& vecData );
235     bool EvaluateReqJitBufMes();
236     bool EvaluateClientIDMes ( const CVector<uint8_t>& vecData );
237     bool EvaluateChanGainMes ( const CVector<uint8_t>& vecData );
238     bool EvaluateChanPanMes ( const CVector<uint8_t>& vecData );
239     bool EvaluateMuteStateHasChangedMes ( const CVector<uint8_t>& vecData );
240     bool EvaluateConClientListMes ( const CVector<uint8_t>& vecData );
241     bool EvaluateReqConnClientsList();
242     bool EvaluateChanInfoMes ( const CVector<uint8_t>& vecData );
243     bool EvaluateReqChanInfoMes();
244     bool EvaluateChatTextMes ( const CVector<uint8_t>& vecData );
245     bool EvaluateNetwTranspPropsMes ( const CVector<uint8_t>& vecData );
246     bool EvaluateReqNetwTranspPropsMes();
247     bool EvaluateReqSplitMessSupportMes();
248     bool EvaluateSplitMessSupportedMes();
249     bool EvaluateLicenceRequiredMes ( const CVector<uint8_t>& vecData );
250     bool EvaluateVersionAndOSMes ( const CVector<uint8_t>& vecData );
251     bool EvaluateRecorderStateMes ( const CVector<uint8_t>& vecData );
252 
253     bool EvaluateCLPingMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );
254     bool EvaluateCLPingWithNumClientsMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );
255     bool EvaluateCLServerFullMes();
256     bool EvaluateCLRegisterServerMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );
257     bool EvaluateCLRegisterServerExMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );
258     bool EvaluateCLUnregisterServerMes ( const CHostAddress& InetAddr );
259     bool EvaluateCLServerListMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );
260     bool EvaluateCLRedServerListMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );
261     bool EvaluateCLReqServerListMes ( const CHostAddress& InetAddr );
262     bool EvaluateCLSendEmptyMesMes ( const CVector<uint8_t>& vecData );
263     bool EvaluateCLDisconnectionMes ( const CHostAddress& InetAddr );
264     bool EvaluateCLVersionAndOSMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );
265     bool EvaluateCLReqVersionAndOSMes ( const CHostAddress& InetAddr );
266     bool EvaluateCLConnClientsListMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );
267     bool EvaluateCLReqConnClientsListMes ( const CHostAddress& InetAddr );
268     bool EvaluateCLChannelLevelListMes ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );
269     bool EvaluateCLRegisterServerResp ( const CHostAddress& InetAddr, const CVector<uint8_t>& vecData );
270 
271     int iOldRecID;
272     int iOldRecCnt;
273 
274     // these two objects must be sequred by a mutex
275     uint8_t                 iCounter;
276     std::list<CSendMessage> SendMessQueue;
277 
278     QTimer TimerSendMess;
279     QMutex Mutex;
280 
281     CVector<uint8_t> vecbySplitMessageStorage;
282     int              iSplitMessageCnt;
283     int              iSplitMessageDataIndex;
284     bool             bSplitMessageSupported;
285 
286 public slots:
OnTimerSendMess()287     void OnTimerSendMess() { SendMessage(); }
288 
289 signals:
290     // transmitting
291     void MessReadyForSending ( CVector<uint8_t> vecMessage );
292     void CLMessReadyForSending ( CHostAddress InetAddr, CVector<uint8_t> vecMessage );
293 
294     // receiving
295     void ChangeJittBufSize ( int iNewJitBufSize );
296     void ReqJittBufSize();
297     void ChangeNetwBlSiFact ( int iNewNetwBlSiFact );
298     void ClientIDReceived ( int iChanID );
299     void ChangeChanGain ( int iChanID, float fNewGain );
300     void ChangeChanPan ( int iChanID, float fNewPan );
301     void MuteStateHasChangedReceived ( int iCurID, bool bIsMuted );
302     void ConClientListMesReceived ( CVector<CChannelInfo> vecChanInfo );
303     void ServerFullMesReceived();
304     void ReqConnClientsList();
305     void ChangeChanInfo ( CChannelCoreInfo ChanInfo );
306     void ReqChanInfo();
307     void ChatTextReceived ( QString strChatText );
308     void NetTranspPropsReceived ( CNetworkTransportProps NetworkTransportProps );
309     void ReqNetTranspProps();
310     void ReqSplitMessSupport();
311     void SplitMessSupported();
312     void LicenceRequired ( ELicenceType eLicenceType );
313     void VersionAndOSReceived ( COSUtil::EOpSystemType eOSType, QString strVersion );
314     void RecorderStateReceived ( ERecorderState eRecorderState );
315 
316     void CLPingReceived ( CHostAddress InetAddr, int iMs );
317     void CLPingWithNumClientsReceived ( CHostAddress InetAddr, int iMs, int iNumClients );
318     void CLRegisterServerReceived ( CHostAddress InetAddr, CHostAddress LInetAddr, CServerCoreInfo ServerInfo );
319     void CLRegisterServerExReceived ( CHostAddress           InetAddr,
320                                       CHostAddress           LInetAddr,
321                                       CServerCoreInfo        ServerInfo,
322                                       COSUtil::EOpSystemType eOSType,
323                                       QString                strVersion );
324     void CLUnregisterServerReceived ( CHostAddress InetAddr );
325     void CLServerListReceived ( CHostAddress InetAddr, CVector<CServerInfo> vecServerInfo );
326     void CLRedServerListReceived ( CHostAddress InetAddr, CVector<CServerInfo> vecServerInfo );
327     void CLReqServerList ( CHostAddress InetAddr );
328     void CLSendEmptyMes ( CHostAddress TargetInetAddr );
329     void CLDisconnection ( CHostAddress InetAddr );
330     void CLVersionAndOSReceived ( CHostAddress InetAddr, COSUtil::EOpSystemType eOSType, QString strVersion );
331     void CLReqVersionAndOS ( CHostAddress InetAddr );
332     void CLConnClientsListMesReceived ( CHostAddress InetAddr, CVector<CChannelInfo> vecChanInfo );
333     void CLReqConnClientsList ( CHostAddress InetAddr );
334     void CLChannelLevelListReceived ( CHostAddress InetAddr, CVector<uint16_t> vecLevelList );
335     void CLRegisterServerResp ( CHostAddress InetAddr, ESvrRegResult eStatus );
336 };
337