void ClientStub::sendRequest(const Message msg)
{
out << (quint16)0;
out << msg;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpSocket->write(block);
tcpSocket->flush();
sended++;
}
void ClientStub::sendRequest(const Message msg)
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_6);
out << (quint16)0;
out << msg;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpSocket->write(block);
tcpSocket->flush();
sended++;
}
To copy to clipboard, switch view to plain text mode
void Server::incomingConnection(int socketId)
{
ClientSocket *socket = new ClientSocket();
socket->setSocketDescriptor(socketId);
}
void Server::incomingConnection(int socketId)
{
ClientSocket *socket = new ClientSocket();
socket->setSocketDescriptor(socketId);
}
To copy to clipboard, switch view to plain text mode
static int countClientSocket = 0;
ClientSocket
::ClientSocket(QObject *parent
){
connect(this, SIGNAL(readyRead()), this, SLOT(receive()));
// connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));
countClientSocket++;
//#ifdef DEBUG_MACHINE
printf("ctor countClientSocket: %i \n", countClientSocket);
std::cout.flush();
//#endif
nextBlockSize = 0;
}
ClientSocket::~ClientSocket()
{
// abort();
countClientSocket--;
#ifdef DEBUG_MACHINE
printf("dtor countClientSocket: %i \n", countClientSocket);
std::cout.flush();
#endif
}
bool ClientSocket::setSocketDescriptor(int socketDescriptor)
{
QTcpSocket::setSocketDescriptor(socketDescriptor
);
return waitForReadyRead(5);
}
void ClientSocket::receive()
{
if (nextBlockSize == 0)
{
if (bytesAvailable() < sizeof(quint16))
return;
in >> nextBlockSize;
}
if (bytesAvailable() < nextBlockSize)
return;
Message msg;
in >> msg;
invoke(msg);
}
void ClientSocket::invoke(Message &msg)
{
QList<QVariant> msgContents = msg.getContents();
WordCounter task = msgContents.first().value<WordCounter>();
msg.setTypes(Message::Info);
msg.
setContents(QList<QVariant>
() <<
QString("received request"));
response(msg);
printf("%i - received request\n", msg.getId());
metaObject = task.metaObject();
int methodIndex = -1;
methodIndex = metaObject->indexOfMethod(normalizedSignature);
printf("Processing request of: %i \n", task.getKey());
QElapsedTimer timer;
timer.start();
method.invoke(
&task,
Qt::DirectConnection, // Qt::AutoConnection,
Q_ARG(QList<QVariant>, msgContents)
);
int t = timer.elapsed();
msg.setTypes(Message::Result);
msg.setContents(QList<QVariant>() << retVal
<<
QString("Time elapsed: %1 miliseconds").
arg(t
));
// << "done!"); printf("id: %i, Result: %i \n-------->>> Time elapsed: %i miliseconds\n", task.getKey(), retVal.toInt(), t);
response(msg);
// waitForDisconnected();
// close();
}
void ClientSocket::response(Message msg)
{
out << quint16(0);
out << msg;
out.device()->seek(0);
out << quint16(block.size() - sizeof(quint16));
write(block);
flush();
}
static int countClientSocket = 0;
ClientSocket::ClientSocket(QObject *parent)
: QTcpSocket(parent)
{
connect(this, SIGNAL(readyRead()), this, SLOT(receive()));
// connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));
countClientSocket++;
//#ifdef DEBUG_MACHINE
printf("ctor countClientSocket: %i \n", countClientSocket);
std::cout.flush();
//#endif
nextBlockSize = 0;
}
ClientSocket::~ClientSocket()
{
// abort();
countClientSocket--;
#ifdef DEBUG_MACHINE
printf("dtor countClientSocket: %i \n", countClientSocket);
std::cout.flush();
#endif
}
bool ClientSocket::setSocketDescriptor(int socketDescriptor)
{
QTcpSocket::setSocketDescriptor(socketDescriptor);
return waitForReadyRead(5);
}
void ClientSocket::receive()
{
QDataStream in(this);
in.setVersion(QDataStream::Qt_4_6);
if (nextBlockSize == 0)
{
if (bytesAvailable() < sizeof(quint16))
return;
in >> nextBlockSize;
}
if (bytesAvailable() < nextBlockSize)
return;
Message msg;
in >> msg;
invoke(msg);
}
void ClientSocket::invoke(Message &msg)
{
QList<QVariant> msgContents = msg.getContents();
WordCounter task = msgContents.first().value<WordCounter>();
msg.setTypes(Message::Info);
msg.setContents(QList<QVariant>() << QString("received request"));
response(msg);
printf("%i - received request\n", msg.getId());
QByteArray normalizedSignature = QMetaObject::normalizedSignature("execute (QList<QVariant> &)");
QVariant retVal;
const QMetaObject *metaObject = 0;
metaObject = task.metaObject();
int methodIndex = -1;
methodIndex = metaObject->indexOfMethod(normalizedSignature);
QMetaMethod method = metaObject->method(methodIndex);
printf("Processing request of: %i \n", task.getKey());
QElapsedTimer timer;
timer.start();
method.invoke(
&task,
Qt::DirectConnection, // Qt::AutoConnection,
Q_RETURN_ARG(QVariant, retVal),
Q_ARG(QList<QVariant>, msgContents)
);
int t = timer.elapsed();
msg.setTypes(Message::Result);
msg.setContents(QList<QVariant>() << retVal
<< QString("Time elapsed: %1 miliseconds").arg(t)); // << "done!");
printf("id: %i, Result: %i \n-------->>> Time elapsed: %i miliseconds\n", task.getKey(), retVal.toInt(), t);
response(msg);
// waitForDisconnected();
// close();
}
void ClientSocket::response(Message msg)
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_6);
out << quint16(0);
out << msg;
out.device()->seek(0);
out << quint16(block.size() - sizeof(quint16));
write(block);
flush();
}
To copy to clipboard, switch view to plain text mode
Bookmarks