Mercurial > hg > Members > nobuyasu > tightVNCProxy
annotate src/myVncProxy/MyRfbProto.java @ 191:b2f0cd0cff6c default tip
Added tag Version-1.0 for changeset 79046b4e5990
author | Yu Taninari <you@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 29 Nov 2011 15:52:44 +0900 |
parents | e7cc8bcf261d |
children |
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; | |
18 | 54 private int messageType; |
55 private int rectangles; | |
23 | 56 private int rectX; |
57 private int rectY; | |
58 private int rectW; | |
59 private int rectH; | |
18 | 60 private int encoding; |
27 | 61 private int zLen; |
104 | 62 private boolean clicomp = false; |
18 | 63 |
23 | 64 private ServerSocket servSock; |
144 | 65 protected int acceptPort; |
184 | 66 //private byte initData[]; |
67 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() { |
187 | 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); | |
187 | 525 */ |
54 | 526 switch (encoding) { |
23 | 527 case RfbProto.EncodingRaw: |
54 | 528 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 |
529 + 16); | |
23 | 530 break; |
531 default: | |
532 } | |
18 | 533 } |
130 | 534 int returnMsgtype() { |
535 return messageType; | |
536 } | |
537 | |
65 | 538 |
77 | 539 void readSpeedCheck() throws IOException { |
540 byte[] b = new byte[1]; | |
65 | 541 readFully(b); |
542 } | |
543 | |
77 | 544 void startSpeedCheck() { |
90 | 545 ByteBuffer b = ByteBuffer.allocate(10); |
546 b.put((byte)SpeedCheckMillis); | |
547 b.flip(); | |
66 | 548 startCheckTime = System.currentTimeMillis(); |
549 System.out.println("startChckTime = "+ startCheckTime); | |
90 | 550 LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>(); |
84 | 551 bufs.add(b); |
552 multicastqueue.put(bufs); | |
65 | 553 } |
554 | |
77 | 555 void endSpeedCheck() { |
65 | 556 long accTime = System.currentTimeMillis(); |
557 long time = accTime - startCheckTime; | |
558 System.out.println("checkMillis: " + time); | |
559 } | |
560 | |
561 | |
562 synchronized void changeStatusFlag() { | |
563 printStatusFlag = true; | |
564 } | |
565 | |
566 void printMills() { | |
567 if(printStatusFlag) { | |
568 | |
569 changeStatusFlag(); | |
570 } else { | |
571 changeStatusFlag(); | |
572 } | |
573 } | |
87 | 574 |
575 void speedCheckMillis() { | |
576 Runnable stdin = new Runnable() { | |
577 public void run() { | |
578 int c; | |
579 try { | |
580 while( (c = System.in.read()) != -1 ) { | |
581 switch(c) { | |
582 case 's': | |
583 break; | |
584 default: | |
585 startSpeedCheck(); | |
586 break; | |
587 } | |
588 } | |
589 }catch(IOException e){ | |
590 System.out.println(e); | |
591 } | |
592 } | |
593 }; | |
594 | |
595 new Thread(stdin).start(); | |
596 } | |
162 | 597 |
598 void requestThreadStart() { | |
599 requestThread.start(); | |
600 } | |
601 | |
184 | 602 |
162 | 603 public synchronized void requestThreadNotify() { |
604 rThread.reStart(); | |
605 } | |
606 | |
86 | 607 |
87 | 608 /** |
609 * gzip byte arrays | |
610 * @param deflater | |
611 * @param inputs | |
612 * byte data[] | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
613 * @param inputIndex |
87 | 614 * @param outputs |
615 * byte data[] | |
616 * @return byte length in last byte array | |
617 * @throws IOException | |
618 */ | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
619 public int zip(Deflater deflater,LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException { |
102 | 620 int len = 0; |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
621 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
|
622 while(inputIndex < inputs.size() ) { |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
623 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
|
624 deflater.setInput(b1.array(),b1.position(),b1.remaining()); |
106 | 625 /** |
626 * If we finish() stream and reset() it, Deflater start new gzip stream, this makes continuous zlib reader unhappy. | |
627 * if we remove finish(), Deflater.deflate() never flushes its output. The original zlib deflate has flush flag. I'm pretty | |
628 * sure this a kind of bug of Java library. | |
629 */ | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
630 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
|
631 deflater.finish(); |
106 | 632 int len1 = 0; |
86 | 633 do { |
106 | 634 len1 = deflater.deflate(c1.array(),c1.position(),c1.remaining()); |
102 | 635 if (len1>0) { |
636 len += len1; | |
637 c1.position(c1.position()+len1); | |
638 if (c1.remaining()==0) { | |
639 c1.flip(); outputs.addLast(c1); | |
640 c1 = ByteBuffer.allocate(INFLATE_BUFSIZE); | |
641 } | |
86 | 642 } |
106 | 643 } 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
|
644 } |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
645 if (c1.position()!=0) { |
102 | 646 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
|
647 } |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
648 deflater.reset(); |
90 | 649 return len; |
86 | 650 } |
87 | 651 |
652 /** | |
653 * gunzip byte arrays | |
654 * @param inflater | |
655 * @param inputs | |
656 * byte data[] | |
657 * @param outputs | |
658 * byte data[] | |
91 | 659 *@return number of total bytes |
87 | 660 * @throws IOException |
661 */ | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
662 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs,int bufSize) |
88 | 663 throws DataFormatException { |
102 | 664 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
|
665 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
|
666 while (inputIndex < inputs.size()) { |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
667 ByteBuffer input = inputs.get(inputIndex++); |
102 | 668 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
|
669 // 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
|
670 // inflater.end(); but this won't work |
88 | 671 do { |
102 | 672 int len0 = inflater.inflate(buf.array(),buf.position(),buf.remaining()); |
673 if (len0>0) { | |
674 buf.position(buf.position()+len0); | |
675 len += len0; | |
676 if (buf.remaining()==0) { | |
677 buf.flip(); | |
678 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
|
679 buf = ByteBuffer.allocate(bufSize); |
102 | 680 } |
91 | 681 } |
102 | 682 } while (!inflater.needsInput()); |
98
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
683 } |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
684 if (buf.position()!=0) { |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
685 buf.flip(); |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
686 outputs.addLast(buf); |
3db7ac2b10f7
JUnit test passed, but VNC stopped.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
97
diff
changeset
|
687 } |
90 | 688 return len; |
86 | 689 } |
156 | 690 |
65 | 691 |
156 | 692 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
|
693 /** |
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 * 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
|
695 * @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
|
696 * @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
|
697 * @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
|
698 * |
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 * 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
|
700 * 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
|
701 * |
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 * 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
|
703 * 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
|
704 * 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
|
705 * 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
|
706 */ |
86 | 707 void readSendData(int dataLen) throws IOException, DataFormatException { |
90 | 708 LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>(); |
709 ByteBuffer header = ByteBuffer.allocate(16); | |
710 readFully(header.array(),0,16); | |
711 header.limit(16); | |
712 if (header.get(0)==RfbProto.FramebufferUpdate) { | |
713 int encoding = header.getInt(12); | |
107 | 714 if (encoding==RfbProto.EncodingZRLE||encoding==RfbProto.EncodingZlib) { // ZRLEE is already recompressed |
90 | 715 ByteBuffer len = ByteBuffer.allocate(4); |
716 readFully(len.array(),0,4); len.limit(4); | |
717 ByteBuffer inputData = ByteBuffer.allocate(dataLen-20); | |
151 | 718 |
719 startTiming(); | |
90 | 720 readFully(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20); |
151 | 721 stopTiming(); |
722 | |
90 | 723 LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>(); |
88 | 724 inputs.add(inputData); |
107 | 725 |
726 header.putInt(12, RfbProto.EncodingZRLEE); // means recompress every time | |
727 // using new Deflecter every time is incompatible with the protocol, clients have to be modified. | |
728 Deflater nDeflater = deflater; // new Deflater(); | |
729 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); | |
730 unzip(inflater, inputs, 0 , out, INFLATE_BUFSIZE); | |
731 // dump32(inputs); | |
732 int len2 = zip(nDeflater, out, 0, bufs); | |
733 ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip(); | |
734 bufs.addFirst(blen); | |
735 | |
87 | 736 bufs.addFirst(header); |
86 | 737 multicastqueue.put(bufs); |
156 | 738 // is.reset(); |
130 | 739 |
156 | 740 /* |
741 System.out.println("ZRLE = "+dataLen); | |
742 System.out.println("ZRLEE = "+(len2+20)); | |
743 float mag = (float)dataLen / (float)(len2 + 20); | |
744 System.out.println("ZRLE / ZRLEE = "+ mag); | |
745 if(mag > maxMag) maxMag = mag; | |
746 System.out.println("maxMag = "+maxMag); | |
747 */ | |
86 | 748 return ; |
749 } | |
87 | 750 bufs.add(header); |
751 if (dataLen>16) { | |
90 | 752 ByteBuffer b = ByteBuffer.allocate(dataLen-16); |
156 | 753 startTiming(); |
90 | 754 readFully(b.array(),0,dataLen-16); b.limit(dataLen-16); |
156 | 755 stopTiming(); |
87 | 756 bufs.add(b); |
757 } | |
86 | 758 multicastqueue.put(bufs); |
135 | 759 // is.reset(); |
760 return ; | |
130 | 761 } |
762 is.reset(); | |
86 | 763 |
764 // It may be compressed. We can inflate here to avoid repeating clients decompressing here, | |
765 // but it may generate too many large data. It is better to do it in each client. | |
766 // But we have do inflation for all input data, so we have to do it here. | |
767 } | |
43 | 768 |
71 | 769 void newClient(AcceptThread acceptThread, final Socket newCli, |
54 | 770 final OutputStream os, final InputStream is) throws IOException { |
771 // createBimgFlag = true; | |
772 // rfb.addSockTmp(newCli); | |
773 // addSock(newCli); | |
112 | 774 final int myId = clients; |
90 | 775 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
|
776 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
|
777 writerRunning.set(1); |
112 | 778 /** |
779 * Timeout thread. If a client is suspended, it has top of queue indefinitely, which caused memory | |
780 * overflow. After the timeout, we poll the queue and discard it. Start long wait if writer is running. | |
781 */ | |
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
|
782 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
|
783 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
|
784 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
|
785 for(;;) { |
114 | 786 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
|
787 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
|
788 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
|
789 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
|
790 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
|
791 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
|
792 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
|
793 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
|
794 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
|
795 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
|
796 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
|
797 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
|
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 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
|
800 } |
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 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
|
802 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
|
803 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
|
804 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
|
805 } |
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 } 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
|
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 } |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
811 }; |
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
107
diff
changeset
|
812 new Thread(timer).start(); |
112 | 813 /** |
814 * discard all incoming from clients | |
815 */ | |
102 | 816 final Runnable reader = new Runnable() { |
817 public void run() { | |
818 byte b[] = new byte[4096]; | |
819 for(;;) { | |
820 try { | |
821 int c = is.read(b); | |
822 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
|
823 // System.out.println("client read "+c); |
102 | 824 } catch (IOException e) { |
825 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
|
826 writerRunning.set(2); |
102 | 827 os.close(); |
828 is.close(); | |
829 } catch (IOException e1) { | |
830 } | |
831 return; | |
832 } | |
833 } | |
834 } | |
835 }; | |
112 | 836 /** |
837 * send packets to a client | |
838 */ | |
54 | 839 Runnable sender = new Runnable() { |
840 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
|
841 writerRunning.set(1); |
54 | 842 try { |
166
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
843 requestThreadNotify(); |
3c055da4d050
add authenticate AuthAccess
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
162
diff
changeset
|
844 |
65 | 845 /** |
846 * initial connection of RFB protocol | |
847 */ | |
54 | 848 sendRfbVersion(os); |
131 | 849 // readVersionMsg(is); |
155 | 850 int rfbMinor = readVersionMsg(is,os); |
54 | 851 sendSecurityType(os); |
852 readSecType(is); | |
853 sendSecResult(os); | |
854 readClientInit(is); | |
855 sendInitData(os); | |
112 | 856 new Thread(reader).start(); // discard incoming packet here after. |
162 | 857 // writeFramebufferUpdateRequest(0,0, framebufferWidth, framebufferHeight, false ); |
54 | 858 for (;;) { |
90 | 859 LinkedList<ByteBuffer> bufs = c.poll(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
860 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
|
861 ByteBuffer header = bufs.get(inputIndex); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
862 if (header==null) continue; |
90 | 863 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
|
864 // System.out.println("client "+ myId); |
87 | 865 } |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
866 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
|
867 writerRunning.set(1); // yes my client is awaking. |
54 | 868 } |
869 } catch (IOException e) { | |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
870 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
|
871 writerRunning.set(2); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
872 os.close(); |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
873 } catch (IOException e1) { |
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
874 } |
87 | 875 /* if socket closed cliList.remove(newCli); */ |
54 | 876 } |
877 } | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
878 |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
879 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
|
880 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
|
881 throws IOException { |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
882 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
|
883 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
|
884 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
|
885 } |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
886 os.flush(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
887 } |
54 | 888 }; |
80 | 889 clients++; |
54 | 890 new Thread(sender).start(); |
891 | |
892 } | |
66 | 893 |
894 | |
105
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
895 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
|
896 int len =0; |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
897 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
|
898 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
|
899 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
|
900 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
|
901 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
|
902 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
|
903 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
|
904 } |
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("tail 0: "); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
906 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
|
907 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
|
908 } |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
909 System.err.println(); |
e166c3cad2b8
new Defleter is working with fixed TightVNC clients
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
104
diff
changeset
|
910 } |
88 | 911 |
912 @Test | |
913 public void test1() { | |
914 try { | |
90 | 915 LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>(); |
916 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); | |
917 LinkedList<ByteBuffer> out2 = new LinkedList<ByteBuffer>(); | |
107 | 918 // if (false) { |
919 // for(int i=0;i<10;i++) { | |
920 // in.add(ByteBuffer.wrap("test1".getBytes())); | |
921 // in.add(ByteBuffer.wrap("test2".getBytes())); | |
922 // in.add(ByteBuffer.wrap("test3".getBytes())); | |
923 // in.add(ByteBuffer.wrap("test44".getBytes())); | |
924 // } | |
925 // } else | |
926 { | |
106 | 927 String t = ""; |
928 for(int i=0;i<10;i++) { | |
929 t += "test1"; | |
930 t += "test2"; | |
931 t += "test3"; | |
932 t += "test44"; | |
933 } | |
934 in.add(ByteBuffer.wrap(t.getBytes())); | |
935 } | |
936 | |
92 | 937 LinkedList<ByteBuffer> in1 = clone(in); |
90 | 938 |
88 | 939 Deflater deflater = new Deflater(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
940 zip(deflater,in,0,out); |
92 | 941 // 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
|
942 unzip(inflater, out, 0,out2, INFLATE_BUFSIZE); |
106 | 943 // inflater.reset(); |
92 | 944 equalByteBuffers(in1, out2); |
945 LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>(); | |
104 | 946 deflater = new Deflater(); |
96
f0790bcf000d
fix concurrent modification
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
95
diff
changeset
|
947 zip(deflater,out2,0,out4); |
92 | 948 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
|
949 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
|
950 int len = equalByteBuffers(in1,out5); |
92 | 951 |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
952 System.out.println("Test Ok. "+len); |
88 | 953 } catch (Exception e) { |
954 assertEquals(0,1); | |
955 } | |
956 } | |
957 | |
92 | 958 private LinkedList<ByteBuffer> clone(LinkedList<ByteBuffer> in) { |
959 LinkedList<ByteBuffer> copy = new LinkedList<ByteBuffer>(); | |
960 for(ByteBuffer b: in) { | |
961 ByteBuffer c = b.duplicate(); | |
962 copy.add(c); | |
963 } | |
964 return copy; | |
965 } | |
966 | |
93 | 967 |
968 | |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
969 public int equalByteBuffers(LinkedList<ByteBuffer> in, |
92 | 970 LinkedList<ByteBuffer> out2) { |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
971 int len = 0; |
93 | 972 Iterable<Byte> i = byteBufferIterator(in); |
973 Iterator<Byte> o = byteBufferIterator(out2).iterator(); | |
974 | |
975 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
|
976 len ++; |
93 | 977 if (o.hasNext()) { |
978 int c = o.next(); | |
979 assertEquals(b,c); | |
980 } else | |
981 assertEquals(0,1); | |
982 } | |
104 | 983 if (o.hasNext()) |
984 assertEquals(0,1); | |
93 | 985 // System.out.println(); |
99
0c5762c3a8dd
Test and VNC not working... Memory Overflow...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
98
diff
changeset
|
986 return len; |
93 | 987 } |
988 | |
989 private Iterable<Byte> byteBufferIterator(final LinkedList<ByteBuffer> in) { | |
990 return new Iterable<Byte>() { | |
991 public Iterator<Byte> iterator() { | |
992 return new Iterator<Byte>() { | |
993 int bytes = 0; | |
994 int buffers = 0; | |
995 public boolean hasNext() { | |
104 | 996 for(;;) { |
997 if (buffers>=in.size()) return false; | |
998 ByteBuffer b = in.get(buffers); | |
999 if (! (bytes<b.remaining())) { | |
1000 buffers ++; bytes=0; | |
1001 } else return true; | |
1002 } | |
92 | 1003 } |
93 | 1004 public Byte next() { |
104 | 1005 ByteBuffer bf =in.get(buffers); |
93 | 1006 byte b = bf.get(bytes++); |
1007 if (bf.remaining()<=bytes) { | |
1008 buffers++; | |
1009 bytes = 0; | |
1010 } | |
1011 // System.out.print(b); | |
1012 return b; | |
1013 } | |
1014 public void remove() { | |
1015 } | |
1016 }; | |
92 | 1017 } |
93 | 1018 }; |
92 | 1019 } |
1020 | |
54 | 1021 } |
66 | 1022 |
1023 |