Skip to content

Commit d628c27

Browse files
committed
Refactor CServer::CreateAndSendChatTextForAllConChannels to remove duplicate code. Split reusable code into functions
1 parent 130584b commit d628c27

4 files changed

Lines changed: 19 additions & 23 deletions

File tree

docs/JSON-RPC.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ Parameters:
657657

658658
### jamulusserver/chatMessageReceived
659659

660-
Emitted when a chat message is received.
660+
Emitted when a chat message is received from either a Jamulus or RPC client and to be broadcast to all connected clients.
661661

662662
Parameters:
663663

src/server.cpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,32 +1300,27 @@ void CServer::CreateAndSendChatTextForAllConChannels ( const int iCurChanID, con
13001300

13011301
const QString strActualMessageText = "<font color=\"" + sCurColor + "\">(" + QTime::currentTime().toString ( "hh:mm:ss AP" ) + ") <b>" +
13021302
ChanName.toHtmlEscaped() + "</b></font> " + strChatText.toHtmlEscaped();
1303+
SendChatTextToAllChannels ( strActualMessageText );
1304+
}
13031305

1304-
// Send chat text to all connected clients ---------------------------------
1306+
void CServer::SendChatTextToAllChannels ( const QString& strChatText )
1307+
{
1308+
// Send chat text to all channels -------------------------------------------
13051309
for ( int i = 0; i < iMaxNumChannels; i++ )
13061310
{
1307-
if ( vecChannels[i].IsConnected() )
1308-
{
1309-
// send message
1310-
vecChannels[i].CreateChatTextMes ( strActualMessageText );
1311-
}
1311+
SendChatTextToConChannel ( i, strChatText );
13121312
}
1313-
emit receivedChatMessage ( strActualMessageText );
1313+
emit sentChatMessage ( strChatText );
13141314
}
13151315

1316-
// external chat
1317-
void CServer::CreateAndSendExtChatTextForAllConChannels ( const QString& strChatText )
1316+
void CServer::SendChatTextToConChannel ( const int iCurChanID, const QString& strChatText )
13181317
{
1319-
// Send chat text to all connected clients ---------------------------------
1320-
for ( int i = 0; i < iMaxNumChannels; i++ )
1318+
// Only send chat text if channel is connected ------------------------------
1319+
if ( vecChannels[iCurChanID].IsConnected() )
13211320
{
1322-
if ( vecChannels[i].IsConnected() )
1323-
{
1324-
// send message
1325-
vecChannels[i].CreateChatTextMes ( strChatText );
1326-
}
1321+
// send message
1322+
vecChannels[iCurChanID].CreateChatTextMes ( strChatText );
13271323
}
1328-
emit receivedChatMessage ( strChatText );
13291324
}
13301325

13311326
void CServer::CreateAndSendRecorderStateForAllConChannels()

src/server.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class CServer : public QObject, public CServerSlots<MAX_NUM_CHANNELS>
191191
void SetEnableDelayPanning ( bool bDelayPanningOn ) { bDelayPan = bDelayPanningOn; }
192192
bool IsDelayPanningEnabled() { return bDelayPan; }
193193

194-
void CreateAndSendExtChatTextForAllConChannels ( const QString& strChatText );
194+
void SendChatTextToAllChannels ( const QString& strChatText );
195195

196196
protected:
197197
// access functions for actual channels
@@ -207,6 +207,7 @@ class CServer : public QObject, public CServerSlots<MAX_NUM_CHANNELS>
207207
virtual void CreateAndSendChanListForThisChan ( const int iCurChanID );
208208

209209
virtual void CreateAndSendChatTextForAllConChannels ( const int iCurChanID, const QString& strChatText );
210+
void SendChatTextToConChannel ( const int iCurChanID, const QString& strChatText );
210211

211212
virtual void CreateOtherMuteStateChanged ( const int iCurChanID, const int iOtherChanID, const bool bIsMuted );
212213

@@ -332,7 +333,7 @@ class CServer : public QObject, public CServerSlots<MAX_NUM_CHANNELS>
332333
void ClientDisconnected ( const int iChID );
333334
void ClientDisconnectedRPC ( const int iChID );
334335
void ClientConnected ( const int iChID, const QHostAddress RecHostAddr, const int iTotChans );
335-
void receivedChatMessage ( const QString& strChatText );
336+
void sentChatMessage ( const QString& strChatText );
336337
void SvrRegStatusChanged();
337338
void AudioFrame ( const int iChID,
338339
const QString stChName,

src/serverrpc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ CServerRpc::CServerRpc ( CServer* pServer, CRpcServer* pRpcServer, QObject* pare
8686
Qt::QueuedConnection );
8787

8888
/// @rpc_notification jamulusserver/chatMessageReceived
89-
/// @brief Emitted when a chat message is received.
89+
/// @brief Emitted when a chat message is received from either a Jamulus or RPC client and to be broadcast to all connected clients.
9090
/// @param {string} params.chatMessage - Chat message text.
91-
connect ( pServer, &CServer::receivedChatMessage, [=] ( const QString& strChatText ) {
91+
connect ( pServer, &CServer::sentChatMessage, [=] ( const QString& strChatText ) {
9292
pRpcServer->BroadcastNotification ( "jamulusserver/chatMessageReceived",
9393
QJsonObject{
9494
{ "chatMessage", strChatText },
@@ -107,7 +107,7 @@ CServerRpc::CServerRpc ( CServer* pServer, CRpcServer* pRpcServer, QObject* pare
107107
return;
108108
}
109109

110-
pServer->CreateAndSendExtChatTextForAllConChannels ( jsonChatMessage.toString() );
110+
pServer->SendChatTextToAllChannels ( jsonChatMessage.toString() );
111111
response["result"] = "ok";
112112
} );
113113

0 commit comments

Comments
 (0)