This commit is contained in:
Administrator
2024-08-18 17:47:11 +08:00
parent e4f4c77c19
commit e243203535
121 changed files with 7499 additions and 2169 deletions
@@ -0,0 +1,94 @@
package org.kne.cloud.network.klalb;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.net.InetSocketAddress;
import java.net.ProtocolFamily;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketOption;
import java.net.SocketOptions;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.Channels;
import java.nio.channels.SocketChannel;
import java.util.Timer;
import java.util.TimerTask;
import org.kne.cloud.network.MultipurposeSocketAddress;
import org.kne.cloud.network.TimeoutTimer;
public class StreamChannelKLALBPacketLink implements KLALBPacketLink {
private static TimeoutTimer tmoTimer=new TimeoutTimer();
@Override
public String toString() {
try {
return new MultipurposeSocketAddress("TCP",(InetSocketAddress)connectSocket.getLocalAddress())+""+new MultipurposeSocketAddress("TCP",(InetSocketAddress)connectSocket.getRemoteAddress());
} catch (IOException e) {
return "?←?";
}
}
private SocketChannel connectSocket;
private int sotimeout;
public StreamChannelKLALBPacketLink(SocketChannel connectSocket) throws IOException {
this.connectSocket=connectSocket;
new KLALBOutputStream(Channels.newOutputStream(connectSocket));
new KLALBInputStream(Channels.newInputStream(connectSocket));
connectSocket.setOption(StandardSocketOptions.TCP_NODELAY,true);
}
@Override
public void writePacket(KLALBPacket kp) throws IOException {
KLALBPacket.writeKLALBPacketToChannel(connectSocket, kp);
}
@Override
public KLALBPacket readPacket() throws IOException {
TimerTask tsk= tmoTimer.createTimeOutTask(connectSocket, sotimeout);
KLALBPacket res;
try {
res=KLALBPacket.readKLALBPacketFromChannel(connectSocket);
}finally {
if(tsk!=null)
tsk.cancel();
}
return res;
}
@Override
public void close() throws IOException {
connectSocket.close();
}
@Override
public boolean isClosed() {
return !connectSocket.isOpen();
}
@Override
public void setSoTimeout(int val) throws SocketException {
sotimeout=val;
}
@Override
public int getSoTimeout() throws SocketException {
return sotimeout;
}
@Override
public void flush() throws IOException {
}
@Override
public boolean isStream() {
return true;
}
}