Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
ec7343d
Add --enabletcp option for server
softins Jan 21, 2025
92f4420
Initial skeleton for TCP protocol server
softins Mar 22, 2024
6de67b3
Add handling of CL msgs for Server and client list
softins Mar 25, 2024
0900a79
Create CLM_TCP_SUPPORTED and related methods
softins Jan 21, 2025
a18d541
Add CLM_TCP_SUPPORTED message generation when TCP enabled
softins Mar 9, 2026
27cc94d
Add flag to request TCP client use
softins Mar 16, 2026
474aefa
Add handlers for TCP Supported message
softins Mar 16, 2026
21ea915
Propagate TCP client flag down to OnSendCLProtMessage
softins Mar 16, 2026
7133516
Delete QTcpServer object when done
softins Mar 19, 2026
fd406dc
Added some debug output
softins Mar 20, 2026
017a463
Update copyright years
softins Mar 20, 2026
21dc1f8
Separate CTcpConnection code from CTcpServer
softins Mar 21, 2026
23a21ef
Make CTcpConnection members private
softins Mar 22, 2026
668f7cc
Add client-side TCP code
softins Mar 23, 2026
0419be5
Request server list via TCP if required
softins Mar 23, 2026
4dae70d
Add message context parameter for CLM_TCP_SUPPORTED
softins Mar 25, 2026
7d07a8f
Add Qt version check for errorOccurred()
softins Mar 25, 2026
406b1cc
Fetch client list over TCP when necessary for a server
softins Mar 26, 2026
ccddfde
Create CLM_CLIENT_ID and related methods
softins Apr 1, 2026
8bbeca8
Add OnClientIDReceived slot to CChannel
softins Apr 1, 2026
5d37602
Skeleton support for connected mode TCP
softins Apr 1, 2026
8dae6dc
Move TCP debug message from connectdlg to client
softins Apr 2, 2026
d1f211f
Add missing return
softins Apr 2, 2026
411ffff
Remove unneeded #include from tcpconnection
softins Apr 2, 2026
2081235
Send client list via TCP when connection available
softins Apr 4, 2026
5fb464e
Add skeleton handler for CLClientID to CServer
softins Apr 4, 2026
5486ddc
Add disconnecFromHost method to CTcpConnection
softins Apr 5, 2026
997b368
Mods to CTcpConnection for future use
softins Apr 5, 2026
1573a8c
In server, link TCP channel to UDP channel by client ID
softins Apr 5, 2026
2d3b54d
Replace boolean TCP flag with multimode enum
softins Apr 5, 2026
01530e0
Add creation of session-long TCP connection
softins Apr 6, 2026
245fb40
Route CLConnClientList depending on whether connected
softins Apr 6, 2026
f6eb414
Be specific about bDisconAfterRecv for TCP
softins Apr 7, 2026
468b7d0
Rework TCP session-mode connection
softins Apr 7, 2026
ac977ae
Minor comment updates
softins Apr 7, 2026
19fe1d0
Add support for sending Empty Message over TCP
softins Apr 8, 2026
774c0f3
Implement keepalive over session long TCP connection
softins Apr 9, 2026
a677748
Clarify comment
softins Apr 9, 2026
5414b03
Make CTcpConnection work in serveronly mode.
softins Apr 9, 2026
738bb11
Add timeout for TCP connection
softins Apr 9, 2026
2c09cb1
Add an idle timeout on the server side
softins May 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Jamulus.pro
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ HEADERS += src/plugins/audioreverb.h \
src/serverlogging.h \
src/settings.h \
src/socket.h \
src/tcpserver.h \
src/tcpconnection.h \
src/util.h \
src/recorder/jamrecorder.h \
src/recorder/creaperproject.h \
Expand Down Expand Up @@ -507,6 +509,8 @@ SOURCES += src/plugins/audioreverb.cpp \
src/settings.cpp \
src/signalhandler.cpp \
src/socket.cpp \
src/tcpserver.cpp \
src/tcpconnection.cpp \
src/util.cpp \
src/recorder/jamrecorder.cpp \
src/recorder/creaperproject.cpp \
Expand Down
25 changes: 24 additions & 1 deletion src/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

