forked from KNEMC/KLALB
122 lines
2.4 KiB
Java
122 lines
2.4 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.SocketException;
|
|
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=-1;
|
|
|
|
/*public Socket getConnect() {
|
|
return connect;
|
|
}*/
|
|
|
|
public DataInputStream getDin() {
|
|
return din;
|
|
}
|
|
|
|
public DataOutputStream getDout() {
|
|
return dout;
|
|
}
|
|
|
|
public void close() {
|
|
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();
|
|
}
|
|
if(tunnel!=null&&connect!=null)
|
|
tunnel.getCCount().decrementAndGet();
|
|
connect = null;
|
|
}
|
|
|
|
public long getDelay() {
|
|
return delay;
|
|
}
|
|
|
|
public void setDelay(long delay) {
|
|
this.delay = delay;
|
|
if(tunnel!=null) {
|
|
tunnel.setDelay(delay);
|
|
}
|
|
if(connect!=null) {
|
|
try {
|
|
connect.setSoTimeout((int) (delay/100000));
|
|
} catch (SocketException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void setTunnel(Tunnel tunnel) {
|
|
this.tunnel = tunnel;
|
|
}
|
|
|
|
public TCPConnection(Tunnel t) throws UnknownHostException, IOException {
|
|
this(t, t.connectClientSocket());
|
|
}
|
|
public TCPConnection(Tunnel t, Socket soc) throws IOException {
|
|
connect = soc;
|
|
tunnel = t;
|
|
if(t!=null) {
|
|
t.getCCount().incrementAndGet();
|
|
}
|
|
// connect.setSoTimeout(10000);
|
|
din = new DataInputStream(new BufferedInputStream(connect.getInputStream(), 65536));
|
|
dout = new DataOutputStream(new BufferedOutputStream(connect.getOutputStream(), 65536));
|
|
}
|
|
|
|
public boolean isOpen() {
|
|
if(connect==null) {
|
|
return false;
|
|
}
|
|
return !connect.isClosed();
|
|
}
|
|
|
|
|
|
private volatile long time=System.nanoTime();
|
|
public boolean checkPingTime() {
|
|
long cu=System.nanoTime();
|
|
if(cu-time>Consts.PINGTIMENS) {
|
|
time=cu;
|
|
return true;
|
|
}else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|