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

Author Topic: [SOLVED] What do I need to send?  (Read 6205 times)

0 Members and 1 Guest are viewing this topic.

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
[SOLVED] What do I need to send?
« on: March 08, 2013, 10:41:39 pm »
Good evening,
Today I wrote a simple pong via server and client. Everything works just fine, but with the code so far, it causes a lot of lag... I am pretty sure, that I send too much or too often... I use TcpSockets and thats what I have so far:
The server updates the ball and sends the position to the two clients, each client updates his pad individually and sends the position to the server, which sends it on to the other client.

Somewhere I read, to limit the packets to 30, is this the solution and if it is, what is the best way to do this?
Should I use Udp instead of Tcp (I hope and don't think so)?
But my main question is: What do I need to send?
Is it right to send the position from the ball and the two pads each frame? Should I also update the ball on the clients and only check the position with the server sometimes?

I hope you know what I mean  ::)
Greetings, Geheim!
« Last Edit: March 13, 2013, 05:45:44 pm by Geheim »
Failing to succeed does not mean failing to progress!

io

  • Jr. Member
  • **
  • Posts: 52
  • z/OS by day, SFML by night
    • View Profile
Re: What do I need to send?
« Reply #1 on: March 09, 2013, 02:37:33 am »
I was actually having the same problem:

http://en.sfml-dev.org/forums/index.php?topic=10803.0

What I was recommended was to limit packets sent (30 packets per second) and to actually send offsets of position instead of just move down.

Laurent said that on my end it was not really network lag as the movements being performed were the same, but actually the way I handled what was being sent.  I'll be working on incorporating his suggestions/fixing it over the weekend and when I get it resolved I will definitely be posting my code.

It will be helpful if you provide a little more information btw -- what exactly do you mean by lag?  Lag as in the paddles are not synching up on each end or the actual program runs slow etc.

Hopefully you'll get a better answer then above, as I'm completely new with networking O: ), but hopefully some of it helps. 


« Last Edit: March 09, 2013, 02:39:59 am by io »

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: What do I need to send?
« Reply #2 on: March 09, 2013, 11:33:44 am »
Yes, I read your post ;)
When I try the pong with two localhosts, it works nearly perfekt, but on different pcs it lags unplayable :(
So I guess I send too much? With lag I mean, that you see the ball not moving, but jumping heavily around. Your pad moves just fine, because you update it on the client ;) but the opposite one again is jumping heavily than moving softly...
Failing to succeed does not mean failing to progress!

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: What do I need to send?
« Reply #3 on: March 09, 2013, 03:21:17 pm »
I'd only send the following in the most basic setup:
Player starts moving bar up.
Player starts moving bar down.
Player stops moving bar.
Point is made.

That of course doesn't include a lot like setting up a game, ending a game or general communication to keep everything working as well as fancy stuff like changing speeds and alike. The idea would be that the clients and the server calculate the movement of the ball simultaniously (using exactly the same algorithm, and only input (moving stopping) and made points are being sent. You'd get practically no traffic and the game stays smooth.

//Game starts: Ball direction is sent to both players.
//Player 1 moves his bar by pressing UP: Only one single packet is sent to the server containing that the player moves his bar. Gets sent to Player 2 by the server.
//Player 1 stops his bar by releasing UP: Only one single packet is sent to the server containing that. Gets sent to Player 2 by the server.
//Ball hits Player 1s bar: No communication. Both clients know where the ball is and where it will move to, and the server knows as well.
//Player 2 dosen't do anything and therefore ball hits border. Server detects that and tells both clients that point was made. Clients may detect it as well, but only to stop ball-movement or any other visual changes.

