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