Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
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  
  Nio - seperating types of incomming data  (Read 1736 times)
0 Members and 1 Guest are viewing this topic.
Offline Rockslide

Junior Newbie





« Posted 2011-03-28 00:29:54 »

What I'm trying to accomplish:
A multiplayer spacesim for about 20-50 players, with in-game chat. Dedicated server program. For now assume I do it all with TCP.

Questions:
Should I make more then one socketchannel for each client? Like chat messages go on channel1, positiondata goes on channel2, otherstuff on channel3, etc...
Or is it better to seperate chat and gamedata by starting each message with an ID. (Like if the first byte is a 1, treat it as a chatmessage, if it's a 2, treat it as a positionmessage etc...)
Is there a 3rd (better) option?

If the answer is only use 1 socketchannel per player, then what use is a selector on the client side? I can understand the use of a selector on the serverside, cause it manages multiple clients, but on the client side it seems needlessly complicated to use one.

I've also read about splitting read and write operations on seperate selectors. Is this a good idea? cause it seems to me it further complicates things.

I would like some advice on the best networking design for my goal please Smiley
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2011-03-28 00:32:21 »

With 50 connections there is no reason to resort to (the can of worms named) NIO.

Just make 50 threads, that push data on a queue, and let 1 thread process all incoming/outgoing data.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-03-28 09:31:31 »

Option 2 is what I use for all networking things I do. The first byte determines what type of packet it is and it sent to the appropriate handler.

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

JGO Knight


Medals: 27
Exp: 18 years


Computers can do that?


« Reply #3 - Posted 2011-03-28 11:09:22 »

A lot of folks here hate NIO, I don't really know why. It works fine and i don't really think it is that hard to use. It works as advertised and i had a full working single threaded n socket capable network manager in a day. I can scale threads and connection per thread. 

On clients you don't need to use a selector... you can use a single socket. Of course you can use one thread per connection on the server as well, at least on linux this is even faster than selectors. But now you have many threads not just one or two with the required synchronization, which can be its own can of worms. Also on windows it grinds to halt after a few 100 threads on xp anyway and is generally slower than selectors.

At 50 connections either method would work. I would still use a selector on the server so i have one network thread (read and write) to sync up with the rest of the program.

As for delimiting messages on your stream, i just use message delimiters like you say. Since some messages can be different lengths, my first field is the length in bytes. If i get out of sync, I "reset" the connection.

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

Junior Newbie





« Reply #4 - Posted 2011-03-31 10:47:25 »

Ok I decided to ditch NIO and go back to IO.

Question:
Cause this socket IO is blocking, wouldn't I need 2 threads per socket? One for reading and one for writing? That would make 100 threads instead of 50.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2011-03-31 11:44:29 »

I have 1 thread for each connection that receives commands (reads) and replies to them (writes).

Offline wingsOrc

Senior Newbie


Medals: 1



« Reply #6 - Posted 2011-03-31 11:44:59 »

No you dont, only the reader needs a thread.
You send messages with methodcall à la

writer = new PrintWriter(socket.getOutputStream());

..
 
public void sendMsg(String msg){
writer.println(msg);
writer.flush;
}
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 4
Exp: 8 years



« Reply #7 - Posted 2011-03-31 12:29:08 »

No you dont, only the reader needs a thread.
You send messages with methodcall à la

writer = new PrintWriter(socket.getOutputStream());

..
 
public void sendMsg(String msg){
writer.println(msg);
writer.flush;
}


Yes, he does . If the sending of the message blocks for any reason, your game will also freeze until the message is sent . Though it may be rare to block the sending message, I have learned in the last years that if something has a chance to happen, it WILL happen . Never neglect a rare case .

It is possible to use just one Thread. It is actually the way I'm doing right now .
First I check if there are bytes to be read. If yes, I parse them and send them to a queue for processing by the game later.
Else, I check if there are messages queued to be sent.
Else , I sleep a little and repeat again .
The code : (try catch have been removed for the sake of readability):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
DataInputStream inFromClient; 
....

   while(active)
      {
            if (inFromClient.available()>0)
              {
              readDataFromClientAndInsertInParsersQueue();
              }
            else  if (!sendMessageQueue.isEmpty())
            {
               sendQueuedMessages();                  
            }
                                else
                                {
                                Thread.sleep(sleepTime);
                                }
           
      }  


I can't tell you if this is an excellent approach but it has been working for me .
Any comments from the specialists ?

Offline Rockslide

Junior Newbie





« Reply #8 - Posted 2011-04-01 06:08:18 »

Well wait a minute, I didn't know that was possible! (The available check)

This is my version of your suggestion:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
    
out = new PrintWriter(game.network.socket.getOutputStream(), true);
InputStream is = socket.getInputStream();
in = new BufferedReader(new InputStreamReader(is));
String chatMsg = "";
         
while (true) {
   if (in.ready()) {
      game.gameGui.addMsg(in.readLine());
   }
             
   if ((chatMsg = game.network.chatQueueOut.poll()) != null) {
      out.println(chatMsg);
   }
             
   Thread.sleep(1);
}

(Also removed try-catches for readability)

Doesn't this effectively make the reading part non-blocking?
It appears to work but like you said, I would like some feedback from others too Smiley
Offline delt0r

JGO Knight


Medals: 27
Exp: 18 years


Computers can do that?


« Reply #9 - Posted 2011-04-01 08:34:58 »

The available call is one way to do non blocking stream IO. You are now just using stream non blocking to mux the read/write rather than a selector. Nothing wrong with that.

However I have only done non blocking writes with selectors. When i am doing the streams i have never needed non blocking writes except with network code, where there is either a thread or a selector.

I have no special talents. I am only passionately curious.--Albert Einstein
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.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (14 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (60 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!