comparison src/main/java/jp/ac/u_ryukyu/treevnc/server/MyRfbProtoProxy.java @ 16:e654b2e4de64

add TreeTask.java
author one
date Mon, 27 Aug 2012 06:31:14 +0900
parents 5d43194fdc51
children 718cdde720d4
comparison
equal deleted inserted replaced
15:5d43194fdc51 16:e654b2e4de64
24 import org.junit.Test; 24 import org.junit.Test;
25 25
26 import com.glavsoft.exceptions.TransportException; 26 import com.glavsoft.exceptions.TransportException;
27 import com.glavsoft.rfb.encoding.EncodingType; 27 import com.glavsoft.rfb.encoding.EncodingType;
28 import com.glavsoft.transport.Reader; 28 import com.glavsoft.transport.Reader;
29 29 import com.glavsoft.transport.Writer;
30 30
31 //import myVncProxy.MulticastQueue.Client; 31 //import myVncProxy.MulticastQueue.Client;
32 32
33 import java.util.concurrent.ExecutorService; 33 import java.util.concurrent.ExecutorService;
34 import java.util.concurrent.atomic.AtomicInteger; 34 import java.util.concurrent.atomic.AtomicInteger;
45 * CheckMillis is one of new msgType for RFB 3.855. 45 * CheckMillis is one of new msgType for RFB 3.855.
46 */ 46 */
47 final static byte SpeedCheckMillis = 4; 47 final static byte SpeedCheckMillis = 4;
48 final static int FramebufferUpdate = 0; 48 final static int FramebufferUpdate = 0;
49 final static int CheckDelay = 11; 49 final static int CheckDelay = 11;
50 final static int FramebufferUpdateRequest = 3;
50 51
51 // Secyrity type of OS X 52 // Secyrity type of OS X
52 final static int SecTypeReqAccess = 32; 53 final static int SecTypeReqAccess = 32;
53 54
54 // Supported authentication types 55 // Supported authentication types
64 private int rectW; 65 private int rectW;
65 private int rectH; 66 private int rectH;
66 private int encoding; 67 private int encoding;
67 private int zLen; 68 private int zLen;
68 private boolean clicomp = false; 69 private boolean clicomp = false;
69
70 70
71 private ServerSocket servSock; 71 private ServerSocket servSock;
72 protected int acceptPort; 72 protected int acceptPort;
73 // private byte initData[]; 73 // private byte initData[];
74 byte initData[]; 74 byte initData[];
75 private LinkedList<Socket> cliListTmp; 75 private LinkedList<Socket> cliListTmp;
76 private LinkedList<Socket> cliList; 76 private LinkedList<Socket> cliList;
77 boolean createBimgFlag; 77 boolean createBimgFlag;
78 boolean proxyFlag = true; 78 boolean proxyFlag = true;
79 private Reader reader; 79 private Reader reader;
80 private Writer writer;
80 ExecutorService executor; 81 ExecutorService executor;
81 82
82 byte[] pngBytes; 83 byte[] pngBytes;
83 84
84 // private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new 85 // private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new
91 // private Thread requestThread; 92 // private Thread requestThread;
92 private RequestScreenThread rThread; 93 private RequestScreenThread rThread;
93 private Thread requestThread; 94 private Thread requestThread;
94 95
95 public MyRfbProtoProxy() { 96 public MyRfbProtoProxy() {
96 } 97 rThread = new RequestScreenThread(this);
97 98 requestThread = new Thread(rThread);
98 public void setReader(Reader _reader) { 99 }
100
101 public void setStream(Reader _reader, Writer _writer) {
99 reader = _reader; 102 reader = _reader;
103 writer = _writer;
100 } 104 }
101 105
102 void initServSock(int port) throws IOException { 106 void initServSock(int port) throws IOException {
103 servSock = new ServerSocket(port); 107 servSock = new ServerSocket(port);
104 acceptPort = port; 108 acceptPort = port;
156 changeStatusFlag(); 160 changeStatusFlag();
157 } else { 161 } else {
158 changeStatusFlag(); 162 changeStatusFlag();
159 } 163 }
160 } 164 }
161
162 165
163 void requestThreadStart() { 166 void requestThreadStart() {
164 requestThread.start(); 167 requestThread.start();
165 } 168 }
166 169
281 * compression, but it is not accepted well in zlib continuous 284 * compression, but it is not accepted well in zlib continuous
282 * reading. So we need new Encoding ZRLEE which reset decoder 285 * reading. So we need new Encoding ZRLEE which reset decoder
283 * for each packet. ZRLEE can be invisible from user, but it 286 * for each packet. ZRLEE can be invisible from user, but it
284 * have to be implemented in the clients. ZRLEE compression is 287 * have to be implemented in the clients. ZRLEE compression is
285 * not context dependent, so no recompression is necessary. 288 * not context dependent, so no recompression is necessary.
286 * @throws TransportException 289 * @throws TransportException
287 */ 290 */
288 291
289 void readSendData(int dataLen) throws IOException, DataFormatException, TransportException { 292 void readSendData(int dataLen) throws IOException, DataFormatException,
293 TransportException {
290 LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>(); 294 LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>();
291 ByteBuffer header = ByteBuffer.allocate(16); 295 ByteBuffer header = ByteBuffer.allocate(16);
292 reader.readBytes(header.array(), 0, 16); 296 reader.readBytes(header.array(), 0, 16);
293 header.limit(16); 297 header.limit(16);
294 if (header.get(0) == FramebufferUpdate) { 298 if (header.get(0) == FramebufferUpdate) {
295 int encoding = header.getInt(12); 299 int encoding = header.getInt(12);
296 if (encoding == EncodingType.ZRLE.getId() 300 if (encoding == EncodingType.ZRLE.getId()
297 || encoding == EncodingType.ZLIB.getId()) { // ZRLEE is already 301 || encoding == EncodingType.ZLIB.getId()) { // ZRLEE is
298 // recompressed 302 // already
303 // recompressed
299 ByteBuffer len = ByteBuffer.allocate(4); 304 ByteBuffer len = ByteBuffer.allocate(4);
300 reader.readBytes(len.array(), 0, 4); 305 reader.readBytes(len.array(), 0, 4);
301 len.limit(4); 306 len.limit(4);
302 ByteBuffer inputData = ByteBuffer.allocate(dataLen - 20); 307 ByteBuffer inputData = ByteBuffer.allocate(dataLen - 20);
303 308
304 reader.readBytes(inputData.array(), 0, inputData.capacity()); 309 reader.readBytes(inputData.array(), 0, inputData.capacity());
305 // System.out.println(dataLen); 310 // System.out.println(dataLen);
306 inputData.limit(dataLen - 20); 311 inputData.limit(dataLen - 20);
307 312
308 LinkedList<ByteBuffer> inputs = new LinkedList<ByteBuffer>(); 313 LinkedList<ByteBuffer> inputs = new LinkedList<ByteBuffer>();
309 inputs.add(inputData); 314 inputs.add(inputData);
310 315
311 header.putInt(12, EncodingType.ZRLEE.getId()); // means recompress 316 header.putInt(12, EncodingType.ZRLEE.getId()); // means
312 // every time 317 // recompress
318 // every time
313 // using new Deflecter every time is incompatible with the 319 // using new Deflecter every time is incompatible with the
314 // protocol, clients have to be modified. 320 // protocol, clients have to be modified.
315 Deflater nDeflater = deflater; // new Deflater(); 321 Deflater nDeflater = deflater; // new Deflater();
316 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); 322 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>();
317 unzip(inflater, inputs, 0, out, INFLATE_BUFSIZE); 323 unzip(inflater, inputs, 0, out, INFLATE_BUFSIZE);
321 blen.putInt(len2); 327 blen.putInt(len2);
322 blen.flip(); 328 blen.flip();
323 bufs.addFirst(blen); 329 bufs.addFirst(blen);
324 330
325 bufs.addFirst(header); 331 bufs.addFirst(header);
326 // if(dataLen<=64000) 332 // if(dataLen<=64000)
327 multicastqueue.put(bufs); 333 multicastqueue.put(bufs);
328 // is.reset(); 334 // is.reset();
329 335
330 /* 336 /*
331 * System.out.println("ZRLE = "+dataLen); 337 * System.out.println("ZRLE = "+dataLen);
332 * System.out.println("ZRLEE = "+(len2+20)); float mag = 338 * System.out.println("ZRLEE = "+(len2+20)); float mag =
456 sendInitData(os); 462 sendInitData(os);
457 new Thread(reader).start(); // discard incoming packet here 463 new Thread(reader).start(); // discard incoming packet here
458 // after. 464 // after.
459 // writeFramebufferUpdateRequest(0,0, framebufferWidth, 465 // writeFramebufferUpdateRequest(0,0, framebufferWidth,
460 // framebufferHeight, false ); 466 // framebufferHeight, false );
461 int i = 0; 467 int i = 0;
462 for (;;) { 468 for (;;) {
463 LinkedList<ByteBuffer> bufs = c.poll(); 469 LinkedList<ByteBuffer> bufs = c.poll();
464 int inputIndex = 0; 470 int inputIndex = 0;
465 ByteBuffer header = bufs.get(inputIndex); 471 ByteBuffer header = bufs.get(inputIndex);
466 if (header == null) 472 if (header == null)
470 continue; 476 continue;
471 } else if (header.get(0) == FramebufferUpdate) { 477 } else if (header.get(0) == FramebufferUpdate) {
472 // System.out.println("client "+ myId); 478 // System.out.println("client "+ myId);
473 } 479 }
474 /* 480 /*
475 if(i%20==0){ 481 * if(i%20==0){ sendDataCheckDelay(); } i++;
476 sendDataCheckDelay(); 482 */
477 }
478 i++;
479 */
480 writeToClient(os, bufs, inputIndex); 483 writeToClient(os, bufs, inputIndex);
481 writerRunning.set(1); // yes my client is awaking. 484 writerRunning.set(1); // yes my client is awaking.
482 } 485 }
483 } catch (IOException e) { 486 } catch (IOException e) {
484 try { 487 try {
634 } 637 }
635 }; 638 };
636 } 639 }
637 }; 640 };
638 } 641 }
639 642
640 void sendRfbVersion(OutputStream os) throws IOException { 643 void sendRfbVersion(OutputStream os) throws IOException {
641 // os.write(versionMsg_3_8.getBytes()); 644 // os.write(versionMsg_3_8.getBytes());
642 os.write(versionMsg_3_855.getBytes()); 645 os.write(versionMsg_3_855.getBytes());
643 } 646 }
644 647
645 int readVersionMsg(InputStream is, OutputStream os) throws IOException { 648 int readVersionMsg(InputStream is, OutputStream os) throws IOException {
646 649
647 byte[] b = new byte[12]; 650 byte[] b = new byte[12];
648 651
649 is.read(b); 652 is.read(b);
669 if (proxyFlag) 672 if (proxyFlag)
670 sendPortNumber(os); 673 sendPortNumber(os);
671 } 674 }
672 return rfbMinor; 675 return rfbMinor;
673 } 676 }
674 677
675 void sendProxyFlag(OutputStream os) throws IOException { 678 void sendProxyFlag(OutputStream os) throws IOException {
676 if (proxyFlag) 679 if (proxyFlag)
677 os.write(1); 680 os.write(1);
678 else 681 else
679 os.write(0); 682 os.write(0);
680 } 683 }
681 684
682 void sendPortNumber(OutputStream os) throws IOException { 685 void sendPortNumber(OutputStream os) throws IOException {
683 byte[] b = new byte[4]; 686 byte[] b = new byte[4];
684 b = castIntByte(geth.port); 687 b = castIntByte(geth.port);
685 os.write(b); 688 os.write(b);
686 } 689 }
690 if (flag == 1) 693 if (flag == 1)
691 return true; 694 return true;
692 else 695 else
693 return false; 696 return false;
694 } 697 }
695 698
696 void sendSecurityType(OutputStream os) throws IOException { 699 void sendSecurityType(OutputStream os) throws IOException {
697 // number-of-security-types 700 // number-of-security-types
698 os.write(1); 701 os.write(1);
699 // security-types 702 // security-types
700 // 1:None 703 // 1:None
701 os.write(1); 704 os.write(1);
750 753
751 void sendSecResult(OutputStream os) throws IOException { 754 void sendSecResult(OutputStream os) throws IOException {
752 byte[] b = castIntByte(0); 755 byte[] b = castIntByte(0);
753 os.write(b); 756 os.write(b);
754 } 757 }
755 758
756 byte[] castIntByte(int len) { 759 byte[] castIntByte(int len) {
757 byte[] b = new byte[4]; 760 byte[] b = new byte[4];
758 b[0] = (byte) ((len >>> 24) & 0xFF); 761 b[0] = (byte) ((len >>> 24) & 0xFF);
759 b[1] = (byte) ((len >>> 16) & 0xFF); 762 b[1] = (byte) ((len >>> 16) & 0xFF);
760 b[2] = (byte) ((len >>> 8) & 0xFF); 763 b[2] = (byte) ((len >>> 8) & 0xFF);
768 } 771 }
769 772
770 void sendInitData(OutputStream os) throws IOException { 773 void sendInitData(OutputStream os) throws IOException {
771 os.write(initData); 774 os.write(initData);
772 } 775 }
773 776
777 void writeFramebufferUpdateRequest(int x, int y, int w, int h,
778 boolean incremental) throws IOException, TransportException {
779 byte[] b = new byte[10];
780
781 b[0] = (byte) FramebufferUpdateRequest;
782 b[1] = (byte) (incremental ? 1 : 0);
783 b[2] = (byte) ((x >> 8) & 0xff);
784 b[3] = (byte) (x & 0xff);
785 b[4] = (byte) ((y >> 8) & 0xff);
786 b[5] = (byte) (y & 0xff);
787 b[6] = (byte) ((w >> 8) & 0xff);
788 b[7] = (byte) (w & 0xff);
789 b[8] = (byte) ((h >> 8) & 0xff);
790 b[9] = (byte) (h & 0xff);
791
792 writer.write(b);
793 }
794
774 } 795 }