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