3
|
1 package ac.ryukyu.treevnc.client;
|
|
2
|
|
3 import java.io.BufferedInputStream;
|
|
4 import java.io.DataInputStream;
|
|
5 import java.io.DataOutputStream;
|
|
6 import java.io.IOException;
|
|
7 import java.io.InputStream;
|
|
8 import java.io.OutputStream;
|
4
|
9 import java.net.BindException;
|
3
|
10 import java.net.InetAddress;
|
4
|
11 import java.net.ServerSocket;
|
3
|
12 import java.net.Socket;
|
|
13 import java.nio.ByteBuffer;
|
4
|
14 import java.util.LinkedList;
|
|
15 import java.util.concurrent.atomic.AtomicInteger;
|
|
16 import java.util.zip.DataFormatException;
|
|
17 import java.util.zip.Deflater;
|
|
18 import java.util.zip.Inflater;
|
3
|
19
|
4
|
20 import ac.ryukyu.*;
|
|
21 import ac.ryukyu.treevnc.MulticastQueue;
|
3
|
22
|
|
23 import com.glavsoft.exceptions.TransportException;
|
4
|
24 import com.glavsoft.rfb.encoding.EncodingType;
|
3
|
25 import com.glavsoft.transport.Reader;
|
|
26 import com.glavsoft.viewer.ContainerManager;
|
|
27 import com.glavsoft.viewer.swing.ParametersHandler.ConnectionParams;
|
|
28
|
|
29 public class MyRfbProtoClient {
|
4
|
30 final static int FramebufferUpdate = 0;
|
|
31 final static int CheckDelay = 11;
|
|
32 final static String versionMsg_3_855 = "RFB 003.855\n";
|
|
33 private static final int INFLATE_BUFSIZE = 1024*100;
|
3
|
34 private Reader reader;
|
|
35 private EchoClient echoValue = new EchoClient();
|
|
36 private String host,treenum,parent,pHost,leaderflag;
|
4
|
37 private int echoPort,port,acceptPort;
|
3
|
38 Socket clientSocket,sock;
|
|
39 DataInputStream is;
|
|
40 OutputStream os;
|
|
41 private ContainerManager containerManager;
|
4
|
42 private ServerSocket servSock;
|
|
43 private byte initData[];
|
|
44 private int clients = 0;
|
|
45 private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>();
|
|
46 boolean proxyFlag = false;
|
|
47 int serverMajor, serverMinor;
|
|
48 int clientMajor, clientMinor;
|
3
|
49
|
4
|
50 private Inflater inflater = new Inflater();
|
|
51 private Deflater deflater = new Deflater();
|
3
|
52
|
|
53 public MyRfbProtoClient(Reader reader,String host,String port) {
|
|
54 this.reader = reader;
|
|
55 }
|
|
56
|
|
57 public void setParam(ConnectionParams connectionParams) {
|
|
58 pHost = connectionParams.hostName;
|
|
59 echoPort = connectionParams.portNumber;
|
|
60 }
|
|
61
|
|
62 public boolean readProxyFlag() throws TransportException {
|
|
63 int flag = reader.readUInt8();
|
|
64 if(flag == 1)
|
|
65 return true;
|
|
66 else
|
|
67 return false;
|
|
68 }
|
|
69
|
|
70 public byte[] readEchoPort() throws Exception {
|
|
71 byte[] b = new byte[4];
|
|
72 reader.readBytes(b, 0, b.length);
|
|
73 //readFully(b);
|
|
74 return b;
|
|
75 }
|
|
76
|
|
77 public void getParentName() {
|
|
78 if (echoValue == null) {
|
|
79
|
|
80 if (clientSocket == null) {
|
|
81
|
|
82 // echo = new EchoClient(pHost, this);
|
|
83 echoValue = new EchoClient(pHost, echoPort);
|
|
84 echoValue.openport();
|
|
85
|
|
86 echoValue = echoValue.requestHostName("1");
|
|
87 } else {
|
|
88 echoValue = new EchoClient();
|
|
89 echoValue = echoValue.Interruption(clientSocket);
|
|
90 }
|
|
91 }
|
|
92 // proxyからの返信で接続先を決定する
|
|
93 host = echoValue.responseLine;
|
|
94 parent = echoValue.parent;
|
|
95 if (echoValue.treenum != null) {
|
|
96 treenum = echoValue.treenum;
|
|
97 } else {
|
|
98 treenum = echoValue.treenum;
|
|
99 }
|
|
100
|
|
101 if (echoValue.leaderflag != null) {
|
|
102 leaderflag = echoValue.leaderflag;
|
|
103 } else {
|
|
104 leaderflag = echoValue.leaderflag;
|
|
105 }
|
|
106 }
|
|
107
|
|
108 int castByteInt(byte[] b) {
|
|
109 ByteBuffer bb = ByteBuffer.wrap(b);
|
|
110 int value = bb.getInt();
|
|
111 return value;
|
|
112 }
|
|
113
|
|
114 Socket changeParent(String host, int port) throws IOException {
|
|
115 sock = new Socket(host, port);
|
|
116 return sock;
|
|
117 }
|
|
118
|
4
|
119 public Socket accept() throws IOException {
|
|
120 return servSock.accept();
|
|
121 }
|
|
122
|
|
123 void initServSock(int port) throws IOException {
|
|
124 servSock = new ServerSocket(port);
|
|
125 acceptPort = port;
|
|
126 }
|
|
127
|
|
128 public void selectPort(int p) {
|
|
129 int port = p;
|
|
130 while (true) {
|
|
131 try {
|
|
132 initServSock(port);
|
|
133 break;
|
|
134 } catch (BindException e) {
|
|
135 port++;
|
|
136 continue;
|
|
137 } catch (IOException e) {
|
|
138
|
|
139 }
|
|
140 }
|
|
141 System.out.println("accept port = " + port);
|
|
142 }
|
|
143
|
|
144 public void newClient(AcceptThread acceptThread, final Socket newCli,
|
|
145 final OutputStream os, final InputStream is) throws IOException {
|
|
146 // createBimgFlag = true;
|
|
147 // rfb.addSockTmp(newCli);
|
|
148 // addSock(newCli);
|
|
149 final int myId = clients;
|
|
150 final MulticastQueue.Client <LinkedList<ByteBuffer>> c = multicastqueue.newClient();
|
|
151 final AtomicInteger writerRunning = new AtomicInteger();
|
|
152 writerRunning.set(1);
|
|
153 /**
|
|
154 * Timeout thread. If a client is suspended, it has top of queue indefinitely, which caused memory
|
|
155 * overflow. After the timeout, we poll the queue and discard it. Start long wait if writer is running.
|
|
156 */
|
|
157 final Runnable timer = new Runnable() {
|
|
158 public void run() {
|
|
159 int count = 0;
|
|
160 for(;;) {
|
|
161 long timeout = 40000/8;
|
|
162 try {
|
|
163 synchronized(this) {
|
|
164 int state,flag;
|
|
165 writerRunning.set(0);
|
|
166 wait(timeout);
|
|
167 flag = 0;
|
|
168 while((state=writerRunning.get())==0) {
|
|
169 c.poll(); // discard, should be timeout
|
|
170 count++;
|
|
171 if (flag==0) {
|
|
172 System.out.println("Discarding "+myId + " count="+ count); flag = 1;
|
|
173 }
|
|
174 wait(10); // if this is too short, writer cannot take the poll, if this is too long, memory will overflow...
|
|
175 }
|
|
176 if (flag==1) System.out.println("Resuming "+myId + " count="+count);
|
|
177 if (state!=1) {
|
|
178 System.out.println("Client died "+myId);
|
|
179 break;
|
|
180 }
|
|
181 }
|
|
182 } catch (InterruptedException e) {
|
|
183 }
|
|
184 }
|
|
185 }
|
|
186 };
|
|
187 new Thread(timer).start();
|
|
188 /**
|
|
189 * discard all incoming from clients
|
|
190 */
|
|
191 final Runnable reader = new Runnable() {
|
|
192 public void run() {
|
|
193 byte b[] = new byte[4096];
|
|
194 for(;;) {
|
|
195 try {
|
|
196 int c = is.read(b);
|
|
197 if (c<=0) throw new IOException();
|
|
198 // System.out.println("client read "+c);
|
|
199 } catch (IOException e) {
|
|
200 try {
|
|
201 writerRunning.set(2);
|
|
202 os.close();
|
|
203 is.close();
|
|
204 } catch (IOException e1) {
|
|
205 }
|
|
206 return;
|
|
207 }
|
|
208 }
|
|
209 }
|
|
210 };
|
|
211 /**
|
|
212 * send packets to a client
|
|
213 */
|
|
214 Runnable sender = new Runnable() {
|
|
215 public void run() {
|
|
216 writerRunning.set(1);
|
|
217 try {
|
|
218 /**
|
|
219 * initial connection of RFB protocol
|
|
220 */
|
|
221 sendRfbVersion(os);
|
|
222 // readVersionMsg(is);
|
|
223 int rfbMinor = readVersionMsg(is,os);
|
|
224 sendSecurityType(os);
|
|
225 readSecType(is);
|
|
226 sendSecResult(os);
|
|
227 readClientInit(is);
|
|
228 sendInitData(os);
|
|
229 new Thread(reader).start(); // discard incoming packet here after.
|
|
230 if(rfbMinor == 855){
|
|
231 //checkDilay(os);
|
|
232 // send jpeg data of full screen.
|
|
233 // sendFullScreen("jpeg" ,os);
|
|
234 } else {
|
|
235 // send raw data of full screen.
|
|
236
|
|
237 }
|
|
238 for (;;) {
|
|
239 LinkedList<ByteBuffer> bufs = c.poll();
|
|
240 int inputIndex = 0;
|
|
241 ByteBuffer header = bufs.get(inputIndex);
|
|
242 if (header==null) continue;
|
|
243 if (header.get(0)==CheckDelay) {
|
|
244 System.out.println("--------------------");
|
|
245 //writeToClient(os, bufs, inputIndex);
|
|
246 }
|
|
247 if (header.get(0)==FramebufferUpdate) {
|
|
248 // System.out.println("client "+ myId);
|
|
249 }
|
|
250 writeToClient(os, bufs, inputIndex);
|
|
251 writerRunning.set(1); // yes my client is awaking.
|
|
252 }
|
|
253 } catch (IOException e) {
|
|
254 try {
|
|
255 writerRunning.set(2);
|
|
256 os.close();
|
|
257 } catch (IOException e1) {
|
|
258 /* if socket closed cliList.remove(newCli); */
|
|
259 }
|
|
260 }
|
|
261 }
|
|
262
|
|
263 public void writeToClient(final OutputStream os,
|
|
264 LinkedList<ByteBuffer> bufs, int inputIndex)
|
|
265 throws IOException {
|
|
266 while(inputIndex < bufs.size()) {
|
|
267 ByteBuffer b = bufs.get(inputIndex++);
|
|
268 os.write(b.array(), b.position(), b.limit());
|
|
269 }
|
|
270 os.flush();
|
|
271 }
|
|
272 };
|
|
273 clients++;
|
|
274 new Thread(sender).start();
|
|
275
|
|
276 }
|
|
277
|
|
278 void sendRfbVersion(OutputStream os) throws IOException {
|
|
279 os.write(versionMsg_3_855.getBytes());
|
|
280 // os.write(versionMsg_3_8.getBytes());
|
|
281 }
|
|
282 int readVersionMsg(InputStream is, OutputStream os) throws IOException {
|
|
283
|
|
284 byte[] b = new byte[12];
|
|
285
|
|
286 is.read(b);
|
|
287
|
|
288 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ')
|
|
289 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9')
|
|
290 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.')
|
|
291 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9')
|
|
292 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) {
|
|
293 throw new IOException("Host " + host + " port " + port
|
|
294 + " is not an RFB server");
|
|
295 }
|
|
296
|
|
297 int rfbMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
|
|
298 int rfbMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
|
|
299
|
|
300 if (rfbMajor < 3) {
|
|
301 throw new IOException(
|
|
302 "RFB server does not support protocol version 3");
|
|
303 }
|
|
304
|
|
305 if (rfbMinor == 855) {
|
|
306 sendProxyFlag(os);
|
|
307 // if(proxyFlag)sendPortNumber(os);
|
|
308 }
|
|
309 return rfbMinor;
|
|
310
|
|
311 } void readVersionMsg(InputStream is) throws IOException {
|
|
312
|
|
313 byte[] b = new byte[12];
|
|
314
|
|
315 is.read(b);
|
|
316
|
|
317 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ')
|
|
318 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9')
|
|
319 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.')
|
|
320 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9')
|
|
321 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) {
|
|
322 throw new IOException("Host " + host + " port " + port
|
|
323 + " is not an RFB server");
|
|
324 }
|
|
325
|
|
326 serverMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0');
|
|
327 serverMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0');
|
|
328
|
|
329 if (serverMajor < 3) {
|
|
330 throw new IOException(
|
|
331 "RFB server does not support protocol version 3");
|
|
332 }
|
|
333
|
|
334 }
|
|
335
|
|
336 void sendSecurityType(OutputStream os) throws IOException {
|
|
337 // number-of-security-types
|
|
338 os.write(1);
|
|
339 // security-types
|
|
340 // 1:None
|
|
341 os.write(1);
|
|
342 }
|
|
343 void readSecType(InputStream is) throws IOException {
|
|
344 byte[] b = new byte[1];
|
|
345 is.read(b);
|
|
346
|
|
347 }
|
|
348
|
|
349 void sendSecResult(OutputStream os) throws IOException {
|
|
350 byte[] b = castIntByte(0);
|
|
351 os.write(b);
|
|
352 }
|
|
353
|
|
354 void readClientInit(InputStream in) throws IOException {
|
|
355 byte[] b = new byte[0];
|
|
356 in.read(b);
|
|
357 }
|
|
358
|
|
359 void sendInitData(OutputStream os) throws IOException {
|
|
360 os.write(initData);
|
|
361 }
|
|
362
|
|
363 void sendProxyFlag(OutputStream os) throws IOException {
|
|
364 if(proxyFlag) os.write(1);
|
|
365 else os.write(0);
|
|
366 }
|
|
367
|
|
368 byte[] castIntByte(int len) {
|
|
369 byte[] b = new byte[4];
|
|
370 b[0] = (byte) ((len >>> 24) & 0xFF);
|
|
371 b[1] = (byte) ((len >>> 16) & 0xFF);
|
|
372 b[2] = (byte) ((len >>> 8) & 0xFF);
|
|
373 b[3] = (byte) ((len >>> 0) & 0xFF);
|
|
374 return b;
|
|
375 }
|
|
376
|
|
377 /**
|
|
378 * gzip byte arrays
|
|
379 * @param deflater
|
|
380 * @param inputs
|
|
381 * byte data[]
|
|
382 * @param inputIndex
|
|
383 * @param outputs
|
|
384 * byte data[]
|
|
385 * @return byte length in last byte array
|
|
386 * @throws IOException
|
|
387 */
|
|
388 public int zip(Deflater deflater,LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException {
|
|
389 int len = 0;
|
|
390 ByteBuffer c1= ByteBuffer.allocate(INFLATE_BUFSIZE);
|
|
391 while(inputIndex < inputs.size() ) {
|
|
392 ByteBuffer b1 = inputs.get(inputIndex++);
|
|
393 deflater.setInput(b1.array(),b1.position(),b1.remaining());
|
|
394 /**
|
|
395 * If we finish() stream and reset() it, Deflater start new gzip stream, this makes continuous zlib reader unhappy.
|
|
396 * if we remove finish(), Deflater.deflate() never flushes its output. The original zlib deflate has flush flag. I'm pretty
|
|
397 * sure this a kind of bug of Java library.
|
|
398 */
|
|
399 if (inputIndex==inputs.size())
|
|
400 deflater.finish();
|
|
401 int len1 = 0;
|
|
402 do {
|
|
403 len1 = deflater.deflate(c1.array(),c1.position(),c1.remaining());
|
|
404 if (len1>0) {
|
|
405 len += len1;
|
|
406 c1.position(c1.position()+len1);
|
|
407 if (c1.remaining()==0) {
|
|
408 c1.flip(); outputs.addLast(c1);
|
|
409 c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
|
|
410 }
|
|
411 }
|
|
412 } while (len1 >0 || !deflater.needsInput()); // &&!deflater.finished());
|
|
413 }
|
|
414 if (c1.position()!=0) {
|
|
415 c1.flip(); outputs.addLast(c1);
|
|
416 }
|
|
417 deflater.reset();
|
|
418 return len;
|
|
419 }
|
|
420
|
|
421
|
|
422 /**
|
|
423 * gunzip byte arrays
|
|
424 * @param inflater
|
|
425 * @param inputs
|
|
426 * byte data[]
|
|
427 * @param outputs
|
|
428 * byte data[]
|
|
429 *@return number of total bytes
|
|
430 * @throws IOException
|
|
431 */
|
|
432 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs,
|
|
433 int inputIndex, LinkedList<ByteBuffer> outputs, int bufSize)
|
|
434 throws DataFormatException {
|
|
435 int len = 0;
|
|
436 ByteBuffer buf = ByteBuffer.allocate(bufSize);
|
|
437 while (inputIndex < inputs.size()) {
|
|
438 ByteBuffer input = inputs.get(inputIndex++);
|
|
439 inflater.setInput(input.array(), input.position(), input.limit());
|
|
440 // if (inputIndex==inputs.size()) if inflater/deflater has symmetry,
|
|
441 // we need this
|
|
442 // inflater.end(); but this won't work
|
|
443 do {
|
|
444 int len0 = inflater.inflate(buf.array(), buf.position(),
|
|
445 buf.remaining());
|
|
446 if (len0 > 0) {
|
|
447 buf.position(buf.position() + len0);
|
|
448 len += len0;
|
|
449 if (buf.remaining() == 0) {
|
|
450 buf.flip();
|
|
451 outputs.addLast(buf);
|
|
452 buf = ByteBuffer.allocate(bufSize);
|
|
453 }
|
|
454 }
|
|
455 } while (!inflater.needsInput());
|
|
456 }
|
|
457 if (buf.position() != 0) {
|
|
458 buf.flip();
|
|
459 outputs.addLast(buf);
|
|
460 }
|
|
461 return len;
|
|
462 }
|
|
463
|
|
464 void readSendData(int dataLen,Reader reader) throws IOException, DataFormatException, TransportException {
|
|
465 LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>();
|
|
466 ByteBuffer header = ByteBuffer.allocate(16);
|
|
467 reader.readBytes(header.array(),0,16);
|
|
468 header.limit(16);
|
|
469 if (header.get(0)==FramebufferUpdate) {
|
|
470 int encoding = header.getInt(12);
|
|
471 if (encoding==EncodingType.ZRLE.getId()||encoding==EncodingType.ZLIB.getId()) { // ZRLEE is already recompressed
|
|
472 ByteBuffer len = ByteBuffer.allocate(4);
|
|
473 reader.readBytes(len.array(),0,4); len.limit(4);
|
|
474 ByteBuffer inputData = ByteBuffer.allocate(dataLen-20);
|
|
475 reader.readBytes(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20);
|
|
476 LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>();
|
|
477 inputs.add(inputData);
|
|
478
|
|
479 header.putInt(12, EncodingType.ZRLEE.getId()); // means recompress every time
|
|
480 // using new Deflecter every time is incompatible with the protocol, clients have to be modified.
|
|
481 Deflater nDeflater = deflater; // new Deflater();
|
|
482 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>();
|
|
483 unzip(inflater, inputs, 0 , out, INFLATE_BUFSIZE);
|
|
484 // dump32(inputs);
|
|
485 int len2 = zip(nDeflater, out, 0, bufs);
|
|
486 ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip();
|
|
487 bufs.addFirst(blen);
|
|
488
|
|
489 bufs.addFirst(header);
|
|
490 multicastqueue.put(bufs);
|
|
491 is.reset();
|
|
492 return ;
|
|
493 }
|
|
494 }
|
|
495 bufs.add(header);
|
|
496 if (dataLen>16) {
|
|
497 ByteBuffer b = ByteBuffer.allocate(dataLen-16);
|
|
498 reader.readBytes(b.array(),0,dataLen-16); b.limit(dataLen-16);
|
|
499 bufs.add(b);
|
|
500 }
|
|
501 multicastqueue.put(bufs);
|
|
502 is.reset();
|
|
503
|
|
504 // It may be compressed. We can inflate here to avoid repeating clients decompressing here,
|
|
505 // but it may generate too many large data. It is better to do it in each client.
|
|
506 // But we have do inflation for all input data, so we have to do it here.
|
|
507 }
|
|
508
|
|
509
|
|
510
|
3
|
511 }
|