Mercurial > hg > Applications > TreeVNC
annotate src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java @ 52:986f16afb753
update before change host.
author | one |
---|---|
date | Sat, 02 Nov 2013 17:55:10 +0900 |
parents | 20326a4b9d88 |
children | 0ae87c7e767c |
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; | |
25 final static String versionMsg_3_855 = "RFB 003.855\n"; | |
26 private int clients; | |
32 | 27 protected MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>(); |
28 | 28 private RequestScreenThread rThread; |
29 private boolean proxyFlag = true; | |
35 | 30 private EchoClient echo; |
45 | 31 private String proxyAddr; |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
32 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
33 public MyRfbProto() { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
34 rThread = new RequestScreenThread(this); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
35 } |
28 | 36 |
37 | |
38 public void newClient(AcceptThread acceptThread, final Socket newCli, | |
39 final Writer os, final Reader is) throws IOException { | |
40 // createBimgFlag = true; | |
41 // rfb.addSockTmp(newCli); | |
42 // addSock(newCli); | |
43 final int myId = clients; | |
44 final MulticastQueue.Client<LinkedList<ByteBuffer>> c = multicastqueue.newClient(); | |
45 final AtomicInteger writerRunning = new AtomicInteger(); | |
46 writerRunning.set(1); | |
47 /** | |
48 * Timeout thread. If a client is suspended, it has top of queue | |
49 * indefinitely, which caused memory overflow. After the timeout, we | |
50 * poll the queue and discard it. Start long wait if writer is running. | |
51 */ | |
52 final Runnable timer = new Runnable() { | |
53 public void run() { | |
54 int count = 0; | |
55 for (;;) { | |
56 long timeout = 50000 / 8; | |
57 try { | |
58 synchronized (this) { | |
59 int state, flag; | |
60 writerRunning.set(0); | |
61 wait(timeout); | |
62 flag = 0; | |
63 while ((state = writerRunning.get()) == 0) { | |
64 c.poll(); // discard, should be timeout | |
65 count++; | |
66 if (flag == 0) { | |
67 System.out.println("Discarding " + myId | |
68 + " count=" + count); | |
69 flag = 1; | |
70 } | |
71 wait(10); // if this is too short, writer cannot | |
72 // take the poll, if this is too | |
73 // long, memory will overflow... | |
74 } | |
75 if (flag == 1) | |
76 System.out.println("Resuming " + myId | |
77 + " count=" + count); | |
78 if (state != 1) { | |
79 System.out.println("Client died " + myId); | |
80 break; | |
81 } | |
82 } | |
83 } catch (InterruptedException e) { | |
84 } | |
85 } | |
86 } | |
87 }; | |
88 new Thread(timer).start(); | |
89 /** | |
90 * discard all incoming from clients | |
91 */ | |
92 final Runnable reader = new Runnable() { | |
93 public void run() { | |
94 byte b[] = new byte[4096]; | |
95 for (;;) { | |
96 try { | |
97 int c = is.readByte(b); | |
98 if (c <= 0) | |
99 throw new IOException(); | |
100 // System.out.println("client read "+c); | |
101 } catch (IOException e) { | |
102 try { | |
103 writerRunning.set(2); | |
104 os.close(); | |
105 is.close(); | |
36 | 106 break; |
28 | 107 } catch (IOException e1) { |
108 } catch (TransportException e1) { | |
109 e1.printStackTrace(); | |
110 } | |
111 return; | |
112 } catch (TransportException e) { | |
113 e.printStackTrace(); | |
114 } | |
115 } | |
116 } | |
117 }; | |
118 /** | |
119 * send packets to a client | |
120 */ | |
121 Runnable sender = new Runnable() { | |
122 public void run() { | |
123 writerRunning.set(1); | |
124 try { | |
125 requestThreadNotify(); | |
126 // rThread.checkDelay(); | |
127 | |
128 /** | |
129 * initial connection of RFB protocol | |
130 */ | |
131 sendRfbVersion(os); | |
132 // readVersionMsg(is); | |
133 readVersionMsg(is, os); | |
134 sendSecurityType(os); | |
135 readSecType(is); | |
136 sendSecResult(os); | |
137 readClientInit(is); | |
138 sendInitData(os); | |
139 new Thread(reader).start(); // discard incoming packet here | |
140 // after. | |
141 // writeFramebufferUpdateRequest(0,0, framebufferWidth, | |
142 // framebufferHeight, false ); | |
143 for (;;) { | |
144 LinkedList<ByteBuffer> bufs = c.poll(); | |
145 int inputIndex = 0; | |
146 ByteBuffer header = bufs.get(inputIndex); | |
147 if (header == null) | |
148 continue; | |
149 else if (header.get(0) == CheckDelay) { | |
150 writeToClient(os, bufs, inputIndex); | |
151 continue; | |
152 } else if (header.get(0) == FramebufferUpdate) { | |
153 // System.out.println("client "+ myId); | |
154 } | |
155 /* | |
156 * if(i%20==0){ sendDataCheckDelay(); } i++; | |
157 */ | |
158 writeToClient(os, bufs, inputIndex); | |
159 writerRunning.set(1); // yes my client is awaking. | |
160 } | |
161 } catch (IOException e) { | |
162 try { | |
163 writerRunning.set(2); | |
164 os.close(); | |
165 } catch (IOException e1) { | |
166 } | |
167 /* if socket closed cliList.remove(newCli); */ | |
168 } catch (TransportException e) { | |
169 e.printStackTrace(); | |
170 } | |
171 } | |
172 | |
173 public void writeToClient(final Writer os, | |
174 LinkedList<ByteBuffer> bufs, int inputIndex) | |
175 throws TransportException { | |
176 while (inputIndex < bufs.size()) { | |
177 ByteBuffer b = bufs.get(inputIndex++); | |
178 os.write(b.array(), b.position(), b.limit()); | |
179 } | |
180 os.flush(); | |
181 } | |
182 }; | |
183 clients++; | |
184 new Thread(sender).start(); | |
185 | |
186 } | |
187 | |
188 public synchronized void requestThreadNotify() { | |
189 rThread.reStart(); | |
190 } | |
191 | |
192 private void sendRfbVersion(Writer writer) throws IOException, TransportException { | |
193 // os.write(versionMsg_3_8.getBytes()); | |
194 writer.write(versionMsg_3_855.getBytes()); | |
195 } | |
196 | |
197 private int readVersionMsg(Reader reader, Writer writer) throws IOException, TransportException { | |
198 | |
199 byte[] b = new byte[12]; | |
200 | |
201 reader.readBytes(b); | |
202 | |
203 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') | |
204 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') | |
205 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.') | |
206 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9') | |
207 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) { | |
208 throw new IOException("this is not an RFB server"); | |
209 } | |
210 | |
211 int rfbMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); | |
212 int rfbMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); | |
213 | |
214 if (rfbMajor < 3) { | |
215 throw new IOException( | |
216 "RFB server does not support protocol version 3"); | |
217 } | |
218 | |
219 if (rfbMinor == 855) { | |
220 sendProxyFlag(writer); | |
221 if (proxyFlag) | |
222 sendPortNumber(writer); | |
223 } | |
224 return rfbMinor; | |
225 } | |
45 | 226 |
227 public void screenChangeRequest() throws IOException { | |
228 Socket echoSocket; | |
229 echoSocket = new Socket(proxyAddr, 10001); | |
230 DataOutputStream os = new DataOutputStream(echoSocket.getOutputStream()); | |
231 os.writeBytes(echo.getMyAddress()+"\n"); | |
232 System.out.println("---------push-------"); | |
233 // os.writeBytes("1240\n"); | |
234 // os.writeBytes("880\n"); | |
235 os.close(); | |
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 | |
290 private void sendInitData(Writer os) throws TransportException { | |
291 os.write(context.getInitData()); | |
292 } | |
293 | |
294 public void setProtocolContext(Protocol workingProtocol) { | |
295 context = workingProtocol; | |
296 } | |
29 | 297 |
298 | |
299 public void readSendData(int dataLen, Reader reader) throws TransportException { | |
300 | |
301 } | |
31 | 302 |
303 public Socket accept() throws IOException { | |
304 return null; | |
305 } | |
306 | |
38 | 307 public int selectPort(int port) { |
308 return port; | |
31 | 309 } |
33
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
310 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
311 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
312 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
|
313 boolean incremental) throws TransportException { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
314 byte[] b = new byte[10]; |
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 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
|
317 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
|
318 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
|
319 b[3] = (byte) (x & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
320 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
|
321 b[5] = (byte) (y & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
322 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
|
323 b[7] = (byte) (w & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
324 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
|
325 b[9] = (byte) (h & 0xff); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
326 |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
327 // os.write(b); |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
328 } |
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 public void notProxy() { |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
331 proxyFlag = false; |
9d3478d11d3b
Add the processing of client
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
332 } |
35 | 333 |
334 public void setEcho(EchoClient _echo) { | |
335 echo = _echo; | |
336 } | |
337 | |
36 | 338 public void setViewer(Viewer v) { |
339 echo.setViewer(v); | |
340 } | |
341 | |
35 | 342 public EchoClient getEcho() { |
343 return echo; | |
344 } | |
43 | 345 |
346 public void setTerminationType(boolean setType) { | |
347 /*nop*/ | |
348 } | |
349 | |
350 public boolean getTerminationType() { | |
351 /*nop*/ | |
352 return true; | |
353 } | |
45 | 354 |
355 public void setProxyAddr(String proxyAddr) { | |
356 this.proxyAddr = proxyAddr; | |
357 } | |
52 | 358 |
359 | |
360 public void close() { | |
361 //nothing | |
362 } | |
45 | 363 |
28 | 364 } |