Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  PyroNet (lowlevel NIO Wrapper)  (Read 21477 times)
0 Members and 1 Guest are viewing this topic.
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #30 - Posted 2009-07-11 07:37:39 »

nioda (nyo-da) - native i/o damage amplifier... haha Grin

弾幕 ☆ @mahonnaiseblog
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #31 - Posted 2009-07-11 11:10:40 »

It's totally aimed at the TCP part of NIO. I mean, UDP is already nearly single-threaded.

So the name shouldn't have NIO in it.

I was thinking of 'Iowa' (IO Wrapper API) ? But how to find it... it will be completely swamped.

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

Junior Newbie





« Reply #32 - Posted 2009-07-12 19:09:19 »

Hmmmm I just wrote some code myself to do exactly this!!

Annoying to have spotted this a few days too late... ah well :-)

If anyone is interested, my code is here:

http://code.google.com/p/mikeralib/source/browse/#svn/trunk/Mikera/src/mikera/net
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #33 - Posted 2009-07-12 19:29:58 »

Nice API, but there are some problems.

When you read from the channel, and you get an IOException, (as opposed to a -1 return value), you will end up with a socket channel that will remain open.

You also create a new selector + a new thread for every connect (not accept). Also, keep in mind that interfacing with the native network buffers is really expensive. You have tied your networking API to a 4byte+payload protocol, but you also read your data that way. It's going to slow you down if you first try to read 4 bytes, and then the payload, especially when you have 20 packets in your inbound buffer, and you have to call channel.read 40 times. Better to just read everything available and slice and dice it in your own code. You also don't seem to support asynchronous connect (OP_CONNECT) which is not really bad...

Overall, nice API, but unfortunately ties the user to a 4B+data protocol, which means you never can implement oldschool stuff like HTTP, FTP, POP3, SMTP, etc, etc, on top of it, but then again, your probably only going to use it in your own program.

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

Junior Newbie





« Reply #34 - Posted 2009-07-13 16:20:53 »

Cool, thanks for the feedback - I'll have a go at fixing these points since this is just my first attempt at hacking together a networking protocol!

My main design criteria was having a simple, asynchronous API with relatively low overhead for my own applications. The point of the 4 byte message length protocol is that the networking library needs to know when the end of the message is, so that it only invokes the message handler once it has received a complete message. This simplifies the receiving code significantly since you don't have to test and wait for a complete message etc.

I'm considering an smarter encoding where the message length is 1 byte for short messages (up to 127 bytes), 2 bytes for slightly longer ones, 3 bytes once it hits 16k etc. 


Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #35 - Posted 2009-07-13 16:42:29 »

I know what the 4B protocol is for, I'm just saying that you don't necessarily have to hardcode that in a I/O library. I added on top of the lib, as tiny classes: Packet and PacketListener. The core classes just pump the received data into a bytebuffer, and from there into the packets, much faster than querying the OS for the next few bytes.

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

JGO Wizard


Medals: 15
Projects: 19


Mojang Specifications


« Reply #36 - Posted 2009-07-15 17:20:05 »

What's the license on this, riven?

Play Minecraft!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #37 - Posted 2009-07-15 18:23:41 »

Hi Notch, special offer for you: BSD.

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

JGO Wizard


Medals: 15
Projects: 19


Mojang Specifications


« Reply #38 - Posted 2009-07-15 18:37:13 »

Hi Notch, special offer for you: BSD.

I love you =D

Play Minecraft!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #39 - Posted 2009-07-15 19:20:27 »

Just to satisfy my personal curiosity: what are you going to use it for?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Markus_Persson

JGO Wizard


Medals: 15
Projects: 19


Mojang Specifications


« Reply #40 - Posted 2009-07-16 06:52:49 »

Minecraft. The code I've written has a couple of bugs because I misunderstood NIO when implementing it, and the code isn't really smooth any more.

Play Minecraft!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #41 - Posted 2009-09-27 22:49:07 »

FWIW: (or if SVN is your thing)

