17
|
1 package myVncClient;
|
19
|
2 import java.awt.Graphics;
|
|
3 import java.awt.Image;
|
18
|
4 import java.awt.image.BufferedImage;
|
19
|
5 import java.io.BufferedOutputStream;
|
15
|
6 import java.io.BufferedReader;
|
18
|
7 import java.io.ByteArrayInputStream;
|
19
|
8 import java.io.ByteArrayOutputStream;
|
7
|
9 import java.io.IOException;
|
15
|
10 import java.io.InputStreamReader;
|
18
|
11 import java.net.BindException;
|
10
|
12 import java.net.ServerSocket;
|
7
|
13 import java.net.Socket;
|
10
|
14 import java.util.LinkedList;
|
8
|
15
|
18
|
16 import javax.imageio.ImageIO;
|
|
17
|
25
|
18 import java.util.concurrent.ExecutorService;
|
|
19 import java.util.concurrent.Executors;
|
|
20 import java.io.OutputStream;
|
7
|
21
|
|
22 class MyRfbProto extends RfbProto {
|
8
|
23
|
15
|
24 private int messageType;
|
|
25 private int rectangles;
|
19
|
26 private int rectX;
|
|
27 private int rectY;
|
|
28 private int rectW;
|
|
29 private int rectH;
|
15
|
30 private int encoding;
|
19
|
31 private int zLen;
|
25
|
32 private int dataLen;
|
15
|
33
|
19
|
34 private ServerSocket servSock;
|
18
|
35 private int acceptPort;
|
8
|
36 private byte initData[];
|
19
|
37 private LinkedList <Socket> cliListTmp;
|
10
|
38 private LinkedList <Socket> cliList;
|
25
|
39 private LinkedList <Thread> sendThreads;
|
19
|
40 boolean createBimgFlag;
|
25
|
41
|
|
42 ExecutorService executor;
|
|
43
|
|
44
|
18
|
45 byte[] pngBytes;
|
25
|
46
|
15
|
47 MyRfbProto(String h, int p, VncViewer v ) throws IOException {
|
7
|
48 super(h, p, v);
|
10
|
49 cliList = new LinkedList <Socket>();
|
19
|
50 cliListTmp = new LinkedList <Socket>();
|
|
51 createBimgFlag = false;
|
25
|
52 sendThreads = new LinkedList <Thread>();
|
|
53 // executor = Executors.newCachedThreadPool();
|
|
54 executor = Executors.newSingleThreadExecutor();
|
10
|
55 }
|
|
56
|
15
|
57 MyRfbProto(String h, int p) throws IOException {
|
|
58 super(h, p);
|
|
59 cliList = new LinkedList <Socket>();
|
19
|
60 cliListTmp = new LinkedList <Socket>();
|
|
61 createBimgFlag = false;
|
25
|
62 sendThreads = new LinkedList <Thread>();
|
|
63 // executor = Executors.newCachedThreadPool();
|
|
64 executor = Executors.newSingleThreadExecutor();
|
15
|
65 }
|
19
|
66
|
10
|
67 void initServSock(int port) throws IOException{
|
|
68 servSock = new ServerSocket(port);
|
18
|
69 acceptPort = port;
|
|
70 }
|
|
71 void selectPort(){
|
|
72 int i = 5550;
|
|
73 while(true){
|
|
74 try{
|
|
75 initServSock(i);
|
|
76 break;
|
|
77 }catch(BindException e){
|
|
78 i++;
|
|
79 continue;
|
|
80 }catch(IOException e){
|
19
|
81
|
18
|
82 }
|
|
83 }
|
25
|
84 System.out.println("accept port = "+i);
|
18
|
85 }
|
|
86 int getAcceptPort(){
|
|
87 return acceptPort;
|
7
|
88 }
|
10
|
89 void setSoTimeout(int num) throws IOException {
|
|
90 servSock.setSoTimeout(num);
|
|
91 }
|
|
92
|
|
93 Socket accept() throws IOException {
|
|
94 return servSock.accept();
|
|
95 }
|
|
96
|
|
97 void addSock(Socket sock){
|
|
98 cliList.add(sock);
|
|
99 }
|
19
|
100 void addSockTmp(Socket sock){
|
22
|
101 System.out.println("connected "+sock.getInetAddress());
|
19
|
102 cliListTmp.add(sock);
|
|
103 }
|
10
|
104
|
7
|
105 void mark(int len) throws IOException {
|
|
106 is.mark(len);
|
|
107 }
|
|
108
|
|
109 void reset() throws IOException {
|
|
110 is.reset();
|
15
|
111 }
|
7
|
112
|
8
|
113 boolean markSupported() {
|
7
|
114 return is.markSupported();
|
|
115 }
|
8
|
116
|
|
117 void readServerInit() throws IOException {
|
|
118
|
|
119 mark(255);
|
|
120 skipBytes(20);
|
|
121 int nlen = readU32();
|
10
|
122 int blen = 20+4+nlen;
|
|
123 initData = new byte[blen];
|
|
124 reset();
|
|
125
|
|
126 mark(blen);
|
8
|
127 readFully(initData);
|
|
128 reset();
|
|
129
|
7
|
130 framebufferWidth = readU16();
|
|
131 framebufferHeight = readU16();
|
|
132 bitsPerPixel = readU8();
|
|
133 depth = readU8();
|
|
134 bigEndian = (readU8() != 0);
|
|
135 trueColour = (readU8() != 0);
|
|
136 redMax = readU16();
|
|
137 greenMax = readU16();
|
|
138 blueMax = readU16();
|
|
139 redShift = readU8();
|
|
140 greenShift = readU8();
|
|
141 blueShift = readU8();
|
8
|
142 byte[] pad = new byte[3];
|
|
143 readFully(pad);
|
|
144 int nameLength = readU32();
|
|
145 byte[] name = new byte[nameLength];
|
|
146 readFully(name);
|
|
147 desktopName = new String(name);
|
7
|
148
|
8
|
149 // Read interaction capabilities (TightVNC protocol extensions)
|
|
150 if (protocolTightVNC) {
|
|
151 int nServerMessageTypes = readU16();
|
|
152 int nClientMessageTypes = readU16();
|
|
153 int nEncodingTypes = readU16();
|
|
154 readU16();
|
|
155 readCapabilityList(serverMsgCaps, nServerMessageTypes);
|
|
156 readCapabilityList(clientMsgCaps, nClientMessageTypes);
|
|
157 readCapabilityList(encodingCaps, nEncodingTypes);
|
|
158 }
|
|
159
|
|
160 inNormalProtocol = true;
|
7
|
161 }
|
|
162
|
9
|
163 void sendInitData(Socket sock) throws IOException{
|
10
|
164 sock.getOutputStream().write(initData);
|
8
|
165 }
|
10
|
166
|
15
|
167 void sendData(byte b[]){
|
|
168 try{
|
|
169 for(Socket cli : cliList){
|
|
170 try{
|
|
171 cli.getOutputStream().write(b, 0, b.length);
|
|
172 }catch(IOException e){
|
|
173 // if socket closed
|
|
174 cliList.remove(cli);
|
|
175 }
|
|
176 }
|
19
|
177 // System.out.println("cliSize="+cliSize());
|
15
|
178 }catch(Exception e){
|
|
179 }
|
19
|
180 }
|
|
181
|
|
182 void sendPngImage(){
|
|
183 try{
|
|
184 for(Socket cli : cliListTmp){
|
|
185 try{
|
|
186 sendPngData(cli);
|
|
187 addSock(cli);
|
|
188 }catch(IOException e){
|
|
189 // if socket closed
|
|
190 cliListTmp.remove(cli);
|
|
191 }
|
|
192 }
|
|
193 // System.out.println("cliSize="+cliSize());
|
|
194 }catch(Exception e){
|
|
195 }
|
|
196 cliListTmp.clear();
|
|
197 }
|
25
|
198
|
|
199
|
19
|
200
|
15
|
201 boolean ready() throws IOException {
|
|
202 BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
|
203 return br.ready();
|
10
|
204 }
|
|
205
|
|
206 int cliSize(){
|
|
207 return cliList.size();
|
|
208 }
|
15
|
209 void printNumBytesRead(){
|
|
210 System.out.println("numBytesRead="+numBytesRead);
|
|
211 }
|
|
212 void bufResetSend(int size) throws IOException {
|
|
213 reset();
|
|
214 int len = size;
|
|
215 if(available() < size )
|
|
216 len = available();
|
|
217 byte buffer[] = new byte[len];
|
|
218 readFully(buffer);
|
|
219 sendData(buffer);
|
|
220 }
|
25
|
221 void readSendData()throws IOException{
|
|
222 byte buffer[] = new byte[dataLen];
|
|
223 readFully(buffer);
|
|
224 reset();
|
|
225
|
|
226 for(Socket cli : cliList){
|
|
227 try{
|
|
228 OutputStream out = cli.getOutputStream();
|
|
229 executor.execute(new SendThread(out, buffer));
|
|
230 }catch(IOException e){
|
|
231 // if client socket closed
|
|
232 cliListTmp.remove(cli);
|
|
233 }catch(Exception e){
|
|
234
|
|
235 }
|
|
236
|
|
237 }
|
|
238 }
|
15
|
239 void regiFramebufferUpdate()throws IOException{
|
19
|
240 mark(20);
|
15
|
241 messageType = readU8();
|
|
242 skipBytes(1);
|
|
243 rectangles = readU16();
|
19
|
244 rectX = readU16();
|
|
245 rectY = readU16();
|
|
246 rectW = readU16();
|
|
247 rectH = readU16();
|
|
248 encoding = readU32();
|
|
249 if(encoding == 16)
|
|
250 zLen = readU32();
|
|
251 reset();
|
|
252 }
|
|
253 void checkAndMark() throws IOException{
|
|
254 switch(encoding){
|
|
255 case RfbProto.EncodingRaw:
|
25
|
256 dataLen = rectW * rectH * 4 + 16;
|
|
257 mark(dataLen);
|
19
|
258 break;
|
|
259 case RfbProto.EncodingZRLE:
|
25
|
260 dataLen = zLen+20;
|
|
261 mark(dataLen);
|
19
|
262 break;
|
|
263 default:
|
|
264 mark(1000000);
|
|
265 }
|
|
266 }
|
|
267 BufferedImage createBufferedImage(Image img){
|
|
268 BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB );
|
|
269
|
|
270 Graphics g = bimg.getGraphics();
|
|
271 g.drawImage(img, 0, 0, null);
|
|
272 g.dispose();
|
|
273 return bimg;
|
|
274 }
|
|
275
|
|
276 void createPngBytes(BufferedImage bimg)throws IOException {
|
|
277 pngBytes = getImageBytes(bimg , "png");
|
|
278 }
|
|
279 byte[] getBytes(BufferedImage img)throws IOException {
|
|
280 byte[] b = getImageBytes(img, "png");
|
|
281 return b;
|
15
|
282 }
|
18
|
283
|
19
|
284 byte[] getImageBytes(BufferedImage image, String imageFormat) throws IOException {
|
|
285 ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
286 BufferedOutputStream os = new BufferedOutputStream(bos);
|
|
287 image.flush();
|
|
288 ImageIO.write(image, imageFormat, os);
|
|
289 os.flush();
|
|
290 os.close();
|
|
291 return bos.toByteArray();
|
|
292 }
|
|
293
|
|
294 void sendPngData(Socket sock)throws IOException{
|
|
295 byte[] dataLength = castIntByte(pngBytes.length);
|
|
296 sock.getOutputStream().write(dataLength);
|
|
297 sock.getOutputStream().write(pngBytes);
|
|
298 }
|
|
299 byte[] castIntByte(int len){
|
|
300 byte[] b = new byte[4];
|
|
301 b[0] = (byte)((len >>> 24 ) & 0xFF);
|
|
302 b[1] = (byte)((len >>> 16 ) & 0xFF);
|
|
303 b[2] = (byte)((len >>> 8 ) & 0xFF);
|
|
304 b[3] = (byte)((len >>> 0 ) & 0xFF);
|
|
305 return b;
|
|
306 }
|
18
|
307
|
19
|
308 BufferedImage createBimg()throws IOException{
|
|
309 BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes));
|
|
310 return bimg;
|
|
311 }
|
25
|
312 void readPngData()throws IOException{
|
|
313 int length = readU32();
|
|
314 pngBytes = new byte[length];
|
|
315 readFully(pngBytes);
|
|
316 }
|
15
|
317 void printFramebufferUpdate(){
|
|
318
|
|
319 System.out.println("messageType=" + messageType);
|
|
320 System.out.println("rectangles="+rectangles);
|
|
321 System.out.println("encoding=" + encoding);
|
19
|
322 switch(encoding){
|
|
323 case RfbProto.EncodingRaw:
|
|
324 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 + 16);
|
|
325 break;
|
|
326 default:
|
|
327 }
|
15
|
328 }
|
7
|
329 }
|