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