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