Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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 Wrapper (TCP / UDP)  (Read 8318 times)
0 Members and 1 Guest are viewing this topic.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Posted 2008-05-29 23:22:58 »

I've been talking about it for quite some time, and I think I got something that nicely wraps NIO into something workable.


You send and receive byte-arrays (!) not ByteBuffers. Because it is just a NIO-wrapper, you have full access to the SelectionKey instances, just in case you need more control.



The basic idea is this:
1  
2  
3  
4  
5  
6  
7  
Network network = new Network(...);
SelectionKey server = network.createServer(port);
SelectionKey client = network.createClient(host, port);
// you can ignore these SelectionKeys if you wish

network.write(client, yourByteArray);
network.write(client, yourByteArray);


Event-handling is done via the listener pattern:
1  
2  
3  
4  
5  
6  
7  
8  
9  
public interface ConnectionHandler
{
   public void onConnected(Network net, SelectionKey key);
   public void onExecute(Network net, SelectionKey key);
   public void onReceivedTCP(Network net, SelectionKey key, byte[] data);
   public void onReceivedUDP(Network net, SelectionKey key, byte[] data, InetSocketAddress source);
   public void onSent(Network net, SelectionKey key, int byteCount);
   public void onDisconnected(Network net, SelectionKey key, IOException cause);
}




http://213.247.55.3/~balk1242/html_stuff/ for a convenient listing of packages and classes, I added most of my utility classes, and the 'relevant' packages are near the bottom of the listing.


NIO Wrapper (implementation)
http://213.247.55.3/~balk1242/html_stuff/source/jawnae/net/Network.html


Examples:
http://213.247.55.3/~balk1242/html_stuff/source/test/jawnae/net/EchoServer.html
http://213.247.55.3/~balk1242/html_stuff/source/test/jawnae/net/EchoClient.html

http://213.247.55.3/~balk1242/html_stuff/source/test/jawnae/net/PacketServer.html
http://213.247.55.3/~balk1242/html_stuff/source/test/jawnae/net/PacketClient.html


I built a toy HTTP server with this wrapper, so I am reasonably confident about its stability.


Give it a whirl, tell me what you think!

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

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #1 - Posted 2008-05-30 05:45:32 »

looks lovely! - I've always hated nio for being too technical to use - this cleans it up quite a bit!

Offline kevglass

JGO Kernel


Medals: 159
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #2 - Posted 2008-05-30 08:18:21 »

It does look great. Nice work Riven. What license is applied?

Kev

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

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #3 - Posted 2008-05-30 09:45:52 »

Really nice, thanks for sharing!
I'd be interested to know about the license too.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2008-05-30 16:16:57 »

I don't know about the license.
I don't care about the license.

As long as you don't sue me, or say you wrote it.


(Does that mean BSD?)


Edit
---------


Oh, and the code is under construction, so... it will change (for the better) soon.


Just one question: I put TCP and UDP in the same class (Network)

// for TCP
network.write(selectionKey, byte[] data); // packets will be merged / split for you

// for UDP
network.write(selectionKey, byte[] data, InetSocketAddress target); // packets will be sent 'as is'.


You receive them in The ConnectionHandler using:
handler.onReceivedTCP(selectionKey, byte[] data); // needs to be split/merged
handler.onReceivedUDP(selectionKey, byte[] data, InetSocketAddres source); // packets are received 'as is'


It's all nice and will, but it's a *bit* messy.




Any suggestions? What needs to be worked on?

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

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #5 - Posted 2008-05-30 18:26:30 »

I don't know about the license.
I don't care about the license.

As long as you don't sue me, or say you wrote it.


(Does that mean BSD?)
yup, basically.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2008-05-30 19:28:42 »

Cleaned up TCP examples...!


Now using these classes
- jawnae.net.Network
- jawnae.net.NetworkHandler
- jawnae.net.NetworkHandlerProvider
---> jawnae.net.TCPHandler (implements NetworkHandler, removes boilerplate for TCP)
---> jawnae.net.UDPHandler (implements NetworkHandler, removes boilerplate for UDP)
source: http://213.247.55.3/~balk1242/html_stuff/


UDP examples:
http://213.247.55.3/~balk1242/html_stuff/source/test/jawnae/net/NibbleServer.html
http://213.247.55.3/~balk1242/html_stuff/source/test/jawnae/net/NibbleClient.html

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline EgonOlsen
« Reply #7 - Posted 2008-05-31 10:40:55 »

Nice. How about a zip that contains all the required files and examples/test for this? It's a bit awkward to copy all you need to use it from that html client.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2008-05-31 13:14:31 »

Fair enough..

The JARs are at the bottom of the listing now, containing both *.java and *.class files.

http://213.247.55.3/~balk1242/html_stuff/jars/base.jar
http://213.247.55.3/~balk1242/html_stuff/jars/components.jar
http://213.247.55.3/~balk1242/html_stuff/jars/jawnae.jar


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline EgonOlsen
« Reply #9 - Posted 2008-06-02 15:30:06 »

Thank you very much. It's much more convenient that way.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2008-06-02 16:50:29 »

Needless to say, I'd love to get some feedback after you (all) played with it Smiley

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

Senior Newbie




oh goody....java games


« Reply #11 - Posted 2008-11-18 14:19:55 »

I am not trying to rain on your parade Riven.

Have any of you checked out: http://mina.apache.org/

On Mina's Homepage:
Quote
Apache MINA is a network application framework which helps users develop high performance and high scalability network applications easily. It provides an abstract · event-driven · asynchronous API over various transports such as TCP/IP and UDP/IP via Java NIO.

I haven't put forth the time to check it out, but it seems like a pretty nice library.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2008-11-18 17:49:41 »

Ofcourse. But MINA does stuff under the hood (resolving IPs on connecting) that
trigger SecurityExceptions when the default (applet) SecurityManager is installed.

Besides that, my API is extremely light weight.


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

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #13 - Posted 2008-11-18 19:16:43 »

Oh awesome. I think I might put this to use very soon.

See my work:
OTC Software
Offline karbon135

Innocent Bystander





« Reply #14 - Posted 2008-11-18 19:51:16 »

Thanks for sharing this!

Some months ago I have been looking for a solid and easy to use NIO-based networking solution and I took a look at MINA, Grizzly and xsocket. I selected xsocket (http://xsocket.sourceforge.net/) and have been developing a multiplayer game server on it for the last months - it offers a straight-forward API and I'm quite happy with it... Have you checked this one? How does it compare to your project?

Many thanks again for sharing the knowledge!
Offline OverKill

Junior Member




Java games rock!


« Reply #15 - Posted 2008-11-19 09:11:41 »

@Mina and a little OT:
There is also the JBoss Netty project (which is by the same author as Mina, the origins are very Soap-Opera-ish).

Maybe we can split off this section into another topic?
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.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

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

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

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

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

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

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

BurntPizza (49 views)
2014-08-09 21:09:32
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!