// CChannel implementation *****************************************************
CChannel::CChannel ( const bool bNIsServer ) :
pTcpConnection ( nullptr ),
vecfGains ( MAX_NUM_CHANNELS, 1.0f ),
vecfPannings ( MAX_NUM_CHANNELS, 0.5f ),
iCurSockBufNumFrames ( INVALID_INDEX ),
Expand Down Expand Up @@ -81,7 +82,7 @@ CChannel::CChannel ( const bool bNIsServer ) :

QObject::connect ( &Protocol, &CProtocol::ChangeChanPan, this, &CChannel::OnChangeChanPan );

QObject::connect ( &Protocol, &CProtocol::ClientIDReceived, this, &CChannel::ClientIDReceived );
QObject::connect ( &Protocol, &CProtocol::ClientIDReceived, this, &CChannel::OnClientIDReceived );

QObject::connect ( &Protocol, &CProtocol::RawAudioSupported, this, &CChannel::RawAudioSupported );

Expand Down Expand Up @@ -714,3 +715,25 @@ void CChannel::UpdateSocketBufferSize()
SetSockBufNumFrames ( SockBuf.GetAutoSetting(), true );
}
}

void CChannel::OnClientIDReceived ( int iChanID )
{
qDebug() << Q_FUNC_INFO << "iChanID =" << iChanID;
emit ClientIDReceived ( iChanID );
}

void CChannel::CreateConClientListMes ( const CVector<CChannelInfo>& vecChanInfo, CProtocol& ConnLessProtocol )
{
if ( pTcpConnection )
{
qDebug() << "- sending client list via TCP";

ConnLessProtocol.CreateCLConnClientsListMes ( InetAddr, vecChanInfo, pTcpConnection );
}
else
{
qDebug() << "- sending client list via UDP";

Protocol.CreateConClientListMes ( vecChanInfo );
}
}
15 changes: 10 additions & 5 deletions src/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class CChannel : public QObject
void SetAddress ( const CHostAddress& NAddr ) { InetAddr = NAddr; }
const CHostAddress& GetAddress() const { return InetAddr; }

void SetTcpConnection ( CTcpConnection* pConnection ) { pTcpConnection = pConnection; }
CTcpConnection* GetTcpConnection() { return pTcpConnection; }

void ResetInfo()
{
bIsIdentified = false;
Expand Down Expand Up @@ -162,7 +165,7 @@ class CChannel : public QObject
void CreateReqChannelLevelListMes() { Protocol.CreateReqChannelLevelListMes(); }
//### TODO: END ###//

void CreateConClientListMes ( const CVector<CChannelInfo>& vecChanInfo ) { Protocol.CreateConClientListMes ( vecChanInfo ); }
void CreateConClientListMes ( const CVector<CChannelInfo>& vecChanInfo, CProtocol& ConnLessProtocol );

void CreateRecorderStateMes ( const ERecorderState eRecorderState ) { Protocol.CreateRecorderStateMes ( eRecorderState ); }

Expand All @@ -187,7 +190,8 @@ class CChannel : public QObject
}

// connection parameters
CHostAddress InetAddr;
CHostAddress InetAddr;
CTcpConnection* pTcpConnection;

// channel info
CChannelCoreInfo ChannelInfo;
Expand Down Expand Up @@ -256,11 +260,12 @@ public slots:
PutProtocolData ( iRecCounter, iRecID, vecbyMesBodyData, RecHostAddr );
}

void OnProtocolCLMessageReceived ( int iRecID, CVector<uint8_t> vecbyMesBodyData, CHostAddress RecHostAddr )
void OnProtocolCLMessageReceived ( int iRecID, CVector<uint8_t> vecbyMesBodyData, CHostAddress RecHostAddr, CTcpConnection* pTcpConnection )
{
emit DetectedCLMessage ( vecbyMesBodyData, iRecID, RecHostAddr );
emit DetectedCLMessage ( vecbyMesBodyData, iRecID, RecHostAddr, pTcpConnection );
}

