Mercurial > hg > Members > nobuyasu > tightVNCProxy
annotate src/myVncProxy/MyRfbProto.java @ 183:1f583d6e0d1a
modify MulticastQueue.java : method poll()
author | e085711 |
---|---|
date | Tue, 25 Oct 2011 17:33:47 +0900 |
parents | f0dc6e6e85dc |
children | 00e031baf065 |
rev | line source |
---|---|
24 | 1 package myVncProxy; |
54 | 2 |
88 | 3 import static org.junit.Assert.*; |
4 | |
25 | 5 import java.awt.Graphics; |
6 import java.awt.Image; | |
7 import java.awt.image.BufferedImage; | |
8 import java.io.BufferedOutputStream; | |
15 | 9 import java.io.BufferedReader; |
25 | 10 import java.io.ByteArrayInputStream; |
11 import java.io.ByteArrayOutputStream; | |
10 | 12 import java.io.IOException; |
43 | 13 import java.io.InputStream; |
15 | 14 import java.io.InputStreamReader; |
23 | 15 import java.net.BindException; |
10 | 16 import java.net.ServerSocket; |
17 import java.net.Socket; | |
90 | 18 import java.nio.ByteBuffer; |
93 | 19 import java.util.Iterator; |
10 | 20 import java.util.LinkedList; |
21 | |
25 | 22 import javax.imageio.ImageIO; |
23 | |
88 | 24 import org.junit.Test; |
25 | |
54 | 26 import myVncProxy.MulticastQueue.Client; |
27 | |
40 | 28 import java.util.concurrent.ExecutorService; |
113
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
29 import java.util.concurrent.atomic.AtomicInteger; |
80 | 30 import java.util.zip.DataFormatException; |
31 import java.util.zip.Deflater; | |
32 import java.util.zip.Inflater; | |
40 | 33 import java.io.OutputStream; |
10 | 34 |
88 | 35 public |
107 | 36 class MyRfbProto extends RfbProto { |
154 | 37 final static String versionMsg_3_855 = "RFB 003.855\n"; |
65 | 38 /** |
154 | 39 * CheckMillis is one of new msgType for RFB 3.855. |
65 | 40 */ |
90 | 41 final static byte SpeedCheckMillis = 4; |
166
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
42 |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
43 // Secyrity type of OS X |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
44 final static int SecTypeReqAccess = 32; |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
45 |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
46 // Supported authentication types |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
47 final static int AuthAccess = 32; |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
48 |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
49 |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
50 |
83 | 51 private static final int INFLATE_BUFSIZE = 1024*100; |
65 | 52 boolean printStatusFlag = false; |
53 long startCheckTime; | |
54 | 54 |
18 | 55 private int messageType; |
56 private int rectangles; | |
23 | 57 private int rectX; |
58 private int rectY; | |
59 private int rectW; | |
60 private int rectH; | |
18 | 61 private int encoding; |
27 | 62 private int zLen; |
104 | 63 private boolean clicomp = false; |
18 | 64 |
23 | 65 private ServerSocket servSock; |
144 | 66 protected int acceptPort; |
10 | 67 private byte initData[]; |
54 | 68 private LinkedList<Socket> cliListTmp; |
69 private LinkedList<Socket> cliList; | |
27 | 70 boolean createBimgFlag; |
155 | 71 boolean proxyFlag = true; |
54 | 72 |
40 | 73 ExecutorService executor; |
54 | 74 |
25 | 75 byte[] pngBytes; |
54 | 76 |
102 | 77 // private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MostRecentMultiCast<LinkedList<ByteBuffer>>(10); |
78 private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>(); | |
80 | 79 private int clients = 0; |
81
9109273b96dc
consume too much memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
80 private Inflater inflater = new Inflater(); |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
81 private Deflater deflater = new Deflater(); |
130 | 82 private CreateThread geth; |
162 | 83 // private Thread requestThread; |
84 private RequestScreenThread rThread; | |
85 private Thread requestThread; | |
130 | 86 |
88 | 87 public |
88 MyRfbProto() throws IOException { | |
89 } | |
90 | |
54 | 91 MyRfbProto(String h, int p, VncViewer v) throws IOException { |
10 | 92 super(h, p, v); |
162 | 93 |
94 rThread = new RequestScreenThread(this); | |
95 requestThread = new Thread(rThread); | |
96 // requestThread = new Thread(new RequestScreenThread(this)); | |
10 | 97 } |
98 | |
130 | 99 MyRfbProto(String h, int p, CreateThread geth) throws IOException { |
128 | 100 super(h, p); |
130 | 101 this.geth = geth; |
155 | 102 proxyFlag = true; |
162 | 103 |
104 rThread = new RequestScreenThread(this); | |
105 requestThread = new Thread(rThread); | |
106 // requestThread = new Thread(new RequestScreenThread(this)); | |
128 | 107 } |
130 | 108 |
13 | 109 MyRfbProto(String h, int p) throws IOException { |
110 super(h, p); | |
162 | 111 |
112 rThread = new RequestScreenThread(this); | |
113 requestThread = new Thread(rThread); | |
114 // requestThread = new Thread(new RequestScreenThread(this)); | |
13 | 115 } |
162 | 116 |
44 | 117 // over write |
43 | 118 void writeVersionMsg() throws IOException { |
119 clientMajor = 3; | |
154 | 120 if (serverMinor == 855) { |
121 clientMinor = 855; | |
122 os.write(versionMsg_3_855.getBytes()); | |
43 | 123 } else if (serverMajor > 3 || serverMinor >= 8) { |
124 clientMinor = 8; | |
125 os.write(versionMsg_3_8.getBytes()); | |
126 } else if (serverMinor >= 7) { | |
127 clientMinor = 7; | |
128 os.write(versionMsg_3_7.getBytes()); | |
129 } else { | |
130 clientMinor = 3; | |
131 os.write(versionMsg_3_3.getBytes()); | |
132 } | |
133 protocolTightVNC = false; | |
134 initCapabilities(); | |
135 } | |
136 | |
54 | 137 void initServSock(int port) throws IOException { |
10 | 138 servSock = new ServerSocket(port); |
23 | 139 acceptPort = port; |
10 | 140 } |
166
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
141 |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
142 void authenticationRequestAccess() throws IOException { |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
143 |
181
f0dc6e6e85dc
modify MyRfbProto.java : method authneticationRequestAccess()
e085711
parents:
180
diff
changeset
|
144 byte[] headBuf = new byte[2]; |
f0dc6e6e85dc
modify MyRfbProto.java : method authneticationRequestAccess()
e085711
parents:
180
diff
changeset
|
145 is.read(headBuf); |
f0dc6e6e85dc
modify MyRfbProto.java : method authneticationRequestAccess()
e085711
parents:
180
diff
changeset
|
146 if(headBuf[1] == 2) { |
f0dc6e6e85dc
modify MyRfbProto.java : method authneticationRequestAccess()
e085711
parents:
180
diff
changeset
|
147 byte[] b = new byte[258]; |
f0dc6e6e85dc
modify MyRfbProto.java : method authneticationRequestAccess()
e085711
parents:
180
diff
changeset
|
148 is.read(b); |
166
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
149 |
181
f0dc6e6e85dc
modify MyRfbProto.java : method authneticationRequestAccess()
e085711
parents:
180
diff
changeset
|
150 byte[] outBuf = new byte[256]; |
f0dc6e6e85dc
modify MyRfbProto.java : method authneticationRequestAccess()
e085711
parents:
180
diff
changeset
|
151 os.write(outBuf); |
f0dc6e6e85dc
modify MyRfbProto.java : method authneticationRequestAccess()
e085711
parents:
180
diff
changeset
|
152 os.flush(); |
f0dc6e6e85dc
modify MyRfbProto.java : method authneticationRequestAccess()
e085711
parents:
180
diff
changeset
|
153 }else if(headBuf[1] == 23) { |
f0dc6e6e85dc
modify MyRfbProto.java : method authneticationRequestAccess()
e085711
parents:
180
diff
changeset
|
154 byte[] b = new byte[130]; |
f0dc6e6e85dc
modify MyRfbProto.java : method authneticationRequestAccess()
e085711
parents:
180
diff
changeset
|
155 is.read(b); |
f0dc6e6e85dc
modify MyRfbProto.java : method authneticationRequestAccess()
e085711
parents:
180
diff
changeset
|
156 byte[] outBuf = new byte[192]; |
f0dc6e6e85dc
modify MyRfbProto.java : method authneticationRequestAccess()
e085711
parents:
180
diff
changeset
|
157 os.write(outBuf); |
f0dc6e6e85dc
modify MyRfbProto.java : method authneticationRequestAccess()
e085711
parents:
180
diff
changeset
|
158 os.flush(); |
f0dc6e6e85dc
modify MyRfbProto.java : method authneticationRequestAccess()
e085711
parents:
180
diff
changeset
|
159 } |
166
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
160 |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
161 int result = readU32(); |
180 | 162 if(result != 0) { |
166
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
163 System.out.println("faild authentication "); |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
164 throw new IOException(); |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
165 } |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
166 |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
167 } |
54 | 168 |
153 | 169 /* |
170 * default port number is 5999. | |
171 */ | |
80 | 172 void selectPort(int p) { |
144 | 173 if(servSock != null ) return ; |
80 | 174 int port = p; |
54 | 175 while (true) { |
176 try { | |
80 | 177 initServSock(port); |
23 | 178 break; |
54 | 179 } catch (BindException e) { |
80 | 180 port++; |
23 | 181 continue; |
54 | 182 } catch (IOException e) { |
10 | 183 |
23 | 184 } |
185 } | |
80 | 186 System.out.println("accept port = " + port); |
23 | 187 } |
54 | 188 |
189 int getAcceptPort() { | |
23 | 190 return acceptPort; |
191 } | |
54 | 192 |
10 | 193 void setSoTimeout(int num) throws IOException { |
194 servSock.setSoTimeout(num); | |
195 } | |
54 | 196 |
10 | 197 Socket accept() throws IOException { |
198 return servSock.accept(); | |
199 } | |
200 | |
54 | 201 void addSock(Socket sock) { |
10 | 202 cliList.add(sock); |
203 } | |
54 | 204 |
205 void addSockTmp(Socket sock) { | |
206 System.out.println("connected " + sock.getInetAddress()); | |
27 | 207 cliListTmp.add(sock); |
208 } | |
54 | 209 |
10 | 210 boolean markSupported() { |
211 return is.markSupported(); | |
212 } | |
54 | 213 |
10 | 214 void readServerInit() throws IOException { |
54 | 215 |
78 | 216 is.mark(255); |
10 | 217 skipBytes(20); |
218 int nlen = readU32(); | |
54 | 219 int blen = 20 + 4 + nlen; |
10 | 220 initData = new byte[blen]; |
78 | 221 is.reset(); |
10 | 222 |
78 | 223 is.mark(blen); |
10 | 224 readFully(initData); |
78 | 225 is.reset(); |
54 | 226 |
10 | 227 framebufferWidth = readU16(); |
228 framebufferHeight = readU16(); | |
229 bitsPerPixel = readU8(); | |
230 depth = readU8(); | |
231 bigEndian = (readU8() != 0); | |
232 trueColour = (readU8() != 0); | |
233 redMax = readU16(); | |
234 greenMax = readU16(); | |
235 blueMax = readU16(); | |
236 redShift = readU8(); | |
237 greenShift = readU8(); | |
238 blueShift = readU8(); | |
239 byte[] pad = new byte[3]; | |
240 readFully(pad); | |
241 int nameLength = readU32(); | |
242 byte[] name = new byte[nameLength]; | |
243 readFully(name); | |
244 desktopName = new String(name); | |
245 | |
246 // Read interaction capabilities (TightVNC protocol extensions) | |
247 if (protocolTightVNC) { | |
248 int nServerMessageTypes = readU16(); | |
249 int nClientMessageTypes = readU16(); | |
250 int nEncodingTypes = readU16(); | |
251 readU16(); | |
252 readCapabilityList(serverMsgCaps, nServerMessageTypes); | |
253 readCapabilityList(clientMsgCaps, nClientMessageTypes); | |
254 readCapabilityList(encodingCaps, nEncodingTypes); | |
255 } | |
256 | |
257 inNormalProtocol = true; | |
258 } | |
259 | |
54 | 260 void sendRfbVersion(OutputStream os) throws IOException { |
138 | 261 // os.write(versionMsg_3_8.getBytes()); |
154 | 262 os.write(versionMsg_3_855.getBytes()); |
43 | 263 } |
54 | 264 |
155 | 265 int readVersionMsg(InputStream is, OutputStream os) throws IOException { |
45 | 266 |
267 byte[] b = new byte[12]; | |
268 | |
269 is.read(b); | |
270 | |
271 if ((b[0] != 'R') || (b[1] != 'F') || (b[2] != 'B') || (b[3] != ' ') | |
272 || (b[4] < '0') || (b[4] > '9') || (b[5] < '0') || (b[5] > '9') | |
273 || (b[6] < '0') || (b[6] > '9') || (b[7] != '.') | |
274 || (b[8] < '0') || (b[8] > '9') || (b[9] < '0') || (b[9] > '9') | |
275 || (b[10] < '0') || (b[10] > '9') || (b[11] != '\n')) { | |
276 throw new IOException("Host " + host + " port " + port | |
277 + " is not an RFB server"); | |
278 } | |
279 | |
155 | 280 int rfbMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); |
281 int rfbMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); | |
45 | 282 |
155 | 283 if (rfbMajor < 3) { |
45 | 284 throw new IOException( |
141 | 285 "RFB server does not support protocol version 3"); |
54 | 286 } |
287 | |
155 | 288 if (rfbMinor == 855) { |
289 sendProxyFlag(os); | |
290 if(proxyFlag)sendPortNumber(os); | |
130 | 291 } |
155 | 292 return rfbMinor; |
293 } | |
294 void sendProxyFlag(OutputStream os) throws IOException { | |
295 if(proxyFlag) os.write(1); | |
296 else os.write(0); | |
297 } | |
298 | |
299 boolean readProxyFlag() throws IOException{ | |
300 int flag = readU8(); | |
301 if(flag == 1) | |
302 return true; | |
303 else | |
304 return false; | |
130 | 305 } |
306 | |
131 | 307 void sendPortNumber(OutputStream os) throws IOException { |
130 | 308 byte[] b = new byte[4]; |
309 b = castIntByte(geth.port); | |
310 os.write(b); | |
54 | 311 } |
312 | |
43 | 313 void sendSecurityType(OutputStream os) throws IOException { |
314 // number-of-security-types | |
315 os.write(1); | |
54 | 316 // security-types |
43 | 317 // 1:None |
318 os.write(1); | |
166
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
319 |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
320 /* |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
321 os.write(4); |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
322 os.write(30); |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
323 os.write(31); |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
324 os.write(32); |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
325 os.write(35); |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
326 os.flush(); |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
327 */ |
43 | 328 } |
54 | 329 |
46
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
330 void readSecType(InputStream is) throws IOException { |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
331 byte[] b = new byte[1]; |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
332 is.read(b); |
166
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
333 } |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
334 |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
335 |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
336 void readSecType(InputStream is, OutputStream os) throws IOException { |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
337 byte[] b = new byte[1]; |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
338 is.read(b); |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
339 |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
340 int count = 260; |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
341 int[] data = {0, 2, 0, -128, -1, -1, -1, -1, -1, -1, -1, -1, -55, 15, -38, -94, 33, 104, -62, 52, -60, -58, 98, -117, -128, -36, 28, -47, 41, 2, 78, 8, -118, 103, -52, 116, 2, 11, -66, -90, 59, 19, -101, 34, 81, 74, 8, 121, -114, 52, 4, -35, -17, -107, 25, -77, -51, 58, 67, 27, 48, 43, 10, 109, -14, 95, 20, 55, 79, -31, 53, 109, 109, 81, -62, 69, -28, -123, -75, 118, 98, 94, 126, -58, -12, 76, 66, -23, -90, 55, -19, 107, 11, -1, 92, -74, -12, 6, -73, -19, -18, 56, 107, -5, 90, -119, -97, -91, -82, -97, 36, 17, 124, 75, 31, -26, 73, 40, 102, 81, -20, -26, 83, -127, -1, -1, -1, -1, -1, -1, -1, -1, -111, 73, -29, 30, 57, -67, -75, -77, -49, -50, -99, -76, -80, -80, 14, 65, 57, -105, -103, -54, -102, 3, 39, -44, 39, 35, 118, -84, -64, 37, -117, -21, 89, -31, -68, 70, 5, 122, -92, -119, 9, 121, 63, -112, -60, 122, -46, -69, -36, 92, -103, -92, 74, 92, -73, 87, 120, -8, 116, -47, 111, 20, -41, 110, 122, -3, -94, 14, 42, -51, -59, 48, -54, -125, 117, 60, 77, -52, -31, 98, 32, -2, -102, -15, -29, 58, -14, -106, -116, -32, -86, 50, -32, -16, -3, -123, 87, 88, -118, 10, 120, -107, -37, 125, -110, 59, 87, 93, -24, 124, -99, 18, 78, -13, -49, -34, -24, -27, 1, 114, -67, -98, -56, -3, 85, -67, -126, 77}; |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
342 for(int i=0; i<count; i++ ) { |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
343 os.write((byte)data[i]); |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
344 os.flush(); |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
345 } |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
346 |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
347 byte[] c = new byte[256]; |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
348 is.read(c); |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
349 |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
350 System.out.println(new String(c)); |
46
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
351 |
11da7dacbc1a
modify MyRfbProto.java and acceptThread.java add readSecType
e085711
parents:
45
diff
changeset
|
352 } |
54 | 353 |
166
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
354 |
47
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
355 void sendSecResult(OutputStream os) throws IOException { |
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
356 byte[] b = castIntByte(0); |
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
357 os.write(b); |
b2bf4e44504a
modify MyRfbProto.java and acceptThread.java , add sendSecResult
e085711
parents:
46
diff
changeset
|
358 } |
54 | 359 |
43 | 360 void readClientInit(InputStream in) throws IOException { |
361 byte[] b = new byte[0]; | |
362 in.read(b); | |
363 } | |
54 | 364 |
365 void sendInitData(OutputStream os) throws IOException { | |
366 os.write(initData); | |
10 | 367 } |
368 | |
54 | 369 |
370 void sendPngImage() { | |
371 try { | |
372 for (Socket cli : cliListTmp) { | |
373 try { | |
27 | 374 sendPngData(cli); |
375 addSock(cli); | |
54 | 376 } catch (IOException e) { |
27 | 377 // if socket closed |
378 cliListTmp.remove(cli); | |
379 } | |
380 } | |
54 | 381 // System.out.println("cliSize="+cliSize()); |
382 } catch (Exception e) { | |
27 | 383 } |
384 cliListTmp.clear(); | |
385 } | |
386 | |
15 | 387 boolean ready() throws IOException { |
388 BufferedReader br = new BufferedReader(new InputStreamReader(is)); | |
389 return br.ready(); | |
54 | 390 } |
10 | 391 |
54 | 392 int cliSize() { |
10 | 393 return cliList.size(); |
54 | 394 } |
395 | |
396 void printNumBytesRead() { | |
397 System.out.println("numBytesRead=" + numBytesRead); | |
398 } | |
399 | |
400 | |
61 | 401 |
54 | 402 void regiFramebufferUpdate() throws IOException { |
78 | 403 is.mark(20); |
80 | 404 messageType = readU8(); // 0 |
405 skipBytes(1); // 1 | |
406 rectangles = readU16(); // 2 | |
407 rectX = readU16(); // 4 | |
408 rectY = readU16(); // 6 | |
409 rectW = readU16(); // 8 | |
410 rectH = readU16(); // 10 | |
411 encoding = readU32(); // 12 | |
122 | 412 // System.out.println("encoding = "+encoding); |
107 | 413 if (encoding == EncodingZRLE|| encoding==EncodingZRLEE||encoding==EncodingZlib) |
27 | 414 zLen = readU32(); |
80 | 415 else |
416 zLen = 0; | |
78 | 417 is.reset(); |
104 | 418 |
15 | 419 } |
54 | 420 |
61 | 421 int checkAndMark() throws IOException { |
422 int dataLen; | |
54 | 423 switch (encoding) { |
23 | 424 case RfbProto.EncodingRaw: |
39 | 425 dataLen = rectW * rectH * 4 + 16; |
130 | 426 // is.mark(dataLen); |
23 | 427 break; |
67 | 428 case RfbProto.EncodingCopyRect: |
429 dataLen = 16 + 4; | |
130 | 430 // is.mark(dataLen); |
67 | 431 break; |
432 case RfbProto.EncodingRRE: | |
433 case RfbProto.EncodingCoRRE: | |
434 case RfbProto.EncodingHextile: | |
80 | 435 case RfbProto.EncodingTight: |
436 dataLen = zLen + 20; | |
130 | 437 // is.mark(dataLen); |
80 | 438 break; |
67 | 439 case RfbProto.EncodingZlib: |
27 | 440 case RfbProto.EncodingZRLE: |
107 | 441 case RfbProto.EncodingZRLEE: |
54 | 442 dataLen = zLen + 20; |
156 | 443 // is.mark(dataLen); |
78 | 444 break; |
445 case RfbProto.EncodingXCursor: | |
446 case RfbProto.EncodingRichCursor: | |
447 int pixArray = rectW * rectH * 4; | |
448 int u8Array = (int)Math.floor((rectW + 7)/8) * rectH; | |
449 dataLen = pixArray + u8Array; | |
450 printFramebufferUpdate(); | |
130 | 451 // is.mark(dataLen); |
27 | 452 break; |
23 | 453 default: |
61 | 454 dataLen = 1000000; |
130 | 455 // is.mark(dataLen); |
54 | 456 } |
61 | 457 return dataLen; |
458 } | |
65 | 459 |
460 | |
81
9109273b96dc
consume too much memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
461 void sendDataToClient() throws Exception { |
61 | 462 regiFramebufferUpdate(); |
183 | 463 printFramebufferUpdate(); |
61 | 464 int dataLen = checkAndMark(); |
465 readSendData(dataLen); | |
23 | 466 } |
54 | 467 |
468 BufferedImage createBufferedImage(Image img) { | |
469 BufferedImage bimg = new BufferedImage(img.getWidth(null), | |
470 img.getHeight(null), BufferedImage.TYPE_INT_RGB); | |
27 | 471 |
25 | 472 Graphics g = bimg.getGraphics(); |
473 g.drawImage(img, 0, 0, null); | |
474 g.dispose(); | |
475 return bimg; | |
476 } | |
477 | |
54 | 478 void createPngBytes(BufferedImage bimg) throws IOException { |
479 pngBytes = getImageBytes(bimg, "png"); | |
25 | 480 } |
54 | 481 |
482 byte[] getBytes(BufferedImage img) throws IOException { | |
25 | 483 byte[] b = getImageBytes(img, "png"); |
484 return b; | |
485 } | |
54 | 486 |
487 byte[] getImageBytes(BufferedImage image, String imageFormat) | |
488 throws IOException { | |
25 | 489 ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
490 BufferedOutputStream os = new BufferedOutputStream(bos); | |
491 image.flush(); | |
492 ImageIO.write(image, imageFormat, os); | |
493 os.flush(); | |
494 os.close(); | |
495 return bos.toByteArray(); | |
496 } | |
497 | |
54 | 498 void sendPngData(Socket sock) throws IOException { |
26 | 499 byte[] dataLength = castIntByte(pngBytes.length); |
500 sock.getOutputStream().write(dataLength); | |
25 | 501 sock.getOutputStream().write(pngBytes); |
502 } | |
54 | 503 |
504 byte[] castIntByte(int len) { | |
26 | 505 byte[] b = new byte[4]; |
54 | 506 b[0] = (byte) ((len >>> 24) & 0xFF); |
507 b[1] = (byte) ((len >>> 16) & 0xFF); | |
508 b[2] = (byte) ((len >>> 8) & 0xFF); | |
509 b[3] = (byte) ((len >>> 0) & 0xFF); | |
26 | 510 return b; |
511 } | |
54 | 512 |
513 BufferedImage createBimg() throws IOException { | |
25 | 514 BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes)); |
515 return bimg; | |
516 } | |
104 | 517 |
54 | 518 void printFramebufferUpdate() { |
519 | |
18 | 520 System.out.println("messageType=" + messageType); |
54 | 521 System.out.println("rectangles=" + rectangles); |
18 | 522 System.out.println("encoding=" + encoding); |
78 | 523 System.out.println("rectX = "+rectX+": rectY = "+rectY); |
524 System.out.println("rectW = "+rectW+": rectH = "+rectH); | |
54 | 525 switch (encoding) { |
23 | 526 case RfbProto.EncodingRaw: |
54 | 527 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 |
528 + 16); | |
23 | 529 break; |
530 default: | |
531 } | |
18 | 532 } |
130 | 533 int returnMsgtype() { |
534 return messageType; | |
535 } | |
536 | |
65 | 537 |
77 | 538 void readSpeedCheck() throws IOException { |
539 byte[] b = new byte[1]; | |
65 | 540 readFully(b); |
541 } | |
542 | |
77 | 543 void startSpeedCheck() { |
90 | 544 ByteBuffer b = ByteBuffer.allocate(10); |
545 b.put((byte)SpeedCheckMillis); | |
546 b.flip(); | |
66 | 547 startCheckTime = System.currentTimeMillis(); |
548 System.out.println("startChckTime = "+ startCheckTime); | |
90 | 549 LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>(); |
84 | 550 bufs.add(b); |
551 multicastqueue.put(bufs); | |
65 | 552 } |
553 | |
77 | 554 void endSpeedCheck() { |
65 | 555 long accTime = System.currentTimeMillis(); |
556 long time = accTime - startCheckTime; | |
557 System.out.println("checkMillis: " + time); | |
558 } | |
559 | |
560 | |
561 synchronized void changeStatusFlag() { | |
562 printStatusFlag = true; | |
563 } | |
564 | |
565 void printMills() { | |
566 if(printStatusFlag) { | |
567 | |
568 changeStatusFlag(); | |
569 } else { | |
570 changeStatusFlag(); | |
571 } | |
572 } | |
87 | 573 |
574 void speedCheckMillis() { | |
575 Runnable stdin = new Runnable() { | |
576 public void run() { | |
577 int c; | |
578 try { | |
579 while( (c = System.in.read()) != -1 ) { | |
580 switch(c) { | |
581 case 's': | |
582 break; | |
583 default: | |
584 startSpeedCheck(); | |
585 break; | |
586 } | |
587 } | |
588 }catch(IOException e){ | |
589 System.out.println(e); | |
590 } | |
591 } | |
592 }; | |
593 | |
594 new Thread(stdin).start(); | |
595 } | |
162 | 596 |
597 void requestThreadStart() { | |
598 requestThread.start(); | |
599 } | |
600 | |
601 public synchronized void requestThreadNotify() { | |
602 rThread.reStart(); | |
603 } | |
604 | |
86 | 605 |
87 | 606 /** |
607 * gzip byte arrays | |
608 * @param deflater | |
609 * @param inputs | |
610 * byte data[] | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
611 * @param inputIndex |
87 | 612 * @param outputs |
613 * byte data[] | |
614 * @return byte length in last byte array | |
615 * @throws IOException | |
616 */ | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
617 public int zip(Deflater deflater,LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException { |
102 | 618 int len = 0; |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
619 ByteBuffer c1= ByteBuffer.allocate(INFLATE_BUFSIZE); |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
620 while(inputIndex < inputs.size() ) { |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
621 ByteBuffer b1 = inputs.get(inputIndex++); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
622 deflater.setInput(b1.array(),b1.position(),b1.remaining()); |
106 | 623 /** |
624 * If we finish() stream and reset() it, Deflater start new gzip stream, this makes continuous zlib reader unhappy. | |
625 * if we remove finish(), Deflater.deflate() never flushes its output. The original zlib deflate has flush flag. I'm pretty | |
626 * sure this a kind of bug of Java library. | |
627 */ | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
628 if (inputIndex==inputs.size()) |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
629 deflater.finish(); |
106 | 630 int len1 = 0; |
86 | 631 do { |
106 | 632 len1 = deflater.deflate(c1.array(),c1.position(),c1.remaining()); |
102 | 633 if (len1>0) { |
634 len += len1; | |
635 c1.position(c1.position()+len1); | |
636 if (c1.remaining()==0) { | |
637 c1.flip(); outputs.addLast(c1); | |
638 c1 = ByteBuffer.allocate(INFLATE_BUFSIZE); | |
639 } | |
86 | 640 } |
106 | 641 } while (len1 >0 || !deflater.needsInput()); // &&!deflater.finished()); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
642 } |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
643 if (c1.position()!=0) { |
102 | 644 c1.flip(); outputs.addLast(c1); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
645 } |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
646 deflater.reset(); |
90 | 647 return len; |
86 | 648 } |
87 | 649 |
650 /** | |
651 * gunzip byte arrays | |
652 * @param inflater | |
653 * @param inputs | |
654 * byte data[] | |
655 * @param outputs | |
656 * byte data[] | |
91 | 657 *@return number of total bytes |
87 | 658 * @throws IOException |
659 */ | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
660 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs,int bufSize) |
88 | 661 throws DataFormatException { |
102 | 662 int len=0; |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
663 ByteBuffer buf = ByteBuffer.allocate(bufSize); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
664 while (inputIndex < inputs.size()) { |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
665 ByteBuffer input = inputs.get(inputIndex++); |
102 | 666 inflater.setInput(input.array(),input.position(),input.limit()); |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
667 // if (inputIndex==inputs.size()) if inflater/deflater has symmetry, we need this |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
668 // inflater.end(); but this won't work |
88 | 669 do { |
102 | 670 int len0 = inflater.inflate(buf.array(),buf.position(),buf.remaining()); |
671 if (len0>0) { | |
672 buf.position(buf.position()+len0); | |
673 len += len0; | |
674 if (buf.remaining()==0) { | |
675 buf.flip(); | |
676 outputs.addLast(buf); | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
677 buf = ByteBuffer.allocate(bufSize); |
102 | 678 } |
91 | 679 } |
102 | 680 } while (!inflater.needsInput()); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
681 } |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
682 if (buf.position()!=0) { |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
683 buf.flip(); |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
684 outputs.addLast(buf); |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
685 } |
90 | 686 return len; |
86 | 687 } |
156 | 688 |
65 | 689 |
156 | 690 float maxMag = 1; |
109
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
691 /** |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
692 * send data to clients |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
693 * @param dataLen |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
694 * @throws IOException |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
695 * @throws DataFormatException |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
696 * |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
697 * Zlibed packet is compressed in context dependent way, that is, it have to send from the beginning. But this is |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
698 * impossible. So we have to compress it again for each clients. Separate deflater for each clients is necessary. |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
699 * |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
700 * Java's deflater does not support flush. This means to get the result, we have to finish the compression. Reseting |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
701 * start new compression, but it is not accepted well in zlib continuous reading. So we need new Encoding ZRLEE |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
702 * which reset decoder for each packet. ZRLEE can be invisible from user, but it have to be implemented in the clients. |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
703 * ZRLEE compression is not context dependent, so no recompression is necessary. |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
704 */ |
86 | 705 void readSendData(int dataLen) throws IOException, DataFormatException { |
90 | 706 LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>(); |
707 ByteBuffer header = ByteBuffer.allocate(16); | |
708 readFully(header.array(),0,16); | |
709 header.limit(16); | |
710 if (header.get(0)==RfbProto.FramebufferUpdate) { | |
711 int encoding = header.getInt(12); | |
107 | 712 if (encoding==RfbProto.EncodingZRLE||encoding==RfbProto.EncodingZlib) { // ZRLEE is already recompressed |
90 | 713 ByteBuffer len = ByteBuffer.allocate(4); |
714 readFully(len.array(),0,4); len.limit(4); | |
715 ByteBuffer inputData = ByteBuffer.allocate(dataLen-20); | |
151 | 716 |
717 startTiming(); | |
90 | 718 readFully(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20); |
151 | 719 stopTiming(); |
720 | |
90 | 721 LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>(); |
88 | 722 inputs.add(inputData); |
107 | 723 |
724 header.putInt(12, RfbProto.EncodingZRLEE); // means recompress every time | |
725 // using new Deflecter every time is incompatible with the protocol, clients have to be modified. | |
726 Deflater nDeflater = deflater; // new Deflater(); | |
727 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); | |
728 unzip(inflater, inputs, 0 , out, INFLATE_BUFSIZE); | |
729 // dump32(inputs); | |
730 int len2 = zip(nDeflater, out, 0, bufs); | |
731 ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip(); | |
732 bufs.addFirst(blen); | |
733 | |
87 | 734 bufs.addFirst(header); |
86 | 735 multicastqueue.put(bufs); |
156 | 736 // is.reset(); |
130 | 737 |
156 | 738 /* |
739 System.out.println("ZRLE = "+dataLen); | |
740 System.out.println("ZRLEE = "+(len2+20)); | |
741 float mag = (float)dataLen / (float)(len2 + 20); | |
742 System.out.println("ZRLE / ZRLEE = "+ mag); | |
743 if(mag > maxMag) maxMag = mag; | |
744 System.out.println("maxMag = "+maxMag); | |
745 */ | |
86 | 746 return ; |
747 } | |
87 | 748 bufs.add(header); |
749 if (dataLen>16) { | |
90 | 750 ByteBuffer b = ByteBuffer.allocate(dataLen-16); |
156 | 751 startTiming(); |
90 | 752 readFully(b.array(),0,dataLen-16); b.limit(dataLen-16); |
156 | 753 stopTiming(); |
87 | 754 bufs.add(b); |
755 } | |
86 | 756 multicastqueue.put(bufs); |
135 | 757 // is.reset(); |
758 return ; | |
130 | 759 } |
760 is.reset(); | |
86 | 761 |
762 // It may be compressed. We can inflate here to avoid repeating clients decompressing here, | |
763 // but it may generate too many large data. It is better to do it in each client. | |
764 // But we have do inflation for all input data, so we have to do it here. | |
765 } | |
43 | 766 |
71 | 767 void newClient(AcceptThread acceptThread, final Socket newCli, |
54 | 768 final OutputStream os, final InputStream is) throws IOException { |
769 // createBimgFlag = true; | |
770 // rfb.addSockTmp(newCli); | |
771 // addSock(newCli); | |
112 | 772 final int myId = clients; |
90 | 773 final Client <LinkedList<ByteBuffer>> c = multicastqueue.newClient(); |
113
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
774 final AtomicInteger writerRunning = new AtomicInteger(); |
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
775 writerRunning.set(1); |
112 | 776 /** |
777 * Timeout thread. If a client is suspended, it has top of queue indefinitely, which caused memory | |
778 * overflow. After the timeout, we poll the queue and discard it. Start long wait if writer is running. | |
779 */ | |
109
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
780 final Runnable timer = new Runnable() { |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
781 public void run() { |
113
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
782 int count = 0; |
109
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
783 for(;;) { |
114 | 784 long timeout = 30000/8; |
109
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
785 try { |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
786 synchronized(this) { |
113
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
787 int state,flag; |
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
788 writerRunning.set(0); |
109
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
789 wait(timeout); |
113
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
790 flag = 0; |
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
791 while((state=writerRunning.get())==0) { |
109
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
792 c.poll(); // discard, should be timeout |
113
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
793 count++; |
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
794 if (flag==0) { |
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
795 System.out.println("Discarding "+myId + " count="+ count); flag = 1; |
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
796 } |
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
797 wait(10); // if this is too short, writer cannot take the poll, if this is too long, memory will overflow... |
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
798 } |
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
799 if (flag==1) System.out.println("Resuming "+myId + " count="+count); |
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
800 if (state!=1) { |
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
801 System.out.println("Client died "+myId); |
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
802 break; |
109
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
803 } |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
804 } |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
805 } catch (InterruptedException e) { |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
806 } |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
807 } |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
808 } |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
809 }; |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
810 new Thread(timer).start(); |
112 | 811 /** |
812 * discard all incoming from clients | |
813 */ | |
102 | 814 final Runnable reader = new Runnable() { |
815 public void run() { | |
816 byte b[] = new byte[4096]; | |
817 for(;;) { | |
818 try { | |
819 int c = is.read(b); | |
820 if (c<=0) throw new IOException(); | |
113
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
821 // System.out.println("client read "+c); |
102 | 822 } catch (IOException e) { |
823 try { | |
113
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
824 writerRunning.set(2); |
102 | 825 os.close(); |
826 is.close(); | |
827 } catch (IOException e1) { | |
828 } | |
829 return; | |
830 } | |
831 } | |
832 } | |
833 }; | |
112 | 834 /** |
835 * send packets to a client | |
836 */ | |
54 | 837 Runnable sender = new Runnable() { |
838 public void run() { | |
113
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
839 writerRunning.set(1); |
54 | 840 try { |
166
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
841 requestThreadNotify(); |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
842 |
65 | 843 /** |
844 * initial connection of RFB protocol | |
845 */ | |
54 | 846 sendRfbVersion(os); |
131 | 847 // readVersionMsg(is); |
155 | 848 int rfbMinor = readVersionMsg(is,os); |
54 | 849 sendSecurityType(os); |
850 readSecType(is); | |
851 sendSecResult(os); | |
852 readClientInit(is); | |
853 sendInitData(os); | |
112 | 854 new Thread(reader).start(); // discard incoming packet here after. |
162 | 855 // writeFramebufferUpdateRequest(0,0, framebufferWidth, framebufferHeight, false ); |
54 | 856 for (;;) { |
90 | 857 LinkedList<ByteBuffer> bufs = c.poll(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
858 int inputIndex = 0; |
109
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
859 ByteBuffer header = bufs.get(inputIndex); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
860 if (header==null) continue; |
90 | 861 if (header.get(0)==RfbProto.FramebufferUpdate) { |
113
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
862 // System.out.println("client "+ myId); |
87 | 863 } |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
864 writeToClient(os, bufs, inputIndex); |
113
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
865 writerRunning.set(1); // yes my client is awaking. |
54 | 866 } |
867 } catch (IOException e) { | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
868 try { |
113
8424f64dd736
time out and discarding. kill time out thread after client death.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
869 writerRunning.set(2); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
870 os.close(); |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
871 } catch (IOException e1) { |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
872 } |
87 | 873 /* if socket closed cliList.remove(newCli); */ |
54 | 874 } |
875 } | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
876 |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
877 public void writeToClient(final OutputStream os, |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
878 LinkedList<ByteBuffer> bufs, int inputIndex) |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
879 throws IOException { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
880 while(inputIndex < bufs.size()) { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
881 ByteBuffer b = bufs.get(inputIndex++); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
882 os.write(b.array(), b.position(), b.limit()); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
883 } |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
884 os.flush(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
885 } |
54 | 886 }; |
80 | 887 clients++; |
54 | 888 new Thread(sender).start(); |
889 | |
890 } | |
66 | 891 |
892 | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
893 public void dump32(LinkedList<ByteBuffer>bufs) { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
894 int len =0; |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
895 for(ByteBuffer b: bufs) len += b.remaining(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
896 ByteBuffer top = bufs.getFirst(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
897 ByteBuffer end = bufs.getLast(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
898 System.err.println("length: "+len); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
899 System.err.print("head 0: "); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
900 for(int i = 0; i<16 && i < top.remaining(); i++) { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
901 System.err.print(" "+ top.get(i)); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
902 } |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
903 System.err.print("tail 0: "); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
904 for(int i = 0; i<16 && i < end.remaining(); i++) { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
905 System.err.print(" "+end.get(i)); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
906 } |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
907 System.err.println(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
908 } |
88 | 909 |
910 @Test | |
911 public void test1() { | |
912 try { | |
90 | 913 LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>(); |
914 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); | |
915 LinkedList<ByteBuffer> out2 = new LinkedList<ByteBuffer>(); | |
107 | 916 // if (false) { |
917 // for(int i=0;i<10;i++) { | |
918 // in.add(ByteBuffer.wrap("test1".getBytes())); | |
919 // in.add(ByteBuffer.wrap("test2".getBytes())); | |
920 // in.add(ByteBuffer.wrap("test3".getBytes())); | |
921 // in.add(ByteBuffer.wrap("test44".getBytes())); | |
922 // } | |
923 // } else | |
924 { | |
106 | 925 String t = ""; |
926 for(int i=0;i<10;i++) { | |
927 t += "test1"; | |
928 t += "test2"; | |
929 t += "test3"; | |
930 t += "test44"; | |
931 } | |
932 in.add(ByteBuffer.wrap(t.getBytes())); | |
933 } | |
934 | |
92 | 935 LinkedList<ByteBuffer> in1 = clone(in); |
90 | 936 |
88 | 937 Deflater deflater = new Deflater(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
938 zip(deflater,in,0,out); |
92 | 939 // LinkedList<ByteBuffer> out3 = clone(out); zipped result is depend on deflator's state |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
940 unzip(inflater, out, 0,out2, INFLATE_BUFSIZE); |
106 | 941 // inflater.reset(); |
92 | 942 equalByteBuffers(in1, out2); |
943 LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>(); | |
104 | 944 deflater = new Deflater(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
945 zip(deflater,out2,0,out4); |
92 | 946 LinkedList<ByteBuffer> out5 = new LinkedList<ByteBuffer>(); |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
947 unzip(inflater,out4,0, out5, INFLATE_BUFSIZE); |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
948 int len = equalByteBuffers(in1,out5); |
92 | 949 |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
950 System.out.println("Test Ok. "+len); |
88 | 951 } catch (Exception e) { |
952 assertEquals(0,1); | |
953 } | |
954 } | |
955 | |
92 | 956 private LinkedList<ByteBuffer> clone(LinkedList<ByteBuffer> in) { |
957 LinkedList<ByteBuffer> copy = new LinkedList<ByteBuffer>(); | |
958 for(ByteBuffer b: in) { | |
959 ByteBuffer c = b.duplicate(); | |
960 copy.add(c); | |
961 } | |
962 return copy; | |
963 } | |
964 | |
93 | 965 |
966 | |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
967 public int equalByteBuffers(LinkedList<ByteBuffer> in, |
92 | 968 LinkedList<ByteBuffer> out2) { |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
969 int len = 0; |
93 | 970 Iterable<Byte> i = byteBufferIterator(in); |
971 Iterator<Byte> o = byteBufferIterator(out2).iterator(); | |
972 | |
973 for(int b: i) { | |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
974 len ++; |
93 | 975 if (o.hasNext()) { |
976 int c = o.next(); | |
977 assertEquals(b,c); | |
978 } else | |
979 assertEquals(0,1); | |
980 } | |
104 | 981 if (o.hasNext()) |
982 assertEquals(0,1); | |
93 | 983 // System.out.println(); |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
984 return len; |
93 | 985 } |
986 | |
987 private Iterable<Byte> byteBufferIterator(final LinkedList<ByteBuffer> in) { | |
988 return new Iterable<Byte>() { | |
989 public Iterator<Byte> iterator() { | |
990 return new Iterator<Byte>() { | |
991 int bytes = 0; | |
992 int buffers = 0; | |
993 public boolean hasNext() { | |
104 | 994 for(;;) { |
995 if (buffers>=in.size()) return false; | |
996 ByteBuffer b = in.get(buffers); | |
997 if (! (bytes<b.remaining())) { | |
998 buffers ++; bytes=0; | |
999 } else return true; | |
1000 } | |
92 | 1001 } |
93 | 1002 public Byte next() { |
104 | 1003 ByteBuffer bf =in.get(buffers); |
93 | 1004 byte b = bf.get(bytes++); |
1005 if (bf.remaining()<=bytes) { | |
1006 buffers++; | |
1007 bytes = 0; | |
1008 } | |
1009 // System.out.print(b); | |
1010 return b; | |
1011 } | |
1012 public void remove() { | |
1013 } | |
1014 }; | |
92 | 1015 } |
93 | 1016 }; |
92 | 1017 } |
1018 | |
54 | 1019 } |
66 | 1020 |
1021 |