Skip to content

Commit fb6150c

Browse files
committed
Add client-side TCP code
1 parent e369e97 commit fb6150c

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/client.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,24 @@ void CClient::OnSendCLProtMessage ( CHostAddress InetAddr, CVector<uint8_t> vecM
264264
if ( bUseTcpClient )
265265
{
266266
// create a TCP client connection and send message
267+
QTcpSocket* pSocket = new QTcpSocket ( this );
268+
269+
connect ( pSocket, &QTcpSocket::errorOccurred, this, [this, pSocket] ( QAbstractSocket::SocketError err ) {
270+
Q_UNUSED ( err );
271+
272+
qWarning() << "- TCP connection error:" << pSocket->errorString();
273+
// may want to specifically handle ConnectionRefusedError?
274+
pSocket->deleteLater();
275+
} );
276+
277+
connect ( pSocket, &QTcpSocket::connected, this, [this, pSocket, InetAddr, vecMessage]() {
278+
// connection succeeded, give it to a CTcpConnection
279+
CTcpConnection* pTcpConnection = new CTcpConnection ( pSocket, InetAddr, nullptr ); // client connection, will self-delete on disconnect
280+
281+
pTcpConnection->write ( (const char*) &( (CVector<uint8_t>) vecMessage )[0], vecMessage.Size() );
282+
283+
// the CTcpConnection object will pass the reply back up to CProtocol
284+
} );
267285
}
268286
else
269287
{

src/tcpconnection.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ void CTcpConnection::OnReadyRead()
122122
// a copy of the vector is used -> avoid malloc in real-time routine
123123
emit ProtocolCLMessageReceived ( iRecID, vecbyMesBodyData, tcpAddress, this );
124124
//### TODO: END ###//
125+
126+
// disconnect if we are a client
127+
if ( !pServer )
128+
{
129+
pTcpSocket->disconnectFromHost();
130+
}
125131
}
126132
else
127133
{

0 commit comments

Comments
 (0)