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 question this time :)  (Read 948 times)
0 Members and 1 Guest are viewing this topic.
Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Posted 2004-01-09 06:14:01 »

Hi
 I'm rewriting some network code (again), and as part of doing this I've read blahblahblah's doc, one thing I wasn't aware of is that when you write your byte buffer to a non blocking socketchannel it might not write all the bytes. So what I was thinking of is having a thread on the client, and one on the server, that selects on op_write (only those channels which messages have been send for are registered) that blocks on on the select and then carries on pumping bytes down till either it can't send any more, in which case it reregisters that channel, or the message is sent. This is more of a design question, but I'm after comments. My reading is done by calling poll on my endpoints, but this just calls selectNow on the channels that are registered for op_read. The only reason I think I need this thread to handle the writes is that I don't want to rely on the poll being called for writing messages, if I only poll say 10 times a second then my latency will be at least 100ms, for any message that can't be written in one go, on top of the actual network lag.

Comments/suggestions?

Cheers

Endolf

Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #1 - Posted 2004-01-09 07:55:02 »

Quote
The only reason I think I need this thread to handle the writes is that I don't want to rely on the poll being called for writing messages, if I only poll say 10 times a second then my latency will be at least 100ms, for any message that can't be written in one go, on top of the actual network lag.


Assuming you are polling because it's one of the stages in your game-loop, then this sounds OK. As you've pointed out, if you don't while on writing those bytes, the client might receive stuttered data that arrives in chunks ten times a second Smiley.

Without an extra thread, you could
1  
 while( not yet written all the bytes ) 
.
but if a client has a particularly slow connection you might run out of your timeslice in your game loop.  I'm guessing this is the other reason why you'd like to parallelize that while loop into a separate thread, in addition to the stuttering problem you mentioned?

I often use separate threads for accepting, reading, and writing (so I'll have three threads in the server, shared amongst all connected clients).

Note, however, that this partly reflects the environment I'm in, where it's really useful to be able to separately throttle the three different aspects; you can artificially delay each thread without affecting the others (generally I fiddle with threads using app code, rather than by using thread priorities in java, because of the lack of guarantees offered by the Thread etc APIs; AFAICS this lack is only because Java runs on some non-pre-emptive OS's, but it makes my life simpler not to assume...).

It would be nice to say "c.f. Game Programming Gems 4", but that's still not out for another 3 months Sad - I've got a gem in there that compares and contrasts different architectures for different parts of a server, and has example code for a 3 + N thread system (the N is for other parts of the server). </plug> Smiley (sadly, authors can't read other authors gems until we get our free copies, so I can't specifically recommend the book, but it's likely to be of similar quality to the other three books).

malloc will be first against the wall when the revolution comes...
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 (39 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!