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