31
|
1 package jp.ac.u_ryukyu.treevnc;
|
11
|
2 import java.net.Socket;
|
|
3 import java.io.IOException;
|
|
4 import java.io.InputStream;
|
|
5 import java.io.OutputStream;
|
|
6
|
17
|
7 import com.glavsoft.transport.Reader;
|
|
8 import com.glavsoft.transport.Writer;
|
|
9
|
11
|
10 public class AcceptThread implements Runnable {
|
38
|
11 public MyRfbProto rfb = null;
|
11
|
12 byte[] imageBytes;
|
|
13 int port;
|
38
|
14 public boolean flag = false;
|
11
|
15
|
31
|
16 public AcceptThread(MyRfbProto _rfb) {
|
11
|
17 rfb = _rfb;
|
|
18 }
|
|
19
|
|
20
|
31
|
21 public AcceptThread(MyRfbProto _rfb, int p) {
|
11
|
22 rfb = _rfb;
|
|
23 port = p;
|
|
24 }
|
|
25
|
31
|
26 public void changeRfb(MyRfbProto _rfb) {
|
11
|
27 rfb = _rfb;
|
|
28 }
|
|
29
|
|
30 public void run() {
|
|
31 rfb.selectPort(port);
|
|
32
|
|
33 while (true) {
|
|
34 try {
|
|
35 Socket newCli = rfb.accept();
|
38
|
36 if(flag) throw new IOException();
|
11
|
37 OutputStream os = newCli.getOutputStream();
|
|
38 InputStream is = newCli.getInputStream();
|
38
|
39 if(confirmHalt(is)) break;
|
17
|
40 rfb.newClient(this, newCli, new Writer(os), new Reader(is));
|
11
|
41 } catch (IOException e) {
|
38
|
42 break;
|
11
|
43 }
|
|
44 }
|
|
45 }
|
38
|
46
|
|
47
|
|
48 /**
|
|
49 * Instruction stop if you come from if WaitReply.
|
|
50 * @return if return true. This Thread halt.
|
|
51 */
|
|
52 private boolean confirmHalt(InputStream is) throws IOException {
|
|
53 byte[] b = new byte[4];
|
|
54 is.mark(4);
|
|
55 is.read(b);
|
|
56 if(String.valueOf(b).equals("halt")) return true;
|
|
57 is.reset();
|
|
58 return false;
|
|
59 }
|
11
|
60 }
|