Skip to content

Commit e644eab

Browse files
committed
Make CTcpConnection work in serveronly mode.
Constructor for CTcpConnection made polymorphic for client and server.
1 parent 1934beb commit e644eab

4 files changed

Lines changed: 33 additions & 29 deletions

File tree

src/client.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ void CClient::OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecM
284284
// connection succeeded, give it to a CTcpConnection
285285
CTcpConnection* pTcpConnection = new CTcpConnection ( pSocket,
286286
InetAddr,
287-
nullptr,
288287
this,
289288
&Channel,
290289
eProtoMode == PROTO_TCP_LONG ); // client connection, will self-delete on disconnect

src/tcpconnection.cpp

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,17 @@
2424

2525
#include "protocol.h"
2626
#include "server.h"
27-
#include "client.h"
27+
#ifndef SERVER_ONLY
28+
# include "client.h"
29+
#endif
2830
#include "channel.h"
2931

30-
CTcpConnection::CTcpConnection ( QTcpSocket* pTcpSocket,
31-
const CHostAddress& tcpAddress,
32-
CServer* pServer,
33-
CClient* pClient,
34-
CChannel* pChannel,
35-
bool bIsSession ) :
32+
#ifndef SERVER_ONLY
33+
// TCP connection used by client
34+
CTcpConnection::CTcpConnection ( QTcpSocket* pTcpSocket, const CHostAddress& tcpAddress, CClient* pClient, CChannel* pChannel, bool bIsSession ) :
3635
pTcpSocket ( pTcpSocket ),
3736
tcpAddress ( tcpAddress ),
38-
pServer ( pServer ),
39-
pClient ( pClient ),
37+
pServer ( nullptr ),
4038
pChannel ( pChannel ),
4139
bIsSession ( bIsSession )
4240
{
@@ -47,24 +45,35 @@ CTcpConnection::CTcpConnection ( QTcpSocket* pTcpSocket,
4745
connect ( pTcpSocket, &QTcpSocket::disconnected, this, &CTcpConnection::OnDisconnected );
4846
connect ( pTcpSocket, &QTcpSocket::readyRead, this, &CTcpConnection::OnReadyRead );
4947

50-
if ( pServer )
51-
{
52-
connect ( this, &CTcpConnection::ProtocolCLMessageReceived, pServer, &CServer::OnProtocolCLMessageReceived );
53-
}
54-
55-
if ( pChannel )
56-
{
57-
connect ( this, &CTcpConnection::ProtocolCLMessageReceived, pChannel, &CChannel::OnProtocolCLMessageReceived );
58-
}
48+
connect ( this, &CTcpConnection::ProtocolCLMessageReceived, pChannel, &CChannel::OnProtocolCLMessageReceived );
5949

60-
if ( pClient && bIsSession )
50+
if ( bIsSession )
6151
{
6252
// set up keepalive CLM_EMPTY_MESSAGE over TCP session connection
6353
connect ( this, &CTcpConnection::CLSendEmptyMes, pClient, &CClient::OnCLSendEmptyMes );
6454
connect ( &TimerKeepalive, &QTimer::timeout, this, &CTcpConnection::OnTimerKeepalive );
6555
TimerKeepalive.start ( TCP_KEEPALIVE_INTERVAL_MS );
6656
}
6757
}
58+
#endif
59+
60+
// TCP connection used by server
61+
CTcpConnection::CTcpConnection ( QTcpSocket* pTcpSocket, const CHostAddress& tcpAddress, CServer* pServer ) :
62+
pTcpSocket ( pTcpSocket ),
63+
tcpAddress ( tcpAddress ),
64+
pServer ( pServer ),
65+
pChannel ( nullptr ),
66+
bIsSession ( false )
67+
{
68+
vecbyRecBuf.Init ( MAX_SIZE_BYTES_NETW_BUF );
69+
iPos = 0;
70+
iPayloadRemain = 0;
71+
72+
connect ( pTcpSocket, &QTcpSocket::disconnected, this, &CTcpConnection::OnDisconnected );
73+
connect ( pTcpSocket, &QTcpSocket::readyRead, this, &CTcpConnection::OnReadyRead );
74+
75+
connect ( this, &CTcpConnection::ProtocolCLMessageReceived, pServer, &CServer::OnProtocolCLMessageReceived );
76+
}
6877

6978
void CTcpConnection::OnDisconnected()
7079
{

src/tcpconnection.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@ class CTcpConnection : public QObject
4848
Q_OBJECT
4949

5050
public:
51-
CTcpConnection ( QTcpSocket* pTcpSocket,
52-
const CHostAddress& tcpAddress,
53-
CServer* pServer,
54-
CClient* pClient,
55-
CChannel* pChannel,
56-
bool bIsSession );
51+
#ifndef SERVER_ONLY
52+
CTcpConnection ( QTcpSocket* pTcpSocket, const CHostAddress& tcpAddress, CClient* pClient, CChannel* pChannel, bool bIsSession );
53+
#endif
54+
CTcpConnection ( QTcpSocket* pTcpSocket, const CHostAddress& tcpAddress, CServer* pServer );
5755
~CTcpConnection() {}
5856

5957
void SetChannel ( CChannel* pChan ) { pChannel = pChan; }
@@ -67,10 +65,8 @@ class CTcpConnection : public QObject
6765
private:
6866
QTcpSocket* pTcpSocket;
6967
CHostAddress tcpAddress;
70-
CHostAddress udpAddress;
7168

7269
CServer* pServer;
73-
CClient* pClient;
7470
CChannel* pChannel;
7571

7672
const bool bIsSession;

src/tcpserver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,5 @@ void CTcpServer::OnNewConnection()
9797

9898
qDebug() << "- Jamulus-TCP: received connection from:" << peerAddress.toString();
9999

100-
new CTcpConnection ( pSocket, peerAddress, pServer, nullptr, nullptr, false ); // will auto-delete on disconnect
100+
new CTcpConnection ( pSocket, peerAddress, pServer ); // will auto-delete on disconnect
101101
}

0 commit comments

Comments
 (0)