Mercurial > hg > Applications > TreeVNC
annotate src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java @ 60:ac6f9e46566f
proxy mode ok.
author | Taninari YU <you@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 21 Dec 2013 18:46:54 +0900 |
parents | 433c79184c05 |
children | d9cf08c6415c |
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; |
60 | 33 public int acceptPort; |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
34 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
35 public MyRfbProto() { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
36 rThread = new RequestScreenThread(this); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
37 } |
28 | 38 |
39 | |
40 public void newClient(AcceptThread acceptThread, final Socket newCli, | |
41 final Writer os, final Reader is) throws IOException { | |
42 // createBimgFlag = true; | |
43 // rfb.addSockTmp(newCli); | |
44 // addSock(newCli); | |
45 final int myId = clients; | |
46 final MulticastQueue.Client<LinkedList<ByteBuffer>> c = multicastqueue.newClient(); | |
47 final AtomicInteger writerRunning = new AtomicInteger(); | |
48 writerRunning.set(1); | |
49 /** | |
50 * Timeout thread. If a client is suspended, it has top of queue | |
51 * indefinitely, which caused memory overflow. After the timeout, we | |
52 * poll the queue and discard it. Start long wait if writer is running. | |
53 */ | |
54 final Runnable timer = new Runnable() { | |
55 public void run() { | |
56 int count = 0; | |
57 for (;;) { | |
58 long timeout = 50000 / 8; | |
59 try { | |
60 synchronized (this) { | |
61 int state, flag; | |
62 writerRunning.set(0); | |
63 wait(timeout); | |
64 flag = 0; | |
65 while ((state = writerRunning.get()) == 0) { | |
66 c.poll(); // discard, should be timeout | |
67 count++; | |
68 if (flag == 0) { | |
69 System.out.println("Discarding " + myId | |
70 + " count=" + count); | |
71 flag = 1; | |
72 } | |
73 wait(10); // if this is too short, writer cannot | |
74 // take the poll, if this is too | |
75 // long, memory will overflow... | |
76 } | |
77 if (flag == 1) | |
78 System.out.println("Resuming " + myId | |
79 + " count=" + count); | |
80 if (state != 1) { | |
81 System.out.println("Client died " + myId); | |
82 break; | |
83 } | |
84 } | |
85 } catch (InterruptedException e) { | |
86 } | |
87 } | |
88 } | |
89 }; | |
90 new Thread(timer).start(); | |
91 /** | |
92 * discard all incoming from clients | |
93 */ | |
94 final Runnable reader = new Runnable() { | |
95 public void run() { | |
96 byte b[] = new byte[4096]; | |
97 for (;;) { | |
98 try { | |
99 int c = is.readByte(b); | |
100 if (c <= 0) | |
101 throw new IOException(); | |
102 // System.out.println("client read "+c); | |
103 } catch (IOException e) { | |
104 try { | |
105 writerRunning.set(2); | |
106 os.close(); | |
107 is.close(); | |
36 | 108 break; |
28 | 109 } catch (IOException e1) { |
110 } catch (TransportException e1) { | |
111 e1.printStackTrace(); | |
112 } | |
113 return; | |
114 } catch (TransportException e) { | |
115 e.printStackTrace(); | |
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 // rThread.checkDelay(); | |
129 | |
130 /** | |
131 * initial connection of RFB protocol | |
132 */ | |
133 sendRfbVersion(os); | |
134 // readVersionMsg(is); | |
135 readVersionMsg(is, os); | |
136 sendSecurityType(os); | |
137 readSecType(is); | |
138 sendSecResult(os); | |
139 readClientInit(is); | |
140 sendInitData(os); | |
141 new Thread(reader).start(); // discard incoming packet here | |
142 // after. | |
143 // writeFramebufferUpdateRequest(0,0, framebufferWidth, | |
144 // framebufferHeight, false ); | |
145 for (;;) { | |
146 LinkedList<ByteBuffer> bufs = c.poll(); | |
147 int inputIndex = 0; | |
148 ByteBuffer header = bufs.get(inputIndex); | |
149 if (header == null) | |
150 continue; | |
151 else if (header.get(0) == CheckDelay) { | |
152 writeToClient(os, bufs, inputIndex); | |
153 continue; | |
154 } else if (header.get(0) == FramebufferUpdate) { | |
155 // System.out.println("client "+ myId); | |
156 } | |
157 /* | |
158 * if(i%20==0){ sendDataCheckDelay(); } i++; | |
159 */ | |
160 writeToClient(os, bufs, inputIndex); | |
161 writerRunning.set(1); // yes my client is awaking. | |
162 } | |
163 } catch (IOException e) { | |
164 try { | |
165 writerRunning.set(2); | |
166 os.close(); | |
167 } catch (IOException e1) { | |
168 } | |
169 /* if socket closed cliList.remove(newCli); */ | |
170 } catch (TransportException e) { | |
171 e.printStackTrace(); | |
172 } | |
173 } | |
174 | |
175 public void writeToClient(final Writer os, | |
176 LinkedList<ByteBuffer> bufs, int inputIndex) | |
177 throws TransportException { | |
178 while (inputIndex < bufs.size()) { | |
179 ByteBuffer b = bufs.get(inputIndex++); | |
180 os.write(b.array(), b.position(), b.limit()); | |
181 } | |
182 os.flush(); | |
183 } | |
184 }; | |
185 clients++; | |
186 new Thread(sender).start(); | |
187 | |
188 } | |
189 | |
190 public synchronized void requestThreadNotify() { | |
191 rThread.reStart(); | |
192 } | |
193 | |
194 private void sendRfbVersion(Writer writer) throws IOException, TransportException { | |
195 // os.write(versionMsg_3_8.getBytes()); | |
54 | 196 writer.write(versionMsg_3_856.getBytes()); |
28 | 197 } |
198 | |
199 private int readVersionMsg(Reader reader, Writer writer) throws IOException, TransportException { | |
200 | |
201 byte[] b = new byte[12]; | |
202 | |
203 reader.readBytes(b); | |
204 | |
205 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') | |
206 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') | |
207 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.') | |
208 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9') | |
209 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) { | |
210 throw new IOException("this is not an RFB server"); | |
211 } | |
212 | |
213 int rfbMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); | |
214 int rfbMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); | |
215 | |
216 if (rfbMajor < 3) { | |
217 throw new IOException( | |
218 "RFB server does not support protocol version 3"); | |
219 } | |
220 | |
221 if (rfbMinor == 855) { | |
222 sendProxyFlag(writer); | |
223 if (proxyFlag) | |
224 sendPortNumber(writer); | |
225 } | |
226 return rfbMinor; | |
227 } | |
45 | 228 |
229 public void screenChangeRequest() throws IOException { | |
230 Socket echoSocket; | |
231 echoSocket = new Socket(proxyAddr, 10001); | |
232 DataOutputStream os = new DataOutputStream(echoSocket.getOutputStream()); | |
233 os.writeBytes(echo.getMyAddress()+"\n"); | |
59 | 234 //os.writeBytes(String.valueOf(echo.client.getFrameWidth())+"\n"); temp comment out for rebuild |
235 //os.writeBytes(String.valueOf(echo.client.getFrameHeight())+"\n"); temp comment out for rebuild | |
45 | 236 System.out.println("---------push-------"); |
237 // os.writeBytes("1240\n"); | |
238 // os.writeBytes("880\n"); | |
239 os.close(); | |
240 } | |
28 | 241 |
242 private void sendProxyFlag(Writer writer) throws TransportException { | |
243 if (proxyFlag) | |
244 writer.writeInt(1); | |
245 else | |
246 writer.writeInt(0); | |
247 } | |
248 | |
249 private void sendPortNumber(Writer writer) throws TransportException { | |
250 byte[] b = new byte[4]; | |
251 //b = castIntByte(getHost.getPort()); | |
252 b = castIntByte(9999); | |
253 writer.write(b); | |
254 } | |
255 | |
256 private byte[] castIntByte(int len) { | |
257 byte[] b = new byte[4]; | |
258 b[0] = (byte) ((len >>> 24) & 0xFF); | |
259 b[1] = (byte) ((len >>> 16) & 0xFF); | |
260 b[2] = (byte) ((len >>> 8) & 0xFF); | |
261 b[3] = (byte) ((len >>> 0) & 0xFF); | |
262 return b; | |
263 } | |
264 | |
45 | 265 |
28 | 266 private void readSecType(Reader reader) throws TransportException { |
267 byte[] b = new byte[1]; | |
268 reader.read(b); | |
269 } | |
270 | |
271 private void sendSecurityType(Writer os) throws TransportException { | |
272 // number-of-security-types | |
273 os.writeInt(1); | |
274 // security-types | |
275 // 1:None | |
276 os.writeInt(1); | |
277 | |
278 /* | |
279 * os.write(4); os.write(30); os.write(31); os.write(32); os.write(35); | |
280 * os.flush(); | |
281 */ | |
282 } | |
283 | |
284 private void sendSecResult(Writer os) throws TransportException { | |
285 byte[] b = castIntByte(0); | |
286 os.write(b); | |
287 } | |
288 | |
289 private void readClientInit(Reader in) throws TransportException { | |
290 byte[] b = new byte[0]; | |
291 in.readBytes(b); | |
292 } | |
293 | |
294 private void sendInitData(Writer os) throws TransportException { | |
295 os.write(context.getInitData()); | |
296 } | |
297 | |
298 public void setProtocolContext(Protocol workingProtocol) { | |
299 context = workingProtocol; | |
300 } | |
29 | 301 |
302 | |
303 public void readSendData(int dataLen, Reader reader) throws TransportException { | |
304 | |
305 } | |
31 | 306 |
307 public Socket accept() throws IOException { | |
308 return null; | |
309 } | |
310 | |
38 | 311 public int selectPort(int port) { |
312 return port; | |
31 | 313 } |
33
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 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
316 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
|
317 boolean incremental) throws TransportException { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
318 byte[] b = new byte[10]; |
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 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
|
321 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
|
322 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
|
323 b[3] = (byte) (x & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
324 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
|
325 b[5] = (byte) (y & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
326 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
|
327 b[7] = (byte) (w & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
328 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
|
329 b[9] = (byte) (h & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
330 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
331 // os.write(b); |
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 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
334 public void notProxy() { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
335 proxyFlag = false; |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
336 } |
35 | 337 |
338 public void setEcho(EchoClient _echo) { | |
339 echo = _echo; | |
340 } | |
341 | |
36 | 342 public void setViewer(Viewer v) { |
343 echo.setViewer(v); | |
344 } | |
345 | |
35 | 346 public EchoClient getEcho() { |
347 return echo; | |
348 } | |
43 | 349 |
350 public void setTerminationType(boolean setType) { | |
351 /*nop*/ | |
352 } | |
353 | |
354 public boolean getTerminationType() { | |
355 /*nop*/ | |
356 return true; | |
357 } | |
45 | 358 |
359 public void setProxyAddr(String proxyAddr) { | |
360 this.proxyAddr = proxyAddr; | |
361 } | |
52 | 362 |
363 | |
364 public void close() { | |
365 //nothing | |
366 } | |
45 | 367 |
60 | 368 public int getAcceptPort() { |
369 return 0; | |
370 } | |
371 | |
28 | 372 } |