#define DATA_SIZE 20000
{
}
Server::~Server()
{
}
void Server::run()
{
connect(pServer, SIGNAL(newConnection()), this, SLOT(onNewConnection()), Qt::DirectConnection);
connect(pMapperReadyRead,
SIGNAL(mapped
(QObject*)),
this,
SLOT(onSocketReadyRead
(QObject*)), Qt
::DirectConnection);
connect(pMapperDisconnected,
SIGNAL(mapped
(QObject*)),
this,
SLOT(onSocketDisconnected
(QObject*)), Qt
::DirectConnection);
//timer d'envoi
lPacketIndex = 0;
connect(pTimer, SIGNAL(timeout()), this, SLOT(onTimeout()));
pTimer->setSingleShot(false);
pTimer->start(100);
exec();
pServer->close();
delete pServer;
delete pMapperReadyRead;
delete pMapperDisconnected;
delete pMapperError;
}
void Server::onNewConnection()
{
QTcpSocket* pSocket
= pServer
->nextPendingConnection
();
if (pSocket == NULL)
return;
qDebug()<<"new connection";
vecClient.push_back(pSocket);
connect(pSocket, SIGNAL(readyRead()), pMapperReadyRead, SLOT(map()), Qt::DirectConnection);
pMapperReadyRead->setMapping(pSocket, pSocket);
connect(pSocket, SIGNAL(disconnected()), pMapperDisconnected, SLOT(map()), Qt::DirectConnection);
pMapperDisconnected->setMapping(pSocket, pSocket);
}
void Server
::onSocketReadyRead( QObject* pObject
) {
}
void Server
::onSocketDisconnected( QObject* pObject
) {
int i;
qDebug()<<"Socket disconnected";
for (i=0;i<vecClient.size();++i)
{
if (pSocket = vecClient[i])
{
vecClient.erase(vecClient.begin() + i);
break;
}
}
pSocket->deleteLater();
}
{
qDebug()<<"Socket error : "<<pSocket->errorString();
}
void Server::onTimeout()
{
int i;
char data[DATA_SIZE];
short sSize = DATA_SIZE - 2;
memset(data, 0, DATA_SIZE);
memcpy(data, &sSize, 2);
memcpy(data+2, &lPacketIndex, 4);
++lPacketIndex;
for (i=0;i<vecClient.size();++i)
{
vecClient[i]->write(data, DATA_SIZE);
}
}
#define DATA_SIZE 20000
Server::Server(QObject *parent) : QThread(parent)
{
}
Server::~Server()
{
}
void Server::run()
{
pServer = new QTcpServer();
pMapperReadyRead = new QSignalMapper();
pMapperDisconnected = new QSignalMapper();
pMapperError = new QSignalMapper();
connect(pServer, SIGNAL(newConnection()), this, SLOT(onNewConnection()), Qt::DirectConnection);
connect(pMapperReadyRead, SIGNAL(mapped(QObject*)), this, SLOT(onSocketReadyRead(QObject*)), Qt::DirectConnection);
connect(pMapperDisconnected, SIGNAL(mapped(QObject*)), this, SLOT(onSocketDisconnected(QObject*)), Qt::DirectConnection);
pServer->listen(QHostAddress::Any, 1234);
//timer d'envoi
lPacketIndex = 0;
QTimer* pTimer = new QTimer(this);
connect(pTimer, SIGNAL(timeout()), this, SLOT(onTimeout()));
pTimer->setSingleShot(false);
pTimer->start(100);
exec();
pServer->close();
delete pServer;
delete pMapperReadyRead;
delete pMapperDisconnected;
delete pMapperError;
}
void Server::onNewConnection()
{
QTcpSocket* pSocket = pServer->nextPendingConnection();
if (pSocket == NULL)
return;
qDebug()<<"new connection";
vecClient.push_back(pSocket);
connect(pSocket, SIGNAL(readyRead()), pMapperReadyRead, SLOT(map()), Qt::DirectConnection);
pMapperReadyRead->setMapping(pSocket, pSocket);
connect(pSocket, SIGNAL(disconnected()), pMapperDisconnected, SLOT(map()), Qt::DirectConnection);
pMapperDisconnected->setMapping(pSocket, pSocket);
}
void Server::onSocketReadyRead( QObject* pObject )
{
}
void Server::onSocketDisconnected( QObject* pObject )
{
int i;
QTcpSocket* pSocket = (QTcpSocket*)pObject;
qDebug()<<"Socket disconnected";
for (i=0;i<vecClient.size();++i)
{
if (pSocket = vecClient[i])
{
vecClient.erase(vecClient.begin() + i);
break;
}
}
pSocket->deleteLater();
}
void Server::onSocketError( QObject* pObject, QAbstractSocket::SocketError error )
{
QTcpSocket* pSocket = (QTcpSocket*)pObject;
qDebug()<<"Socket error : "<<pSocket->errorString();
}
void Server::onTimeout()
{
int i;
char data[DATA_SIZE];
short sSize = DATA_SIZE - 2;
memset(data, 0, DATA_SIZE);
memcpy(data, &sSize, 2);
memcpy(data+2, &lPacketIndex, 4);
++lPacketIndex;
for (i=0;i<vecClient.size();++i)
{
vecClient[i]->write(data, DATA_SIZE);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks