Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
e1aae81
Add --enabletcp option for server
softins Jan 21, 2025
780b8f9
Initial skeleton for TCP protocol server
softins Mar 22, 2024
7c18955
Add handling of CL msgs for Server and client list
softins Mar 25, 2024
44da5c1
Create CLM_TCP_SUPPORTED and related methods
softins Jan 21, 2025
eb281fd
Add CLM_TCP_SUPPORTED message generation when TCP enabled
softins Mar 9, 2026
76527cc
Add flag to request TCP client use
softins Mar 16, 2026
b4a25c4
Add handlers for TCP Supported message
softins Mar 16, 2026
d21b895
Propagate TCP client flag down to OnSendCLProtMessage
softins Mar 16, 2026
d993533
Delete QTcpServer object when done
softins Mar 19, 2026
1a9cc0e
Added some debug output
softins Mar 20, 2026
401fb01
Update copyright years
softins Mar 20, 2026
7bc93c2
Separate CTcpConnection code from CTcpServer
softins Mar 21, 2026
43afcd8
Make CTcpConnection members private
softins Mar 22, 2026
325a46a
Add client-side TCP code
softins Mar 23, 2026
f37b065
Request server list via TCP if required
softins Mar 23, 2026
7c54222
Add message context parameter for CLM_TCP_SUPPORTED
softins Mar 25, 2026
11bff37
Add Qt version check for errorOccurred()
softins Mar 25, 2026
2b51b13
Fetch client list over TCP when necessary for a server
softins Mar 26, 2026
bbcbeb3
Create CLM_CLIENT_ID and related methods
softins Apr 1, 2026
d068fb4
Add OnClientIDReceived slot to CChannel
softins Apr 1, 2026
6826d4b
Skeleton support for connected mode TCP
softins Apr 1, 2026
ca86215
Move TCP debug message from connectdlg to client
softins Apr 2, 2026
2c13829
Add missing return
softins Apr 2, 2026
d1c8229
Remove unneeded #include from tcpconnection
softins Apr 2, 2026
11768e9
Send client list via TCP when connection available
softins Apr 4, 2026
80644be
Add skeleton handler for CLClientID to CServer
softins Apr 4, 2026
7322345
Add disconnecFromHost method to CTcpConnection
softins Apr 5, 2026
739fe04
Mods to CTcpConnection for future use
softins Apr 5, 2026
0bcd7ad
In server, link TCP channel to UDP channel by client ID
softins Apr 5, 2026
5e7a8ca
Replace boolean TCP flag with multimode enum
softins Apr 5, 2026
f2e59a5
Add creation of session-long TCP connection
softins Apr 6, 2026
fc65fe3
Route CLConnClientList depending on whether connected
softins Apr 6, 2026
db7437c
Be specific about bDisconAfterRecv for TCP
softins Apr 7, 2026
9447816
Rework TCP session-mode connection
softins Apr 7, 2026
c0960fe
Minor comment updates
softins Apr 7, 2026
7a85d0a
Add support for sending Empty Message over TCP
softins Apr 8, 2026
a621461
Implement keepalive over session long TCP connection
softins Apr 9, 2026
1934beb
Clarify comment
softins Apr 9, 2026
e644eab
Make CTcpConnection work in serveronly mode.
softins Apr 9, 2026
0858dda
Add timeout for TCP connection
softins Apr 9, 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::MuteStateHasChangedReceived, this, &CChannel::MuteStateHasChangedReceived );

Expand Down Expand Up @@ -712,3 +713,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 @@ -161,7 +164,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 @@ -186,7 +189,8 @@ class CChannel : public QObject
}

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

// channel info
CChannelCoreInfo ChannelInfo;
Expand Down Expand Up @@ -255,11 +259,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 @@ -282,7 +287,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 @@ -145,7 +145,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 @@ -249,11 +251,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 @@ -268,10 +339,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 @@ -975,6 +1046,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 All @@ -989,11 +1070,52 @@ void CClient::OnClientIDReceived ( int iServerChanID )
emit ClientIDReceived ( iChanID );
}

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 @@ -1014,6 +1136,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 @@ -422,12 +428,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 @@ -441,8 +451,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 @@ -456,6 +467,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 @@ -471,6 +489,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