https://sourceforge.net/projects/jawnae-iowa/


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #42 - Posted 2009-09-28 15:46:33 »

After some 'good advice', the project is both renamed and relocated:

http://code.google.com/p/priobit/

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

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #43 - Posted 2009-09-30 09:37:21 »

Cool, Google Code is much better. I'm pretty done with SourceForge and their ugly new site. I still have a project over there I need to move over.

I have some trouble navigating your code. I keep looking for things similar to my code and not finding them. Wink I'm reading my code, trying to remember gotchas, then trying to find it in your code.

I remember that when you do socketChannel.close(), it doesn't completely close the socket. You need to do one more select to properly clean up the socket resources, so be careful not to close and exit your select loop without doing the extra select. Unfortunately I don't remember how the problem manifests. Maybe if you close then try to open on the same port you'll find it is still open, or check with netstat.

I noticed you are always using OP_WRITE to do writes. Apparently it is better to try the write and only use OP_WRITE if the write is partial. Apparently the write will almost always succeed the first time and is much more efficient.

Line 92 in NioServer, non-blocking accept can return null.

You might want to use setTcpNoDelay to turn off Nagle.

I saw on your old homepage you buffer some number (1400?) of bytes before sending? What happens if I send less than that? Does it timeout waiting for more and then send? How long is the timeout?

When writing my serialization lib, I found this comparison project very helpful. It gave me reasonable goals for my performance. Maybe it would be cool to setup a comparison project for Priobit/KryoNet/MINA/JGN/etc. We'd need to come up with a few tests and implement them in the different projects. I'd like to issue a preemptive "not it" for writing the MINA tests!  persecutioncomplex

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #44 - Posted 2009-09-30 10:22:44 »

Cool, Google Code is much better. I'm pretty done with SourceForge and their ugly new site. I still have a project over there I need to move over.
SF is so slow, it's not funny.

I have some trouble navigating your code. I keep looking for things similar to my code and not finding them. Wink I'm reading my code, trying to remember gotchas, then trying to find it in your code.
Same for me. Your code is not like mine at all.

I remember that when you do socketChannel.close(), it doesn't completely close the socket. You need to do one more select to properly clean up the socket resources, so be careful not to close and exit your select loop without doing the extra select. Unfortunately I don't remember how the problem manifests. Maybe if you close then try to open on the same port you'll find it is still open, or check with netstat.
Sockets always linger, even if you close() then, use setSoLinger(), or terminate the JVM. This is OS level stuff that you can't control. Try to make ~5000 TCP connections (in Windows) -- it will tell you it can't make any more. Then terminate the JVM, relaunch it, and you'll find that you still can't make any new Sockets. You simply have to wait a few minutes.

I noticed you are always using OP_WRITE to do writes. Apparently it is better to try the write and only use OP_WRITE if the write is partial. Apparently the write will almost always succeed the first time and is much more efficient.
Good idea.

Line 92 in NioServer, non-blocking accept can return null.
this is inside the onReadyToAccept() method, which will only be invoked when OP_ACCEPT was flagged. Therefore I'm quite confident it will never return null.

You might want to use setTcpNoDelay to turn off Nagle.
Already the case. All NioClients go through a setup procedure in the constructor, where that is set: NioClient.configure

I saw on your old homepage you buffer some number (1400?) of bytes before sending? What happens if I send less than that? Does it timeout waiting for more and then send? How long is the timeout?
That would be silly, eh? Smiley It will send it immediately.

When writing my serialization lib, I found this comparison project very helpful. It gave me reasonable goals for my performance. Maybe it would be cool to setup a comparison project for Priobit/KryoNet/MINA/JGN/etc. We'd need to come up with a few tests and implement them in the different projects. I'd like to issue a preemptive "not it" for writing the MINA tests!  persecutioncomplex
I'll give that a look later.

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

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #45 - Posted 2009-09-30 10:53:43 »

