I have read this tutorial several times,
http://grexengine.com/sections/externalgames/articles/Adam%20Martin-Java%20NIO%20Networking%20for%20Games-2.html as well as others, and came up with this, I would like you to tell me if I am doing anything wrong as well as answering some points I still don't get (check comments).
What I think is having the User class hold the data (ArrayList<String> TxMessages;) to be sent to that user. Using a Hasmap<SelectionKey, User> to find out who's user the selectionkey belongs to I guess.
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| public class NIOClass {
public NIOCLass (int port) { ConnSelector = Selector.open(); RxSelector = Selector.open(); TxSelector = Selector.open(); charset = Charset.forName("UTF-8"); TxPort = port; RxPort = port; init(); }
public void init() { ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.configureBlocking( false );
ServerSocket ss = ssc.socket(); InetSocketAddress address = new InetSocketAddress(RxPort); ss.bind(address); ssc.register(ConnSelector, SelectionKey.OP_ACCEPT); ssc.register(RxSelector, SelectionKey.OP_READ); ssc.register(TxSelector, SelectionKey.OP_WRITE); } public void serve (int minimum_time, int maximum_time) { long currentTime = System.currentTimeMillis(); long minTime = currentTime + minimum_time; long maxTime = currentTime + maximum_time; boolean keepGoing = true; while (keepGoing) { ConnSelector.selectNow(); Set keys = ConnSelector.selectedKeys(); if(keys.isEmpty()) { TxSelector.selectNow(); keys = TxSelector.selectedKeys(); }; if(keys.isEmpty()) { RxSelector.selectNow(); keys = RxSelector.selectedKeys(); } Iterator it = keys.iterator(); while (keepGoing) { if(it.hasNext()) { SelectionKey key = (SelectionKey)it.next(); it.remove(); if(key.isAcceptable()) { ServerSocketChannel ssc = (ServerSocketChannel)key.channel(); SocketChannel sc = ssc.accept(); sc.configureBlocking(false); sc.socket().setTcpNoDelay(true); keys.add(sc.register(RxSelector, SelectionKey.OP_READ)); sc.register(TxSelector, SelectionKey.OP_WRITE); } else if(key.isWritable()) { String message; ByteBuffer bb = charset.encode(message); WritableByteChannel wbc = (WritableByteChannel)key.channel(); SocketChannel sc; wbc.write(bb); } else if(key.isReadable()) { ReadableByteChannel rbc = (ReadableByteChannel) key.channel(); ByteBuffer bb = null; bb = (ByteBuffer) key.attachment(); if(bb == null) { bb = ByteBuffer.allocate(1024); key.attach(bb); } else { bb.limit(bb.capacity()); } int numBytesRead = rbc.read(bb); if(numBytesRead < 0) { key.cancel(); key.channel().close(); } else { while( true ) { } }
rbc.read(bb); String text = charset.decode(bb).toString(); } }; currentTime = System.currentTimeMillis(); if(currentTime > minTime) keepGoing = false; if(currentTime > maxTime) keepGoing = false; } } return; } public void write(User user, String message) { }
private int TxPort; private int RxPort; private java.nio.channels.Selector ConnSelector; private java.nio.channels.Selector RxSelector; private java.nio.channels.Selector TxSelector; Charset charset; } |