Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Good latency proxy? (TMnetsim causing stream corruption!)  (Read 1604 times)
0 Members and 1 Guest are viewing this topic.
Offline Abuse

JGO Knight


Medals: 13


falling into the abyss of reality


« Posted 2014-03-23 15:22:49 »

I'm currently writing an unofficial multiplayer mod for the game Starsector. (great game btw! and a bargain for $10)

For simplicity* I'm making the game simulation(and fp) deterministic, syncing the peers, and transmitting just user-originated events using a lock-step model, with input delay appropriate to the latency between peers.
*(simplicity is of paramount importance because the entire mod is going to be bytecode injected, meaning there are practical limitations upon how much of the codebase I can interact with.)

To this end I created a prototype to test how well the lock-step model handles at various latencies. (using the tool TMnetsim as a latency proxy)
It seemed to work fine with low latencies, but exhibited weird data corruption at higher latencies.
After much hair pulling I determined it couldn't possibly be my code, and wrote the below code as a sanity test to confirm TMnetsim is a useless piece of crap.

So 2 questions:

1) The below code should work perfectly, right? (obviously with the relevant ports redirected)
2) As TMnetsim appears to be a piece of unreliable crap, can anyone suggest a good tool for introducing semi-realistic latency between connections? (I could of course write one... but given the plethora of multiplayer games surely such tools already exist?!)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
import java.io.IOException;
import java.net.Socket;

public class Client {

   public static void main(String[] args) throws IOException {
      Socket s = new Socket("localhost", 9665);

      new Thread(new SocketReader(s)).start();

      new Thread(new SocketWriter(s)).start();
   }

}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
   public static void main(String[] args) throws IOException {
      Socket s = new ServerSocket(9765).accept();

      new Thread(new SocketReader(s)).start();

      new Thread(new SocketWriter(s)).start();
   }

}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

class SocketReader implements Runnable {

   Socket s;

   public SocketReader(Socket s) {
      this.s = s;
   }

   @Override
   public void run() {
      try {
         DataInputStream dis = new DataInputStream(s.getInputStream());
         int counter = 0;

         while (true) {
            int val = dis.readInt();
            if (val != counter) {
               throw new RuntimeException("stream corruption! Expected: " + counter + " received: " + val);
            }
            counter++;
         }
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }

}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

class SocketWriter implements Runnable {

   Socket s;

   public SocketWriter(Socket s) {
      this.s = s;
   }

   @Override
   public void run() {

      try {
         DataOutputStream dos = new DataOutputStream(s.getOutputStream());
         int counter = 0;
         
         while (true) {
            dos.writeInt(counter);
            counter++;
         }
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }

}

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline moogie

JGO Knight


Medals: 13
Projects: 6
Exp: 10 years


Java games rock!


« Reply #1 - Posted 2014-03-23 23:16:50 »

I agree, the code listed should always be in sync.

I am not familiar with TMnetsim but if I was to simulate latency I would do it very simply: extend Socket and override getInputStream() to return a wrapped parent.getInputStream(). This wrapped inputstream calls the inner inputstream methods but waits for the desired delay before returning the result.
Offline saucymeatman
« Reply #2 - Posted 2014-03-24 02:33:27 »

What does it mean for the code to be "bytecode injecteD"? Sounds painfull Wink
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Abuse

JGO Knight


Medals: 13


falling into the abyss of reality


« Reply #3 - Posted 2014-03-24 02:42:29 »

What does it mean for the code to be "bytecode injecteD"? Sounds painfull Wink

bytecode instrumentation / code weaving.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (20 views)
2014-10-25 15:46:29

Longarmx (53 views)
2014-10-17 03:59:02

Norakomi (47 views)
2014-10-16 15:22:06

Norakomi (35 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (69 views)
2014-10-14 00:39:48

TehJavaDev (69 views)
2014-10-14 00:35:47

TehJavaDev (61 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (47 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!