Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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  
  Networked bit of a client  (Read 899 times)
0 Members and 1 Guest are viewing this topic.
Offline Mads

JGO Ninja


Medals: 24
Projects: 3


One for all!


« Posted 2012-01-02 00:42:01 »

Hello guys!

What does JGO know about client design?

How should packets be handled when they're incoming? How about outgoing?
You could technically set it up exactly as on the server, but it just feels wrong.

I began with java when writing servers, and working on those exclusively, so that i can do pretty well. I never participated in the work regarding clients, though, hence I don't know anything about their design.

Is there any good reads on this, you guys can approve of?

This is for a game that requires a constant flow of packets, both ways.

Offline delt0r

JGO Coder


Medals: 22


Computers can do that?


« Reply #1 - Posted 2012-01-02 08:50:22 »

There are a lot of topics here on this sort of thing. But otherwise the requirements are very vague. You need to define what you want to do a bit better than "constant flow of packets".

I have no special talents. I am only passionately curious.--Albert Einstein
Offline Mads

JGO Ninja


Medals: 24
Projects: 3


One for all!


« Reply #2 - Posted 2012-01-02 23:04:50 »

I'm sending out packets at least twice every second to each client, for updating.
The client is supposed to send packets when something happens, such as movement or chat.

I'm using KryoNet, so I kind of want to use the same bindings for packet-handlers on both the client and the server.
It's an array with classes that handles what happens in the application every time a packet is rechieved. Each of these handlers, are on the index in the array that matches the packetID of whateverr packet needs handling. It's kind of like a huge switch.

I'm thinking I would like to bind them like this the same way on the client, as on the server. However, I'd like to override the actual handlers.
Would that even be possible, let alone "correct" for a networked application?

Can anyone give me some pseudo-code or soemthing, around how the entry-point of the client, the game-code, and the network-relating code is/could be/should be connected?  Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline counterp

Senior Member


Medals: 11



« Reply #3 - Posted 2012-01-03 22:01:37 »

sounds a lot like my system Tongue cool

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  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
public class Packets {

   private static final int[] sizes = new int[256];
   private static final Packet[] packets = new Packet[256];
   static {
      packets[187] = new AttemptLogin();
      sizes[187] = -1;
      packets[23] = new RemotePlayer();
      sizes[23] = -1;
      packets[112] = new DrawingAction();
      sizes[112] = -1;
      packets[83] = new ChatMessage();
      sizes[83] = -1; // size can change, send it as a byte
     packets[241] = new RegionRequest();
      sizes[241] = -2; // size can change, send it as an int
  }

   public static Buffer fragment;
   public static ISAAC decryptor;

   public static void handle(Buffer buffer) { // read a chunk of data as a byte[] and pass it along to my custom Buffer class for easy reading
     boolean decrypt = true;
      if (fragment != null) {
         buffer.insertStart(fragment.array());
         fragment = null;
         decrypt = false;
      }
      while (buffer.remaining() > 0) {
         int opcode = buffer.getSigned();
         if (decrypt) {
            if (decryptor != null)
               opcode -= decryptor.next();
         } else {
            decrypt = true;
         }
         opcode &= 0xFF;

         int size = sizes[opcode];
         Packet packet = packets[opcode];
         if (packet == null) {
            System.out.println("Non-existant packet: " + opcode);
            return;
         }

         if (size == -1) {
            if (buffer.remaining() > 0)
               size = buffer.get();
            else {
               fragment = new Buffer(2).put(opcode);
               buffer.clear();
               System.out.println("Storing fragmented packet " + opcode + ".");
               return;
            }
         } else if (size == -2) {
            if (buffer.remaining() > 3)
               size = buffer.getInt();
            else {
               fragment = new Buffer(2).put(opcode);
               buffer.clear();
               System.out.println("Storing fragmented packet " + opcode + ".");
               return;
            }
         }

         int remaining = buffer.remaining() - size;

         if (remaining < 0) {
            fragment = new Buffer().put(opcode);
            if (sizes[opcode] == -2)
               fragment.putInt(size);
            else
               fragment.put(size);
            fragment.put(buffer.getBackingBuffer(), buffer.readPosition(), buffer.remaining());
            buffer.clear();
            System.out.println("Storing fragmented packet " + opcode + ", size " + fragment.writePosition() + ".");
            return;
         }

         try {
            packet.execute(buffer, opcode, size);
         } catch (Exception e) {
            e.printStackTrace();
         }

         if (buffer.remaining() != remaining) {
            System.out.println("<Opcode:" + opcode + "> Expected: " + remaining +", Actual: " + buffer.remaining());
            buffer.skip(buffer.remaining() - remaining);
         }
      }
   }

}
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.

xsi3rr4x (72 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (79 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (80 views)
2014-04-14 15:48:38

tom_mai78101 (104 views)
2014-04-10 04:04:31

BurntPizza (164 views)
2014-04-08 23:06:04

tom_mai78101 (260 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!