void OnClientIDReceived ( int iChanID );
void OnNewConnection() { emit NewConnection(); }

signals:
Expand All @@ -284,7 +289,7 @@ public slots:
void RecorderStateReceived ( ERecorderState eRecorderState );
void Disconnected();

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

void ParseMessageBody ( CVector<uint8_t> vecbyMesBodyData, int iRecCounter, int iRecID );
};
140 changes: 135 additions & 5 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ CClient::CClient ( const quint16 iPortNumber,

QObject::connect ( &ConnLessProtocol, &CProtocol::CLRedServerListReceived, this, &CClient::CLRedServerListReceived );

QObject::connect ( &ConnLessProtocol, &CProtocol::CLConnClientsListMesReceived, this, &CClient::CLConnClientsListMesReceived );
QObject::connect ( &ConnLessProtocol, &CProtocol::CLTcpSupported, this, &CClient::OnCLTcpSupported );

QObject::connect ( &ConnLessProtocol, &CProtocol::CLConnClientsListMesReceived, this, &CClient::OnCLConnClientsListMesReceived );

QObject::connect ( &ConnLessProtocol, &CProtocol::CLPingReceived, this, &CClient::OnCLPingReceived );

Expand Down Expand Up @@ -252,11 +254,80 @@ void CClient::OnSendProtMessage ( CVector<uint8_t> vecMessage )
Socket.SendPacket ( vecMessage, Channel.GetAddress() );
}

void CClient::OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecMessage )
void CClient::OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecMessage, CTcpConnection* pTcpConnection, enum EProtoMode eProtoMode )
{
if ( pTcpConnection )
{
// already have TCP connection - just send and return
pTcpConnection->write ( (const char*) &( (CVector<uint8_t>) vecMessage )[0], vecMessage.Size() );
return;
}

// the protocol queries me to call the function to send the message
// send it through the network
Socket.SendPacket ( vecMessage, InetAddr );
if ( eProtoMode != PROTO_UDP )
{
// create a TCP client connection and send message
QTcpSocket* pSocket = new QTcpSocket ( this );

// timer for TCP connect timeout shorter than Qt default 30 seconds
QTimer* pTimer = new QTimer ( this );
pTimer->setSingleShot ( true );

connect ( pTimer, &QTimer::timeout, this, [this, pSocket, pTimer]() {
if ( pSocket->state() != QAbstractSocket::ConnectedState )
{
pSocket->abort();
pSocket->deleteLater();
qDebug() << "- TCP connect timeout";
}
pTimer->deleteLater();
} );

#if QT_VERSION >= QT_VERSION_CHECK( 5, 15, 0 )
# define ERRORSIGNAL &QTcpSocket::errorOccurred
#else
# define ERRORSIGNAL QOverload<QAbstractSocket::SocketError>::of ( &QAbstractSocket::error )
#endif
connect ( pSocket, ERRORSIGNAL, this, [this, pSocket, pTimer] ( QAbstractSocket::SocketError err ) {
Q_UNUSED ( err );

pTimer->stop();
pTimer->deleteLater();

qWarning() << "- TCP connection error:" << pSocket->errorString();
// may want to specifically handle ConnectionRefusedError?
pSocket->deleteLater();
} );

connect ( pSocket, &QTcpSocket::connected, this, [this, pSocket, pTimer, InetAddr, vecMessage, eProtoMode]() {
pTimer->stop();
pTimer->deleteLater();

// connection succeeded, give it to a CTcpConnection
CTcpConnection* pTcpConnection = new CTcpConnection ( pSocket,
InetAddr,
this,
&Channel,
eProtoMode == PROTO_TCP_LONG ); // client connection, will self-delete on disconnect

if ( eProtoMode == PROTO_TCP_LONG )
{
Channel.SetTcpConnection ( pTcpConnection ); // link session connection with channel
}

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

// the CTcpConnection object will pass the reply back up to CClient::Channel
} );

pSocket->connectToHost ( InetAddr.InetAddr, InetAddr.iPort );
pTimer->start ( TCP_CONNECT_TIMEOUT_MS );
}
else
{
Socket.SendPacket ( vecMessage, InetAddr );
}
}

