Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Best way to setup GUI/TCP?  (Read 6013 times)

0 Members and 1 Guest are viewing this topic.

azkay

  • Newbie
  • *
  • Posts: 9
    • MSN Messenger - final_fantasie_2@hotmail.com
    • View Profile
Best way to setup GUI/TCP?
« on: October 31, 2010, 08:42:19 am »
Right now ive got a while loop in main() that loops a tcpreceive and parses the data from there, eg;

loop start
recv
switch recv
case 1:
move person left
case 2:
move person right
default:

loop end

Now, using the tutorial on using a sprite, I would want to code it so, when it receives a "move left" packet, it moves the sprite to the left.

I tried putting it all in the same receive loop, but there was lots of delay in there, so, I need two threads?

Whats the best way to setup multiple threads where data can still be accessed by either?

eg; One thread runs the OpenGL GUI loop, other thread runs the TCP receiving?

Svenstaro

  • Full Member
  • ***
  • Posts: 222
    • View Profile
Best way to setup GUI/TCP?
« Reply #1 on: October 31, 2010, 01:08:30 pm »
You should not need two threads. You should, however, control the amount of packages sent out. I'm pretty sure you are spamming lots of packages, leading to the delay. Try to limit the update, send and receive rate and it will work nicely. Start with 60 ticks a second and go from there.

bastien

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • http://bastien-leonard.alwaysdata.net
Best way to setup GUI/TCP?
« Reply #2 on: October 31, 2010, 09:29:36 pm »
If you use TCP, by default the API calls probably block when you're not receiving anything, which means that if the server doesn't send anything for one second, the game will be stuck one second.

You can use something like timeouts, select() (I would recommend this) or threads (only if really needed) to avoid that.
Check out pysfml-cython, an up to date Python 2/3 binding for SFML 2: https://github.com/bastienleonard/pysfml-cython

azkay

  • Newbie
  • *
  • Posts: 9
    • MSN Messenger - final_fantasie_2@hotmail.com
    • View Profile
Best way to setup GUI/TCP?
« Reply #3 on: November 02, 2010, 09:01:16 am »
Well, after a day of raging I still havnt gotten it working.
Any ideas on how to get it all working properly?

I have taken out the data/user/login/etcetc, so it obviously wont run, same with the custom includes. But hopefully it shouldnt matter for this problem.

Basically, need to get the receive stuff working with the graphics stuff.
Now that im (not?) using threads, dont know if this thread should stay under "Systems", but oh well.

Code: [Select]

#define _CRT_SECURE_NO_WARNINGS

#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <sstream>
#include "misc.h"
#include "f.h"
#include "md5.h"

sf::SocketTCP Client;
sf::SelectorTCP Selector;

std::string STX = "\x02";
std::string ETX = "\x03";
std::string SOH = "\x01";

int main(){
std::vector<std::string> results;
std::stringstream all;
std::string ReceivedStr;
size_t Size = 0;
char Buffer[1];

std::vector<std::string> gInfo = fLogin("", "");

std::string hood = "";
std::string server = fServer(hood);
Client.Connect(8080, server);
//Client.SetBlocking(false);
Selector.Add(Client);

fSend(Client, "some,data");
fSend(Client, "some,data");
fSend(Client, "some,data");

sf::Event Event;
sf::RenderWindow App(sf::VideoMode(1024, 1024, 32), "SFML Graphics");

sf::Image Map;
sf::Image avatarMe;

Map.LoadFromFile("image1.jpg");
avatarMe.LoadFromFile("1aff79cba4bac.png");

sf::Sprite Flash(Map);
sf::Sprite Sprite(avatarMe);

while(App.IsOpened()){
while(App.GetEvent(Event)){
if (Event.Type == sf::Event::Closed)
App.Close();
}

float ElapsedTime = App.GetFrameTime();

if (App.GetInput().IsKeyDown(sf::Key::Left))  Sprite.Move(-100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Right)) Sprite.Move( 100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Up))    Sprite.Move(0, -100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Down))  Sprite.Move(0,  100 * ElapsedTime);

App.Clear();
App.Draw(Flash);
App.Draw(Sprite);
App.Display();

if(Selector.Wait()){
Client.Receive(Buffer, sizeof(Buffer), Size);
ReceivedStr.append(Buffer, Buffer + Size);
if(Size <= sizeof(Buffer)){
if(ReceivedStr != ETX){
all << ReceivedStr;
}else{
if(all.str().length() > 2){
if(!isdigit(all.str().substr(0, 1)[0]))
all.str(all.str().substr(1, all.str().length()));

StringExplode(all.str(), STX, &results);
switch(atoi(results[0].c_str())){
case 35:
std::cout << all.str() << std::endl;
break;

default:
std::cout << all.str() << std::endl;
break;
}
}
all.str("");
}
ReceivedStr = "";
results.clear();
}
}
}

std::cout << "Press enter to exit..." << std::endl;
    std::cin.ignore(10000, '\n');
Client.Close();

    return EXIT_SUCCESS;
}

azkay

  • Newbie
  • *
  • Posts: 9
    • MSN Messenger - final_fantasie_2@hotmail.com
    • View Profile
Best way to setup GUI/TCP?
« Reply #4 on: November 04, 2010, 03:59:32 am »
Still lags whether I set blocking to false or just comment it out.
If I comment out everything within the if(Selector.Wait()), except for the receive, it still lags. Tried increasing the buffer to 1024, still lags.

Im out of ideas.

Svenstaro

  • Full Member
  • ***
  • Posts: 222
    • View Profile
Best way to setup GUI/TCP?
« Reply #5 on: November 04, 2010, 08:02:00 pm »
We're using UDP, but you can look at our receive and send code here: https://github.com/svenstaro/NoisyHunter/blob/master/engine/src/NetworkManager.cpp

Also, use Wireshark to see how many packages you are receiving and sending out.

Amadeus

  • Newbie
  • *
  • Posts: 5
    • View Profile
Best way to setup GUI/TCP?
« Reply #6 on: November 05, 2010, 08:04:24 am »
Hi

I use on both client and server nonblocking mode and use select to handle incoming, outgoing and error stuff. Never had any lag problems on that set up.

On client side loop is about
Code: [Select]
read from server
parse data if any
do logic
send to server


server loop something like
Code: [Select]
check new connection
check and handle clients, kick out dropped etc not valid
Read data
do logic
send data


Tho I dont use sfml network pack on client or server side. Orginally both client and server was on linux side, but now on only server is on linux. Used Allegro on graphic API back then when I wrote my game.

Currently Im remaking client side with SFML and partially remaking server side.

http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html was my bible when I wrote my network code to my game.




A.

azkay

  • Newbie
  • *
  • Posts: 9
    • MSN Messenger - final_fantasie_2@hotmail.com
    • View Profile
Best way to setup GUI/TCP?
« Reply #7 on: November 05, 2010, 02:59:15 pm »
But, does anyone see where/if I went wrong in the code I posted?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Best way to setup GUI/TCP?
« Reply #8 on: November 22, 2010, 07:10:22 pm »
Can you reduce your code to a minimal and complete example which still reproduces your problem? That is, remove all stuff irrelevant for the error, while keeping a compilable code that still shows the mentioned issues.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything