|
| 1 | +/******************************************************************************\ |
| 2 | + * Copyright (c) 2024 |
| 3 | + * |
| 4 | + * Author(s): |
| 5 | + * Tony Mountifield |
| 6 | + * |
| 7 | + ****************************************************************************** |
| 8 | + * |
| 9 | + * This program is free software; you can redistribute it and/or modify it under |
| 10 | + * the terms of the GNU General Public License as published by the Free Software |
| 11 | + * Foundation; either version 2 of the License, or (at your option) any later |
| 12 | + * version. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 16 | + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 17 | + * details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public License along with |
| 20 | + * this program; if not, write to the Free Software Foundation, Inc., |
| 21 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 22 | + * |
| 23 | + \******************************************************************************/ |
| 24 | + |
| 25 | +#include "tcpserver.h" |
| 26 | + |
| 27 | +#include "server.h" |
| 28 | +#include "channel.h" |
| 29 | + |
| 30 | +CTcpServer::CTcpServer ( CServer* pNServP, const QString& strServerBindIP, int iPort, bool bEnableIPv6 ) : |
| 31 | + pServer ( pNServP ), |
| 32 | + strServerBindIP ( strServerBindIP ), |
| 33 | + iPort ( iPort ), |
| 34 | + bEnableIPv6 ( bEnableIPv6 ), |
| 35 | + pTcpServer ( new QTcpServer ( this ) ) |
| 36 | +{ |
| 37 | + //// connect ( this, &CTcpServer::ProtocolCLMessageReceived, pServer, &CServer::OnProtocolCLMessageReceived ); |
| 38 | + connect ( pTcpServer, &QTcpServer::newConnection, this, &CTcpServer::OnNewConnection ); |
| 39 | +} |
| 40 | + |
| 41 | +CTcpServer::~CTcpServer() |
| 42 | +{ |
| 43 | + if ( pTcpServer->isListening() ) |
| 44 | + { |
| 45 | + qInfo() << "- stopping Jamulus-TCP server"; |
| 46 | + pTcpServer->close(); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +bool CTcpServer::Start() |
| 51 | +{ |
| 52 | + if ( iPort < 0 ) |
| 53 | + { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + // default to any-address for either both IP protocols or just IPv4 |
| 58 | + QHostAddress hostAddress = bEnableIPv6 ? QHostAddress::Any : QHostAddress::AnyIPv4; |
| 59 | + |
| 60 | + if ( !bEnableIPv6 ) |
| 61 | + { |
| 62 | + if ( !strServerBindIP.isEmpty() ) |
| 63 | + { |
| 64 | + hostAddress = QHostAddress ( strServerBindIP ); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if ( pTcpServer->listen ( hostAddress, iPort ) ) |
| 69 | + { |
| 70 | + qInfo() << qUtf8Printable ( |
| 71 | + QString ( "- Jamulus-TCP: Server started on %1:%2" ).arg ( pTcpServer->serverAddress().toString() ).arg ( pTcpServer->serverPort() ) ); |
| 72 | + return true; |
| 73 | + } |
| 74 | + qInfo() << "- Jamulus-TCP: Unable to start server:" << pTcpServer->errorString(); |
| 75 | + return false; |
| 76 | +} |
| 77 | + |
| 78 | +void CTcpServer::OnNewConnection() |
| 79 | +{ |
| 80 | + QTcpSocket* pSocket = pTcpServer->nextPendingConnection(); |
| 81 | + if ( !pSocket ) |
| 82 | + { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // express IPv4 address as IPv4 |
| 87 | + CHostAddress peerAddress ( pSocket->peerAddress(), pSocket->peerPort() ); |
| 88 | + |
| 89 | + if ( peerAddress.InetAddr.protocol() == QAbstractSocket::IPv6Protocol ) |
| 90 | + { |
| 91 | + bool ok; |
| 92 | + quint32 ip4 = peerAddress.InetAddr.toIPv4Address ( &ok ); |
| 93 | + if ( ok ) |
| 94 | + { |
| 95 | + peerAddress.InetAddr.setAddress ( ip4 ); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + CTcpConnection* pTcpConnection = new CTcpConnection ( pSocket, peerAddress ); |
| 100 | + |
| 101 | + qDebug() << "- Jamulus-TCP: received connection from:" << peerAddress.InetAddr.toString(); |
| 102 | + |
| 103 | + // allocate memory for network receive and send buffer in samples |
| 104 | + CVector<uint8_t> vecbyRecBuf; |
| 105 | + vecbyRecBuf.Init ( MAX_SIZE_BYTES_NETW_BUF ); |
| 106 | + |
| 107 | + connect ( pSocket, &QTcpSocket::disconnected, [this, pTcpConnection]() { |
| 108 | + qDebug() << "- Jamulus-TCP: connection from:" << pTcpConnection->tcpAddress.InetAddr.toString() << "closed"; |
| 109 | + pTcpConnection->pTcpSocket->deleteLater(); |
| 110 | + delete pTcpConnection; |
| 111 | + } ); |
| 112 | + |
| 113 | + connect ( pSocket, &QTcpSocket::readyRead, [this, pTcpConnection, vecbyRecBuf]() { |
| 114 | + // handle received Jamulus protocol packet |
| 115 | + |
| 116 | + // check if this is a protocol message |
| 117 | + int iRecCounter; |
| 118 | + int iRecID; |
| 119 | + CVector<uint8_t> vecbyMesBodyData; |
| 120 | + |
| 121 | + long iNumBytesRead = pTcpConnection->pTcpSocket->read ( (char*) &vecbyRecBuf[0], MESS_HEADER_LENGTH_BYTE ); |
| 122 | + if ( iNumBytesRead == -1 ) |
| 123 | + { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + if ( iNumBytesRead < MESS_HEADER_LENGTH_BYTE ) |
| 128 | + { |
| 129 | + qDebug() << "-- short read: expected" << MESS_HEADER_LENGTH_BYTE << "bytes, got" << iNumBytesRead; |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + long iPayloadLength = CProtocol::GetBodyLength ( vecbyRecBuf ); |
| 134 | + |
| 135 | + long iNumBytesRead2 = pTcpConnection->pTcpSocket->read ( (char*) &vecbyRecBuf[MESS_HEADER_LENGTH_BYTE], iPayloadLength ); |
| 136 | + if ( iNumBytesRead2 == -1 ) |
| 137 | + { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + if ( iNumBytesRead2 < iPayloadLength ) |
| 142 | + { |
| 143 | + qDebug() << "-- short read: expected" << iPayloadLength << "bytes, got" << iNumBytesRead2; |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + iNumBytesRead += iNumBytesRead2; |
| 148 | + |
| 149 | + qDebug() << "- Jamulus-TCP: received protocol message of length" << iNumBytesRead; |
| 150 | + |
| 151 | + if ( !CProtocol::ParseMessageFrame ( vecbyRecBuf, iNumBytesRead, vecbyMesBodyData, iRecCounter, iRecID ) ) |
| 152 | + { |
| 153 | + qDebug() << "- Jamulus-TCP: message parsed OK, ID =" << iRecID; |
| 154 | + |
| 155 | + // this is a protocol message, check the type of the message |
| 156 | + if ( CProtocol::IsConnectionLessMessageID ( iRecID ) ) |
| 157 | + { |
| 158 | + //### TODO: BEGIN ###// |
| 159 | + // a copy of the vector is used -> avoid malloc in real-time routine |
| 160 | + //// emit ProtocolCLMessageReceived ( iRecID, vecbyMesBodyData, peerAddress, pSocket ); |
| 161 | + //### TODO: END ###// |
| 162 | + } |
| 163 | + else |
| 164 | + { |
| 165 | + //### TODO: BEGIN ###// |
| 166 | + // a copy of the vector is used -> avoid malloc in real-time routine |
| 167 | + // emit ProtocolMessageReceived ( iRecCounter, iRecID, vecbyMesBodyData, peerAddress, pSocket ); |
| 168 | + //### TODO: END ###// |
| 169 | + } |
| 170 | + } |
| 171 | + } ); |
| 172 | +} |
| 173 | + |
| 174 | +#if 0 |
| 175 | +void CTcpServer::Send ( QTcpSocket* pSocket ) { |
| 176 | + // pSocket->write ( ); |
| 177 | +} |
| 178 | +#endif |
0 commit comments