PDA

View Full Version : Client server based application issue (one server , multiple clients ) QT



vkaushikQorvo
18th November 2015, 22:49
Hi guys,

I have a client server Application, where multiple clients are connecting to the server . What I am doing is every time a client connects , a thread is created for that client and a sessionId is generated and stored in the Database.

Now say for example, a client connects but somehow client is forcefully terminated (by task manager )
Now I have a situation when trying to connect the same client - what I want is to use the same sessionID and thread and doesnot start a new thread , becuase session is already there in the database.

Well , above mentioned funcitonality works well if there is no network (but client is running, and if network is back again, connection is established) but when client is terminated , and relogin attempt is made again, it should basically be doing the same thing repatriate the user with the old running session/Thread.

But its not working at all. :( :(

here is the code that is not working , basically invokeMethod is not working for the already created thread.


QMetaObject::invokeMethod(entry.Connection(), "SetSocket", Qt::BlockingQueuedConnection, Q_ARG(SocketPointerStruct, socketPointerStruct));



qDebug()<<"User was logged in earlier, now in disconnected mode, a repatriation attempt will be made";

bool tempFlag = fSocket->disconnect();
if(tempFlag)
qDebug()<<"Disconnected all slots connected to fSocket";
else
qDebug()<<"failed to disconnected all slots connected to fSocket";

SocketPointerStruct socketPointerStruct;
socketPointerStruct.fSocketPointer = fSocket;
socketPointerStruct.fAutomaticLoginAttempt = automaticLoginAttempts;
fSocket->moveToThread(entry.Connection()->thread());
QMetaObject::invokeMethod(entry.Connection(), "SetSocket", Qt::BlockingQueuedConnection, Q_ARG(SocketPointerStruct, socketPointerStruct));

qDebug()<<"User is repatriated";

/* Set entry's connection's socket to this guys socket */
fSocket = NULL;
emit ended(this);

Problem is setSocket is never called in case client is force terminated, but if there is not network and client is still running then above code works well if network is back again.

any pointers will be usefull.

basically I have created a ConnectionThread class by subclassing QThread and trying the above code inside that.

Thanks!
Vishal

anda_skoa
19th November 2015, 08:35
Any reason for the BlockingQueuedConnection vs just a QueuedConnection?
Does the receiver thread run its event loop at this point?

Cheers,
_

vkaushikQorvo
19th November 2015, 14:27
Hi,

yes, thread is running its event loop. I tried both the options , but nothing seems to be working. (or how can i check if event loop is running? actually i tried to check if thread is running or not.... and its running... but no idea how to check if event loop is running)
Surprisingly the same piece of code works when network is plugged out , client still running. but when client is terminated forcefully and then connected again , that piece of code doesn't invoke setSocket method as mentioned in my post.

Thanks,
Vishal

anda_skoa
19th November 2015, 18:28
yes, thread is running its event loop. I tried both the options , but nothing seems to be working. (or how can i check if event loop is running? actually i tried to check if thread is running or not.... and its running... but no idea how to check if event loop is running)

You could run a timer in that thread that writes some log output every couple of seconds.

Cheers,
_

vkaushikQorvo
19th November 2015, 21:17
Thanks!
Yeah it seems eventLoop is running for the thread.
ahhh, this is making me crazy now :( :(

-vishal

anda_skoa
20th November 2015, 09:17
So you get into that method (one of the two early qDebug is logged), but the invokeMethod fails?

Cheers,
_

vkaushikQorvo
20th November 2015, 15:56
What i can see is :
bool debug3=QMetaObject::invokeMethod(entry.Connection( ), "SetSocket",Qt::BlockingQueuedConnection, Q_ARG(SocketPointerStruct, socketPointerStruct));
qDebug()<<"User is repatriated"<<debug3;

it never returns , qDebug() after invokeMethod is never executed.

Regards,
Vishal

vkaushikQorvo
20th November 2015, 20:41
One more thing actually i forgot to mention or I realized just now, that client is terminated when it was processing some data over the socket , which means client was forcekilled and there was some data still in Qsocket stream and may be that is the reason that it never returns and invokemethod of that thread is never called.

Can anybody please point , how can we close that socket on that thread ? or am i really overthinking on it?
Thanks!
vishal

vkaushikQorvo
2nd December 2015, 14:52
Hello Qt Experts,

please shed some light on this topic.

Thanks!