support for custom server port number selection

This commit is contained in:
Volker Fischer 2008-07-22 15:17:19 +00:00
parent f30d4990e3
commit 11fb1dbacd
7 changed files with 55 additions and 38 deletions

View file

@ -198,7 +198,7 @@ void CClient::run()
// problem: how to catch errors in a different thread...? // problem: how to catch errors in a different thread...?
// quick hack solution // quick hack solution
QMessageBox::critical ( 0, APP_NAME, generr.strError, "Quit", 0 ); QMessageBox::critical ( 0, APP_NAME, generr.GetErrorText(), "Quit", 0 );
exit ( 0 ); exit ( 0 );
} }

View file

@ -144,8 +144,25 @@ typedef unsigned int _MESSAGE_IDENT;
class CGenErr class CGenErr
{ {
public: public:
CGenErr ( QString strNE ) : strError ( strNE ) {} CGenErr ( QString strNewErrorMsg, QString strNewErrorType = "" ) :
QString strError; 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 class CLlconEvent : public QEvent

View file

@ -40,11 +40,13 @@ QDialog* pMainWindow = NULL;
int main ( int argc, char** argv ) int main ( int argc, char** argv )
{ {
std::string strArgument; std::string strArgument;
double rDbleArgument;
/* check if server or client application shall be started */ /* check if server or client application shall be started */
bool bIsClient = true; bool bIsClient = true;
bool bUseGUI = true; bool bUseGUI = true;
bool bUseServerLogging = false; bool bUseServerLogging = false;
quint16 iPortNumber = LLCON_PORT_NUMBER;
std::string strIniFileName = ""; std::string strIniFileName = "";
/* QT docu: argv()[0] is the program name, argv()[1] is the first /* 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" ) ) if ( GetFlagArgument ( argc, argv, i, "-s", "--server" ) )
{ {
bIsClient = false; bIsClient = false;
cerr << "server mode chosen" << std::endl;
cerr << "server ";
continue; continue;
} }
@ -66,9 +66,7 @@ cerr << "server ";
if ( GetFlagArgument ( argc, argv, i, "-n", "--nogui" ) ) if ( GetFlagArgument ( argc, argv, i, "-n", "--nogui" ) )
{ {
bUseGUI = false; bUseGUI = false;
cerr << "no GUI mode chosen" << std::endl;
cerr << "nogui ";
continue; continue;
} }
@ -76,9 +74,16 @@ cerr << "nogui ";
if ( GetFlagArgument ( argc, argv, i, "-l", "--log" ) ) if ( GetFlagArgument ( argc, argv, i, "-l", "--log" ) )
{ {
bUseServerLogging = true; 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<quint16> ( rDbleArgument );
cerr << "selected port number: " << iPortNumber << std::endl;
continue; continue;
} }
@ -150,7 +155,7 @@ cerr << "logging ";
{ {
// server // server
// actual server object // actual server object
CServer Server ( bUseServerLogging ); CServer Server ( bUseServerLogging, iPortNumber );
if ( bUseGUI ) if ( bUseGUI )
{ {
@ -179,11 +184,11 @@ cerr << "logging ";
// show generic error // show generic error
if ( bUseGUI ) if ( bUseGUI )
{ {
QMessageBox::critical ( 0, APP_NAME, generr.strError, "Quit", 0 ); QMessageBox::critical ( 0, APP_NAME, generr.GetErrorText(), "Quit", 0 );
} }
else else
{ {
qDebug() << generr.strError; qDebug() << generr.GetErrorText();
} }
} }

View file

@ -26,7 +26,8 @@
/* Implementation *************************************************************/ /* 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 ); vecsSendData.Init ( MIN_BLOCK_SIZE_SAMPLES );

View file

@ -41,7 +41,7 @@ class CServer : public QObject
Q_OBJECT Q_OBJECT
public: public:
CServer ( const bool bUseLogging ); CServer ( const bool bUseLogging, const quint16 iPortNumber );
virtual ~CServer() {} virtual ~CServer() {}
void Start(); void Start();

View file

@ -26,35 +26,29 @@
/* Implementation *************************************************************/ /* Implementation *************************************************************/
void CSocket::Init() void CSocket::Init ( const quint16 iPortNumber )
{ {
// allocate memory for network receive and send buffer in samples // allocate memory for network receive and send buffer in samples
vecbyRecBuf.Init ( MAX_SIZE_BYTES_NETW_BUF ); vecbyRecBuf.Init ( MAX_SIZE_BYTES_NETW_BUF );
// initialize the listening socket // initialize the listening socket
bool bSuccess = SocketDevice.bind ( 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 server and client is on same machine, decrease port number by
if ( !bSuccess ) // one by definition
{ bSuccess = SocketDevice.bind (
// if server and client is on same machine, decrease port number by QHostAddress( QHostAddress::Any ), iPortNumber - 1 );
// one by definition
bSuccess = SocketDevice.bind (
QHostAddress( QHostAddress::Any ), LLCON_PORT_NUMBER - 1 );
}
} }
if ( !bSuccess ) if ( !bSuccess )
{ {
// show error message // we cannot bind socket, throw error
QMessageBox::critical ( 0, "Network Error", "Cannot bind the socket.", throw CGenErr ( "Cannot bind the socket (maybe "
QMessageBox::Ok, QMessageBox::NoButton ); "the software is already running).", "Network Error" );
// exit application
exit ( 1 );
} }
// connect the "activated" signal // connect the "activated" signal

View file

@ -46,18 +46,18 @@ class CSocket : public QObject
Q_OBJECT Q_OBJECT
public: public:
CSocket(CChannel* pNewChannel) : pChannel(pNewChannel), bIsClient(true) CSocket ( CChannel* pNewChannel ) : pChannel( pNewChannel ), bIsClient ( true )
{ Init(); }
CSocket(CChannelSet* pNewChannelSet, QObject* pNServP) :
pChannelSet(pNewChannelSet), pServer ( pNServP ), bIsClient(false)
{ Init(); } { Init(); }
CSocket ( CChannelSet* pNewChannelSet, QObject* pNServP, const quint16 iPortNumber ) :
pChannelSet(pNewChannelSet), pServer ( pNServP ), bIsClient ( false )
{ Init ( iPortNumber ); }
virtual ~CSocket() {} virtual ~CSocket() {}
void SendPacket ( const CVector<unsigned char>& vecbySendBuf, void SendPacket ( const CVector<unsigned char>& vecbySendBuf,
const CHostAddress& HostAddr ); const CHostAddress& HostAddr );
protected: protected:
void Init(); void Init ( const quint16 iPortNumber = LLCON_PORT_NUMBER );
QUdpSocket SocketDevice; QUdpSocket SocketDevice;