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