Mercurial > hg > Members > nobuyasu > tightVNCProxy
comparison src/myVncProxy/MyRfbProto.java @ 24:87b29d6039a6
add package myVncProxy
author | e085711 |
---|---|
date | Sun, 24 Apr 2011 23:03:00 +0900 |
parents | src/MyRfbProto.java@b51bb7bc0766 |
children | cded9fd297ab |
comparison
equal
deleted
inserted
replaced
23:b51bb7bc0766 | 24:87b29d6039a6 |
---|---|
1 package myVncProxy; | |
2 import java.io.BufferedReader; | |
3 import java.io.IOException; | |
4 import java.io.InputStreamReader; | |
5 import java.net.BindException; | |
6 import java.net.ServerSocket; | |
7 import java.net.Socket; | |
8 import java.util.LinkedList; | |
9 | |
10 | |
11 class MyRfbProto extends RfbProto { | |
12 | |
13 private int messageType; | |
14 private int rectangles; | |
15 private int rectX; | |
16 private int rectY; | |
17 private int rectW; | |
18 private int rectH; | |
19 private int encoding; | |
20 | |
21 private ServerSocket servSock; | |
22 private int acceptPort; | |
23 private byte initData[]; | |
24 private LinkedList <Socket> cliList; | |
25 | |
26 MyRfbProto(String h, int p, VncViewer v ) throws IOException { | |
27 super(h, p, v); | |
28 cliList = new LinkedList <Socket>(); | |
29 } | |
30 | |
31 MyRfbProto(String h, int p) throws IOException { | |
32 super(h, p); | |
33 cliList = new LinkedList <Socket>(); | |
34 } | |
35 | |
36 void initServSock(int port) throws IOException{ | |
37 servSock = new ServerSocket(port); | |
38 acceptPort = port; | |
39 } | |
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){ | |
50 | |
51 } | |
52 } | |
53 } | |
54 int getAcceptPort(){ | |
55 return acceptPort; | |
56 } | |
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(); | |
75 } | |
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 | |
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 } | |
143 // System.out.println("cliSize="+cliSize()); | |
144 }catch(Exception e){ | |
145 // System.out.println("cliSize 0"); | |
146 } | |
147 } | |
148 boolean ready() throws IOException { | |
149 BufferedReader br = new BufferedReader(new InputStreamReader(is)); | |
150 return br.ready(); | |
151 } | |
152 | |
153 int cliSize(){ | |
154 return cliList.size(); | |
155 } | |
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 } | |
168 void regiFramebufferUpdate()throws IOException{ | |
169 mark(16); | |
170 messageType = readU8(); | |
171 skipBytes(1); | |
172 rectangles = readU16(); | |
173 rectX = readU16(); | |
174 rectY = readU16(); | |
175 rectW = readU16(); | |
176 rectH = readU16(); | |
177 encoding = readU32(); | |
178 reset(); | |
179 } | |
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 | |
190 void printFramebufferUpdate(){ | |
191 | |
192 System.out.println("messageType=" + messageType); | |
193 System.out.println("rectangles="+rectangles); | |
194 System.out.println("encoding=" + encoding); | |
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 } | |
203 } | |
204 | |
205 | |
206 } |