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