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