TCP连接池功能
This commit is contained in:
@@ -8,28 +8,16 @@ import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
public class TCPConnection {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TCPConnection [tunnel=" + tunnel + ", connect=" + connect + ", delay=" + delay + "]";
|
||||
}
|
||||
|
||||
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;
|
||||
}*/
|
||||
protected DataInputStream din;
|
||||
protected DataOutputStream dout;
|
||||
|
||||
public DataInputStream getDin() {
|
||||
return din;
|
||||
@@ -61,44 +49,19 @@ public class TCPConnection {
|
||||
// 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(10000);
|
||||
} 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 {
|
||||
public TCPConnection( Socket soc) throws IOException {
|
||||
connect = soc;
|
||||
tunnel = t;
|
||||
if(t!=null) {
|
||||
t.getCCount().incrementAndGet();
|
||||
}
|
||||
initIO();
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected void initIO() throws IOException {
|
||||
dout = new DataOutputStream( connect.getOutputStream());
|
||||
din = new DataInputStream(connect.getInputStream());
|
||||
dout = new DataOutputStream(connect.getOutputStream());
|
||||
}
|
||||
|
||||
public boolean isOpen() {
|
||||
@@ -107,24 +70,6 @@ public class TCPConnection {
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
private volatile long RTT=10000000;
|
||||
public void nextRTT(long NRTT) {
|
||||
RTT=(long) ((1.0-Consts.A)*RTT+Consts.A*NRTT);
|
||||
System.out.println(RTT);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user