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