Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
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  
  rainning socket  (Read 2022 times)
0 Members and 1 Guest are viewing this topic.
Offline vrm

Junior Duke




where I should sign ?


« Posted 2004-03-22 12:42:19 »

I'm looking in code of "Rainning Sockets" : http://jniosocket.sourceforge.net I wondering if somebody use it for his own game here, and if you got comments about it (blah³  ? Grin) . I'm starting to hack around for understand how it work and kick HTTP out for put my own connected protocol
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #1 - Posted 2004-03-22 13:00:20 »

Quote
I'm looking in code of "Rainning Sockets" : http://jniosocket.sourceforge.net I wondering if somebody use it for his own game here, and if you got comments about it (blah³  ? Grin) . I'm starting to hack around for understand how it work and kick HTTP out for put my own connected protocol


'fraid I haven't really used it, just scanned it. Seems a worthwhile project, and seems to work fairly well.

If you want advice on HTTP...this is the standard way to optimize HTTP for ultra-high performance:

  • Use asynchronous I/O
  • Use one-pass parsers (i.e. they read directly from the ByteBuffer, building the in-memory data structures as they go; they don't read the whole thing, convert to a string, read it all again as a string, parse it several times over)
  • Work out what parts of an HTTP response are unchanging on your server, and clump them into direct ByteBuffers. Use gathering writes of your response body's, using views of those direct BB's to write.
  • ...but watch out! If you don't use views then you can't send partial responses. Since you're using asynch I/O you probably will be sneding partial responses. If you can be sure each response will write atomically, you could actually use just one copy of each header buffer, and rewind it after each use.
  • ...and in the experiments I did last year, 1.4.1 gathering writes can be VERY slow for no apparent reason (read: Sun's NIO team screwed up on the implementation). There's a thread here somewhere which has profiling data - I made a small demo of gathering writes and people posted performance figures. Most people could write about 10 buffers at *almost* full speed, others could only get about 4. In all cases, non-gathering writes were at least as fast or faster. This was only a simple test, so all I'm saying is test carefully - at the moment we use a "faked" gathering write class which actually uses single writes; if/when we get a fast gathering write working, we can just replace that one class.
  • Investigate shceduling algos - these make a BIG difference. In particular, look up SRPT, which seems to provide most bang for your buck. Bear in mind though that this makes your network logic start to get pretty complex. I wouldn't bother unless you are trying to show off or have a real need for this.
  • Use caches, lots of caches. Multiple layers of caches. Webserver's have managed to serve millions of hits hourly running Perl (!) just by intelligently layering caches on top of each other so that as much as possible of each page is cached between hits (usually you have front-end caches that cache things that never change, like images. Behind that you have 2nd line caches that cache things that rarely change. Then another layer with things that often change, etc etc. Really this is just an over-simplified version of staged servers, allowing you to tune each stage separately).
  • use hardware where possible. SSL accelerators in particular have a big effect (encrypting and decrypting can really slow down a webserver)....but I'm guessing this isn't an option for you
  • load-balancing across multiple servers. Most requests are side-effect free, so you can serve each request to a different server without having to worry about synchronization etc.

malloc will be first against the wall when the revolution comes...
Offline vrm

Junior Duke




where I should sign ?


« Reply #2 - Posted 2004-03-22 13:08:07 »

well I don't want to use http, I  implement my own protocol (connecterd, not like HTTP)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #3 - Posted 2004-03-22 13:22:28 »

Quote
well I don't want to use http, I  implement my own protocol (connecterd, not like HTTP)


O, I C. It should be fairly obvious that HTTP optimization is quite specialized, so I wouldn't recommend that. The best you could hope for is that raining sockets wasn't HTTP biased, in which case it would probably be hardly any better than writing your own NIO server from scratch.

malloc will be first against the wall when the revolution comes...
Offline vrm

Junior Duke




where I should sign ?


« Reply #4 - Posted 2004-03-22 13:27:15 »

I whould like to make my own NIO server from scratch, but where is sun doc about it ? So I try to find a starting point (like a working framework)
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #5 - Posted 2004-03-22 13:58:05 »

Quote
I whould like to make my own NIO server from scratch, but where is sun doc about it ? So I try to find a starting point (like a working framework)


Have you looked at my NIO articles (incomplete, but still)? I updated them last in late February. There should be enough there to get a framework up and running (nb: I've had quite a few emails from people telling me they've used them successfully this way).

If there's some part that is lacking, let me know and I can try and improve it.

http://grexengine.com/sections/people/adam/ (I don't have a permanent direct link to the articles themselves yet - just scroll down to the section titled "articles").

malloc will be first against the wall when the revolution comes...
Offline vrm

Junior Duke




where I should sign ?


« Reply #6 - Posted 2004-03-22 14:17:54 »

yes looked at it (and printed it), it's a good start
It helped me a lot to get started, now I think I get the selector/key/non-blocking system after lot of trial/error

Didn't seen the Tetris part it's new ? going to read it.

My remark about the article is the lack of a simple examples for the first part (like a simple ECHO server)

for example I wondered if I needed to cancel the key or unregister the channel when I finished to read a packet and want to register the channel as OP_WRITE, that can sound obvious but a simple example could solved it in 2 sec.

I whould like to have more info about how it behave in multiple threads environement and example of implementation using the usual strategies (4 stages, etc..)

but if you give me time next week (need to finish my work travel) I think I can write a simple echo server:) I whould like to write doc about NIO & server when I finished to understand the whole block, but my english doesn't help me much Smiley
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #7 - Posted 2004-03-22 15:10:50 »

Quote

My remark about the article is the lack of a simple examples for the first part (like a simple ECHO server)


Yeah, that's been suggested a couple of times now; my plan is to write complete source code for the tetris game, and make the final article just a description of the source code plus javadocs link plus the downloadable source itself.

Just haven't had time Sad.

Quote

for example I wondered if I needed to cancel the key or unregister the channel when I finished to read a packet and want to register the channel as OP_WRITE, that can sound obvious but a simple example could solved it in 2 sec.


The different ops are independent. You can be registered for read and write simultaneously on one selector, or simultaneously on multiple selectors. They don't interact AT ALL, except that each key can only have one attachment.

Quote

but if you give me time next week (need to finish my work travel) I think I can write a simple echo server:) I whould like to write doc about NIO & server when I finished to understand the whole block, but my english doesn't help me much Smiley


