mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 04:50:25 +08:00
update *Transport
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package logicface;
|
||||
|
||||
import common.LoggerHelper;
|
||||
import packet.LpPacket;
|
||||
|
||||
import java.net.*;
|
||||
@@ -30,19 +31,33 @@ public class UdpTransport extends Transport implements ITransport{
|
||||
* 用于接收数据的transport的初始化函数
|
||||
* @param channel
|
||||
*/
|
||||
public void init(DatagramChannel channel) throws LogicFaceException {
|
||||
public void init(DatagramChannel channel,SocketAddress remoteUdpAddr) throws LogicFaceException {
|
||||
try {
|
||||
this.channel=channel;
|
||||
this.localAddr=channel.getLocalAddress().toString();
|
||||
this.localUri="udp://"+this.localAddr;
|
||||
this.remoteAddr=channel.getRemoteAddress().toString();
|
||||
|
||||
this.remoteAddr=remoteUdpAddr.toString();
|
||||
this.remoteUri="udp://"+this.remoteAddr;
|
||||
this.recvBuf=new byte[9000];
|
||||
this.remoteUdpAddr=remoteUdpAddr;
|
||||
} catch (IOException e) {
|
||||
throw new LogicFaceException("UdpTransport.init: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭
|
||||
*/
|
||||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
this.channel.close();
|
||||
} catch (IOException e) {
|
||||
LoggerHelper.logger.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送一个lpPacket
|
||||
* @param lpPacket
|
||||
@@ -55,6 +70,7 @@ public class UdpTransport extends Transport implements ITransport{
|
||||
if(encodeBuf.length<=0){
|
||||
return false;
|
||||
}
|
||||
LoggerHelper.logger.debug("start write to udp : "+this.remoteUdpAddr.toString());
|
||||
this.channel.socket().connect(remoteUdpAddr);
|
||||
int res=this.channel.write(ByteBuffer.wrap(encodeBuf));
|
||||
if(res<0){
|
||||
@@ -76,7 +92,7 @@ public class UdpTransport extends Transport implements ITransport{
|
||||
if(remoteUdpAddr==null){
|
||||
return null;
|
||||
}
|
||||
//todo: 日志记录addr
|
||||
LoggerHelper.logger.debug("recv from udp: "+this.remoteAddr);
|
||||
LpPacket lpPacket=this.parseByteArray2LpPacket(this.recvBuf);
|
||||
if(lpPacket==null){
|
||||
return null;
|
||||
@@ -100,7 +116,7 @@ public class UdpTransport extends Transport implements ITransport{
|
||||
return true;
|
||||
}
|
||||
this.channel.socket().setSoTimeout((int)(duration));
|
||||
return false;
|
||||
return true;
|
||||
} catch (SocketException e) {
|
||||
throw new LogicFaceException("UdpTransport.setReadTimeout"+e.getMessage());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user