Sign in to follow this  
VitaliBR

Loop while in Window QT

Recommended Posts

I'm porting a program made for Windows Console QT, but I'm having a doubt about using a while loop

I'm doing the following:
#ifdef _WIN32
#include "Kbhit.h"
#endif
#if defined(_CONSOLE_2)
#include "Console2SampleIncludes.h"
#endif
#ifdef _CONSOLE_2
_CONSOLE_2_SetSystemProcessParams
#endif

//...

while(!server->shouldQuit)
server->Update();




Update() from Server.cpp
void Server::Update()
{
char message[2048];

#ifdef _WIN32
if (_kbhit()) {
gets(message);

if (strcmp(message, "quit") == 0) { puts("Quitting."); shouldQuit = true; }
if (strcmp(message, "ping") == 0) { server->Ping(clientID); }
if (strcmp(message, "clear") == 0) { system("CLS"); }

// Message now holds what we want to broadcast
char message2[2048];

// Append Server: to the message so clients know that it ORIGINATED from the server all messages to all clients come from the server either directly or by being relayed from other clients
message2[0] = 0;
strcpy(message2, "Server: ");
strcat(message2, message);

// We arbitrarily pick 0 for the ordering stream, UNASSIGNED_SYSTEM_ADDRESS means don't exclude anyone from the broadcast, true means broadcast the message to everyone connected
server->Send(message2, (const int) strlen(message2) + 1, HIGH_PRIORITY, RELIABLE_ORDERED, 0, UNASSIGNED_SYSTEM_ADDRESS, true);
}
#endif
// Get a packet from either the server or the client
for (p = server->Receive(); p; server->DeallocatePacket(p), p = server->Receive()) {
// We got a packet, get the identifier with our handy function
packetIdentifier = GetPacketIdentifier(p);

// Check if this is a network message packet
switch (packetIdentifier) {
case ID_DISCONNECTION_NOTIFICATION: // Connection lost normally
removePlayer(p->systemAddress);
printf("DISCONNECTION_NOTIFICATION from %s\n", p->systemAddress.ToString(true));
}
}
}



In short, it is refreshing (loop) received the message and waits for User Commands (gets (message))

I like to do something in a window (using QT), ie, it is refreshing to receive the message (loop) and waits for commands (buttons, text, etc ...)

how could I do?

Thanks

Share this post


Link to post
Share on other sites
Threads are probably the way to go. User input and handling incoming data shouldn't be related to each other unlike your Server::Update which seems to handle both.

Share this post


Link to post
Share on other sites
I never used QThreads, and very few of the conventional thread, one would like to help you, what would be the best way to solve this problem with Threads?

I run the thread responsible for command inputs only when the "Send" button is pressed?

Share this post


Link to post
Share on other sites
One way to do this is having two threads.
One responsible for user input so when the user presses a button
a method is called and an action is taken( possibly sending data) note that usually this is the main thread's responsibility.
The other thread is responsible to receive incoming packets and handle them accordingly. I've never used Qt so can't help you on the implementation details.

HTH.

Share this post


Link to post
Share on other sites
You don't need a loop to create that application in Qt.

You'd have a QTcpServer listening for new connections. When a new connection is made, a signal is emitted, which is where you get given a QTcpSocket from the nextPendingConnection() function. You would then listen on the readyRead() signal of the QTcpSocket, which is signalled each time new data arrives from the client.

To get keyboard input you could implement the virtual keyReleaseEvent in your QMainWindow derived class. Edit: I see you're actually reading console input, so I would probably end up making a QThread just to read console input.

Share this post


Link to post
Share on other sites
Quote:
Original post by CableGuy
One way to do this is having two threads.
One responsible for user input so when the user presses a button
a method is called and an action is taken( possibly sending data) note that usually this is the main thread's responsibility.
The other thread is responsible to receive incoming packets and handle them accordingly. I've never used Qt so can't help you on the implementation details.

HTH.


Well Qt's sockets don't need any threading as they are all event based.

Here's a guide guide on advice guide on threading: http://blog.exys.org/entries/2010/thread_abuse.html

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this