1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
//Initialisaton du serveur
LocalServer::LocalServer(QString serverName) : m_server(this)
{
connect(&this->m_server,SIGNAL(newConnection()),this,SLOT(NewConnectionOnServer()));
connect(client,SIGNAL(readyRead()),this,SLOT(ReadOK()));
connect(MessagingSystemSerial::getInstance(),SIGNAL(MessageReceived()),this,SLOT(SendDataToClient()));
connect(client, SIGNAL(disconnected()),client, SLOT(deleteLater()));
if( !this->m_server.listen(serverName))
{
qDebug() << "Problème le server [" << serverName << "] est déjà à l'écoute..." ;
QTimer::singleShot(2000, QCoreApplication::instance(), SLOT(quit()));
}
else
{
qDebug() << "Installation du serveur [" << serverName << "]" ;
this->m_server.setMaxPendingConnections(10);
this->m_server.waitForNewConnection(2000000);
}
}
//Y a-t-il une nouvelle connexion ?
void LocalServer::NewConnectionOnServer()
{
if(this->m_server.hasPendingConnections() == true)
{
client = this->m_server.nextPendingConnection();
qDebug() << "Nouvelle connexion sur le serveur ";
}
else
qDebug() << " Il n'y a pas de connexions en attentes ";
}
void LocalServer::ReadOK()
{
client = qobject_cast<QLocalSocket*>(sender());
QString str = client->readAll();
qDebug()<< QString("##: " + str +"\n");
}
// Envoi des données(QByteArray) par la socket
void LocalServer::SendDataToClient()
{
if(client)
{
QByteArray block = MessagingSystemSerial::getInstance()->GetDataByteArray();
if(client->write(block) == -1)
qDebug() << " L'écriture des données à échouée ";
else
qDebug() << " Ecriture des données ";
client->flush();
client->disconnectFromServer();
}
else
qDebug() << " Les données ne peuvent pas être envoyées ";
} |
Partager