If you (or anyone else) wants to compose the example snippets in the articles into complete working source I'm happy to help debug and advise on getting it working (and I can probably provide much of the necessary code for you) if I can then publish it in the article (with obvious credit to you of course). Best of all would be if someone would actually write a tiny tetris game as described in the article.

Part of the problem is that it's harder for me to write complete code because I have to be very careful not to copy code from my day job, and I find that difficult. So it's easy for me to contribute ideas and bits of code, and someone else will produce an example in their own "style" and I won't get into any trouble for giving away any of our classes Smiley.

malloc will be first against the wall when the revolution comes...
Offline vrm

Junior Duke




where I should sign ?


« Reply #8 - Posted 2004-03-23 06:44:09 »

ok made a simple Hello server, if you say him hello it replies, quite simple

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  
93  
94  
95  
96  
97  
98  
99  
100  
101  
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.WritableByteChannel;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HelloServer {
      private int port;
      private boolean running=false;
      private Selector selector;
     
      public HelloServer(int port) {
            this.port=port;      
      }
     
      public void start() {
            if(running)
                  return;
            try {
                  InetSocketAddress addr = new InetSocketAddress(port);
                  selector = Selector.open();
                  ServerSocketChannel channel = ServerSocketChannel.open();
                  channel.socket().bind(addr);
                  channel.configureBlocking(false);
                  channel.register(selector, SelectionKey.OP_ACCEPT);
                  running=true;
                  new Thread() {
                        public void run() {
                              try {
                                    while(running) {
                                          selector.select();
                                          Set keys = selector.selectedKeys();
                                          Iterator iter=keys.iterator();
                                          while(iter.hasNext()) {
                                                SelectionKey key=(SelectionKey) iter.next();
                                                iter.remove();
                                                System.out.println("Key incoming : "+key.interestOps());
                                                if(key.isAcceptable()) {
                                                      // we accept all the incoming connections
                                                      SocketChannel sock=((ServerSocketChannel) key.channel()).accept();
                                                      // we configure the new server socket
                                                      sock.configureBlocking(false);
                                                      // we register the channel for Byte reading
                                                      sock.register(selector,SelectionKey.OP_READ);
                                                } else if (key.isReadable()) {
                                                      // a byte buffer for reading datas
                                                      ByteBuffer buffer=ByteBuffer.allocate(1024);
                                                      int bytesRead=((ReadableByteChannel)key.channel()).read(buffer);
                                                      // if negative amount of data was recieved it means the client was disconnected (thx Sun's doc...)
                                                      if(bytesRead<0) {
                                                            key.cancel();
                                                            key.channel().close();
                                                      } else {
                                                            // I know it's bad to don't handle charsets :)
                                                            String textValue=new String(buffer.array());
                                                            textValue=textValue.trim();
                                                            System.out.println("Incoming data ("+bytesRead+") : "+textValue+"|");
                                                            if(textValue.equals("hello")) {
                                                                  System.out.println("Registering as writable");
                                                                  key.channel().register(selector,SelectionKey.OP_WRITE).attach("Hello too !\n");
                                                            }
                                                      }
                                                } else if(key.isWritable()) {
                                                      System.out.println("Writing : "+key.attachment());
                                                      // we write waitig datas to the socket
                                                      if(key.attachment() instanceof String) {

                                                            ((WritableByteChannel)key.channel()).write(ByteBuffer.wrap( ((String)key.attachment()).getBytes()));
                                                            key.attach(null);
                                                      }
                                                      key.channel().register(selector,SelectionKey.OP_READ);
                                                }

                                          }
                                    }
                              } catch(Exception e) {
                                    e.printStackTrace();
                              }
                        }
                  }.start();
            } catch(Exception e) {
                  e.printStackTrace();
            }
      }

      public void stop() {
            running=false;
      }

      public static void main(String[] args) {
            HelloServer service= new HelloServer(4005);
            service.start();
      }
}
Offline vrm

Junior Duke




where I should sign ?


« Reply #9 - Posted 2004-03-23 12:05:33 »

well there is few oddities, but it should be a start point for ppl like me who wondered how to attach things and dectect when a client disconnected
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.

theagentd (20 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (46 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (69 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (46 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!