TCP连接池功能
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
package org.kne.cloud.network.klalb;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.BlockingDeque;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
|
||||
import org.kne.cloud.network.mport.IPPort;
|
||||
|
||||
public class RemoteTCPConnection extends TCPConnection {
|
||||
|
||||
public RemoteTCPConnection(Tunnel t) throws UnknownHostException, IOException {
|
||||
this(t,t.connectClientSocket());
|
||||
}
|
||||
public RemoteTCPConnection(Tunnel t,Socket s) throws UnknownHostException, IOException {
|
||||
super(s);
|
||||
this.tunnel=t;
|
||||
s.setSoTimeout(Consts.SO_TIMEOUT);
|
||||
}
|
||||
private long delay=-1;
|
||||
private Tunnel tunnel;
|
||||
public Tunnel getTunnel() {
|
||||
return tunnel;
|
||||
}
|
||||
|
||||
public void setTunnel(Tunnel tunnel) {
|
||||
this.tunnel = tunnel;
|
||||
}
|
||||
public long getDelay() {
|
||||
return delay;
|
||||
}
|
||||
|
||||
public void setDelay(long delay) {
|
||||
this.delay = delay;
|
||||
if(tunnel!=null) {
|
||||
tunnel.setDelay(delay);
|
||||
}
|
||||
|
||||
}
|
||||
public void sendBlock(KLALBBlock data) throws IOException {
|
||||
dout.writeLong(data.sn);
|
||||
dout.writeLong(data.cuid.getMostSignificantBits());
|
||||
dout.writeLong(data.cuid.getLeastSignificantBits());
|
||||
dout.writeLong(data.number);
|
||||
if(data.number>0) {
|
||||
dout.writeInt(data.data.length);
|
||||
dout.write(data.data);
|
||||
}else if(data.number==0) {
|
||||
dout.write(data.command);
|
||||
switch(data.command) {
|
||||
case 0:
|
||||
case 1:
|
||||
dout.writeLong(data.pingtime);
|
||||
break;
|
||||
case 2:
|
||||
dout.writeUTF(data.lservice);
|
||||
dout.writeUTF(data.lipport.toString());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
dout.flush();
|
||||
|
||||
System.out.println("SEND:"+data);
|
||||
}
|
||||
public KLALBBlock receiveBlock() throws IOException {
|
||||
KLALBBlock klb=new KLALBBlock();
|
||||
klb.sn=din.readLong();
|
||||
klb.cuid=new UUID(din.readLong(), din.readLong());
|
||||
klb.number=din.readLong();
|
||||
if(klb.number>0) {
|
||||
int size=din.readInt();
|
||||
byte[]d=new byte[size];
|
||||
din.readFully(d);
|
||||
klb.data=d;
|
||||
}else if(klb.number==0) {
|
||||
klb.command=din.read();
|
||||
switch(klb.command) {
|
||||
case 0:
|
||||
case 1:
|
||||
klb.pingtime=din.readLong();
|
||||
break;
|
||||
case 2:
|
||||
klb.lservice=din.readUTF();
|
||||
klb.lipport=new IPPort(din.readUTF());
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("RECEIVE:"+klb);
|
||||
return klb;
|
||||
}
|
||||
|
||||
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 BlockingDeque<KLALBBlock>sendDeque=new LinkedBlockingDeque<>();
|
||||
public BlockingDeque<KLALBBlock> getSendDeque() {
|
||||
return sendDeque;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user