void CClient::OnInvalidPacketReceived ( CHostAddress RecHostAddr )
Expand All @@ -271,10 +342,10 @@ void CClient::OnInvalidPacketReceived ( CHostAddress RecHostAddr )
}
}

void CClient::OnDetectedCLMessage ( CVector<uint8_t> vecbyMesBodyData, int iRecID, CHostAddress RecHostAddr )
void CClient::OnDetectedCLMessage ( CVector<uint8_t> vecbyMesBodyData, int iRecID, CHostAddress RecHostAddr, CTcpConnection* pTcpConnection )
{
// connection less messages are always processed
ConnLessProtocol.ParseConnectionLessMessageBody ( vecbyMesBodyData, iRecID, RecHostAddr );
ConnLessProtocol.ParseConnectionLessMessageBody ( vecbyMesBodyData, iRecID, RecHostAddr, pTcpConnection );
}

void CClient::OnJittBufSizeChanged ( int iNewJitBufSize )
Expand Down Expand Up @@ -978,6 +1049,16 @@ void CClient::OnClientIDReceived ( int iServerChanID )
ClearClientChannels();
}

// if TCP Supported has already been received, make TCP connection to server
iClientID = iServerChanID; // for sending back to server over TCP

if ( bTcpSupported )
{
// *** Make TCP connection
qDebug() << Q_FUNC_INFO << "need to make TCP connection for" << iClientID;
ConnLessProtocol.CreateCLClientIDMes ( Channel.GetAddress(), iClientID, PROTO_TCP_LONG ); // create persistent TCP connection
}

// allocate and map client-side channel 0
int iChanID = FindClientChannel ( iServerChanID, true ); // should always return channel 0

Expand Down Expand Up @@ -1013,11 +1094,52 @@ void CClient::OnRawAudioSupported()
}
}

void CClient::OnCLTcpSupported ( CHostAddress InetAddr, int iID )
{
qDebug() << "- TCP supported at server" << InetAddr.toString() << "for ID =" << iID;

if ( iID != PROTMESSID_CLM_CLIENT_ID )
{
emit CLTcpSupported ( InetAddr, iID ); // pass to connect dialog
return;
}

// if client ID already received, make TCP connection to server
bTcpSupported = true;

if ( iClientID != INVALID_INDEX )
{
// *** Make TCP connection
qDebug() << Q_FUNC_INFO << "need to make TCP connection for" << iClientID;
Q_ASSERT ( InetAddr == Channel.GetAddress() );
ConnLessProtocol.CreateCLClientIDMes ( InetAddr, iClientID, PROTO_TCP_LONG ); // create persistent TCP connection
}
}

void CClient::OnCLConnClientsListMesReceived ( CHostAddress InetAddr, CVector<CChannelInfo> vecChanInfo, CTcpConnection* pTcpConnection )
{
// test if we are receiving for the connect dialog or a connected session
if ( pTcpConnection && pTcpConnection->IsSession() )
{
qDebug() << "- sending client list to client dialog";
OnConClientListMesReceived ( vecChanInfo ); // connected session
}
else
{
qDebug() << "- sending client list to connect dialog";
emit CLConnClientsListMesReceived ( InetAddr, vecChanInfo ); // connect dialog
}
}

