# HG changeset patch # User e085711 # Date 1310096294 -32400 # Node ID 796f2b3b8665defae80a8fa8866217a3b282a3bc # Parent 6eb7d0c8f11d720b16661b772533d5ecc222de6b modify MyRfbProto.java diff -r 6eb7d0c8f11d -r 796f2b3b8665 src/myVncProxy/MyRfbProto.java --- a/src/myVncProxy/MyRfbProto.java Thu Jul 07 21:13:09 2011 +0900 +++ b/src/myVncProxy/MyRfbProto.java Fri Jul 08 12:38:14 2011 +0900 @@ -1,4 +1,5 @@ package myVncProxy; + import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage; @@ -16,6 +17,8 @@ import javax.imageio.ImageIO; +import myVncProxy.MulticastQueue.Client; + import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.io.OutputStream; @@ -23,8 +26,7 @@ class MyRfbProto extends RfbProto { final static String versionMsg_3_998 = "RFB 003.998\n"; - - + private int messageType; private int rectangles; private int rectX; @@ -33,47 +35,48 @@ private int rectH; private int encoding; private int zLen; - private int dataLen; + private int dataLen; private ServerSocket servSock; private int acceptPort; private byte initData[]; - private LinkedList cliListTmp; - private LinkedList cliList; - private LinkedList sendThreads; + private LinkedList cliListTmp; + private LinkedList cliList; + private LinkedList sendThreads; boolean createBimgFlag; - + ExecutorService executor; - - + byte[] pngBytes; - - MyRfbProto(String h, int p, VncViewer v ) throws IOException { + + private MulticastQueue multicastqueue = new MulticastQueue(); + + MyRfbProto(String h, int p, VncViewer v) throws IOException { super(h, p, v); - cliList = new LinkedList (); - cliListTmp = new LinkedList (); + cliList = new LinkedList(); + cliListTmp = new LinkedList(); createBimgFlag = false; - sendThreads = new LinkedList (); -// executor = Executors.newCachedThreadPool(); - executor = Executors.newSingleThreadExecutor(); + sendThreads = new LinkedList(); + // executor = Executors.newCachedThreadPool(); + // executor = Executors.newSingleThreadExecutor(); } MyRfbProto(String h, int p) throws IOException { super(h, p); - cliList = new LinkedList (); - cliListTmp = new LinkedList (); + cliList = new LinkedList(); + cliListTmp = new LinkedList(); createBimgFlag = false; - sendThreads = new LinkedList (); -// executor = Executors.newCachedThreadPool(); - executor = Executors.newSingleThreadExecutor(); + sendThreads = new LinkedList(); + // executor = Executors.newCachedThreadPool(); + // executor = Executors.newSingleThreadExecutor(); } // over write void writeVersionMsg() throws IOException { clientMajor = 3; if (serverMinor >= 9) { - clientMinor = 9; - os.write(versionMsg_3_998.getBytes()); + clientMinor = 9; + os.write(versionMsg_3_998.getBytes()); } else if (serverMajor > 3 || serverMinor >= 8) { clientMinor = 8; os.write(versionMsg_3_8.getBytes()); @@ -91,72 +94,74 @@ initCapabilities(); } - - - void initServSock(int port) throws IOException{ + void initServSock(int port) throws IOException { servSock = new ServerSocket(port); acceptPort = port; } - // 5550を開けるが、開いてないなら+1のポートを開ける。 - void selectPort(){ + + // 5550を開けるが、開いてないなら+1のポートを開ける。 + void selectPort() { int i = 5550; - while(true){ - try{ + while (true) { + try { initServSock(i); break; - }catch(BindException e){ + } catch (BindException e) { i++; continue; - }catch(IOException e){ + } catch (IOException e) { } } - System.out.println("accept port = "+i); + System.out.println("accept port = " + i); } - int getAcceptPort(){ + + int getAcceptPort() { return acceptPort; } + void setSoTimeout(int num) throws IOException { servSock.setSoTimeout(num); } - + Socket accept() throws IOException { return servSock.accept(); } - void addSock(Socket sock){ + void addSock(Socket sock) { cliList.add(sock); } - void addSockTmp(Socket sock){ - System.out.println("connected "+sock.getInetAddress()); + + void addSockTmp(Socket sock) { + System.out.println("connected " + sock.getInetAddress()); cliListTmp.add(sock); } - + void mark(int len) throws IOException { is.mark(len); } void reset() throws IOException { is.reset(); - } + } boolean markSupported() { return is.markSupported(); } - + void readServerInit() throws IOException { - + mark(255); skipBytes(20); int nlen = readU32(); - int blen = 20+4+nlen; + int blen = 20 + 4 + nlen; initData = new byte[blen]; reset(); mark(blen); readFully(initData); reset(); - + framebufferWidth = readU16(); framebufferHeight = readU16(); bitsPerPixel = readU8(); @@ -190,9 +195,10 @@ inNormalProtocol = true; } - void sendRfbVersion(OutputStream os) throws IOException{ - os.write(versionMsg_3_998.getBytes()); + void sendRfbVersion(OutputStream os) throws IOException { + os.write(versionMsg_3_998.getBytes()); } + void readVersionMsg(InputStream is) throws IOException { byte[] b = new byte[12]; @@ -214,108 +220,113 @@ if (serverMajor < 3) { throw new IOException( "RFB server does not support protocol version 3"); - } - - } + } + + } + void sendSecurityType(OutputStream os) throws IOException { // number-of-security-types os.write(1); - // security-types + // security-types // 1:None os.write(1); } + void readSecType(InputStream is) throws IOException { byte[] b = new byte[1]; is.read(b); } + void sendSecResult(OutputStream os) throws IOException { byte[] b = castIntByte(0); os.write(b); } - + void readClientInit(InputStream in) throws IOException { byte[] b = new byte[0]; in.read(b); } - - void sendInitData(OutputStream os) throws IOException{ - os.write(initData); + + void sendInitData(OutputStream os) throws IOException { + os.write(initData); } - void sendData(byte b[]){ - try{ - for(Socket cli : cliList){ - try{ - cli.getOutputStream().write(b, 0, b.length); - }catch(IOException e){ - // if socket closed - cliList.remove(cli); - } - } -// System.out.println("cliSize="+cliSize()); - }catch(Exception e){ + void sendData(byte b[]) { + try { + multicastqueue.put(b); + + /* + * // for(Socket cli : cliList){ // try{ // + * cli.getOutputStream().write(b, 0, b.length); // + * }catch(IOException e){ // // if socket closed // + * cliList.remove(cli); // } // } + */ + // System.out.println("cliSize="+cliSize()); + } catch (Exception e) { } } - - void sendPngImage(){ - try{ - for(Socket cli : cliListTmp){ - try{ + + void sendPngImage() { + try { + for (Socket cli : cliListTmp) { + try { sendPngData(cli); addSock(cli); - }catch(IOException e){ + } catch (IOException e) { // if socket closed cliListTmp.remove(cli); } } -// System.out.println("cliSize="+cliSize()); - }catch(Exception e){ + // System.out.println("cliSize="+cliSize()); + } catch (Exception e) { } cliListTmp.clear(); } - - boolean ready() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(is)); return br.ready(); - } + } - int cliSize(){ + int cliSize() { return cliList.size(); - } - void printNumBytesRead(){ - System.out.println("numBytesRead="+numBytesRead); - } + } + + void printNumBytesRead() { + System.out.println("numBytesRead=" + numBytesRead); + } + void bufResetSend(int size) throws IOException { reset(); int len = size; - if(available() < size ) + if (available() < size) len = available(); byte buffer[] = new byte[len]; readFully(buffer); sendData(buffer); } - void readSendData()throws IOException{ + + void readSendData() throws IOException { byte buffer[] = new byte[dataLen]; readFully(buffer); reset(); - for(Socket cli : cliList){ - try{ + for (Socket cli : cliList) { + try { OutputStream out = cli.getOutputStream(); executor.execute(new SendThread(out, buffer)); - }catch(IOException e){ + } catch (IOException e) { // if client socket closed cliListTmp.remove(cli); - }catch(Exception e){ - + } catch (Exception e) { + } } } - void regiFramebufferUpdate()throws IOException{ + + void regiFramebufferUpdate() throws IOException { mark(20); messageType = readU8(); skipBytes(1); @@ -325,26 +336,29 @@ rectW = readU16(); rectH = readU16(); encoding = readU32(); - if(encoding == 16) + if (encoding == 16) zLen = readU32(); reset(); } - void checkAndMark() throws IOException{ - switch(encoding){ + + void checkAndMark() throws IOException { + switch (encoding) { case RfbProto.EncodingRaw: dataLen = rectW * rectH * 4 + 16; mark(dataLen); break; case RfbProto.EncodingZRLE: - dataLen = zLen+20; + dataLen = zLen + 20; mark(dataLen); break; default: mark(1000000); - } + } } - BufferedImage createBufferedImage(Image img){ - BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB ); + + BufferedImage createBufferedImage(Image img) { + BufferedImage bimg = new BufferedImage(img.getWidth(null), + img.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics g = bimg.getGraphics(); g.drawImage(img, 0, 0, null); @@ -352,15 +366,17 @@ return bimg; } - void createPngBytes(BufferedImage bimg)throws IOException { - pngBytes = getImageBytes(bimg , "png"); + void createPngBytes(BufferedImage bimg) throws IOException { + pngBytes = getImageBytes(bimg, "png"); } - byte[] getBytes(BufferedImage img)throws IOException { + + byte[] getBytes(BufferedImage img) throws IOException { byte[] b = getImageBytes(img, "png"); return b; } - - byte[] getImageBytes(BufferedImage image, String imageFormat) throws IOException { + + byte[] getImageBytes(BufferedImage image, String imageFormat) + throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); BufferedOutputStream os = new BufferedOutputStream(bos); image.flush(); @@ -370,40 +386,78 @@ return bos.toByteArray(); } - void sendPngData(Socket sock)throws IOException{ + void sendPngData(Socket sock) throws IOException { byte[] dataLength = castIntByte(pngBytes.length); sock.getOutputStream().write(dataLength); sock.getOutputStream().write(pngBytes); } - byte[] castIntByte(int len){ + + byte[] castIntByte(int len) { byte[] b = new byte[4]; - b[0] = (byte)((len >>> 24 ) & 0xFF); - b[1] = (byte)((len >>> 16 ) & 0xFF); - b[2] = (byte)((len >>> 8 ) & 0xFF); - b[3] = (byte)((len >>> 0 ) & 0xFF); + b[0] = (byte) ((len >>> 24) & 0xFF); + b[1] = (byte) ((len >>> 16) & 0xFF); + b[2] = (byte) ((len >>> 8) & 0xFF); + b[3] = (byte) ((len >>> 0) & 0xFF); return b; } - - BufferedImage createBimg()throws IOException{ + + BufferedImage createBimg() throws IOException { BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes)); return bimg; } - void readPngData()throws IOException{ + + void readPngData() throws IOException { pngBytes = new byte[is.available()]; readFully(pngBytes); } - void printFramebufferUpdate(){ - + + void printFramebufferUpdate() { + System.out.println("messageType=" + messageType); - System.out.println("rectangles="+rectangles); + System.out.println("rectangles=" + rectangles); System.out.println("encoding=" + encoding); - switch(encoding){ + switch (encoding) { case RfbProto.EncodingRaw: - System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 + 16); + System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 + + 16); break; default: } } -} + void newClient(acceptThread acceptThread, final Socket newCli, + final OutputStream os, final InputStream is) throws IOException { + // createBimgFlag = true; + // rfb.addSockTmp(newCli); + // addSock(newCli); + final Client c = multicastqueue.newClient(); + Runnable sender = new Runnable() { + @Override + public void run() { + try { + // 初期接続確立の部分 + sendRfbVersion(os); + readVersionMsg(is); + sendSecurityType(os); + readSecType(is); + sendSecResult(os); + readClientInit(is); + sendInitData(os); + for (;;) { + byte[] b = c.poll(); + os.write(b, 0, b.length); + } + } catch (IOException e) { + //接続が切れた処理 + //lockしないと駄目 + // cliList.remove(newCli); + } + + } + + }; + new Thread(sender).start(); + + } +} diff -r 6eb7d0c8f11d -r 796f2b3b8665 src/myVncProxy/acceptThread.java --- a/src/myVncProxy/acceptThread.java Thu Jul 07 21:13:09 2011 +0900 +++ b/src/myVncProxy/acceptThread.java Fri Jul 08 12:38:14 2011 +0900 @@ -16,19 +16,11 @@ while (true) { try { Socket newCli = rfb.accept(); + OutputStream os = newCli.getOutputStream(); InputStream is = newCli.getInputStream(); - rfb.sendRfbVersion(os); - rfb.readVersionMsg(is); - rfb.sendSecurityType(os); - rfb.readSecType(is); - rfb.sendSecResult(os); - rfb.readClientInit(is); - rfb.sendInitData(os); - rfb.createBimgFlag = true; -// rfb.addSockTmp(newCli); - rfb.addSock(newCli); - } catch (IOException e) { + rfb.newClient(this, newCli, os, is); + } catch (IOException e) { e.printStackTrace(); System.out.println(e); }