Skip to content

Commit 23e9c36

Browse files
committed
Replace boolean TCP flag with multimode enum
To provide three modes: UDP, TCP once or TCP long connection.
1 parent 868416e commit 23e9c36

11 files changed

Lines changed: 47 additions & 37 deletions

File tree

src/client.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ void CClient::OnSendProtMessage ( CVector<uint8_t> vecMessage )
251251
Socket.SendPacket ( vecMessage, Channel.GetAddress() );
252252
}
253253

254-
void CClient::OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecMessage, CTcpConnection* pTcpConnection, bool bUseTcpClient )
254+
void CClient::OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecMessage, CTcpConnection* pTcpConnection, enum EProtoMode eProtoMode )
255255
{
256256
if ( pTcpConnection )
257257
{
@@ -261,7 +261,7 @@ void CClient::OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecM
261261

262262
// the protocol queries me to call the function to send the message
263263
// send it through the network
264-
if ( bUseTcpClient )
264+
if ( eProtoMode != PROTO_UDP )
265265
{
266266
// create a TCP client connection and send message
267267
QTcpSocket* pSocket = new QTcpSocket ( this );
@@ -279,11 +279,13 @@ void CClient::OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecM
279279
pSocket->deleteLater();
280280
} );
281281

282-
connect ( pSocket, &QTcpSocket::connected, this, [this, pSocket, InetAddr, vecMessage]() {
282+
connect ( pSocket, &QTcpSocket::connected, this, [this, pSocket, InetAddr, vecMessage, eProtoMode]() {
283283
// connection succeeded, give it to a CTcpConnection
284-
CTcpConnection* pTcpConnection =
285-
new CTcpConnection ( pSocket, InetAddr, nullptr, &Channel, true ); // client connection, auto-disconn, will self-delete on disconnect
286-
// TODO: do not set bDisconAfterRecv when sending CLM_CLIENT_ID for long-term connection
284+
CTcpConnection* pTcpConnection = new CTcpConnection ( pSocket,
285+
InetAddr,
286+
nullptr,
287+
&Channel,
288+
eProtoMode == PROTO_TCP_ONCE ); // client connection, will self-delete on disconnect
287289

288290
pTcpConnection->write ( (const char*) &( (CVector<uint8_t>) vecMessage )[0], vecMessage.Size() );
289291

src/client.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,14 @@ class CClient : public QObject
278278

279279
void CreateCLServerListReqVerAndOSMes ( const CHostAddress& InetAddr ) { ConnLessProtocol.CreateCLReqVersionAndOSMes ( InetAddr ); }
280280

281-
void CreateCLServerListReqConnClientsListMes ( const CHostAddress& InetAddr, bool bUseTcpClient )
281+
void CreateCLServerListReqConnClientsListMes ( const CHostAddress& InetAddr, enum EProtoMode eProtoMode )
282282
{
283-
ConnLessProtocol.CreateCLReqConnClientsListMes ( InetAddr, bUseTcpClient );
283+
ConnLessProtocol.CreateCLReqConnClientsListMes ( InetAddr, eProtoMode );
284284
}
285285

286-
void CreateCLReqServerListMes ( const CHostAddress& InetAddr, bool bUseTcpClient )
286+
void CreateCLReqServerListMes ( const CHostAddress& InetAddr, enum EProtoMode eProtoMode )
287287
{
288-
ConnLessProtocol.CreateCLReqServerListMes ( InetAddr, bUseTcpClient );
288+
ConnLessProtocol.CreateCLReqServerListMes ( InetAddr, eProtoMode );
289289
}
290290

291291
int EstimatedOverallDelay ( const int iPingTimeMs );
@@ -453,7 +453,7 @@ protected slots:
453453
void OnCLPingReceived ( CHostAddress InetAddr, int iMs );
454454
void OnCLTcpSupported ( CHostAddress InetAddr, int iID );
455455

456-
void OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecMessage, CTcpConnection* pTcpConnection, bool bUseTcpClient );
456+
void OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecMessage, CTcpConnection* pTcpConnection, enum EProtoMode eProtoMode );
457457

458458
void OnCLPingWithNumClientsReceived ( CHostAddress InetAddr, int iMs, int iNumClients );
459459

src/clientdlg.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,15 @@ public slots:
202202

203203
void OnNewLocalInputText ( QString strChatText ) { pClient->CreateChatTextMes ( strChatText ); }
204204

205-
void OnReqServerListQuery ( CHostAddress InetAddr, bool bUseTcpClient ) { pClient->CreateCLReqServerListMes ( InetAddr, bUseTcpClient ); }
205+
void OnReqServerListQuery ( CHostAddress InetAddr, enum EProtoMode eProtoMode ) { pClient->CreateCLReqServerListMes ( InetAddr, eProtoMode ); }
206206

207207
void OnCreateCLServerListPingMes ( CHostAddress InetAddr ) { pClient->CreateCLServerListPingMes ( InetAddr ); }
208208

209209
void OnCreateCLServerListReqVerAndOSMes ( CHostAddress InetAddr ) { pClient->CreateCLServerListReqVerAndOSMes ( InetAddr ); }
210210

211-
void OnCreateCLServerListReqConnClientsListMes ( CHostAddress InetAddr, bool bUseTcpClient )
211+
void OnCreateCLServerListReqConnClientsListMes ( CHostAddress InetAddr, enum EProtoMode eProtoMode )
212212
{
213-
pClient->CreateCLServerListReqConnClientsListMes ( InetAddr, bUseTcpClient );
213+
pClient->CreateCLServerListReqConnClientsListMes ( InetAddr, eProtoMode );
214214
}
215215

216216
void OnCLServerListReceived ( CHostAddress InetAddr, CVector<CServerInfo> vecServerInfo )

src/clientrpc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ CClientRpc::CClientRpc ( CClient* pClient, CClientSettings* pSettings, CRpcServe
171171
if ( NetworkUtil::ParseNetworkAddress ( jsonDirectoryIp.toString(), haDirectoryAddress, false ) )
172172
{
173173
// send the request for the server list
174-
pClient->CreateCLReqServerListMes ( haDirectoryAddress, false ); // UDP
174+
pClient->CreateCLReqServerListMes ( haDirectoryAddress, PROTO_UDP );
175175
response["result"] = "ok";
176176
}
177177
else

src/connectdlg.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ void CConnectDlg::RequestServerList()
336336
false ) )
337337
{
338338
// send the request for the server list
339-
emit ReqServerListQuery ( haDirectoryAddress, false ); // UDP
339+
emit ReqServerListQuery ( haDirectoryAddress, PROTO_UDP );
340340

341341
// start timer, if this message did not get any respond to retransmit
342342
// the server list request message
@@ -379,7 +379,7 @@ void CConnectDlg::OnTimerReRequestServList()
379379
{
380380
// note that this is a connection less message which may get lost
381381
// and therefore it makes sense to re-transmit it
382-
emit ReqServerListQuery ( haDirectoryAddress, false ); // UDP
382+
emit ReqServerListQuery ( haDirectoryAddress, PROTO_UDP );
383383
}
384384
}
385385

@@ -557,7 +557,7 @@ void CConnectDlg::SetTcpSupported ( const CHostAddress& InetAddr, int iID )
557557
if ( !bServerListReceived )
558558
{
559559
// send the request for the server list
560-
emit ReqServerListQuery ( InetAddr, true ); // TCP
560+
emit ReqServerListQuery ( InetAddr, PROTO_TCP_ONCE ); // Close TCP connection after receiving reply
561561
}
562562
break;
563563

@@ -578,7 +578,7 @@ void CConnectDlg::SetTcpSupported ( const CHostAddress& InetAddr, int iID )
578578
eFetchMode = CFM_TCP;
579579
pCurListViewItem->setData ( LVC_CLIENTS, Qt::UserRole, eFetchMode ); // remember for future fetches
580580

581-
emit CreateCLServerListReqConnClientsListMes ( InetAddr, true ); // TCP
581+
emit CreateCLServerListReqConnClientsListMes ( InetAddr, PROTO_TCP_ONCE ); // Close TCP connection after receiving reply
582582
}
583583
}
584584
}
@@ -1045,7 +1045,7 @@ void CConnectDlg::SetPingTimeAndNumClientsResult ( const CHostAddress& InetAddr,
10451045
eFetchMode = CFM_UDP_REQUEST;
10461046
pCurListViewItem->setData ( LVC_CLIENTS, Qt::UserRole, eFetchMode );
10471047
}
1048-
emit CreateCLServerListReqConnClientsListMes ( InetAddr, eFetchMode == CFM_TCP ); // UDP or TCP
1048+
emit CreateCLServerListReqConnClientsListMes ( InetAddr, eFetchMode == CFM_TCP ? PROTO_TCP_ONCE : PROTO_UDP );
10491049
}
10501050

