comparison src/main/java/jp/ac/u_ryukyu/treevnc/test/MyRfbProto.java @ 28:18fad65bc447

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