void CClient::Start()
{
// init object
Init();

// clear TCP info
iClientID = INVALID_INDEX;
bTcpSupported = false;

// initialise client channels
ClearClientChannels();

Expand All @@ -1038,6 +1160,14 @@ void CClient::Stop()
// stop audio interface
Sound.Stop();

// close any session TCP connection
CTcpConnection* pTcpConnection = Channel.GetTcpConnection();
if ( pTcpConnection )
{
Channel.SetTcpConnection ( nullptr );
pTcpConnection->disconnectFromHost();
}

// disable channel
Channel.SetEnable ( false );

Expand Down
28 changes: 24 additions & 4 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,15 @@ class CClient : public QObject

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

void CreateCLServerListReqConnClientsListMes ( const CHostAddress& InetAddr ) { ConnLessProtocol.CreateCLReqConnClientsListMes ( InetAddr ); }
void CreateCLServerListReqConnClientsListMes ( const CHostAddress& InetAddr, enum EProtoMode eProtoMode )
{
ConnLessProtocol.CreateCLReqConnClientsListMes ( InetAddr, eProtoMode );
}

void CreateCLReqServerListMes ( const CHostAddress& InetAddr ) { ConnLessProtocol.CreateCLReqServerListMes ( InetAddr ); }
void CreateCLReqServerListMes ( const CHostAddress& InetAddr, enum EProtoMode eProtoMode )
{
ConnLessProtocol.CreateCLReqServerListMes ( InetAddr, eProtoMode );
}

int EstimatedOverallDelay ( const int iPingTimeMs );

Expand Down Expand Up @@ -423,12 +429,16 @@ class CClient : public QObject
int maxGainOrPanId;
int iCurPingTime;

// for TCP protocol support
bool bTcpSupported;
int iClientID;

protected slots:
void OnHandledSignal ( int sigNum );
void OnSendProtMessage ( CVector<uint8_t> vecMessage );
void OnInvalidPacketReceived ( CHostAddress RecHostAddr );

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

void OnReqJittBufSize() { CreateServerJitterBufferMessage(); }
void OnJittBufSizeChanged ( int iNewJitBufSize );
Expand All @@ -442,8 +452,9 @@ protected slots:
}
}
void OnCLPingReceived ( CHostAddress InetAddr, int iMs );
void OnCLTcpSupported ( CHostAddress InetAddr, int iID );

void OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecMessage );
void OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecMessage, CTcpConnection* pTcpConnection, enum EProtoMode eProtoMode );

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

Expand All @@ -458,6 +469,13 @@ protected slots:
void OnMuteStateHasChangedReceived ( int iServerChanID, bool bIsMuted );
void OnCLChannelLevelListReceived ( CHostAddress InetAddr, CVector<uint16_t> vecLevelList );
void OnConClientListMesReceived ( CVector<CChannelInfo> vecChanInfo );
void OnCLConnClientsListMesReceived ( CHostAddress InetAddr, CVector<CChannelInfo> vecChanInfo, CTcpConnection* pTcpConnection );

public slots:
void OnCLSendEmptyMes ( CHostAddress InetAddr, CTcpConnection* pTcpConnection )
{
ConnLessProtocol.CreateCLEmptyMes ( InetAddr, pTcpConnection );
}

signals:
void ConClientListMesReceived ( CVector<CChannelInfo> vecChanInfo );
Expand All @@ -473,6 +491,8 @@ protected slots:

void CLRedServerListReceived ( CHostAddress InetAddr, CVector<CServerInfo> vecServerInfo );

void CLTcpSupported ( CHostAddress InetAddr, int iID );

void CLConnClientsListMesReceived ( CHostAddress InetAddr, CVector<CChannelInfo> vecChanInfo );

void CLPingTimeWithNumClientsReceived ( CHostAddress InetAddr, int iPingTime, int iNumClients );
Expand Down
2 changes: 2 additions & 0 deletions src/clientdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,8 @@ CClientDlg::CClientDlg ( CClient* pNCliP,

QObject::connect ( pClient, &CClient::CLRedServerListReceived, this, &CClientDlg::OnCLRedServerListReceived );

QObject::connect ( pClient, &CClient::CLTcpSupported, this, &CClientDlg::OnCLTcpSupported );

QObject::connect ( pClient, &CClient::CLConnClientsListMesReceived, this, &CClientDlg::OnCLConnClientsListMesReceived );

QObject::connect ( pClient, &CClient::CLPingTimeWithNumClientsReceived, this, &CClientDlg::OnCLPingTimeWithNumClientsReceived );
Expand Down
Loading
Loading