# HG changeset patch # User one # Date 1336304151 -32400 # Node ID a14076dac5034f61617d1e541dcae3a5fbefaffe # Parent f77309fa8a9ca7a81aa442d0580d032c677a1cf4 add DataInputStream2.java diff -r f77309fa8a9c -r a14076dac503 src/treeVnc/DataInputStream1.java --- a/src/treeVnc/DataInputStream1.java Sun May 06 15:07:46 2012 +0900 +++ b/src/treeVnc/DataInputStream1.java Sun May 06 20:35:51 2012 +0900 @@ -1,64 +1,89 @@ package treeVnc; import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; +import java.net.DatagramSocket; +import java.net.Socket; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; +import java.nio.channels.spi.AbstractSelectableChannel; public class DataInputStream1 implements MyDataInputStream { - DataInputStream is; - public DataInputStream1(BufferedInputStream bufferedInputStream) { - is = new DataInputStream(bufferedInputStream); + ByteBuffer buf; + + private SocketChannel channel; + public DataInputStream1(Socket sock) { + channel = sock.getChannel(); } + + + public DataInputStream1() { + } + + + @Override public void readFully(byte[] b, int off, int len) throws IOException { - is.readFully(b, off, len); + long count = 0; + ByteBuffer[] bf = {ByteBuffer.wrap(b)}; + while(count < len) { + long i = channel.read(bf, off, len); + if(i==-1) { + throw new IOException(); + } + count += i; + } + buf = bf[0]; } @Override public int available() throws IOException { - return is.available(); + return buf.remaining(); } @Override public int skipBytes(int n) throws IOException { - return is.skipBytes(n); + return buf.position(buf.position() + n).position(); } @Override public int readUnsignedByte() throws IOException { - return is.readUnsignedByte(); + return buf.get() & 0xff; } @Override public int readUnsignedShort() throws IOException { - return is.readUnsignedShort(); + return readUnsignedByte() * 256 + readUnsignedByte(); } @Override public int readInt() throws IOException { - return is.readInt(); + return buf.getInt(); } @Override public void read(byte[] headBuf) throws IOException { - is.read(headBuf); + for(int i = 0 ; i < headBuf.length ; i++) { + headBuf[i] = buf.get(); + } } @Override public boolean markSupported() { - return is.markSupported(); + return true; } @Override public void mark(int i) { - is.mark(i); + buf.position(i).mark(); } @Override public void reset() throws IOException { - is.reset(); - + buf.reset(); } } diff -r f77309fa8a9c -r a14076dac503 src/treeVnc/MyRfbProtoClient.java --- a/src/treeVnc/MyRfbProtoClient.java Sun May 06 15:07:46 2012 +0900 +++ b/src/treeVnc/MyRfbProtoClient.java Sun May 06 20:35:51 2012 +0900 @@ -14,6 +14,9 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.net.BindException; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.MulticastSocket; import java.net.ServerSocket; import java.net.Socket; import java.nio.ByteBuffer; @@ -91,7 +94,7 @@ } MyRfbProtoClient(String h, int p, MyVncClient v) throws IOException { - super(h, p); + super(h, p, v); this.viewer = v; cliList = new LinkedList(); cliListTmp = new LinkedList(); @@ -102,6 +105,17 @@ // executor = Executors.newSingleThreadExecutor(); } + MyRfbProtoClient(String h, int p, boolean b) throws IOException { + super(h, p, b); + cliList = new LinkedList(); + cliListTmp = new LinkedList(); + createBimgFlag = false; + proxyFlag = false; + // sendThreads = new LinkedList(); + // executor = Executors.newCachedThreadPool(); + // executor = Executors.newSingleThreadExecutor(); + } + MyRfbProtoClient(String h, int p, CuiMyVncClient v) throws IOException { super(h, p); this.viewer = v; @@ -161,8 +175,7 @@ port = p; sock = new Socket(host, port); - is = new DataInputStream1(new BufferedInputStream(sock.getInputStream(), - 16384)); + is = new DataInputStream1(sock); os = sock.getOutputStream(); timing = false; @@ -402,8 +415,7 @@ void printNumBytesRead() { System.out.println("numBytesRead=" + numBytesRead); } - - + void regiFramebufferUpdate() throws IOException { is.mark(20); diff -r f77309fa8a9c -r a14076dac503 src/treeVnc/MyRfbProtoProxy.java --- a/src/treeVnc/MyRfbProtoProxy.java Sun May 06 15:07:46 2012 +0900 +++ b/src/treeVnc/MyRfbProtoProxy.java Sun May 06 20:35:51 2012 +0900 @@ -976,7 +976,7 @@ // System.out.println("Multicast = " + mcastAddr); // while(buf != null) { // int len = System.in.read(buf); - sendPacket = new DatagramPacket(buf, len, mAddr, port); + sendPacket = new DatagramPacket(buf, len - off, mAddr, port); soc.send(sendPacket); // } soc.close(); @@ -991,7 +991,8 @@ throws IOException { while (inputIndex < bufs.size()) { ByteBuffer b = bufs.get(inputIndex++); - broadcastCommunication(b.array(),b.position(),b.limit()); + if(b.limit()-b.position()<64000) + broadcastCommunication(b.array(),b.position(),b.limit());System.out.println("send"); //os.write(b.array(), b.position(), b.limit()); } os.flush(); @@ -1151,17 +1152,18 @@ // int high = rectH / 4; // System.out.println(INFLATE_BUFSIZE * (input.size() - 1)+ // input.getLast().limit()); + int dataSize = 64; int width = header.getShort(8); int height = header.getShort(10); int y = header.getShort(6); - int dataLen = width * 64 * 3 * 2; + int dataLen = width * 64 * 3; int temp = 0, preserv = 0; - int count = height / 128; + int count = height / dataSize; if (width % 64 == 0) - dataLen += (width / 64) * 2; + dataLen += (width / 64); else - dataLen += (((width / 64) + 1) * 2); + dataLen += (width / 64) + 1; for (int i = 0; i < count; i++) { int tempDataLen = dataLen - temp; @@ -1172,7 +1174,7 @@ } if (tempDataLen == INFLATE_BUFSIZE) { output.addLast(input.poll()); - createHeader(header, i, height, y); + createHeader(header, i, height, y,dataSize); if(header.getShort(10) > 1080) { System.out.println("error"); } @@ -1189,7 +1191,7 @@ tempBuf.get(buf1.array(), 0, tempDataLen); buf1.limit(tempDataLen); output.addLast(buf1); - createHeader(header, i, height, y); + createHeader(header, i, height, y,dataSize); zipSplitData(header, output); output.clear(); // checkPallet(output); @@ -1214,7 +1216,7 @@ // checkPallet(output); if(count==0) preserv -= 1; - createHeader(header, preserv + 1, height, y); + createHeader(header, preserv + 1, height, y,dataSize); zipSplitData(header, output); output.clear(); @@ -1225,18 +1227,7 @@ throws IOException, DataFormatException { splitBuffer(input, header); - // for (int i = 0; i < (rectH / 128) + 1; i++) { - // LinkedList tempBuf = new LinkedList(); - // while (buf.peek() != null) { - // tempBuf.addLast(buf.poll()); - // } - // // Deflater nDeflater = deflater; - // zipSplitData(header, tempBuf); - // // System.out.println("pass"); - // if (buf.size() != 0) - // buf.remove(); - // } - // System.out.println("throw"); + } private void zipSplitData(ByteBuffer header, LinkedList tempBuf) @@ -1245,6 +1236,8 @@ LinkedList bufs = new LinkedList(); int len2 = zip(nDeflater, tempBuf, 0, bufs); + //if(len2 > 64000) + //System.out.println("too long"+len2); ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip(); @@ -1262,15 +1255,16 @@ int h = header.getShort(10); // System.err.println("p:w="+w+" h="+h); assert(w>0); + assert(h>0); } - private void createHeader(ByteBuffer header, int count, int h, int y) { - int rH = Math.min(128, h - (128 * count)); - int rY = y + (128 * count); + private void createHeader(ByteBuffer header, int count, int h, int y,int size) { + int rH = Math.min(size, h - (size * count)); + int rY = y + (size * count); if(rH<0) { - rY += 128; + rY += size; } header.putShort(10, (short)rH); header.putShort(6, (short)rY); diff -r f77309fa8a9c -r a14076dac503 src/treeVnc/MyVncClient.java --- a/src/treeVnc/MyVncClient.java Sun May 06 15:07:46 2012 +0900 +++ b/src/treeVnc/MyVncClient.java Sun May 06 20:35:51 2012 +0900 @@ -130,6 +130,14 @@ readParameters(); +// try { +// rfb = new MyRfbProtoClient(pHost, port, true); +// rfb.initOnce(); +// //rfb.close(); +// } catch (IOException e1) { +// e1.printStackTrace(); +// } + refApplet = this; if (inSeparateFrame) { diff -r f77309fa8a9c -r a14076dac503 src/treeVnc/ReadHandler.java --- a/src/treeVnc/ReadHandler.java Sun May 06 15:07:46 2012 +0900 +++ b/src/treeVnc/ReadHandler.java Sun May 06 20:35:51 2012 +0900 @@ -1,26 +1,21 @@ package treeVnc; -import java.nio.channels.SocketChannel; import java.nio.channels.spi.AbstractSelectableChannel; public class ReadHandler { - private AbstractSelectableChannel channel; + private RfbProto rfb; public ReadHandler(RfbProto rfbProto, AbstractSelectableChannel ssChannel) { - this.setChannel(ssChannel); - } - - public ReadHandler(InputStreamTest inputStreamTest, SocketChannel ssChannel) { - this.setChannel(ssChannel); + this.rfb = rfbProto; } - public AbstractSelectableChannel getChannel() { - return channel; - } - public void setChannel(AbstractSelectableChannel channel) { - this.channel = channel; + + + public void exec() { + } + } diff -r f77309fa8a9c -r a14076dac503 src/treeVnc/RfbProto.java --- a/src/treeVnc/RfbProto.java Sun May 06 15:07:46 2012 +0900 +++ b/src/treeVnc/RfbProto.java Sun May 06 20:35:51 2012 +0900 @@ -28,7 +28,9 @@ import java.io.*; import java.awt.event.*; import java.net.DatagramSocket; +import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.NetworkInterface; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; @@ -38,6 +40,7 @@ import java.nio.channels.SocketChannel; import java.nio.channels.spi.AbstractSelector; import java.nio.channels.spi.SelectorProvider; +import java.util.Enumeration; import java.util.zip.*; public class RfbProto { @@ -191,6 +194,18 @@ // private boolean closed; protected boolean closed; + + private DatagramSocket broadcast; + + + public DataInputStream1 ib; + + + public OutputStream ob; + + + public DataInputStream1 iss; + // // Constructor. Make TCP connection to RFB server. // @@ -215,24 +230,25 @@ throw new IOException(e.getMessage()); } } - is = new DataInputStream1(new BufferedInputStream(sock.getInputStream(), - 16384)); - os = sock.getOutputStream(); - - timing = false; - timeWaitedIn100us = 5; - timedKbits = 0; + createSocket(h,p); } RfbProto(String h, int p) throws IOException { + createSocket(h, p); + } + + private void createSocket(String h, int p) throws UnknownHostException, + IOException { host = h; port = p; - sock = new Socket(host, port); - is = new DataInputStream1(new BufferedInputStream(sock.getInputStream(), - 16384)); + sock = newSocket(host, port); + iss = is = new DataInputStream1(sock); os = sock.getOutputStream(); + broadcast = newDatagramSocket(host,port); + ib = new DataInputStream2(broadcast); + ob = sock.getOutputStream(); timing = false; timeWaitedIn100us = 5; timedKbits = 0; @@ -262,17 +278,35 @@ // this should work for IPv6/IPv4 dual stack // check this using netstat -an result tcp46. try { - InetSocketAddress address = new InetSocketAddress(host, port); - ssChannel.socket().bind(address); + Enumeration i = NetworkInterface.getNetworkInterfaces(); + while(i.hasMoreElements()) { + NetworkInterface n = i.nextElement(); + InetAddress a = n.getInterfaceAddresses().get(0).getBroadcast(); + ssChannel.socket().bind(new InetSocketAddress(a,port)); + } } catch (SocketException e) { // for some bad IPv6 implementation ssChannel.socket().bind(new InetSocketAddress(port)); } ssChannel.configureBlocking(false); - ssChannel.register(selector, SelectionKey.OP_READ, new ReadHandler(this, ssChannel)); + ssChannel.register(selector, SelectionKey.OP_READ, new ReadBroadcastHandler(this, ssChannel)); return ssChannel.socket(); } + public void select() { + try { + selector.select(); + for(SelectionKey k : selector.selectedKeys()) { + ReadHandler h = (ReadHandler)k.attachment(); + h.exec(); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + public RfbProto() { } @@ -280,14 +314,8 @@ public RfbProto(String h, int p, boolean b) throws UnknownHostException, IOException { host = h; port = p; - sock = new Socket(host, port); - is = new DataInputStream1(new BufferedInputStream(sock.getInputStream(), - 16384)); - os = sock.getOutputStream(); - - timing = false; - timeWaitedIn100us = 5; - timedKbits = 0; + sock = newSocket(host, port); + createSocket(h,p); } public void initOnce() throws IOException { @@ -298,13 +326,8 @@ host = h; sock=null; sock = newSocket(host, port); - is = new DataInputStream1(new BufferedInputStream(sock.getInputStream(), - 16384)); - os = sock.getOutputStream(); + createSocket(h,port); - timing = false; - timeWaitedIn100us = 5; - timedKbits = 0; } diff -r f77309fa8a9c -r a14076dac503 src/treeVnc/VncCanvas.java --- a/src/treeVnc/VncCanvas.java Sun May 06 15:07:46 2012 +0900 +++ b/src/treeVnc/VncCanvas.java Sun May 06 20:35:51 2012 +0900 @@ -393,7 +393,7 @@ long count = 0; // new Thread(){public void run() {TestComet.main(null);}}.start(); - + // insert into changeIs() while (true) { // System.out.println("\ncount=" + count); @@ -406,7 +406,8 @@ * */ //rfbc.sendDataCheckDelay(); - rfbc.sendDataToClient(); + rfbc.sendDataToClient(); + rfbc.select(); // rfb.printNumBytesRead(); long bufSize = rfbc.getNumBytesRead();