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