24
|
1 package myVncProxy;
|
15
|
2 import java.io.BufferedReader;
|
10
|
3 import java.io.IOException;
|
15
|
4 import java.io.InputStreamReader;
|
23
|
5 import java.net.BindException;
|
10
|
6 import java.net.ServerSocket;
|
|
7 import java.net.Socket;
|
|
8 import java.util.LinkedList;
|
|
9
|
|
10
|
|
11 class MyRfbProto extends RfbProto {
|
|
12
|
18
|
13 private int messageType;
|
|
14 private int rectangles;
|
23
|
15 private int rectX;
|
|
16 private int rectY;
|
|
17 private int rectW;
|
|
18 private int rectH;
|
18
|
19 private int encoding;
|
|
20
|
23
|
21 private ServerSocket servSock;
|
|
22 private int acceptPort;
|
10
|
23 private byte initData[];
|
|
24 private LinkedList <Socket> cliList;
|
|
25
|
13
|
26 MyRfbProto(String h, int p, VncViewer v ) throws IOException {
|
10
|
27 super(h, p, v);
|
|
28 cliList = new LinkedList <Socket>();
|
|
29 }
|
|
30
|
13
|
31 MyRfbProto(String h, int p) throws IOException {
|
|
32 super(h, p);
|
|
33 cliList = new LinkedList <Socket>();
|
|
34 }
|
24
|
35
|
10
|
36 void initServSock(int port) throws IOException{
|
|
37 servSock = new ServerSocket(port);
|
23
|
38 acceptPort = port;
|
10
|
39 }
|
23
|
40 void selectPort(){
|
|
41 int i = 5550;
|
|
42 while(true){
|
|
43 try{
|
|
44 initServSock(i);
|
|
45 break;
|
|
46 }catch(BindException e){
|
|
47 i++;
|
|
48 continue;
|
|
49 }catch(IOException e){
|
10
|
50
|
23
|
51 }
|
|
52 }
|
|
53 }
|
|
54 int getAcceptPort(){
|
|
55 return acceptPort;
|
|
56 }
|
10
|
57 void setSoTimeout(int num) throws IOException {
|
|
58 servSock.setSoTimeout(num);
|
|
59 }
|
|
60
|
|
61 Socket accept() throws IOException {
|
|
62 return servSock.accept();
|
|
63 }
|
|
64
|
|
65 void addSock(Socket sock){
|
|
66 cliList.add(sock);
|
|
67 }
|
|
68
|
|
69 void mark(int len) throws IOException {
|
|
70 is.mark(len);
|
|
71 }
|
|
72
|
|
73 void reset() throws IOException {
|
|
74 is.reset();
|
18
|
75 }
|
10
|
76
|
|
77 boolean markSupported() {
|
|
78 return is.markSupported();
|
|
79 }
|
|
80
|
|
81 void readServerInit() throws IOException {
|
|
82
|
|
83 mark(255);
|
|
84 skipBytes(20);
|
|
85 int nlen = readU32();
|
|
86 int blen = 20+4+nlen;
|
|
87 initData = new byte[blen];
|
|
88 reset();
|
|
89
|
|
90 mark(blen);
|
|
91 readFully(initData);
|
|
92 reset();
|
|
93
|
|
94 framebufferWidth = readU16();
|
|
95 framebufferHeight = readU16();
|
|
96 bitsPerPixel = readU8();
|
|
97 depth = readU8();
|
|
98 bigEndian = (readU8() != 0);
|
|
99 trueColour = (readU8() != 0);
|
|
100 redMax = readU16();
|
|
101 greenMax = readU16();
|
|
102 blueMax = readU16();
|
|
103 redShift = readU8();
|
|
104 greenShift = readU8();
|
|
105 blueShift = readU8();
|
|
106 byte[] pad = new byte[3];
|
|
107 readFully(pad);
|
|
108 int nameLength = readU32();
|
|
109 byte[] name = new byte[nameLength];
|
|
110 readFully(name);
|
|
111 desktopName = new String(name);
|
|
112
|
|
113 // Read interaction capabilities (TightVNC protocol extensions)
|
|
114 if (protocolTightVNC) {
|
|
115 int nServerMessageTypes = readU16();
|
|
116 int nClientMessageTypes = readU16();
|
|
117 int nEncodingTypes = readU16();
|
|
118 readU16();
|
|
119 readCapabilityList(serverMsgCaps, nServerMessageTypes);
|
|
120 readCapabilityList(clientMsgCaps, nClientMessageTypes);
|
|
121 readCapabilityList(encodingCaps, nEncodingTypes);
|
|
122 }
|
|
123
|
|
124 inNormalProtocol = true;
|
|
125 }
|
|
126
|
|
127 void sendInitData(Socket sock) throws IOException{
|
|
128 sock.getOutputStream().write(initData);
|
|
129 }
|
|
130
|
17
|
131 // void sendData(byte b[]) throws IOException{
|
|
132 void sendData(byte b[]){
|
|
133 try{
|
|
134 for(Socket cli : cliList){
|
|
135 try{
|
|
136 cli.getOutputStream().write(b, 0, b.length);
|
|
137 }catch(IOException e){
|
|
138 // if socket closed
|
|
139 // cliList.remove(cli);
|
|
140 cliList.remove(cli);
|
|
141 }
|
|
142 }
|
20
|
143 // System.out.println("cliSize="+cliSize());
|
17
|
144 }catch(Exception e){
|
20
|
145 // System.out.println("cliSize 0");
|
17
|
146 }
|
10
|
147 }
|
15
|
148 boolean ready() throws IOException {
|
|
149 BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
|
150 return br.ready();
|
|
151 }
|
10
|
152
|
|
153 int cliSize(){
|
|
154 return cliList.size();
|
|
155 }
|
15
|
156 void printNumBytesRead(){
|
|
157 System.out.println("numBytesRead="+numBytesRead);
|
|
158 }
|
|
159 void bufResetSend(int size) throws IOException {
|
|
160 reset();
|
|
161 int len = size;
|
|
162 if(available() < size )
|
|
163 len = available();
|
|
164 byte buffer[] = new byte[len];
|
|
165 readFully(buffer);
|
|
166 sendData(buffer);
|
|
167 }
|
18
|
168 void regiFramebufferUpdate()throws IOException{
|
|
169 mark(16);
|
|
170 messageType = readU8();
|
|
171 skipBytes(1);
|
|
172 rectangles = readU16();
|
23
|
173 rectX = readU16();
|
|
174 rectY = readU16();
|
|
175 rectW = readU16();
|
|
176 rectH = readU16();
|
18
|
177 encoding = readU32();
|
23
|
178 reset();
|
15
|
179 }
|
23
|
180 void checkAndMark() throws IOException{
|
|
181 switch(encoding){
|
|
182 case RfbProto.EncodingRaw:
|
|
183 mark(rectW * rectH * 4 + 16);
|
|
184 break;
|
|
185 default:
|
|
186 mark(1000000);
|
|
187 }
|
|
188 }
|
|
189
|
18
|
190 void printFramebufferUpdate(){
|
|
191
|
|
192 System.out.println("messageType=" + messageType);
|
|
193 System.out.println("rectangles="+rectangles);
|
|
194 System.out.println("encoding=" + encoding);
|
23
|
195 switch(encoding){
|
|
196 case RfbProto.EncodingRaw:
|
|
197 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 + 16);
|
|
198 break;
|
|
199 default:
|
|
200
|
|
201
|
|
202 }
|
18
|
203 }
|
15
|
204
|
10
|
205
|
|
206 }
|