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