10511051
// this is the first time a ping time was received, set item to visible

src/connectdlg.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ public slots:
146146
void OnCurrentServerItemChanged ( QTreeWidgetItem* current, QTreeWidgetItem* previous );
147147

148148
signals:
149-
void ReqServerListQuery ( CHostAddress InetAddr, bool bUseTcpClient );
149+
void ReqServerListQuery ( CHostAddress InetAddr, enum EProtoMode eProtoMode );
150150
void CreateCLServerListPingMes ( CHostAddress InetAddr );
151151
void CreateCLServerListReqVerAndOSMes ( CHostAddress InetAddr );
152-
void CreateCLServerListReqConnClientsListMes ( CHostAddress InetAddr, bool bUseTcpClient );
152+
void CreateCLServerListReqConnClientsListMes ( CHostAddress InetAddr, enum EProtoMode eProtoMode );
153153
};

src/protocol.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ void CProtocol::CreateAndImmSendConLessMessage ( const int iID,
636636
const CVector<uint8_t>& vecData,
637637
const CHostAddress& InetAddr,
638638
CTcpConnection* pTcpConnection,
639-
bool bUseTcpClient )
639+
enum EProtoMode eProtoMode )
640640
{
641641
CVector<uint8_t> vecNewMessage;
642642

@@ -645,7 +645,7 @@ void CProtocol::CreateAndImmSendConLessMessage ( const int iID,
645645
GenMessageFrame ( vecNewMessage, 0, iID, vecData );
646646

647647
// immediately send message
648-
emit CLMessReadyForSending ( InetAddr, vecNewMessage, pTcpConnection, bUseTcpClient );
648+
emit CLMessReadyForSending ( InetAddr, vecNewMessage, pTcpConnection, eProtoMode );
649649
}
650650

651651
void CProtocol::ParseMessageBody ( const CVector<uint8_t>& vecbyMesBodyData, const int iRecCounter, const int iRecID )
@@ -2262,9 +2262,9 @@ bool CProtocol::EvaluateCLRedServerListMes ( const CHostAddress& InetAddr, const
22622262
return false; // no error
22632263
}
22642264

2265-
void CProtocol::CreateCLReqServerListMes ( const CHostAddress& InetAddr, bool bUseTcpClient )
2265+
void CProtocol::CreateCLReqServerListMes ( const CHostAddress& InetAddr, enum EProtoMode eProtoMode )
22662266
{
2267-
CreateAndImmSendConLessMessage ( PROTMESSID_CLM_REQ_SERVER_LIST, CVector<uint8_t> ( 0 ), InetAddr, nullptr, bUseTcpClient );
2267+
CreateAndImmSendConLessMessage ( PROTMESSID_CLM_REQ_SERVER_LIST, CVector<uint8_t> ( 0 ), InetAddr, nullptr, eProtoMode );
22682268
}
22692269

22702270
bool CProtocol::EvaluateCLReqServerListMes ( const CHostAddress& InetAddr, CTcpConnection* pTcpConnection )
@@ -2515,9 +2515,9 @@ bool CProtocol::EvaluateCLConnClientsListMes ( const CHostAddress& InetAddr, con
25152515
return false; // no error
25162516
}
25172517

2518-
void CProtocol::CreateCLReqConnClientsListMes ( const CHostAddress& InetAddr, bool bUseTcpClient )
2518+
void CProtocol::CreateCLReqConnClientsListMes ( const CHostAddress& InetAddr, enum EProtoMode eProtoMode )
25192519
{
2520-
CreateAndImmSendConLessMessage ( PROTMESSID_CLM_REQ_CONN_CLIENTS_LIST, CVector<uint8_t> ( 0 ), InetAddr, nullptr, bUseTcpClient );
2520+
CreateAndImmSendConLessMessage ( PROTMESSID_CLM_REQ_CONN_CLIENTS_LIST, CVector<uint8_t> ( 0 ), InetAddr, nullptr, eProtoMode );
25212521
}
25222522

25232523
bool CProtocol::EvaluateCLReqConnClientsListMes ( const CHostAddress& InetAddr, CTcpConnection* pTcpConnection )

src/protocol.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@
101101
#define MESS_SPLIT_PART_SIZE_BYTES 550
102102
#define MAX_NUM_MESS_SPLIT_PARTS ( MAX_SIZE_BYTES_NETW_BUF / MESS_SPLIT_PART_SIZE_BYTES )
103103

104+
/* Enum for protocol mode *****************************************************/
105+
enum EProtoMode
106+
{
107+
PROTO_UDP,
108+
PROTO_TCP_ONCE,
109+
PROTO_TCP_LONG,
110+
};
111+
104112
/* Classes ********************************************************************/
105113
class CProtocol : public QObject
106114
{
@@ -146,14 +154,14 @@ class CProtocol : public QObject
146154
void CreateCLUnregisterServerMes ( const CHostAddress& InetAddr );
147155
void CreateCLServerListMes ( const CHostAddress& InetAddr, const CVector<CServerInfo> vecServerInfo, CTcpConnection* pTcpConnection );
148156
void CreateCLRedServerListMes ( const CHostAddress& InetAddr, const CVector<CServerInfo> vecServerInfo );
149-
void CreateCLReqServerListMes ( const CHostAddress& InetAddr, bool bUseTcpClient );
157+
void CreateCLReqServerListMes ( const CHostAddress& InetAddr, enum EProtoMode eProtoMode );
150158
void CreateCLSendEmptyMesMes ( const CHostAddress& InetAddr, const CHostAddress& TargetInetAddr );
151159
void CreateCLEmptyMes ( const CHostAddress& InetAddr );
152160
void CreateCLDisconnection ( const CHostAddress& InetAddr );
153161
void CreateCLVersionAndOSMes ( const CHostAddress& InetAddr );
154162
void CreateCLReqVersionAndOSMes ( const CHostAddress& InetAddr );
155163
void CreateCLConnClientsListMes ( const CHostAddress& InetAddr, const CVector<CChannelInfo>& vecChanInfo, CTcpConnection* pTcpConnection );
156-
void CreateCLReqConnClientsListMes ( const CHostAddress& InetAddr, bool bUseTcpClient );
164+
void CreateCLReqConnClientsListMes ( const CHostAddress& InetAddr, enum EProtoMode eProtoMode );
157165
void CreateCLChannelLevelListMes ( const CHostAddress& InetAddr, const CVector<uint16_t>& vecLevelList, const int iNumClients );
158166
void CreateCLRegisterServerResp ( const CHostAddress& InetAddr, const ESvrRegResult eResult );
159167
void CreateCLTcpSupportedMes ( const CHostAddress& InetAddr, const int iID );
@@ -255,7 +263,7 @@ class CProtocol : public QObject
255263
const CVector<uint8_t>& vecData,
256264
const CHostAddress& InetAddr,
257265
CTcpConnection* pTcpConnection = nullptr,
258-
bool bUseTcpClient = false );
266+
enum EProtoMode eProtoMode = PROTO_UDP );
259267

