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