106 lines
2.2 KiB
Java
106 lines
2.2 KiB
Java
package org.kne.cloud.network.klalb;
|
|
|
|
import java.io.BufferedInputStream;
|
|
import java.io.BufferedOutputStream;
|
|
import java.io.DataInputStream;
|
|
import java.io.DataOutputStream;
|
|
import java.io.IOException;
|
|
import java.net.Socket;
|
|
import java.net.UnknownHostException;
|
|
|
|
public class TCPConnection {
|
|
|
|
private Tunnel tunnel;
|
|
private Socket connect;
|
|
private DataInputStream din;
|
|
|
|
public Tunnel getTunnel() {
|
|
return tunnel;
|
|
}
|
|
|
|
private DataOutputStream dout;
|
|
private long delay;
|
|
|
|
public Socket getConnect() {
|
|
return connect;
|
|
}
|
|
|
|
public DataInputStream getDin() {
|
|
return din;
|
|
}
|
|
|
|
public DataOutputStream getDout() {
|
|
return dout;
|
|
}
|
|
|
|
public void close() {
|
|
// TODO 自动生成的方法存根
|
|
try {
|
|
if (din != null)
|
|
din.close();
|
|
} catch (IOException e1) {
|
|
// TODO 自动生成的 catch 块
|
|
e1.printStackTrace();
|
|
}
|
|
try {
|
|
if (dout != null)
|
|
dout.close();
|
|
} catch (IOException e1) {
|
|
// TODO 自动生成的 catch 块
|
|
e1.printStackTrace();
|
|
}
|
|
try {
|
|
if (connect != null)
|
|
connect.close();
|
|
} catch (IOException e) {
|
|
// TODO 自动生成的 catch 块
|
|
e.printStackTrace();
|
|
}
|
|
connect = null;
|
|
}
|
|
|
|
public long getDelay() {
|
|
return delay;
|
|
}
|
|
|
|
public void setDelay(long delay) {
|
|
this.delay = delay;
|
|
}
|
|
|
|
public TCPConnection(Tunnel t) throws UnknownHostException, IOException {
|
|
this(t, t.connectClientSocket());
|
|
}
|
|
|
|
public TCPConnection(Tunnel t, Socket soc) throws IOException {
|
|
connect = soc;
|
|
tunnel = t;
|
|
// connect.setSoTimeout(10000);
|
|
din = new DataInputStream(new BufferedInputStream(connect.getInputStream(), 65536));
|
|
dout = new DataOutputStream(new BufferedOutputStream(connect.getOutputStream(), 65536));
|
|
ThreadTool.makeVThreadIfSupport("FLUSH", () -> {
|
|
try {
|
|
while (true) {
|
|
Thread.sleep(10);
|
|
if(flush) {
|
|
synchronized (dout) {
|
|
dout.flush();
|
|
|
|
}
|
|
flush=false;
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
private volatile boolean flush = false;
|
|
|
|
public void flush() throws IOException {
|
|
flush = true;
|
|
}
|
|
}
|