I've never done anything like that, so it's just a basic idea. It doesn't take varying latency into account as well as lag on any of the machines which would make the movement of anything out of sync, or anything else you'd have to think of.

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: What do I need to send?
« Reply #4 on: March 09, 2013, 04:07:47 pm »
Alright, sounds legit ;) thx!
I will try it today and tell you then ;)
Now, I have also an idea with the ball to let everything stay syncronized.
If it will, I will of course tell you ^^
Failing to succeed does not mean failing to progress!

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: What do I need to send?
« Reply #5 on: March 09, 2013, 10:22:49 pm »
So I tested it and it works now pretty good, the lag is gone and everything moves smooth. I have to test it later with better internet, because I connect via hamachi ip, but the relay tunnel is not ready, so there is now quite a delay;)
Maybe I also have an issue in the code, but the problem is solved now I guess, thx!
Failing to succeed does not mean failing to progress!

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Sorry
« Reply #6 on: March 11, 2013, 08:28:41 pm »
I'm really sorry, I missed something, it has nothing to do with the bool/thread (if you read my previous post)
The Topic can be closed/solved... ;)
« Last Edit: March 11, 2013, 08:38:06 pm by Geheim »
Failing to succeed does not mean failing to progress!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10396
    • View Profile
    • development blog
    • Email
Re: What do I need to send?
« Reply #7 on: March 11, 2013, 08:42:03 pm »
I tested now a lot and I found something strange and interesting with bools and threads.
If a client presses "W" it sends it to the server, which sends it to the other client, this works perfektly!
Then in the client I change a bool (enemyUp) to "true" and output "bool changed!" in the console! So far so good, but in the main-thread, I move the pad, if(enemyUp) and output "move!".
So it should spam the console and move the pad? Well, but sometimes the bool did not change, because it prints EVERY TIME "bool changed!" if I press "W", but sometimes it does not move the pad and spam the console, so the bool did not change every time?! Then I thought maybe I need a mutex, but it did not help^^
Well you got a perfect example of a race condition. It's also a nice example that booleans aren't atomic as it's a widely spread misconception.
Programming in a multi-threaded environment isn't easy at all. There's so much to look out for and needs quite a bit of knowledge to get right. You should probably read some articles on the topic.

The problem is, that if 2 threads running in parallel want to read and write from the same memory. If now A writes while B reads the outcome is undefined.

There are many ways to work around this issue. The most simplest one is using a lock. How did you use the mutex?
« Last Edit: March 11, 2013, 08:53:52 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: What do I need to send?
« Reply #8 on: March 11, 2013, 10:23:36 pm »
Thx for your fast response!
I'm so sorry, because I found the mistake right after I posted this, now everything works fine. (I edited my post or?)
Yeah its really difficult with threads and sockets and that stuff, but also really cool, interesting and you can do so much with it! Thats my first game with sockets, so I have a lot to learn^^ Some issues I never had before, at the beginning it was really difficult for me to handle everything with mutexes and pointers, that no memoryleaks occur or that the program does not crash.
But now it works and I can concentrate on the game itself and how to send, what to send and so on ;)
Do you know some good books/tutorials about that topic? Either in englisch or in german. Would be really helpful I guess ;)
Failing to succeed does not mean failing to progress!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10396
    • View Profile
    • development blog
    • Email
Re: What do I need to send?
« Reply #9 on: March 12, 2013, 12:02:14 am »
Do you know some good books/tutorials about that topic? Either in englisch or in german. Would be really helpful I guess ;)
I haven't read any, but then again I've a class at the university covering that part.

But form the SO list, this seems to be a good suggestion.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: What do I need to send?
« Reply #10 on: March 12, 2013, 06:23:19 pm »
Thank you, I will take a closer look an that!
Btw, this topic can be marked as solved (I have no idea how to do that) ;)
Failing to succeed does not mean failing to progress!

io

  • Jr. Member
  • **
  • Posts: 52
  • z/OS by day, SFML by night
    • View Profile
Re: What do I need to send?
« Reply #11 on: March 13, 2013, 01:37:11 am »
Just click modify on the original post and change the subject line ;]