12
|
1 package jp.ac.u_ryukyu.treevnc.server;
|
11
|
2
|
|
3 import static org.junit.Assert.*;
|
|
4
|
|
5 import java.awt.Graphics;
|
|
6 import java.awt.Image;
|
|
7 import java.awt.image.BufferedImage;
|
|
8 import java.io.BufferedOutputStream;
|
|
9 import java.io.BufferedReader;
|
|
10 import java.io.ByteArrayInputStream;
|
|
11 import java.io.ByteArrayOutputStream;
|
|
12 import java.io.IOException;
|
|
13 import java.io.InputStream;
|
|
14 import java.io.InputStreamReader;
|
|
15 import java.net.BindException;
|
|
16 import java.net.ServerSocket;
|
|
17 import java.net.Socket;
|
|
18 import java.nio.ByteBuffer;
|
|
19 import java.util.Iterator;
|
|
20 import java.util.LinkedList;
|
|
21
|
|
22 import javax.imageio.ImageIO;
|
|
23
|
|
24 import org.junit.Test;
|
|
25
|
15
|
26 import com.glavsoft.exceptions.TransportException;
|
|
27 import com.glavsoft.rfb.encoding.EncodingType;
|
|
28 import com.glavsoft.transport.Reader;
|
16
|
29 import com.glavsoft.transport.Writer;
|
11
|
30
|
|
31 //import myVncProxy.MulticastQueue.Client;
|
|
32
|
|
33 import java.util.concurrent.ExecutorService;
|
|
34 import java.util.concurrent.atomic.AtomicInteger;
|
|
35 import java.util.zip.DataFormatException;
|
|
36 import java.util.zip.Deflater;
|
|
37 import java.util.zip.Inflater;
|
|
38 import java.io.OutputStream;
|
|
39
|
12
|
40 import jp.ac.u_ryukyu.treevnc.MulticastQueue;
|
|
41
|
11
|
42 public class MyRfbProtoProxy {
|
|
43 final static String versionMsg_3_855 = "RFB 003.855\n";
|
|
44 /**
|
|
45 * CheckMillis is one of new msgType for RFB 3.855.
|
|
46 */
|
|
47 final static byte SpeedCheckMillis = 4;
|
15
|
48 final static int FramebufferUpdate = 0;
|
|
49 final static int CheckDelay = 11;
|
16
|
50 final static int FramebufferUpdateRequest = 3;
|
11
|
51
|
|
52 // Secyrity type of OS X
|
|
53 final static int SecTypeReqAccess = 32;
|
|
54
|
|
55 // Supported authentication types
|
|
56 final static int AuthAccess = 32;
|
|
57
|
|
58 private static final int INFLATE_BUFSIZE = 1024 * 100;
|
|
59 boolean printStatusFlag = false;
|
|
60 long startCheckTime;
|
|
61 private int messageType;
|
|
62 private int rectangles;
|
|
63 private int rectX;
|
|
64 private int rectY;
|
|
65 private int rectW;
|
|
66 private int rectH;
|
|
67 private int encoding;
|
|
68 private int zLen;
|
|
69 private boolean clicomp = false;
|
|
70
|
|
71 private ServerSocket servSock;
|
|
72 protected int acceptPort;
|
|
73 // private byte initData[];
|
|
74 byte initData[];
|
|
75 private LinkedList<Socket> cliListTmp;
|
|
76 private LinkedList<Socket> cliList;
|
|
77 boolean createBimgFlag;
|
|
78 boolean proxyFlag = true;
|
17
|
79 private Reader is;
|
|
80 private Writer os;
|
11
|
81 ExecutorService executor;
|
|
82
|
|
83 byte[] pngBytes;
|
|
84
|
|
85 // private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new
|
|
86 // MostRecentMultiCast<LinkedList<ByteBuffer>>(10);
|
|
87 private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>();
|
|
88 private int clients = 0;
|
|
89 private Inflater inflater = new Inflater();
|
|
90 private Deflater deflater = new Deflater();
|
17
|
91 private CreateThread getHost;
|
11
|
92 // private Thread requestThread;
|
|
93 private RequestScreenThread rThread;
|
|
94 private Thread requestThread;
|
|
95
|
15
|
96 public MyRfbProtoProxy() {
|
16
|
97 rThread = new RequestScreenThread(this);
|
|
98 requestThread = new Thread(rThread);
|
11
|
99 }
|
|
100
|
16
|
101 public void setStream(Reader _reader, Writer _writer) {
|
17
|
102 is = _reader;
|
|
103 os = _writer;
|
15
|
104 }
|
11
|
105
|
|
106 void initServSock(int port) throws IOException {
|
|
107 servSock = new ServerSocket(port);
|
|
108 acceptPort = port;
|
|
109 }
|
|
110
|
|
111 /*
|
|
112 * default port number is 5999.
|
|
113 */
|
|
114 public void selectPort(int p) {
|
|
115 if (servSock != null)
|
|
116 return;
|
|
117 int port = p;
|
|
118 while (true) {
|
|
119 try {
|
|
120 initServSock(port);
|
|
121 break;
|
|
122 } catch (BindException e) {
|
|
123 port++;
|
|
124 continue;
|
|
125 } catch (IOException e) {
|
|
126
|
|
127 }
|
|
128 }
|
|
129 System.out.println("accept port = " + port);
|
|
130 }
|
|
131
|
|
132 int getAcceptPort() {
|
|
133 return acceptPort;
|
|
134 }
|
|
135
|
|
136 void setSoTimeout(int num) throws IOException {
|
|
137 servSock.setSoTimeout(num);
|
|
138 }
|
|
139
|
|
140 public Socket accept() throws IOException {
|
|
141 return servSock.accept();
|
|
142 }
|
|
143
|
|
144 void addSock(Socket sock) {
|
|
145 cliList.add(sock);
|
|
146 }
|
|
147
|
|
148 void addSockTmp(Socket sock) {
|
|
149 System.out.println("connected " + sock.getInetAddress());
|
|
150 cliListTmp.add(sock);
|
|
151 }
|
|
152
|
|
153 synchronized void changeStatusFlag() {
|
|
154 printStatusFlag = true;
|
|
155 }
|
|
156
|
|
157 void printMills() {
|
|
158 if (printStatusFlag) {
|
|
159
|
|
160 changeStatusFlag();
|
|
161 } else {
|
|
162 changeStatusFlag();
|
|
163 }
|
|
164 }
|
|
165
|
|
166 void requestThreadStart() {
|
|
167 requestThread.start();
|
|
168 }
|
|
169
|
|
170 public synchronized void requestThreadNotify() {
|
|
171 rThread.reStart();
|
|
172 }
|
|
173
|
|
174 /**
|
|
175 * gzip byte arrays
|
|
176 *
|
|
177 * @param deflater
|
|
178 * @param inputs
|
|
179 * byte data[]
|
|
180 * @param inputIndex
|
|
181 * @param outputs
|
|
182 * byte data[]
|
|
183 * @return byte length in last byte array
|
|
184 * @throws IOException
|
|
185 */
|
|
186 public int zip(Deflater deflater, LinkedList<ByteBuffer> inputs,
|
|
187 int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException {
|
|
188 int len = 0;
|
|
189 ByteBuffer c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
|
|
190 while (inputIndex < inputs.size()) {
|
|
191 ByteBuffer b1 = inputs.get(inputIndex++);
|
|
192 deflater.setInput(b1.array(), b1.position(), b1.remaining());
|
|
193 /**
|
|
194 * If we finish() stream and reset() it, Deflater start new gzip
|
|
195 * stream, this makes continuous zlib reader unhappy. if we remove
|
|
196 * finish(), Deflater.deflate() never flushes its output. The
|
|
197 * original zlib deflate has flush flag. I'm pretty sure this a kind
|
|
198 * of bug of Java library.
|
|
199 */
|
|
200 if (inputIndex == inputs.size())
|
|
201 deflater.finish();
|
|
202 int len1 = 0;
|
|
203 do {
|
|
204 len1 = deflater.deflate(c1.array(), c1.position(),
|
|
205 c1.remaining());
|
|
206 if (len1 > 0) {
|
|
207 len += len1;
|
|
208 c1.position(c1.position() + len1);
|
|
209 if (c1.remaining() == 0) {
|
|
210 c1.flip();
|
|
211 outputs.addLast(c1);
|
|
212 c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
|
|
213 }
|
|
214 }
|
|
215 } while (len1 > 0 || !deflater.needsInput()); // &&!deflater.finished());
|
|
216 }
|
|
217 if (c1.position() != 0) {
|
|
218 c1.flip();
|
|
219 outputs.addLast(c1);
|
|
220 }
|
|
221 deflater.reset();
|
|
222 return len;
|
|
223 }
|
|
224
|
|
225 /**
|
|
226 * gunzip byte arrays
|
|
227 *
|
|
228 * @param inflater
|
|
229 * @param inputs
|
|
230 * byte data[]
|
|
231 * @param outputs
|
|
232 * byte data[]
|
|
233 * @return number of total bytes
|
|
234 * @throws IOException
|
|
235 */
|
|
236 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs,
|
|
237 int inputIndex, LinkedList<ByteBuffer> outputs, int bufSize)
|
|
238 throws DataFormatException {
|
|
239 int len = 0;
|
|
240 ByteBuffer buf = ByteBuffer.allocate(bufSize);
|
|
241 while (inputIndex < inputs.size()) {
|
|
242 ByteBuffer input = inputs.get(inputIndex++);
|
|
243 inflater.setInput(input.array(), input.position(), input.limit());
|
|
244 // if (inputIndex==inputs.size()) if inflater/deflater has symmetry,
|
|
245 // we need this
|
|
246 // inflater.end(); but this won't work
|
|
247 do {
|
|
248 int len0 = inflater.inflate(buf.array(), buf.position(),
|
|
249 buf.remaining());
|
|
250 if (len0 > 0) {
|
|
251 buf.position(buf.position() + len0);
|
|
252 len += len0;
|
|
253 if (buf.remaining() == 0) {
|
|
254 buf.flip();
|
|
255 outputs.addLast(buf);
|
|
256 buf = ByteBuffer.allocate(bufSize);
|
|
257 }
|
|
258 }
|
|
259 } while (!inflater.needsInput());
|
|
260 }
|
|
261 if (buf.position() != 0) {
|
|
262 buf.flip();
|
|
263 outputs.addLast(buf);
|
|
264 }
|
|
265 return len;
|
|
266 }
|
|
267
|
|
268 float maxMag = 1;
|
|
269
|
|
270 /**
|
|
271 * send data to clients
|
|
272 *
|
|
273 * @param dataLen
|
|
274 * @throws IOException
|
|
275 * @throws DataFormatException
|
|
276 *
|
|
277 * Zlibed packet is compressed in context dependent way, that
|
|
278 * is, it have to send from the beginning. But this is
|
|
279 * impossible. So we have to compress it again for each clients.
|
|
280 * Separate deflater for each clients is necessary.
|
|
281 *
|
|
282 * Java's deflater does not support flush. This means to get the
|
|
283 * result, we have to finish the compression. Reseting start new
|
|
284 * compression, but it is not accepted well in zlib continuous
|
|
285 * reading. So we need new Encoding ZRLEE which reset decoder
|
|
286 * for each packet. ZRLEE can be invisible from user, but it
|
|
287 * have to be implemented in the clients. ZRLEE compression is
|
|
288 * not context dependent, so no recompression is necessary.
|
16
|
289 * @throws TransportException
|
11
|
290 */
|
|
291
|
16
|
292 void readSendData(int dataLen) throws IOException, DataFormatException,
|
|
293 TransportException {
|
11
|
294 LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>();
|
|
295 ByteBuffer header = ByteBuffer.allocate(16);
|
17
|
296 is.readBytes(header.array(), 0, 16);
|
11
|
297 header.limit(16);
|
16
|
298 if (header.get(0) == FramebufferUpdate) {
|
11
|
299 int encoding = header.getInt(12);
|
15
|
300 if (encoding == EncodingType.ZRLE.getId()
|
16
|
301 || encoding == EncodingType.ZLIB.getId()) { // ZRLEE is
|
|
302 // already
|
|
303 // recompressed
|
11
|
304 ByteBuffer len = ByteBuffer.allocate(4);
|
17
|
305 is.readBytes(len.array(), 0, 4);
|
11
|
306 len.limit(4);
|
|
307 ByteBuffer inputData = ByteBuffer.allocate(dataLen - 20);
|
|
308
|
17
|
309 is.readBytes(inputData.array(), 0, inputData.capacity());
|
16
|
310 // System.out.println(dataLen);
|
11
|
311 inputData.limit(dataLen - 20);
|
|
312
|
|
313 LinkedList<ByteBuffer> inputs = new LinkedList<ByteBuffer>();
|
|
314 inputs.add(inputData);
|
|
315
|
16
|
316 header.putInt(12, EncodingType.ZRLEE.getId()); // means
|
|
317 // recompress
|
|
318 // every time
|
11
|
319 // using new Deflecter every time is incompatible with the
|
|
320 // protocol, clients have to be modified.
|
|
321 Deflater nDeflater = deflater; // new Deflater();
|
|
322 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>();
|
|
323 unzip(inflater, inputs, 0, out, INFLATE_BUFSIZE);
|
|
324 // dump32(inputs);
|
|
325 int len2 = zip(nDeflater, out, 0, bufs);
|
|
326 ByteBuffer blen = ByteBuffer.allocate(4);
|
|
327 blen.putInt(len2);
|
|
328 blen.flip();
|
|
329 bufs.addFirst(blen);
|
|
330
|
|
331 bufs.addFirst(header);
|
16
|
332 // if(dataLen<=64000)
|
|
333 multicastqueue.put(bufs);
|
11
|
334 // is.reset();
|
|
335
|
|
336 /*
|
|
337 * System.out.println("ZRLE = "+dataLen);
|
|
338 * System.out.println("ZRLEE = "+(len2+20)); float mag =
|
|
339 * (float)dataLen / (float)(len2 + 20);
|
|
340 * System.out.println("ZRLE / ZRLEE = "+ mag); if(mag > maxMag)
|
|
341 * maxMag = mag; System.out.println("maxMag = "+maxMag);
|
|
342 */
|
|
343 return;
|
|
344 }
|
|
345 bufs.add(header);
|
|
346 if (dataLen > 16) {
|
|
347 ByteBuffer b = ByteBuffer.allocate(dataLen - 16);
|
17
|
348 is.readBytes(b.array(), 0, dataLen - 16);
|
11
|
349 b.limit(dataLen - 16);
|
|
350 bufs.add(b);
|
|
351 }
|
|
352 multicastqueue.put(bufs);
|
|
353 // is.reset();
|
|
354 return;
|
|
355 }
|
17
|
356 is.reset();
|
11
|
357
|
|
358 // It may be compressed. We can inflate here to avoid repeating clients
|
|
359 // decompressing here,
|
|
360 // but it may generate too many large data. It is better to do it in
|
|
361 // each client.
|
|
362 // But we have do inflation for all input data, so we have to do it
|
|
363 // here.
|
|
364 }
|
|
365
|
|
366 public void newClient(AcceptThread acceptThread, final Socket newCli,
|
17
|
367 final Writer os, final Reader is) throws IOException {
|
11
|
368 // createBimgFlag = true;
|
|
369 // rfb.addSockTmp(newCli);
|
|
370 // addSock(newCli);
|
|
371 final int myId = clients;
|
|
372 final MulticastQueue.Client<LinkedList<ByteBuffer>> c = multicastqueue
|
|
373 .newClient();
|
|
374 final AtomicInteger writerRunning = new AtomicInteger();
|
|
375 writerRunning.set(1);
|
|
376 /**
|
|
377 * Timeout thread. If a client is suspended, it has top of queue
|
|
378 * indefinitely, which caused memory overflow. After the timeout, we
|
|
379 * poll the queue and discard it. Start long wait if writer is running.
|
|
380 */
|
|
381 final Runnable timer = new Runnable() {
|
|
382 public void run() {
|
|
383 int count = 0;
|
|
384 for (;;) {
|
|
385 long timeout = 50000 / 8;
|
|
386 try {
|
|
387 synchronized (this) {
|
|
388 int state, flag;
|
|
389 writerRunning.set(0);
|
|
390 wait(timeout);
|
|
391 flag = 0;
|
|
392 while ((state = writerRunning.get()) == 0) {
|
|
393 c.poll(); // discard, should be timeout
|
|
394 count++;
|
|
395 if (flag == 0) {
|
|
396 System.out.println("Discarding " + myId
|
|
397 + " count=" + count);
|
|
398 flag = 1;
|
|
399 }
|
|
400 wait(10); // if this is too short, writer cannot
|
|
401 // take the poll, if this is too
|
|
402 // long, memory will overflow...
|
|
403 }
|
|
404 if (flag == 1)
|
|
405 System.out.println("Resuming " + myId
|
|
406 + " count=" + count);
|
|
407 if (state != 1) {
|
|
408 System.out.println("Client died " + myId);
|
|
409 break;
|
|
410 }
|
|
411 }
|
|
412 } catch (InterruptedException e) {
|
|
413 }
|
|
414 }
|
|
415 }
|
|
416 };
|
|
417 new Thread(timer).start();
|
|
418 /**
|
|
419 * discard all incoming from clients
|
|
420 */
|
|
421 final Runnable reader = new Runnable() {
|
|
422 public void run() {
|
|
423 byte b[] = new byte[4096];
|
|
424 for (;;) {
|
|
425 try {
|
17
|
426 int c = is.readByte(b);
|
11
|
427 if (c <= 0)
|
|
428 throw new IOException();
|
|
429 // System.out.println("client read "+c);
|
|
430 } catch (IOException e) {
|
|
431 try {
|
|
432 writerRunning.set(2);
|
|
433 os.close();
|
|
434 is.close();
|
|
435 } catch (IOException e1) {
|
17
|
436 } catch (TransportException e1) {
|
|
437 e1.printStackTrace();
|
11
|
438 }
|
|
439 return;
|
17
|
440 } catch (TransportException e) {
|
|
441 e.printStackTrace();
|
11
|
442 }
|
|
443 }
|
|
444 }
|
|
445 };
|
|
446 /**
|
|
447 * send packets to a client
|
|
448 */
|
|
449 Runnable sender = new Runnable() {
|
|
450 public void run() {
|
|
451 writerRunning.set(1);
|
|
452 try {
|
|
453 requestThreadNotify();
|
|
454 // rThread.checkDelay();
|
|
455
|
|
456 /**
|
|
457 * initial connection of RFB protocol
|
|
458 */
|
|
459 sendRfbVersion(os);
|
|
460 // readVersionMsg(is);
|
|
461 int rfbMinor = readVersionMsg(is, os);
|
|
462 sendSecurityType(os);
|
|
463 readSecType(is);
|
|
464 sendSecResult(os);
|
|
465 readClientInit(is);
|
|
466 sendInitData(os);
|
|
467 new Thread(reader).start(); // discard incoming packet here
|
|
468 // after.
|
|
469 // writeFramebufferUpdateRequest(0,0, framebufferWidth,
|
|
470 // framebufferHeight, false );
|
16
|
471 int i = 0;
|
11
|
472 for (;;) {
|
|
473 LinkedList<ByteBuffer> bufs = c.poll();
|
|
474 int inputIndex = 0;
|
|
475 ByteBuffer header = bufs.get(inputIndex);
|
|
476 if (header == null)
|
|
477 continue;
|
15
|
478 else if (header.get(0) == CheckDelay) {
|
11
|
479 writeToClient(os, bufs, inputIndex);
|
|
480 continue;
|
15
|
481 } else if (header.get(0) == FramebufferUpdate) {
|
11
|
482 // System.out.println("client "+ myId);
|
|
483 }
|
|
484 /*
|
16
|
485 * if(i%20==0){ sendDataCheckDelay(); } i++;
|
|
486 */
|
11
|
487 writeToClient(os, bufs, inputIndex);
|
|
488 writerRunning.set(1); // yes my client is awaking.
|
|
489 }
|
|
490 } catch (IOException e) {
|
|
491 try {
|
|
492 writerRunning.set(2);
|
|
493 os.close();
|
|
494 } catch (IOException e1) {
|
|
495 }
|
|
496 /* if socket closed cliList.remove(newCli); */
|
17
|
497 } catch (TransportException e) {
|
|
498 e.printStackTrace();
|
11
|
499 }
|
|
500 }
|
|
501
|
17
|
502 public void writeToClient(final Writer os,
|
11
|
503 LinkedList<ByteBuffer> bufs, int inputIndex)
|
17
|
504 throws TransportException {
|
11
|
505 while (inputIndex < bufs.size()) {
|
|
506 ByteBuffer b = bufs.get(inputIndex++);
|
|
507 os.write(b.array(), b.position(), b.limit());
|
|
508 }
|
|
509 os.flush();
|
|
510 }
|
|
511 };
|
|
512 clients++;
|
|
513 new Thread(sender).start();
|
|
514
|
|
515 }
|
|
516
|
|
517 public void dump32(LinkedList<ByteBuffer> bufs) {
|
|
518 int len = 0;
|
|
519 for (ByteBuffer b : bufs)
|
|
520 len += b.remaining();
|
|
521 ByteBuffer top = bufs.getFirst();
|
|
522 ByteBuffer end = bufs.getLast();
|
|
523 System.err.println("length: " + len);
|
|
524 System.err.print("head 0: ");
|
|
525 for (int i = 0; i < 16 && i < top.remaining(); i++) {
|
|
526 System.err.print(" " + top.get(i));
|
|
527 }
|
|
528 System.err.print("tail 0: ");
|
|
529 for (int i = 0; i < 16 && i < end.remaining(); i++) {
|
|
530 System.err.print(" " + end.get(i));
|
|
531 }
|
|
532 System.err.println();
|
|
533 }
|
|
534
|
|
535 @Test
|
|
536 public void test1() {
|
|
537 try {
|
|
538 LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>();
|
|
539 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>();
|
|
540 LinkedList<ByteBuffer> out2 = new LinkedList<ByteBuffer>();
|
|
541 // if (false) {
|
|
542 // for(int i=0;i<10;i++) {
|
|
543 // in.add(ByteBuffer.wrap("test1".getBytes()));
|
|
544 // in.add(ByteBuffer.wrap("test2".getBytes()));
|
|
545 // in.add(ByteBuffer.wrap("test3".getBytes()));
|
|
546 // in.add(ByteBuffer.wrap("test44".getBytes()));
|
|
547 // }
|
|
548 // } else
|
|
549 {
|
|
550 String t = "";
|
|
551 for (int i = 0; i < 10; i++) {
|
|
552 t += "test1";
|
|
553 t += "test2";
|
|
554 t += "test3";
|
|
555 t += "test44";
|
|
556 }
|
|
557 in.add(ByteBuffer.wrap(t.getBytes()));
|
|
558 }
|
|
559
|
|
560 LinkedList<ByteBuffer> in1 = clone(in);
|
|
561
|
|
562 Deflater deflater = new Deflater();
|
|
563 zip(deflater, in, 0, out);
|
|
564 // LinkedList<ByteBuffer> out3 = clone(out); zipped result is depend
|
|
565 // on deflator's state
|
|
566 unzip(inflater, out, 0, out2, INFLATE_BUFSIZE);
|
|
567 // inflater.reset();
|
|
568 equalByteBuffers(in1, out2);
|
|
569 LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>();
|
|
570 deflater = new Deflater();
|
|
571 zip(deflater, out2, 0, out4);
|
|
572 LinkedList<ByteBuffer> out5 = new LinkedList<ByteBuffer>();
|
|
573 unzip(inflater, out4, 0, out5, INFLATE_BUFSIZE);
|
|
574 int len = equalByteBuffers(in1, out5);
|
|
575
|
|
576 System.out.println("Test Ok. " + len);
|
|
577 } catch (Exception e) {
|
|
578 assertEquals(0, 1);
|
|
579 }
|
|
580 }
|
|
581
|
|
582 private LinkedList<ByteBuffer> clone(LinkedList<ByteBuffer> in) {
|
|
583 LinkedList<ByteBuffer> copy = new LinkedList<ByteBuffer>();
|
|
584 for (ByteBuffer b : in) {
|
|
585 ByteBuffer c = b.duplicate();
|
|
586 copy.add(c);
|
|
587 }
|
|
588 return copy;
|
|
589 }
|
|
590
|
|
591 public int equalByteBuffers(LinkedList<ByteBuffer> in,
|
|
592 LinkedList<ByteBuffer> out2) {
|
|
593 int len = 0;
|
|
594 Iterable<Byte> i = byteBufferIterator(in);
|
|
595 Iterator<Byte> o = byteBufferIterator(out2).iterator();
|
|
596
|
|
597 for (int b : i) {
|
|
598 len++;
|
|
599 if (o.hasNext()) {
|
|
600 int c = o.next();
|
|
601 assertEquals(b, c);
|
|
602 } else
|
|
603 assertEquals(0, 1);
|
|
604 }
|
|
605 if (o.hasNext())
|
|
606 assertEquals(0, 1);
|
|
607 // System.out.println();
|
|
608 return len;
|
|
609 }
|
|
610
|
|
611 private Iterable<Byte> byteBufferIterator(final LinkedList<ByteBuffer> in) {
|
|
612 return new Iterable<Byte>() {
|
|
613 public Iterator<Byte> iterator() {
|
|
614 return new Iterator<Byte>() {
|
|
615 int bytes = 0;
|
|
616 int buffers = 0;
|
|
617
|
|
618 public boolean hasNext() {
|
|
619 for (;;) {
|
|
620 if (buffers >= in.size())
|
|
621 return false;
|
|
622 ByteBuffer b = in.get(buffers);
|
|
623 if (!(bytes < b.remaining())) {
|
|
624 buffers++;
|
|
625 bytes = 0;
|
|
626 } else
|
|
627 return true;
|
|
628 }
|
|
629 }
|
|
630
|
|
631 public Byte next() {
|
|
632 ByteBuffer bf = in.get(buffers);
|
|
633 byte b = bf.get(bytes++);
|
|
634 if (bf.remaining() <= bytes) {
|
|
635 buffers++;
|
|
636 bytes = 0;
|
|
637 }
|
|
638 // System.out.print(b);
|
|
639 return b;
|
|
640 }
|
|
641
|
|
642 public void remove() {
|
|
643 }
|
|
644 };
|
|
645 }
|
|
646 };
|
|
647 }
|
16
|
648
|
17
|
649 void sendRfbVersion(Writer writer) throws IOException, TransportException {
|
15
|
650 // os.write(versionMsg_3_8.getBytes());
|
17
|
651 writer.write(versionMsg_3_855.getBytes());
|
15
|
652 }
|
16
|
653
|
17
|
654 int readVersionMsg(Reader reader, Writer writer) throws IOException, TransportException {
|
11
|
655
|
15
|
656 byte[] b = new byte[12];
|
|
657
|
17
|
658 reader.readBytes(b);
|
15
|
659
|
|
660 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ')
|
|
661 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9')
|
|
662 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.')
|
|
663 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9')
|
|
664 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) {
|
|
665 throw new IOException("this is not an RFB server");
|
|
666 }
|
|
667
|
|
668 int rfbMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
|
|
669 int rfbMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
|
|
670
|
|
671 if (rfbMajor < 3) {
|
|
672 throw new IOException(
|
|
673 "RFB server does not support protocol version 3");
|
|
674 }
|
|
675
|
|
676 if (rfbMinor == 855) {
|
17
|
677 sendProxyFlag(writer);
|
15
|
678 if (proxyFlag)
|
17
|
679 sendPortNumber(writer);
|
15
|
680 }
|
|
681 return rfbMinor;
|
|
682 }
|
16
|
683
|
17
|
684 void sendProxyFlag(Writer writer) throws TransportException {
|
15
|
685 if (proxyFlag)
|
17
|
686 writer.writeInt(1);
|
15
|
687 else
|
17
|
688 writer.writeInt(0);
|
15
|
689 }
|
16
|
690
|
17
|
691 void sendPortNumber(Writer writer) throws TransportException {
|
15
|
692 byte[] b = new byte[4];
|
17
|
693 //b = castIntByte(getHost.getPort());
|
|
694 b = castIntByte(9999);
|
|
695 writer.write(b);
|
15
|
696 }
|
|
697
|
|
698 boolean readProxyFlag() throws IOException, TransportException {
|
17
|
699 int flag = is.readUInt8();
|
15
|
700 if (flag == 1)
|
|
701 return true;
|
|
702 else
|
|
703 return false;
|
|
704 }
|
16
|
705
|
17
|
706 void sendSecurityType(Writer os) throws TransportException {
|
15
|
707 // number-of-security-types
|
17
|
708 os.writeInt(1);
|
15
|
709 // security-types
|
|
710 // 1:None
|
17
|
711 os.writeInt(1);
|
15
|
712
|
|
713 /*
|
|
714 * os.write(4); os.write(30); os.write(31); os.write(32); os.write(35);
|
|
715 * os.flush();
|
|
716 */
|
|
717 }
|
|
718
|
17
|
719 void readSecType(Reader reader) throws TransportException {
|
15
|
720 byte[] b = new byte[1];
|
17
|
721 reader.read(b);
|
15
|
722 }
|
|
723
|
17
|
724 void readSecType(Reader is, Writer os) throws TransportException {
|
15
|
725 byte[] b = new byte[1];
|
17
|
726 is.readBytes(b);
|
15
|
727
|
|
728 int count = 260;
|
|
729 int[] data = { 0, 2, 0, -128, -1, -1, -1, -1, -1, -1, -1, -1, -55, 15,
|
|
730 -38, -94, 33, 104, -62, 52, -60, -58, 98, -117, -128, -36, 28,
|
|
731 -47, 41, 2, 78, 8, -118, 103, -52, 116, 2, 11, -66, -90, 59,
|
|
732 19, -101, 34, 81, 74, 8, 121, -114, 52, 4, -35, -17, -107, 25,
|
|
733 -77, -51, 58, 67, 27, 48, 43, 10, 109, -14, 95, 20, 55, 79,
|
|
734 -31, 53, 109, 109, 81, -62, 69, -28, -123, -75, 118, 98, 94,
|
|
735 126, -58, -12, 76, 66, -23, -90, 55, -19, 107, 11, -1, 92, -74,
|
|
736 -12, 6, -73, -19, -18, 56, 107, -5, 90, -119, -97, -91, -82,
|
|
737 -97, 36, 17, 124, 75, 31, -26, 73, 40, 102, 81, -20, -26, 83,
|
|
738 -127, -1, -1, -1, -1, -1, -1, -1, -1, -111, 73, -29, 30, 57,
|
|
739 -67, -75, -77, -49, -50, -99, -76, -80, -80, 14, 65, 57, -105,
|
|
740 -103, -54, -102, 3, 39, -44, 39, 35, 118, -84, -64, 37, -117,
|
|
741 -21, 89, -31, -68, 70, 5, 122, -92, -119, 9, 121, 63, -112,
|
|
742 -60, 122, -46, -69, -36, 92, -103, -92, 74, 92, -73, 87, 120,
|
|
743 -8, 116, -47, 111, 20, -41, 110, 122, -3, -94, 14, 42, -51,
|
|
744 -59, 48, -54, -125, 117, 60, 77, -52, -31, 98, 32, -2, -102,
|
|
745 -15, -29, 58, -14, -106, -116, -32, -86, 50, -32, -16, -3,
|
|
746 -123, 87, 88, -118, 10, 120, -107, -37, 125, -110, 59, 87, 93,
|
|
747 -24, 124, -99, 18, 78, -13, -49, -34, -24, -27, 1, 114, -67,
|
|
748 -98, -56, -3, 85, -67, -126, 77 };
|
|
749 for (int i = 0; i < count; i++) {
|
|
750 os.write((byte) data[i]);
|
|
751 os.flush();
|
|
752 }
|
|
753
|
|
754 byte[] c = new byte[256];
|
17
|
755 is.readBytes(c);
|
15
|
756
|
|
757 System.out.println(new String(c));
|
|
758
|
|
759 }
|
|
760
|
17
|
761 void sendSecResult(Writer os) throws TransportException {
|
15
|
762 byte[] b = castIntByte(0);
|
|
763 os.write(b);
|
|
764 }
|
16
|
765
|
15
|
766 byte[] castIntByte(int len) {
|
|
767 byte[] b = new byte[4];
|
|
768 b[0] = (byte) ((len >>> 24) & 0xFF);
|
|
769 b[1] = (byte) ((len >>> 16) & 0xFF);
|
|
770 b[2] = (byte) ((len >>> 8) & 0xFF);
|
|
771 b[3] = (byte) ((len >>> 0) & 0xFF);
|
|
772 return b;
|
|
773 }
|
|
774
|
17
|
775 void readClientInit(Reader in) throws TransportException {
|
15
|
776 byte[] b = new byte[0];
|
17
|
777 in.readBytes(b);
|
15
|
778 }
|
|
779
|
17
|
780 void sendInitData(Writer os) throws TransportException {
|
15
|
781 os.write(initData);
|
|
782 }
|
16
|
783
|
|
784 void writeFramebufferUpdateRequest(int x, int y, int w, int h,
|
17
|
785 boolean incremental) throws TransportException {
|
16
|
786 byte[] b = new byte[10];
|
|
787
|
|
788 b[0] = (byte) FramebufferUpdateRequest;
|
|
789 b[1] = (byte) (incremental ? 1 : 0);
|
|
790 b[2] = (byte) ((x >> 8) & 0xff);
|
|
791 b[3] = (byte) (x & 0xff);
|
|
792 b[4] = (byte) ((y >> 8) & 0xff);
|
|
793 b[5] = (byte) (y & 0xff);
|
|
794 b[6] = (byte) ((w >> 8) & 0xff);
|
|
795 b[7] = (byte) (w & 0xff);
|
|
796 b[8] = (byte) ((h >> 8) & 0xff);
|
|
797 b[9] = (byte) (h & 0xff);
|
|
798
|
17
|
799 os.write(b);
|
16
|
800 }
|
18
|
801
|
|
802 public void setInitData(byte[] _initData) {
|
|
803 initData = _initData;
|
|
804 }
|
11
|
805 }
|