Mercurial > hg > Members > riono > TreeVNC_ja_comment
annotate src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java @ 89:1503a373ab73
close client correctly in -d mode.
author | oc |
---|---|
date | Fri, 09 May 2014 19:20:33 +0900 |
parents | a719aa609e14 |
children | 75879c316796 |
rev | line source |
---|---|
32 | 1 package jp.ac.u_ryukyu.treevnc; |
28 | 2 |
45 | 3 import java.io.DataOutputStream; |
28 | 4 import java.io.IOException; |
5 import java.net.Socket; | |
6 import java.nio.ByteBuffer; | |
7 import java.util.LinkedList; | |
8 import java.util.concurrent.atomic.AtomicInteger; | |
9 | |
35 | 10 import jp.ac.u_ryukyu.treevnc.client.EchoClient; |
28 | 11 import jp.ac.u_ryukyu.treevnc.server.RequestScreenThread; |
12 | |
13 import com.glavsoft.exceptions.TransportException; | |
14 import com.glavsoft.rfb.protocol.Protocol; | |
15 import com.glavsoft.rfb.protocol.ProtocolContext; | |
16 import com.glavsoft.transport.Reader; | |
17 import com.glavsoft.transport.Writer; | |
65 | 18 import com.glavsoft.viewer.ViewerImpl; |
28 | 19 |
20 public class MyRfbProto { | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
21 final static int FramebufferUpdateRequest = 3; |
28 | 22 final static int CheckDelay = 11; |
23 final static int FramebufferUpdate = 0; | |
24 private ProtocolContext context; | |
54 | 25 //final static String versionMsg_3_855 = "RFB 003.855\n"; |
26 final static String versionMsg_3_856 = "RFB 003.856\n"; | |
28 | 27 private int clients; |
32 | 28 protected MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>(); |
28 | 29 private RequestScreenThread rThread; |
30 private boolean proxyFlag = true; | |
35 | 31 private EchoClient echo; |
45 | 32 private String proxyAddr; |
60 | 33 public int acceptPort; |
65 | 34 protected boolean readyReconnect; |
35 private boolean cuiVersion; | |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
36 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
37 public MyRfbProto() { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
38 rThread = new RequestScreenThread(this); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
39 } |
28 | 40 |
41 | |
42 public void newClient(AcceptThread acceptThread, final Socket newCli, | |
43 final Writer os, final Reader is) throws IOException { | |
44 // createBimgFlag = true; | |
45 // rfb.addSockTmp(newCli); | |
46 // addSock(newCli); | |
47 final int myId = clients; | |
48 final MulticastQueue.Client<LinkedList<ByteBuffer>> c = multicastqueue.newClient(); | |
49 final AtomicInteger writerRunning = new AtomicInteger(); | |
50 writerRunning.set(1); | |
51 /** | |
52 * Timeout thread. If a client is suspended, it has top of queue | |
53 * indefinitely, which caused memory overflow. After the timeout, we | |
54 * poll the queue and discard it. Start long wait if writer is running. | |
55 */ | |
56 final Runnable timer = new Runnable() { | |
57 public void run() { | |
58 int count = 0; | |
59 for (;;) { | |
60 long timeout = 50000 / 8; | |
61 try { | |
62 synchronized (this) { | |
63 int state, flag; | |
64 writerRunning.set(0); | |
65 wait(timeout); | |
66 flag = 0; | |
67 while ((state = writerRunning.get()) == 0) { | |
68 c.poll(); // discard, should be timeout | |
69 count++; | |
70 if (flag == 0) { | |
71 System.out.println("Discarding " + myId | |
72 + " count=" + count); | |
73 flag = 1; | |
74 } | |
75 wait(10); // if this is too short, writer cannot | |
76 // take the poll, if this is too | |
77 // long, memory will overflow... | |
78 } | |
79 if (flag == 1) | |
80 System.out.println("Resuming " + myId | |
81 + " count=" + count); | |
82 if (state != 1) { | |
83 System.out.println("Client died " + myId); | |
84 break; | |
85 } | |
86 } | |
87 } catch (InterruptedException e) { | |
88 } | |
89 } | |
90 } | |
91 }; | |
88 | 92 new Thread(timer, "timer-discard-multicastqueue").start(); |
28 | 93 /** |
94 * discard all incoming from clients | |
95 */ | |
96 final Runnable reader = new Runnable() { | |
97 public void run() { | |
98 byte b[] = new byte[4096]; | |
99 for (;;) { | |
100 try { | |
101 int c = is.readByte(b); | |
102 if (c <= 0) | |
103 throw new IOException(); | |
104 // System.out.println("client read "+c); | |
89 | 105 } catch (Exception e) { |
28 | 106 try { |
107 writerRunning.set(2); | |
108 os.close(); | |
109 is.close(); | |
36 | 110 break; |
28 | 111 } catch (IOException e1) { |
112 } catch (TransportException e1) { | |
113 e1.printStackTrace(); | |
114 } | |
115 return; | |
116 } | |
117 } | |
118 } | |
119 }; | |
120 /** | |
121 * send packets to a client | |
122 */ | |
123 Runnable sender = new Runnable() { | |
124 public void run() { | |
125 writerRunning.set(1); | |
126 try { | |
127 requestThreadNotify(); | |
128 | |
129 /** | |
130 * initial connection of RFB protocol | |
131 */ | |
132 sendRfbVersion(os); | |
133 // readVersionMsg(is); | |
134 readVersionMsg(is, os); | |
135 sendSecurityType(os); | |
136 readSecType(is); | |
137 sendSecResult(os); | |
138 readClientInit(is); | |
139 sendInitData(os); | |
88 | 140 // after this, we discard upward packet. |
141 new Thread(reader, "discard-upward-comm").start(); | |
28 | 142 // writeFramebufferUpdateRequest(0,0, framebufferWidth, |
143 // framebufferHeight, false ); | |
144 for (;;) { | |
145 LinkedList<ByteBuffer> bufs = c.poll(); | |
146 int inputIndex = 0; | |
147 ByteBuffer header = bufs.get(inputIndex); | |
148 if (header == null) | |
149 continue; | |
150 else if (header.get(0) == CheckDelay) { | |
151 writeToClient(os, bufs, inputIndex); | |
152 continue; | |
153 } else if (header.get(0) == FramebufferUpdate) { | |
63 | 154 //System.out.println("client "+ myId); |
28 | 155 } |
156 /* | |
157 * if(i%20==0){ sendDataCheckDelay(); } i++; | |
158 */ | |
159 writeToClient(os, bufs, inputIndex); | |
160 writerRunning.set(1); // yes my client is awaking. | |
161 } | |
89 | 162 } catch (Exception e) { |
28 | 163 try { |
164 writerRunning.set(2); | |
165 os.close(); | |
166 } catch (IOException e1) { | |
167 } | |
168 /* if socket closed cliList.remove(newCli); */ | |
169 } | |
170 } | |
171 | |
172 public void writeToClient(final Writer os, | |
173 LinkedList<ByteBuffer> bufs, int inputIndex) | |
174 throws TransportException { | |
175 while (inputIndex < bufs.size()) { | |
176 ByteBuffer b = bufs.get(inputIndex++); | |
177 os.write(b.array(), b.position(), b.limit()); | |
178 } | |
179 os.flush(); | |
74 | 180 bufs = null; |
181 multicastqueue.heapAvailable(); | |
28 | 182 } |
183 }; | |
184 clients++; | |
88 | 185 new Thread(sender, "writer-to-lower-node").start(); |
28 | 186 |
187 } | |
188 | |
88 | 189 public void requestThreadNotify() { |
28 | 190 rThread.reStart(); |
191 } | |
192 | |
193 private void sendRfbVersion(Writer writer) throws IOException, TransportException { | |
194 // os.write(versionMsg_3_8.getBytes()); | |
54 | 195 writer.write(versionMsg_3_856.getBytes()); |
28 | 196 } |
197 | |
198 private int readVersionMsg(Reader reader, Writer writer) throws IOException, TransportException { | |
199 | |
200 byte[] b = new byte[12]; | |
201 | |
202 reader.readBytes(b); | |
203 | |
204 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') | |
205 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') | |
206 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.') | |
207 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9') | |
208 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) { | |
209 throw new IOException("this is not an RFB server"); | |
210 } | |
211 | |
212 int rfbMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); | |
213 int rfbMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); | |
214 | |
215 if (rfbMajor < 3) { | |
216 throw new IOException( | |
217 "RFB server does not support protocol version 3"); | |
218 } | |
219 | |
220 if (rfbMinor == 855) { | |
221 sendProxyFlag(writer); | |
222 if (proxyFlag) | |
223 sendPortNumber(writer); | |
224 } | |
225 return rfbMinor; | |
226 } | |
45 | 227 |
228 public void screenChangeRequest() throws IOException { | |
64
e73e2c30c9a6
before update for build CUI version.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
229 Socket echoSocket = new Socket(proxyAddr, 10002); |
45 | 230 DataOutputStream os = new DataOutputStream(echoSocket.getOutputStream()); |
231 os.writeBytes(echo.getMyAddress()+"\n"); | |
59 | 232 //os.writeBytes(String.valueOf(echo.client.getFrameWidth())+"\n"); temp comment out for rebuild |
233 //os.writeBytes(String.valueOf(echo.client.getFrameHeight())+"\n"); temp comment out for rebuild | |
45 | 234 os.close(); |
77 | 235 echoSocket.close(); |
45 | 236 } |
28 | 237 |
238 private void sendProxyFlag(Writer writer) throws TransportException { | |
239 if (proxyFlag) | |
240 writer.writeInt(1); | |
241 else | |
242 writer.writeInt(0); | |
243 } | |
244 | |
245 private void sendPortNumber(Writer writer) throws TransportException { | |
246 byte[] b = new byte[4]; | |
247 //b = castIntByte(getHost.getPort()); | |
248 b = castIntByte(9999); | |
249 writer.write(b); | |
250 } | |
251 | |
252 private byte[] castIntByte(int len) { | |
253 byte[] b = new byte[4]; | |
254 b[0] = (byte) ((len >>> 24) & 0xFF); | |
255 b[1] = (byte) ((len >>> 16) & 0xFF); | |
256 b[2] = (byte) ((len >>> 8) & 0xFF); | |
257 b[3] = (byte) ((len >>> 0) & 0xFF); | |
258 return b; | |
259 } | |
260 | |
45 | 261 |
28 | 262 private void readSecType(Reader reader) throws TransportException { |
263 byte[] b = new byte[1]; | |
264 reader.read(b); | |
265 } | |
266 | |
267 private void sendSecurityType(Writer os) throws TransportException { | |
268 // number-of-security-types | |
269 os.writeInt(1); | |
270 // security-types | |
271 // 1:None | |
272 os.writeInt(1); | |
273 | |
274 /* | |
275 * os.write(4); os.write(30); os.write(31); os.write(32); os.write(35); | |
276 * os.flush(); | |
277 */ | |
278 } | |
279 | |
280 private void sendSecResult(Writer os) throws TransportException { | |
281 byte[] b = castIntByte(0); | |
282 os.write(b); | |
283 } | |
284 | |
285 private void readClientInit(Reader in) throws TransportException { | |
286 byte[] b = new byte[0]; | |
287 in.readBytes(b); | |
288 } | |
289 | |
85 | 290 byte initData[] = {7, -128, 4, 56, 32, 24, 0, 1, 0, -1, 0, -1, 0, -1, 16, 8, 0, 0, 0, 0, 0, 0, 0, 7, 102, 105, 114, 101, 102, 108, 121}; |
28 | 291 private void sendInitData(Writer os) throws TransportException { |
85 | 292 // In case of "-d" we have no context |
293 if (context != null){ | |
294 os.write(context.getInitData()); | |
295 } else { | |
296 // Send dummy data | |
297 os.write(initData); | |
298 | |
299 } | |
28 | 300 } |
301 | |
302 public void setProtocolContext(Protocol workingProtocol) { | |
303 context = workingProtocol; | |
304 } | |
29 | 305 |
306 | |
307 public void readSendData(int dataLen, Reader reader) throws TransportException { | |
308 | |
309 } | |
31 | 310 |
311 public Socket accept() throws IOException { | |
312 return null; | |
313 } | |
314 | |
38 | 315 public int selectPort(int port) { |
316 return port; | |
31 | 317 } |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
318 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
319 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
320 public void writeFramebufferUpdateRequest(int x, int y, int w, int h, |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
321 boolean incremental) throws TransportException { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
322 byte[] b = new byte[10]; |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
323 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
324 b[0] = (byte) FramebufferUpdateRequest; // 3 is FrameBufferUpdateRequest |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
325 b[1] = (byte) (incremental ? 1 : 0); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
326 b[2] = (byte) ((x >> 8) & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
327 b[3] = (byte) (x & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
328 b[4] = (byte) ((y >> 8) & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
329 b[5] = (byte) (y & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
330 b[6] = (byte) ((w >> 8) & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
331 b[7] = (byte) (w & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
332 b[8] = (byte) ((h >> 8) & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
333 b[9] = (byte) (h & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
334 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
335 // os.write(b); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
336 } |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
337 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
338 public void notProxy() { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
339 proxyFlag = false; |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
340 } |
35 | 341 |
342 public void setEcho(EchoClient _echo) { | |
343 echo = _echo; | |
344 } | |
345 | |
65 | 346 public void setViewer(ViewerImpl v) { |
36 | 347 echo.setViewer(v); |
348 } | |
349 | |
65 | 350 public ViewerImpl getViewer() { |
351 return echo.getViewer(); | |
352 } | |
353 | |
35 | 354 public EchoClient getEcho() { |
355 return echo; | |
356 } | |
43 | 357 |
358 public void setTerminationType(boolean setType) { | |
359 /*nop*/ | |
360 } | |
361 | |
362 public boolean getTerminationType() { | |
363 /*nop*/ | |
364 return true; | |
365 } | |
45 | 366 |
367 public void setProxyAddr(String proxyAddr) { | |
368 this.proxyAddr = proxyAddr; | |
369 } | |
52 | 370 |
371 | |
372 public void close() { | |
373 //nothing | |
374 } | |
45 | 375 |
60 | 376 public int getAcceptPort() { |
377 return 0; | |
378 } | |
379 | |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
380 public boolean getReadyReconnect() { |
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
381 return readyReconnect; |
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
382 } |
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
383 |
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
384 public void setReadyReconnect(boolean ready) { |
65 | 385 } |
386 | |
387 | |
388 public boolean getCuiVersion() { | |
389 return cuiVersion; | |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
390 } |
65 | 391 |
392 public void setCuiVersion(boolean flag) { | |
393 cuiVersion = flag; | |
394 } | |
66 | 395 |
396 public void readCheckDelay(Reader reader) throws TransportException { | |
397 | |
398 } | |
399 | |
400 public String getProxyAddr() { | |
401 return proxyAddr; | |
402 } | |
65 | 403 |
28 | 404 } |