comparison src/treeVnc/RfbProto.java @ 3:3b341997141a

broadcast socket trying
author one
date Mon, 23 Apr 2012 20:18:23 +0900
parents 5d72f4c7371d
children 657c691c2936
comparison
equal deleted inserted replaced
2:5d72f4c7371d 3:3b341997141a
1 package treeVnc; 1 package treeVnc;
2
2 // 3 //
3 // Copyright (C) 2001-2004 HorizonLive.com, Inc. All Rights Reserved. 4 // Copyright (C) 2001-2004 HorizonLive.com, Inc. All Rights Reserved.
4 // Copyright (C) 2001-2006 Constantin Kaplinsky. All Rights Reserved. 5 // Copyright (C) 2001-2006 Constantin Kaplinsky. All Rights Reserved.
5 // Copyright (C) 2000 Tridia Corporation. All Rights Reserved. 6 // Copyright (C) 2000 Tridia Corporation. All Rights Reserved.
6 // Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved. 7 // Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
25 // RfbProto.java 26 // RfbProto.java
26 // 27 //
27 28
28 import java.io.*; 29 import java.io.*;
29 import java.awt.event.*; 30 import java.awt.event.*;
31 import java.net.InetSocketAddress;
30 import java.net.Socket; 32 import java.net.Socket;
33 import java.net.SocketException;
31 import java.nio.ByteBuffer; 34 import java.nio.ByteBuffer;
35 import java.nio.channels.ServerSocketChannel;
36 import java.nio.channels.spi.AbstractSelector;
37 import java.nio.channels.spi.SelectorProvider;
32 import java.util.LinkedList; 38 import java.util.LinkedList;
33 import java.util.zip.*; 39 import java.util.zip.*;
34 40
35 public class RfbProto { 41 public class RfbProto {
36 42
37 final static String versionMsg_3_3 = "RFB 003.003\n", 43 final static String versionMsg_3_3 = "RFB 003.003\n",
38 versionMsg_3_7 = "RFB 003.007\n", versionMsg_3_8 = "RFB 003.008\n",versionMsg_3_855 = "RFB 003.855\n"; 44 versionMsg_3_7 = "RFB 003.007\n", versionMsg_3_8 = "RFB 003.008\n",
39 45 versionMsg_3_855 = "RFB 003.855\n";
40 46
41 // Vendor signatures: standard VNC/RealVNC, TridiaVNC, and TightVNC 47 // Vendor signatures: standard VNC/RealVNC, TridiaVNC, and TightVNC
42 final static String StandardVendor = "STDV", TridiaVncVendor = "TRDV", 48 final static String StandardVendor = "STDV", TridiaVncVendor = "TRDV",
43 TightVncVendor = "TGHT"; 49 TightVncVendor = "TGHT";
44 50
59 final static int VncAuthOK = 0, VncAuthFailed = 1, VncAuthTooMany = 2; 65 final static int VncAuthOK = 0, VncAuthFailed = 1, VncAuthTooMany = 2;
60 66
61 // Standard server-to-client messages 67 // Standard server-to-client messages
62 final static int FramebufferUpdate = 0, SetColourMapEntries = 1, Bell = 2, 68 final static int FramebufferUpdate = 0, SetColourMapEntries = 1, Bell = 2,
63 ServerCutText = 3; 69 ServerCutText = 3;
64 70
65 // Check Delay Top form Bottom 71 // Check Delay Top form Bottom
66 final static int CheckDelay = 11; 72 final static int CheckDelay = 11;
67 73
68 // Non-standard server-to-client messages 74 // Non-standard server-to-client messages
69 final static int EndOfContinuousUpdates = 150; 75 final static int EndOfContinuousUpdates = 150;
78 final static int EnableContinuousUpdates = 150; 84 final static int EnableContinuousUpdates = 150;
79 final static String SigEnableContinuousUpdates = "CUC_ENCU"; 85 final static String SigEnableContinuousUpdates = "CUC_ENCU";
80 86
81 // Supported encodings and pseudo-encodings 87 // Supported encodings and pseudo-encodings
82 final static int EncodingRaw = 0, EncodingCopyRect = 1, EncodingRRE = 2, 88 final static int EncodingRaw = 0, EncodingCopyRect = 1, EncodingRRE = 2,
83 EncodingCoRRE = 4, EncodingHextile = 5, EncodingZlib = 6, 89 EncodingCoRRE = 4, EncodingHextile = 5, EncodingZlib = 6,
84 EncodingTight = 7, EncodingZRLEE = 15, EncodingZRLE = 16, 90 EncodingTight = 7, EncodingZRLEE = 15, EncodingZRLE = 16,
85 EncodingCompressLevel0 = 0xFFFFFF00, 91 EncodingCompressLevel0 = 0xFFFFFF00,
86 EncodingQualityLevel0 = 0xFFFFFFE0, EncodingXCursor = 0xFFFFFF10, 92 EncodingQualityLevel0 = 0xFFFFFFE0, EncodingXCursor = 0xFFFFFF10,
87 EncodingRichCursor = 0xFFFFFF11, EncodingPointerPos = 0xFFFFFF18, 93 EncodingRichCursor = 0xFFFFFF11, EncodingPointerPos = 0xFFFFFF18,
88 EncodingLastRect = 0xFFFFFF20, EncodingNewFBSize = 0xFFFFFF21; 94 EncodingLastRect = 0xFFFFFF20, EncodingNewFBSize = 0xFFFFFF21;
89 final static String SigEncodingRaw = "RAW_____", 95 final static String SigEncodingRaw = "RAW_____",
90 SigEncodingCopyRect = "COPYRECT", SigEncodingRRE = "RRE_____", 96 SigEncodingCopyRect = "COPYRECT", SigEncodingRRE = "RRE_____",
91 SigEncodingCoRRE = "CORRE___", SigEncodingHextile = "HEXTILE_", 97 SigEncodingCoRRE = "CORRE___", SigEncodingHextile = "HEXTILE_",
92 SigEncodingZlib = "ZLIB____", SigEncodingTight = "TIGHT___", 98 SigEncodingZlib = "ZLIB____", SigEncodingTight = "TIGHT___",
93 SigEncodingZRLEE = "ZRLEE___", 99 SigEncodingZRLEE = "ZRLEE___", SigEncodingZRLE = "ZRLE____",
94 SigEncodingZRLE = "ZRLE____",
95 SigEncodingCompressLevel0 = "COMPRLVL", 100 SigEncodingCompressLevel0 = "COMPRLVL",
96 SigEncodingQualityLevel0 = "JPEGQLVL", 101 SigEncodingQualityLevel0 = "JPEGQLVL",
97 SigEncodingXCursor = "X11CURSR", 102 SigEncodingXCursor = "X11CURSR",
98 SigEncodingRichCursor = "RCHCURSR", 103 SigEncodingRichCursor = "RCHCURSR",
99 SigEncodingPointerPos = "POINTPOS", 104 SigEncodingPointerPos = "POINTPOS",
112 final static int TightExplicitFilter = 0x04, TightFill = 0x08, 117 final static int TightExplicitFilter = 0x04, TightFill = 0x08,
113 TightJpeg = 0x09, TightMaxSubencoding = 0x09, 118 TightJpeg = 0x09, TightMaxSubencoding = 0x09,
114 TightFilterCopy = 0x00, TightFilterPalette = 0x01, 119 TightFilterCopy = 0x00, TightFilterPalette = 0x01,
115 TightFilterGradient = 0x02; 120 TightFilterGradient = 0x02;
116 121
122 static AbstractSelector selector;
117 String host; 123 String host;
118 int port; 124 int port;
119 Socket sock; 125 Socket sock;
120 OutputStream os; 126 OutputStream os;
121 SessionRecorder rec; 127 SessionRecorder rec;
124 MyVncClient myVncClient; 130 MyVncClient myVncClient;
125 131
126 // Input stream is declared private to make sure it can be accessed 132 // Input stream is declared private to make sure it can be accessed
127 // only via RfbProto methods. We have to do this because we want to 133 // only via RfbProto methods. We have to do this because we want to
128 // count how many bytes were read. 134 // count how many bytes were read.
129 // private DataInputStream is; 135 // private DataInputStream is;
130 protected DataInputStream is; 136 protected DataInputStream is;
131 // private long numBytesRead = 0; 137 // private long numBytesRead = 0;
132 protected long numBytesRead = 0; 138 protected long numBytesRead = 0;
133 139
134 public long getNumBytesRead() { 140 public long getNumBytesRead() {
135 return numBytesRead; 141 return numBytesRead;
136 } 142 }
137
138 143
139 // Java on UNIX does not call keyPressed() on some keys, for example 144 // Java on UNIX does not call keyPressed() on some keys, for example
140 // swedish keys To prevent our workaround to produce duplicate 145 // swedish keys To prevent our workaround to produce duplicate
141 // keypresses on JVMs that actually works, keep track of if 146 // keypresses on JVMs that actually works, keep track of if
142 // keyPressed() for a "broken" key was called or not. 147 // keyPressed() for a "broken" key was called or not.
176 CapsContainer tunnelCaps, authCaps; 181 CapsContainer tunnelCaps, authCaps;
177 CapsContainer serverMsgCaps, clientMsgCaps; 182 CapsContainer serverMsgCaps, clientMsgCaps;
178 CapsContainer encodingCaps; 183 CapsContainer encodingCaps;
179 184
180 // If true, informs that the RFB socket was closed. 185 // If true, informs that the RFB socket was closed.
181 // private boolean closed; 186 // private boolean closed;
182 protected boolean closed; 187 protected boolean closed;
183 188
184 private byte[] broadCastBuf = new byte[64000]; 189 private byte[] broadCastBuf = new byte[64000];
185 190
186 // 191 //
187 // Constructor. Make TCP connection to RFB server. 192 // Constructor. Make TCP connection to RFB server.
188 // 193 //
190 viewer = v; 195 viewer = v;
191 host = h; 196 host = h;
192 port = p; 197 port = p;
193 198
194 if (viewer.socketFactory == null) { 199 if (viewer.socketFactory == null) {
195 sock = new Socket(host, port); 200 sock = newSocket(host, port);
196 } else { 201 } else {
197 try { 202 try {
198 Class factoryClass = Class.forName(viewer.socketFactory); 203 Class factoryClass = Class.forName(viewer.socketFactory);
199 SocketFactory factory = (SocketFactory) factoryClass 204 SocketFactory factory = (SocketFactory) factoryClass
200 .newInstance(); 205 .newInstance();
213 218
214 timing = false; 219 timing = false;
215 timeWaitedIn100us = 5; 220 timeWaitedIn100us = 5;
216 timedKbits = 0; 221 timedKbits = 0;
217 } 222 }
218 223
219 RfbProto(String h, int p) throws IOException { 224 RfbProto(String h, int p) throws IOException {
220 host = h; 225 host = h;
221 port = p; 226 port = p;
222 227
223 sock = new Socket(host, port); 228 sock = newSocket(host, port);
224 229
225 is = new DataInputStream(new BufferedInputStream(sock.getInputStream(),16384)); 230 is = new DataInputStream(new BufferedInputStream(sock.getInputStream(),
231 16384));
226 os = sock.getOutputStream(); 232 os = sock.getOutputStream();
227 233
228 timing = false; 234 timing = false;
229 timeWaitedIn100us = 5; 235 timeWaitedIn100us = 5;
230 timedKbits = 0; 236 timedKbits = 0;
231 } 237 }
232 238
239 private ServerSocket newSocket(String host, int port) {
240 ServerSocketChannel ssChannel = SelectorProvider.provider().openServerSocketChannel();
241 ssChannel.socket().setReuseAddress(true);
242 // this should work for IPv6/IPv4 dual stack
243 // check this using netstat -an result tcp46.
244 try {
245 InetSocketAddress address = new InetSocketAddress(host, port);
246 ssChannel.socket().bind(address);
247 } catch (SocketException e) {
248 // for some bad IPv6 implementation
249 ssChannel.socket().bind(new InetSocketAddress(port));
250 }
251 ssChannel.configureBlocking(false);
252 return ssChannel.socket();
253 }
254
233 public RfbProto() { 255 public RfbProto() {
234 256
235 } 257 }
236 258
237 public void changeRfbProto(String h,int port) throws IOException { 259 public void initOnce() throws IOException {
260 selector = SelectorProvider.provider().openSelector();
261 }
262
263 public void changeRfbProto(String h, int port) throws IOException {
238 host = h; 264 host = h;
239 sock=null; 265 sock = null;
240 sock = new Socket(host, port); 266 sock = newSocket(host, port);
241 is = new DataInputStream(new BufferedInputStream(sock.getInputStream(), 267 is = new DataInputStream(new BufferedInputStream(sock.getInputStream(),
242 16384)); 268 16384));
243 os = sock.getOutputStream(); 269 os = sock.getOutputStream();
244 270
245 timing = false; 271 timing = false;
246 timeWaitedIn100us = 5; 272 timeWaitedIn100us = 5;
247 timedKbits = 0; 273 timedKbits = 0;
248 } 274 }
249
250
251 275
252 synchronized void close() { 276 synchronized void close() {
253 try { 277 try {
254 sock.close(); 278 sock.close();
255 closed = true; 279 closed = true;
312 os.write(versionMsg_3_3.getBytes()); 336 os.write(versionMsg_3_3.getBytes());
313 } 337 }
314 protocolTightVNC = false; 338 protocolTightVNC = false;
315 initCapabilities(); 339 initCapabilities();
316 } 340 }
317 341
318 // 342 //
319 // Negotiate the authentication scheme. 343 // Negotiate the authentication scheme.
320 // 344 //
321 345
322 int negotiateSecurity() throws Exception { 346 int negotiateSecurity() throws Exception {
365 protocolTightVNC = true; 389 protocolTightVNC = true;
366 os.write(SecTypeTight); 390 os.write(SecTypeTight);
367 return SecTypeTight; 391 return SecTypeTight;
368 } 392 }
369 } 393 }
370
371 394
372 // Find first supported security type. 395 // Find first supported security type.
373 for (int i = 0; i < nSecTypes; i++) { 396 for (int i = 0; i < nSecTypes; i++) {
374 // if (secTypes[i] == SecTypeNone || secTypes[i] == SecTypeVncAuth) { 397 // if (secTypes[i] == SecTypeNone || secTypes[i] == SecTypeVncAuth)
375 if (secTypes[i] == SecTypeNone || secTypes[i] == SecTypeVncAuth 398 // {
376 || secTypes[i] == MyRfbProtoProxy.SecTypeReqAccess) { 399 if (secTypes[i] == SecTypeNone || secTypes[i] == SecTypeVncAuth
377 secType = secTypes[i]; 400 || secTypes[i] == MyRfbProtoProxy.SecTypeReqAccess) {
401 secType = secTypes[i];
378 break; 402 break;
379 } 403 }
380 } 404 }
381
382 405
383 if (secType == SecTypeInvalid) { 406 if (secType == SecTypeInvalid) {
384 throw new Exception("Server did not offer supported security type"); 407 throw new Exception("Server did not offer supported security type");
385 } else { 408 } else {
386 os.write(secType); 409 os.write(secType);
492 encodingCaps.add(EncodingCoRRE, StandardVendor, SigEncodingCoRRE, 515 encodingCaps.add(EncodingCoRRE, StandardVendor, SigEncodingCoRRE,
493 "Standard CoRRE encoding"); 516 "Standard CoRRE encoding");
494 encodingCaps.add(EncodingHextile, StandardVendor, SigEncodingHextile, 517 encodingCaps.add(EncodingHextile, StandardVendor, SigEncodingHextile,
495 "Standard Hextile encoding"); 518 "Standard Hextile encoding");
496 encodingCaps.add(EncodingZRLE, StandardVendor, SigEncodingZRLE, 519 encodingCaps.add(EncodingZRLE, StandardVendor, SigEncodingZRLE,
497 "Standard ZRLE encoding"); 520 "Standard ZRLE encoding");
498 encodingCaps.add(EncodingZRLEE, StandardVendor, SigEncodingZRLEE, 521 encodingCaps.add(EncodingZRLEE, StandardVendor, SigEncodingZRLEE,
499 "Standard ZRLE(E) encoding"); 522 "Standard ZRLE(E) encoding");
500 encodingCaps.add(EncodingZlib, TridiaVncVendor, SigEncodingZlib, 523 encodingCaps.add(EncodingZlib, TridiaVncVendor, SigEncodingZlib,
501 "Zlib encoding"); 524 "Zlib encoding");
502 encodingCaps.add(EncodingTight, TightVncVendor, SigEncodingTight, 525 encodingCaps.add(EncodingTight, TightVncVendor, SigEncodingTight,
503 "Tight encoding"); 526 "Tight encoding");
504 527
587 // 610 //
588 // Write the client initialisation message 611 // Write the client initialisation message
589 // 612 //
590 613
591 void writeClientInit() throws IOException { 614 void writeClientInit() throws IOException {
592 /* 615 /*
593 if (viewer.options.shareDesktop) { 616 * if (viewer.options.shareDesktop) {
594 */ 617 */
595 618
596 /** 619 /**
597 * shared flag 620 * shared flag
598 */ 621 */
599 os.write(1); 622 os.write(1);
600 // os.write(0); 623 // os.write(0);
601 624
602 // viewer.options.disableShareDesktop(); 625 // viewer.options.disableShareDesktop();
603 } 626 }
604 627
605 // 628 //
606 // Read the server initialisation message 629 // Read the server initialisation message
607 // 630 //
852 } 875 }
853 876
854 // 877 //
855 // Write a FramebufferUpdateRequest message 878 // Write a FramebufferUpdateRequest message
856 // 879 //
857 880
858
859 void checkDelayData() throws IOException { 881 void checkDelayData() throws IOException {
860 System.out.println("sousinn"); 882 System.out.println("sousinn");
861 byte[] b = new byte[1]; 883 byte[] b = new byte[1];
862 b[0] = (byte) CheckDelay; 884 b[0] = (byte) CheckDelay;
863 os.write(b); 885 os.write(b);
864 } 886 }
865 887
866 void writeFramebufferUpdateRequest(int x, int y, int w, int h, 888 void writeFramebufferUpdateRequest(int x, int y, int w, int h,
867 boolean incremental) throws IOException { 889 boolean incremental) throws IOException {
868 byte[] b = new byte[10]; 890 byte[] b = new byte[10];
990 final static int CTRL_MASK = InputEvent.CTRL_MASK; 1012 final static int CTRL_MASK = InputEvent.CTRL_MASK;
991 final static int SHIFT_MASK = InputEvent.SHIFT_MASK; 1013 final static int SHIFT_MASK = InputEvent.SHIFT_MASK;
992 final static int META_MASK = InputEvent.META_MASK; 1014 final static int META_MASK = InputEvent.META_MASK;
993 final static int ALT_MASK = InputEvent.ALT_MASK; 1015 final static int ALT_MASK = InputEvent.ALT_MASK;
994 1016
995
996
997 // 1017 //
998 // Write a pointer event message. We may need to send modifier key events 1018 // Write a pointer event message. We may need to send modifier key events
999 // around it to set the correct modifier state. 1019 // around it to set the correct modifier state.
1000 // 1020 //
1001 1021
1005 int modifiers = evt.getModifiers(); 1025 int modifiers = evt.getModifiers();
1006 1026
1007 int mask2 = 2; 1027 int mask2 = 2;
1008 int mask3 = 4; 1028 int mask3 = 4;
1009 /* 1029 /*
1010 if (viewer.options.reverseMouseButtons2And3) { 1030 * if (viewer.options.reverseMouseButtons2And3) { mask2 = 4; mask3 = 2;
1011 mask2 = 4; 1031 * }
1012 mask3 = 2; 1032 */
1013 }
1014 */
1015 1033
1016 // Note: For some reason, AWT does not set BUTTON1_MASK on left 1034 // Note: For some reason, AWT does not set BUTTON1_MASK on left
1017 // button presses. Here we think that it was the left button if 1035 // button presses. Here we think that it was the left button if
1018 // modifiers do not include BUTTON2_MASK or BUTTON3_MASK. 1036 // modifiers do not include BUTTON2_MASK or BUTTON3_MASK.
1019 1037
1368 1386
1369 public void readFully(byte b[]) throws IOException { 1387 public void readFully(byte b[]) throws IOException {
1370 readFully(b, 0, b.length); 1388 readFully(b, 0, b.length);
1371 } 1389 }
1372 1390
1373 long before = System.currentTimeMillis(); 1391 long before = System.currentTimeMillis();
1392
1374 public void readFully(byte b[], int off, int len) throws IOException { 1393 public void readFully(byte b[], int off, int len) throws IOException {
1375 // long before = 0; 1394 // long before = 0;
1376 if (timing) 1395 if (timing)
1377 before = System.currentTimeMillis(); 1396 before = System.currentTimeMillis();
1378 1397
1379 is.readFully(b, off, len); 1398 is.readFully(b, off, len);
1380 1399
1381 /* 1400 /*
1382 if(b.length==16) { 1401 * if(b.length==16) { b[4] = (byte)0; b[5] = (byte)0; b[6] = (byte)0;
1383 b[4] = (byte)0; 1402 * b[7] = (byte)0; System.out.println("----------------------"); }
1384 b[5] = (byte)0; 1403 */
1385 b[6] = (byte)0; 1404 // System.out.println("Blength:"+b.length);
1386 b[7] = (byte)0; 1405 // for(int i=0 ; i<=b.length ; i++) {
1387 System.out.println("----------------------"); 1406 // if(i>b.length/2)
1388 } 1407 // b[i] = 10;
1389 */ 1408 // }
1390 //System.out.println("Blength:"+b.length);
1391 //for(int i=0 ; i<=b.length ; i++) {
1392 //if(i>b.length/2)
1393 //b[i] = 10;
1394 //}
1395 1409
1396 /* 1410 /*
1397 if (timing) { 1411 * if (timing) { long after = System.currentTimeMillis(); long
1398 long after = System.currentTimeMillis(); 1412 * newTimeWaited = (after - before) * 10; int newKbits = len * 8 / 1000;
1399 long newTimeWaited = (after - before) * 10; 1413 *
1400 int newKbits = len * 8 / 1000; 1414 * // limit rate to between 10kbit/s and 40Mbit/s
1401 1415 *
1402 // limit rate to between 10kbit/s and 40Mbit/s 1416 * if (newTimeWaited > newKbits * 1000) newTimeWaited = newKbits * 1000;
1403 1417 * if (newTimeWaited < newKbits / 4) newTimeWaited = newKbits / 4;
1404 if (newTimeWaited > newKbits * 1000) 1418 *
1405 newTimeWaited = newKbits * 1000; 1419 * timeWaitedIn100us += newTimeWaited; timedKbits += newKbits; before =
1406 if (newTimeWaited < newKbits / 4) 1420 * after; }
1407 newTimeWaited = newKbits / 4; 1421 */
1408
1409 timeWaitedIn100us += newTimeWaited;
1410 timedKbits += newKbits;
1411 before = after;
1412 }
1413 */
1414 numBytesRead += len; 1422 numBytesRead += len;
1415 //System.out.println("numBytesRead:"+numBytesRead); 1423 // System.out.println("numBytesRead:"+numBytesRead);
1416 } 1424 }
1417 1425
1418 final int available() throws IOException { 1426 final int available() throws IOException {
1419 return is.available(); 1427 return is.available();
1420 } 1428 }
1444 int r = is.readInt(); 1452 int r = is.readInt();
1445 numBytesRead += 4; 1453 numBytesRead += 4;
1446 return r; 1454 return r;
1447 } 1455 }
1448 1456
1449 public LinkedList<ByteBuffer> blockingUpdateRectangle(ByteBuffer input,int w,int h) { 1457 public LinkedList<ByteBuffer> blockingUpdateRectangle(
1458 LinkedList<ByteBuffer> input, int w, int h) {
1450 1459
1451 return null; 1460 return null;
1452 } 1461 }
1453 } 1462 }