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