From 11fb1dbacd8bed2ea6bbe679bdce53c1c0f36c12 Mon Sep 17 00:00:00 2001 From: Volker Fischer Date: Tue, 22 Jul 2008 15:17:19 +0000 Subject: [PATCH] support for custom server port number selection --- src/client.cpp | 2 +- src/global.h | 21 +++++++++++++++++++-- src/main.cpp | 27 ++++++++++++++++----------- src/server.cpp | 3 ++- src/server.h | 2 +- src/socket.cpp | 28 +++++++++++----------------- src/socket.h | 10 +++++----- 7 files changed, 55 insertions(+), 38 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index 89c4c813..d80734ae 100755 --- a/src/client.cpp +++ b/src/client.cpp @@ -198,7 +198,7 @@ void CClient::run() // problem: how to catch errors in a different thread...? // quick hack solution - QMessageBox::critical ( 0, APP_NAME, generr.strError, "Quit", 0 ); + QMessageBox::critical ( 0, APP_NAME, generr.GetErrorText(), "Quit", 0 ); exit ( 0 ); } diff --git a/src/global.h b/src/global.h index 78e46a17..e4e5847d 100755 --- a/src/global.h +++ b/src/global.h @@ -144,8 +144,25 @@ typedef unsigned int _MESSAGE_IDENT; class CGenErr { public: - CGenErr ( QString strNE ) : strError ( strNE ) {} - QString strError; + CGenErr ( QString strNewErrorMsg, QString strNewErrorType = "" ) : + strErrorMsg ( strNewErrorMsg ), strErrorType ( strNewErrorType ) {} + + QString GetErrorText() + { + // return formatted error text + if ( strErrorType.isEmpty() ) + { + return strErrorMsg; + } + else + { + return strErrorType + ": " + strErrorMsg; + } + } + +protected: + QString strErrorType; + QString strErrorMsg; }; class CLlconEvent : public QEvent diff --git a/src/main.cpp b/src/main.cpp index 7ed9178e..c767be9c 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -40,11 +40,13 @@ QDialog* pMainWindow = NULL; int main ( int argc, char** argv ) { std::string strArgument; + double rDbleArgument; /* check if server or client application shall be started */ bool bIsClient = true; bool bUseGUI = true; bool bUseServerLogging = false; + quint16 iPortNumber = LLCON_PORT_NUMBER; std::string strIniFileName = ""; /* QT docu: argv()[0] is the program name, argv()[1] is the first @@ -56,9 +58,7 @@ int main ( int argc, char** argv ) if ( GetFlagArgument ( argc, argv, i, "-s", "--server" ) ) { bIsClient = false; - -cerr << "server "; - + cerr << "server mode chosen" << std::endl; continue; } @@ -66,9 +66,7 @@ cerr << "server "; if ( GetFlagArgument ( argc, argv, i, "-n", "--nogui" ) ) { bUseGUI = false; - -cerr << "nogui "; - + cerr << "no GUI mode chosen" << std::endl; continue; } @@ -76,9 +74,16 @@ cerr << "nogui "; if ( GetFlagArgument ( argc, argv, i, "-l", "--log" ) ) { bUseServerLogging = true; + cerr << "logging enabled" << std::endl; + continue; + } -cerr << "logging "; - + /* Port number ------------------------------------------------------------ */ + if ( GetNumericArgument ( argc, argv, i, "-p", "--port", + 0, 65535, rDbleArgument ) ) + { + iPortNumber = static_cast ( rDbleArgument ); + cerr << "selected port number: " << iPortNumber << std::endl; continue; } @@ -150,7 +155,7 @@ cerr << "logging "; { // server // actual server object - CServer Server ( bUseServerLogging ); + CServer Server ( bUseServerLogging, iPortNumber ); if ( bUseGUI ) { @@ -179,11 +184,11 @@ cerr << "logging "; // show generic error if ( bUseGUI ) { - QMessageBox::critical ( 0, APP_NAME, generr.strError, "Quit", 0 ); + QMessageBox::critical ( 0, APP_NAME, generr.GetErrorText(), "Quit", 0 ); } else { - qDebug() << generr.strError; + qDebug() << generr.GetErrorText(); } } diff --git a/src/server.cpp b/src/server.cpp index 53c8a8b2..dd3813ca 100755 --- a/src/server.cpp +++ b/src/server.cpp @@ -26,7 +26,8 @@ /* Implementation *************************************************************/ -CServer::CServer ( const bool bUseLogging ) : Socket ( &ChannelSet, this ) +CServer::CServer ( const bool bUseLogging, const quint16 iPortNumber ) : + Socket ( &ChannelSet, this, iPortNumber ) { vecsSendData.Init ( MIN_BLOCK_SIZE_SAMPLES ); diff --git a/src/server.h b/src/server.h index e9538f36..60218c1f 100755 --- a/src/server.h +++ b/src/server.h @@ -41,7 +41,7 @@ class CServer : public QObject Q_OBJECT public: - CServer ( const bool bUseLogging ); + CServer ( const bool bUseLogging, const quint16 iPortNumber ); virtual ~CServer() {} void Start(); diff --git a/src/socket.cpp b/src/socket.cpp index 0e7d23a1..86b1e9e0 100755 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -26,35 +26,29 @@ /* Implementation *************************************************************/ -void CSocket::Init() +void CSocket::Init ( const quint16 iPortNumber ) { // allocate memory for network receive and send buffer in samples vecbyRecBuf.Init ( MAX_SIZE_BYTES_NETW_BUF ); // initialize the listening socket bool bSuccess = SocketDevice.bind ( - QHostAddress ( QHostAddress::Any ), LLCON_PORT_NUMBER ); + QHostAddress ( QHostAddress::Any ), iPortNumber ); - if ( bIsClient ) + // if no success, try if server is on same machine (only for client) + if ( ( !bSuccess ) && bIsClient ) { - // if no success, try if server is on same machine (only for client) - if ( !bSuccess ) - { - // if server and client is on same machine, decrease port number by - // one by definition - bSuccess = SocketDevice.bind ( - QHostAddress( QHostAddress::Any ), LLCON_PORT_NUMBER - 1 ); - } + // if server and client is on same machine, decrease port number by + // one by definition + bSuccess = SocketDevice.bind ( + QHostAddress( QHostAddress::Any ), iPortNumber - 1 ); } if ( !bSuccess ) { - // show error message - QMessageBox::critical ( 0, "Network Error", "Cannot bind the socket.", - QMessageBox::Ok, QMessageBox::NoButton ); - - // exit application - exit ( 1 ); + // we cannot bind socket, throw error + throw CGenErr ( "Cannot bind the socket (maybe " + "the software is already running).", "Network Error" ); } // connect the "activated" signal diff --git a/src/socket.h b/src/socket.h index cbfdb76a..02c2c64e 100755 --- a/src/socket.h +++ b/src/socket.h @@ -46,18 +46,18 @@ class CSocket : public QObject Q_OBJECT public: - CSocket(CChannel* pNewChannel) : pChannel(pNewChannel), bIsClient(true) - { Init(); } - CSocket(CChannelSet* pNewChannelSet, QObject* pNServP) : - pChannelSet(pNewChannelSet), pServer ( pNServP ), bIsClient(false) + CSocket ( CChannel* pNewChannel ) : pChannel( pNewChannel ), bIsClient ( true ) { Init(); } + CSocket ( CChannelSet* pNewChannelSet, QObject* pNServP, const quint16 iPortNumber ) : + pChannelSet(pNewChannelSet), pServer ( pNServP ), bIsClient ( false ) + { Init ( iPortNumber ); } virtual ~CSocket() {} void SendPacket ( const CVector& vecbySendBuf, const CHostAddress& HostAddr ); protected: - void Init(); + void Init ( const quint16 iPortNumber = LLCON_PORT_NUMBER ); QUdpSocket SocketDevice;