Mercurial > hg > Applications > TreeVNC
annotate src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java @ 77:cdf7bbe45885
remove some warnings
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 29 Apr 2014 18:00:34 +0900 |
parents | f1938dd3b518 |
children | 432dac0b9dae |
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 }; | |
92 new Thread(timer).start(); | |
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); | |
105 } catch (IOException e) { | |
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 } catch (TransportException e) { | |
117 e.printStackTrace(); | |
118 } | |
119 } | |
120 } | |
121 }; | |
122 /** | |
123 * send packets to a client | |
124 */ | |
125 Runnable sender = new Runnable() { | |
126 public void run() { | |
127 writerRunning.set(1); | |
128 try { | |
129 requestThreadNotify(); | |
130 // rThread.checkDelay(); | |
131 | |
132 /** | |
133 * initial connection of RFB protocol | |
134 */ | |
135 sendRfbVersion(os); | |
136 // readVersionMsg(is); | |
137 readVersionMsg(is, os); | |
138 sendSecurityType(os); | |
139 readSecType(is); | |
140 sendSecResult(os); | |
141 readClientInit(is); | |
142 sendInitData(os); | |
143 new Thread(reader).start(); // discard incoming packet here | |
144 // after. | |
145 // writeFramebufferUpdateRequest(0,0, framebufferWidth, | |
146 // framebufferHeight, false ); | |
147 for (;;) { | |
148 LinkedList<ByteBuffer> bufs = c.poll(); | |
149 int inputIndex = 0; | |
150 ByteBuffer header = bufs.get(inputIndex); | |
151 if (header == null) | |
152 continue; | |
153 else if (header.get(0) == CheckDelay) { | |
154 writeToClient(os, bufs, inputIndex); | |
155 continue; | |
156 } else if (header.get(0) == FramebufferUpdate) { | |
63 | 157 //System.out.println("client "+ myId); |
28 | 158 } |
159 /* | |
160 * if(i%20==0){ sendDataCheckDelay(); } i++; | |
161 */ | |
162 writeToClient(os, bufs, inputIndex); | |
163 writerRunning.set(1); // yes my client is awaking. | |
164 } | |
165 } catch (IOException e) { | |
166 try { | |
167 writerRunning.set(2); | |
168 os.close(); | |
169 } catch (IOException e1) { | |
170 } | |
171 /* if socket closed cliList.remove(newCli); */ | |
172 } catch (TransportException e) { | |
173 e.printStackTrace(); | |
174 } | |
175 } | |
176 | |
177 public void writeToClient(final Writer os, | |
178 LinkedList<ByteBuffer> bufs, int inputIndex) | |
179 throws TransportException { | |
180 while (inputIndex < bufs.size()) { | |
181 ByteBuffer b = bufs.get(inputIndex++); | |
182 os.write(b.array(), b.position(), b.limit()); | |
183 } | |
184 os.flush(); | |
74 | 185 bufs = null; |
186 multicastqueue.heapAvailable(); | |
28 | 187 } |
188 }; | |
189 clients++; | |
190 new Thread(sender).start(); | |
191 | |
192 } | |
193 | |
194 public synchronized void requestThreadNotify() { | |
195 rThread.reStart(); | |
196 } | |
197 | |
198 private void sendRfbVersion(Writer writer) throws IOException, TransportException { | |
199 // os.write(versionMsg_3_8.getBytes()); | |
54 | 200 writer.write(versionMsg_3_856.getBytes()); |
28 | 201 } |
202 | |
203 private int readVersionMsg(Reader reader, Writer writer) throws IOException, TransportException { | |
204 | |
205 byte[] b = new byte[12]; | |
206 | |
207 reader.readBytes(b); | |
208 | |
209 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') | |
210 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') | |
211 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.') | |
212 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9') | |
213 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) { | |
214 throw new IOException("this is not an RFB server"); | |
215 } | |
216 | |
217 int rfbMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); | |
218 int rfbMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); | |
219 | |
220 if (rfbMajor < 3) { | |
221 throw new IOException( | |
222 "RFB server does not support protocol version 3"); | |
223 } | |
224 | |
225 if (rfbMinor == 855) { | |
226 sendProxyFlag(writer); | |
227 if (proxyFlag) | |
228 sendPortNumber(writer); | |
229 } | |
230 return rfbMinor; | |
231 } | |
45 | 232 |
233 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
|
234 Socket echoSocket = new Socket(proxyAddr, 10002); |
45 | 235 DataOutputStream os = new DataOutputStream(echoSocket.getOutputStream()); |
236 os.writeBytes(echo.getMyAddress()+"\n"); | |
59 | 237 //os.writeBytes(String.valueOf(echo.client.getFrameWidth())+"\n"); temp comment out for rebuild |
238 //os.writeBytes(String.valueOf(echo.client.getFrameHeight())+"\n"); temp comment out for rebuild | |
45 | 239 os.close(); |
77 | 240 echoSocket.close(); |
45 | 241 } |
28 | 242 |
243 private void sendProxyFlag(Writer writer) throws TransportException { | |
244 if (proxyFlag) | |
245 writer.writeInt(1); | |
246 else | |
247 writer.writeInt(0); | |
248 } | |
249 | |
250 private void sendPortNumber(Writer writer) throws TransportException { | |
251 byte[] b = new byte[4]; | |
252 //b = castIntByte(getHost.getPort()); | |
253 b = castIntByte(9999); | |
254 writer.write(b); | |
255 } | |
256 | |
257 private byte[] castIntByte(int len) { | |
258 byte[] b = new byte[4]; | |
259 b[0] = (byte) ((len >>> 24) & 0xFF); | |
260 b[1] = (byte) ((len >>> 16) & 0xFF); | |
261 b[2] = (byte) ((len >>> 8) & 0xFF); | |
262 b[3] = (byte) ((len >>> 0) & 0xFF); | |
263 return b; | |
264 } | |
265 | |
45 | 266 |
28 | 267 private void readSecType(Reader reader) throws TransportException { |
268 byte[] b = new byte[1]; | |
269 reader.read(b); | |
270 } | |
271 | |
272 private void sendSecurityType(Writer os) throws TransportException { | |
273 // number-of-security-types | |
274 os.writeInt(1); | |
275 // security-types | |
276 // 1:None | |
277 os.writeInt(1); | |
278 | |
279 /* | |
280 * os.write(4); os.write(30); os.write(31); os.write(32); os.write(35); | |
281 * os.flush(); | |
282 */ | |
283 } | |
284 | |
285 private void sendSecResult(Writer os) throws TransportException { | |
286 byte[] b = castIntByte(0); | |
287 os.write(b); | |
288 } | |
289 | |
290 private void readClientInit(Reader in) throws TransportException { | |
291 byte[] b = new byte[0]; | |
292 in.readBytes(b); | |
293 } | |
294 | |
295 private void sendInitData(Writer os) throws TransportException { | |
296 os.write(context.getInitData()); | |
297 } | |
298 | |
299 public void setProtocolContext(Protocol workingProtocol) { | |
300 context = workingProtocol; | |
301 } | |
29 | 302 |
303 | |
304 public void readSendData(int dataLen, Reader reader) throws TransportException { | |
305 | |
306 } | |
31 | 307 |
308 public Socket accept() throws IOException { | |
309 return null; | |
310 } | |
311 | |
38 | 312 public int selectPort(int port) { |
313 return port; | |
31 | 314 } |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
315 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
316 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
317 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
|
318 boolean incremental) throws TransportException { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
319 byte[] b = new byte[10]; |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
320 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
321 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
|
322 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
|
323 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
|
324 b[3] = (byte) (x & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
325 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
|
326 b[5] = (byte) (y & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
327 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
|
328 b[7] = (byte) (w & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
329 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
|
330 b[9] = (byte) (h & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
331 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
332 // os.write(b); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
333 } |
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 public void notProxy() { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
336 proxyFlag = false; |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
337 } |
35 | 338 |
339 public void setEcho(EchoClient _echo) { | |
340 echo = _echo; | |
341 } | |
342 | |
65 | 343 public void setViewer(ViewerImpl v) { |
36 | 344 echo.setViewer(v); |
345 } | |
346 | |
65 | 347 public ViewerImpl getViewer() { |
348 return echo.getViewer(); | |
349 } | |
350 | |
35 | 351 public EchoClient getEcho() { |
352 return echo; | |
353 } | |
43 | 354 |
355 public void setTerminationType(boolean setType) { | |
356 /*nop*/ | |
357 } | |
358 | |
359 public boolean getTerminationType() { | |
360 /*nop*/ | |
361 return true; | |
362 } | |
45 | 363 |
364 public void setProxyAddr(String proxyAddr) { | |
365 this.proxyAddr = proxyAddr; | |
366 } | |
52 | 367 |
368 | |
369 public void close() { | |
370 //nothing | |
371 } | |
45 | 372 |
60 | 373 public int getAcceptPort() { |
374 return 0; | |
375 } | |
376 | |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
377 public boolean getReadyReconnect() { |
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
378 return readyReconnect; |
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
379 } |
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
380 |
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
381 public void setReadyReconnect(boolean ready) { |
65 | 382 } |
383 | |
384 | |
385 public boolean getCuiVersion() { | |
386 return cuiVersion; | |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
387 } |
65 | 388 |
389 public void setCuiVersion(boolean flag) { | |
390 cuiVersion = flag; | |
391 } | |
66 | 392 |
393 public void readCheckDelay(Reader reader) throws TransportException { | |
394 | |
395 } | |
396 | |
397 public String getProxyAddr() { | |
398 return proxyAddr; | |
399 } | |
65 | 400 |
28 | 401 } |