Sockets always linger, even if you close() then, use setSoLinger(), or terminate the JVM. This is OS level stuff that you can't control. Try to make ~5000 TCP connections (in Windows) -- it will tell you it can't make any more. Then terminate the JVM, relaunch it, and you'll find that you still can't make any new Sockets. You simply have to wait a few minutes.
Not what I meant. Let me find some text on it rather than vague waving my hands around... Wink I think the SelectableChannel javadoc has all the vagueness we need... it states a channel cannot be deregistered directly, but that its key must be canceled, which can only happen in a select. So when you close a socket, it cancels the keys, which are actually canceled in the next select, which then deregisters the channel. The symptom is that your sockets will stay in close_wait.

this is inside the onReadyToAccept() method, which will only be invoked when OP_ACCEPT was flagged. Therefore I'm quite confident it will never return null.
Just going by the javadocs. I'm not confident of anything NIO-related. Smiley

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #46 - Posted 2009-11-24 15:59:29 »

Major Minor overhaul!


Updates:
  • eager write: NioClient.enqueue(...) tries to write the data immediately to the channel
  • Replaced 'ReadFutures' with 'ByteFilter' which is much more flexible, allowing to write very basic 'state machines' that can yield results on (for example) fixed packet length, 2-byte-length + payload, byte[] pattern delimiters (like "\r\n" for line based protocols: HTTP, FTP, SMTP, POP3, etc, etc)
  • non-copy, non-split, non-merge ByteStream class that acts like a FIFO byte-queue, replacing ByteBufferStreamer that could potentially merge/split/merge on successive incomplete writes
-

Check out the SVN repository
http://code.google.com/p/priobit/source/browse/trunk/+priobit/# priobit/jawnae/priobit

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #47 - Posted 2009-11-24 16:44:18 »

To listen for infinite packets (2b+payload) using ByteFilterPacket
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
   @Override
   public void connectedClient(final NioClient client)
   {
      client.enqueueByteFilter(new ByteFilterPacket()
      {
         @Override
         public void onReady(ByteBuffer buffer)
         {
            byte[] payload = new byte[buffer.remaining()];
            buffer.get(payload);
            System.out.println("Received: " + new String(payload));

            // listen for next packet
           this.reset();
            client.enqueueByteFilter(this);
         }
      });
   }




To listen for infinite text lines (up to 4096 bytes long) using ByteFilterEndsWith
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
   @Override
   public void connectedClient(final NioClient client)
   {
      client.enqueueByteFilter(new ByteFilterEndsWith("\r\n".getBytes(), 4096)
      {
         @Override
         public void onReady(ByteBuffer buffer)
         {
            byte[] line = new byte[buffer.remaining() - ("\r\n".length())];
            buffer.get(line);
            System.out.println("Received line: " + new String(line));

            // listen for next line
           this.reset();
            client.enqueueByteFilter(this);
         }
      });
   }

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

Junior Member




Carte Noir Java


« Reply #48 - Posted 2010-01-31 12:13:28 »

(pump up)
Topic title needs a refresh, project's current name is Pyronet  Grin Good name if you ask me.
Offline Nate

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #49 - Posted 2010-01-31 12:14:30 »

Yes, a surprisingly good name!  Roll Eyes Tongue

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #50 - Posted 2010-01-31 12:23:53 »

KevGlass had been harrassing me about Priobit, and he liked PyroNet. Although I knew it was way too similar to KryoNet, I just couldn't help myself and gave in.

Besides that, as absolutely nobody is using it, I'm not even updating the SVN repo anymore.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline gouessej
« Reply #51 - Posted 2010-02-02 09:57:32 »

KevGlass had been harrassing me about Priobit, and he liked PyroNet. Although I knew it was way too similar to KryoNet, I just couldn't help myself and gave in.

Besides that, as absolutely nobody is using it, I'm not even updating the SVN repo anymore.
Everything is on pyronet now, isn't it? I'm going to read your source code...

Do you plan to implement something similar to RMI?

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #52 - Posted 2010-02-02 11:19:08 »

Do you plan to implement something similar to RMI?

Did you read the summary?
http://code.google.com/p/pyronet/

