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...?
// quick hack solution
QMessageBox::critical ( 0, APP_NAME, generr.strError, "Quit", 0 );
QMessageBox::critical ( 0, APP_NAME, generr.GetErrorText(), "Quit", 0 );
exit ( 0 );
}

View File

@ -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

View File

@ -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<quint16> ( 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();
}
}

View File

@ -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 );

View File

@ -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();

View File

@ -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

View File

@ -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<unsigned char>& vecbySendBuf,
const CHostAddress& HostAddr );
protected:
void Init();
void Init ( const quint16 iPortNumber = LLCON_PORT_NUMBER );
QUdpSocket SocketDevice;