260268
bool EvaluateJitBufMes ( const CVector<uint8_t>& vecData );
261269
bool EvaluateReqJitBufMes();
@@ -317,7 +325,7 @@ public slots:
317325
signals:
318326
// transmitting
319327
void MessReadyForSending ( CVector<uint8_t> vecMessage );
320-
void CLMessReadyForSending ( CHostAddress InetAddr, CVector<uint8_t> vecMessage, CTcpConnection* pTcpConnection, bool bUseTcpClient );
328+
void CLMessReadyForSending ( CHostAddress InetAddr, CVector<uint8_t> vecMessage, CTcpConnection* pTcpConnection, enum EProtoMode eProtoMode );
321329

322330
// receiving
323331
void ChangeJittBufSize ( int iNewJitBufSize );

src/server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,9 @@ void CServer::OnServerFull ( CHostAddress RecHostAddr )
477477
ConnLessProtocol.CreateCLServerFullMes ( RecHostAddr );
478478
}
479479

480-
void CServer::OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecMessage, CTcpConnection* pTcpConnection, bool bUseTcpClient )
480+
void CServer::OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecMessage, CTcpConnection* pTcpConnection, enum EProtoMode eProtoMode )
481481
{
482-
if ( bUseTcpClient )
482+
if ( eProtoMode != PROTO_UDP )
483483
{
484484
qWarning() << "Server send cannot use TCP client";
485485
return;

src/server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ public slots:
341341

342342
void OnServerFull ( CHostAddress RecHostAddr );
343343

344-
void OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecMessage, CTcpConnection* pTcpConnection, bool bUseTcpClient );
344+
void OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecMessage, CTcpConnection* pTcpConnection, enum EProtoMode eProtoMode );
345345

346346
void OnProtocolCLMessageReceived ( int iRecID, CVector<uint8_t> vecbyMesBodyData, CHostAddress RecHostAddr, CTcpConnection* pTcpConnection );
347347

0 commit comments

Comments
 (0)