PyroNet is a fast, minimalistic, lowlevel abstraction layer over the high performance Java NIO API. The aim of the API is not to provide new functionality, but to reduce the complexity of interfacing with non-blocking I/O. The PyroNet API takes care of the boilerplate code, while not forcing any protocol on the developer. Then API is event driven and uses a single Selector per PyroNetwork. You will receive event notifications through a listener interface.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline gouessej
« Reply #53 - Posted 2010-02-02 11:51:34 »

Did you read the summary?
http://code.google.com/p/pyronet/

PyroNet is a fast, minimalistic, lowlevel abstraction layer over the high performance Java NIO API. The aim of the API is not to provide new functionality, but to reduce the complexity of interfacing with non-blocking I/O. The PyroNet API takes care of the boilerplate code, while not forcing any protocol on the developer. Then API is event driven and uses a single Selector per PyroNetwork. You will receive event notifications through a listener interface.
Yes but JGN has something like RMI, I was wondering whether your API has such a mechanism but you're right, it is only a low-level API.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #54 - Posted 2010-04-26 17:33:09 »

Excuse me for the long time without updates.

If anybody is using it (I know at least 1 person besides me is using it... yay) here is a complete rewrite.


The barely high-level stuff is removed from the core, and can be bolted on top. The core is basically nothing-but-a-nio-wrapper, with the classes at its core:
=> PyroSelector
=> PyroServer
=> PyroClient


Usability wise, the most important feature is being able to listen for events per server or client instance.

selector.addListener(PyroSelectorListener);
=> public void taskCrashed(Runnable, Throwable);
=> public void serverBindFailed(IOException);
=> public void clientBindFailed(IOException);

server.addListener(PyroServerListener);
=> public void acceptedClient(PyroClient);

client.addListener(PyroClientListener);
=> public void connectedClient(PyroClient);
=> public void unconnectableClient(PyroClient);
=> public void droppedClient(PyroClient, IOException);
=> public void disconnectedClient(PyroClient);
=> public void receivedData(PyroClient, ByteBuffer);
=> public void sentData(PyroClient, int);



Technically, the most important feature is that PyroNet now supports 'worker selectors', transparantly! That means that you can have a pool of NIO selectors over which the load is spread. This is useful for high-I/O, otherwise saturating a single CPU core.

It also fixes minor issues with SocketChannel.close() that happened every other thursday.


The JAR includes sourcecode: http://indiespot.net/files/pyronet_20100426.jar

An embedded usecase/unittest is a multithreaded SOCKS4 proxy server. Setup your browser to use a proxy, and browse away!


Will update the SVN in a few days.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #55 - Posted 2010-05-10 12:06:40 »

Finally found time to update the SVN, and made some stability updates too.

Socket timeouts for connect/read/write/close, now also work reliably.

To my amazement nonblocking channels screw up on those occasionally too, leading to resource leaks. PyroNet now efficiently tracks timeouts itself.

client.setTimeout(millis);

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Online CommanderKeith
« Reply #56 - Posted 2010-05-15 02:08:19 »

Nice work here, very useful.

Thanks for open-sourcing it.

By the way, are the files listed here different versions or just old releases?
http://code.google.com/p/pyronet/downloads/list

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #57 - Posted 2010-05-15 11:14:30 »

Thanks!

Regarding the downloads: just pick the highest number Smiley GoogleCode is very protective of *my* data, up to the point where I cannot (seem to) overwrite my own files. The latest version should match the SVN version.

Oh, and before exploring/using this NIO wrapper, you should really ask yourself whether you actually need NIO. The non-blocking nature makes your code hard to debug, full of callbacks and plain complex compared to old I/O... and if you're not handling thousands of connections, it's mainly a (fast) royal PITA.

I find I use it less and less myself persecutioncomplex There is actually one project remaining that I use it for: a cluster of datamining servers that is handling incoming data at 500 (new) connections per second. The Unix Load is 0.02, yay.

If you are not absolutely sure you need it, don't use it!

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: 1 [2]
  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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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!