Mercurial > hg > Applications > TreeVNC
annotate src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java @ 59:433c79184c05
merge version2.7.2
author | Taninari YU <you@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 17 Dec 2013 20:16:48 +0900 |
parents | 9fdb8ff88497 |
children | ac6f9e46566f |
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; | |
36 | 18 import com.glavsoft.viewer.Viewer; |
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; |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
33 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
34 public MyRfbProto() { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
35 rThread = new RequestScreenThread(this); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
36 } |
28 | 37 |
38 | |
39 public void newClient(AcceptThread acceptThread, final Socket newCli, | |
40 final Writer os, final Reader is) throws IOException { | |
41 // createBimgFlag = true; | |
42 // rfb.addSockTmp(newCli); | |
43 // addSock(newCli); | |
44 final int myId = clients; | |
45 final MulticastQueue.Client<LinkedList<ByteBuffer>> c = multicastqueue.newClient(); | |
46 final AtomicInteger writerRunning = new AtomicInteger(); | |
47 writerRunning.set(1); | |
48 /** | |
49 * Timeout thread. If a client is suspended, it has top of queue | |
50 * indefinitely, which caused memory overflow. After the timeout, we | |
51 * poll the queue and discard it. Start long wait if writer is running. | |
52 */ | |
53 final Runnable timer = new Runnable() { | |
54 public void run() { | |
55 int count = 0; | |
56 for (;;) { | |
57 long timeout = 50000 / 8; | |
58 try { | |
59 synchronized (this) { | |
60 int state, flag; | |
61 writerRunning.set(0); | |
62 wait(timeout); | |
63 flag = 0; | |
64 while ((state = writerRunning.get()) == 0) { | |
65 c.poll(); // discard, should be timeout | |
66 count++; | |
67 if (flag == 0) { | |
68 System.out.println("Discarding " + myId | |
69 + " count=" + count); | |
70 flag = 1; | |
71 } | |
72 wait(10); // if this is too short, writer cannot | |
73 // take the poll, if this is too | |
74 // long, memory will overflow... | |
75 } | |
76 if (flag == 1) | |
77 System.out.println("Resuming " + myId | |
78 + " count=" + count); | |
79 if (state != 1) { | |
80 System.out.println("Client died " + myId); | |
81 break; | |
82 } | |
83 } | |
84 } catch (InterruptedException e) { | |
85 } | |
86 } | |
87 } | |
88 }; | |
89 new Thread(timer).start(); | |
90 /** | |
91 * discard all incoming from clients | |
92 */ | |
93 final Runnable reader = new Runnable() { | |
94 public void run() { | |
95 byte b[] = new byte[4096]; | |
96 for (;;) { | |
97 try { | |
98 int c = is.readByte(b); | |
99 if (c <= 0) | |
100 throw new IOException(); | |
101 // System.out.println("client read "+c); | |
102 } catch (IOException e) { | |
103 try { | |
104 writerRunning.set(2); | |
105 os.close(); | |
106 is.close(); | |
36 | 107 break; |
28 | 108 } catch (IOException e1) { |
109 } catch (TransportException e1) { | |
110 e1.printStackTrace(); | |
111 } | |
112 return; | |
113 } catch (TransportException e) { | |
114 e.printStackTrace(); | |
115 } | |
116 } | |
117 } | |
118 }; | |
119 /** | |
120 * send packets to a client | |
121 */ | |
122 Runnable sender = new Runnable() { | |
123 public void run() { | |
124 writerRunning.set(1); | |
125 try { | |
126 requestThreadNotify(); | |
127 // rThread.checkDelay(); | |
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); | |
140 new Thread(reader).start(); // discard incoming packet here | |
141 // after. | |
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) { | |
154 // System.out.println("client "+ myId); | |
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 } | |
162 } catch (IOException e) { | |
163 try { | |
164 writerRunning.set(2); | |
165 os.close(); | |
166 } catch (IOException e1) { | |
167 } | |
168 /* if socket closed cliList.remove(newCli); */ | |
169 } catch (TransportException e) { | |
170 e.printStackTrace(); | |
171 } | |
172 } | |
173 | |
174 public void writeToClient(final Writer os, | |
175 LinkedList<ByteBuffer> bufs, int inputIndex) | |
176 throws TransportException { | |
177 while (inputIndex < bufs.size()) { | |
178 ByteBuffer b = bufs.get(inputIndex++); | |
179 os.write(b.array(), b.position(), b.limit()); | |
180 } | |
181 os.flush(); | |
182 } | |
183 }; | |
184 clients++; | |
185 new Thread(sender).start(); | |
186 | |
187 } | |
188 | |
189 public synchronized void requestThreadNotify() { | |
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 { | |
229 Socket echoSocket; | |
230 echoSocket = new Socket(proxyAddr, 10001); | |
231 DataOutputStream os = new DataOutputStream(echoSocket.getOutputStream()); | |
232 os.writeBytes(echo.getMyAddress()+"\n"); | |
59 | 233 //os.writeBytes(String.valueOf(echo.client.getFrameWidth())+"\n"); temp comment out for rebuild |
234 //os.writeBytes(String.valueOf(echo.client.getFrameHeight())+"\n"); temp comment out for rebuild | |
45 | 235 System.out.println("---------push-------"); |
236 // os.writeBytes("1240\n"); | |
237 // os.writeBytes("880\n"); | |
238 os.close(); | |
239 } | |
28 | 240 |
241 private void sendProxyFlag(Writer writer) throws TransportException { | |
242 if (proxyFlag) | |
243 writer.writeInt(1); | |
244 else | |
245 writer.writeInt(0); | |
246 } | |
247 | |
248 private void sendPortNumber(Writer writer) throws TransportException { | |
249 byte[] b = new byte[4]; | |
250 //b = castIntByte(getHost.getPort()); | |
251 b = castIntByte(9999); | |
252 writer.write(b); | |
253 } | |
254 | |
255 private byte[] castIntByte(int len) { | |
256 byte[] b = new byte[4]; | |
257 b[0] = (byte) ((len >>> 24) & 0xFF); | |
258 b[1] = (byte) ((len >>> 16) & 0xFF); | |
259 b[2] = (byte) ((len >>> 8) & 0xFF); | |
260 b[3] = (byte) ((len >>> 0) & 0xFF); | |
261 return b; | |
262 } | |
263 | |
45 | 264 |
28 | 265 private void readSecType(Reader reader) throws TransportException { |
266 byte[] b = new byte[1]; | |
267 reader.read(b); | |
268 } | |
269 | |
270 private void sendSecurityType(Writer os) throws TransportException { | |
271 // number-of-security-types | |
272 os.writeInt(1); | |
273 // security-types | |
274 // 1:None | |
275 os.writeInt(1); | |
276 | |
277 /* | |
278 * os.write(4); os.write(30); os.write(31); os.write(32); os.write(35); | |
279 * os.flush(); | |
280 */ | |
281 } | |
282 | |
283 private void sendSecResult(Writer os) throws TransportException { | |
284 byte[] b = castIntByte(0); | |
285 os.write(b); | |
286 } | |
287 | |
288 private void readClientInit(Reader in) throws TransportException { | |
289 byte[] b = new byte[0]; | |
290 in.readBytes(b); | |
291 } | |
292 | |
293 private void sendInitData(Writer os) throws TransportException { | |
294 os.write(context.getInitData()); | |
295 } | |
296 | |
297 public void setProtocolContext(Protocol workingProtocol) { | |
298 context = workingProtocol; | |
299 } | |
29 | 300 |
301 | |
302 public void readSendData(int dataLen, Reader reader) throws TransportException { | |
303 | |
304 } | |
31 | 305 |
306 public Socket accept() throws IOException { | |
307 return null; | |
308 } | |
309 | |
38 | 310 public int selectPort(int port) { |
311 return port; | |
31 | 312 } |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
313 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
314 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
315 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
|
316 boolean incremental) throws TransportException { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
317 byte[] b = new byte[10]; |
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 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
|
320 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
|
321 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
|
322 b[3] = (byte) (x & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
323 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
|
324 b[5] = (byte) (y & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
325 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
|
326 b[7] = (byte) (w & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
327 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
|
328 b[9] = (byte) (h & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
329 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
330 // os.write(b); |
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 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
333 public void notProxy() { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
334 proxyFlag = false; |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
335 } |
35 | 336 |
337 public void setEcho(EchoClient _echo) { | |
338 echo = _echo; | |
339 } | |
340 | |
36 | 341 public void setViewer(Viewer v) { |
342 echo.setViewer(v); | |
343 } | |
344 | |
35 | 345 public EchoClient getEcho() { |
346 return echo; | |
347 } | |
43 | 348 |
349 public void setTerminationType(boolean setType) { | |
350 /*nop*/ | |
351 } | |
352 | |
353 public boolean getTerminationType() { | |
354 /*nop*/ | |
355 return true; | |
356 } | |
45 | 357 |
358 public void setProxyAddr(String proxyAddr) { | |
359 this.proxyAddr = proxyAddr; | |
360 } | |
52 | 361 |
362 | |
363 public void close() { | |
364 //nothing | |
365 } | |
45 | 366 |
28 | 367 } |