mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 04:50:25 +08:00
writing UdpTransport&StreamTransport ...
This commit is contained in:
@@ -4,6 +4,11 @@ import packet.LpPacket;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.Channel;
|
||||
import java.nio.channels.DatagramChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.sql.Time;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
@@ -13,19 +18,91 @@ import java.io.*;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class UdpTransport extends Transport implements ITransport{
|
||||
// public DatagramSocket datagramSocket;
|
||||
// UDP句柄 (channel比socket性能更好)
|
||||
public DatagramChannel channel;
|
||||
// 接收缓冲区,大小为 9000
|
||||
public byte[] recvBuf;
|
||||
// 对端UDP地址,用于发送UDP包
|
||||
public SocketAddress remoteUdpAddr;
|
||||
|
||||
/**
|
||||
* 用于接收数据的transport的初始化函数
|
||||
* @param channel
|
||||
*/
|
||||
public void init(DatagramChannel channel) throws LogicFaceException {
|
||||
try {
|
||||
this.channel=channel;
|
||||
this.localAddr=channel.getLocalAddress().toString();
|
||||
this.localUri="udp://"+this.localAddr;
|
||||
this.remoteAddr=channel.getRemoteAddress().toString();
|
||||
this.remoteUri="udp://"+this.remoteAddr;
|
||||
this.recvBuf=new byte[9000];
|
||||
} catch (IOException e) {
|
||||
throw new LogicFaceException("UdpTransport.init: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送一个lpPacket
|
||||
* @param lpPacket
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean send(LpPacket lpPacket) {
|
||||
return false;
|
||||
public boolean send(LpPacket lpPacket) throws LogicFaceException {
|
||||
try {
|
||||
byte[] encodeBuf=this.encodeLpPacket2ByteArray(lpPacket);
|
||||
if(encodeBuf.length<=0){
|
||||
return false;
|
||||
}
|
||||
this.channel.socket().connect(remoteUdpAddr);
|
||||
int res=this.channel.write(ByteBuffer.wrap(encodeBuf));
|
||||
if(res<0){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (LogicFaceException | IOException e) {
|
||||
throw new LogicFaceException("UdpTransport.send: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从UDP句柄中接收到UDP包,并返回给上层
|
||||
* @return
|
||||
*/
|
||||
private LpPacket doReceive() throws LogicFaceException {
|
||||
try {
|
||||
SocketAddress remoteUdpAddr=this.channel.receive(ByteBuffer.wrap(this.recvBuf));
|
||||
if(remoteUdpAddr==null){
|
||||
return null;
|
||||
}
|
||||
//todo: 日志记录addr
|
||||
LpPacket lpPacket=this.parseByteArray2LpPacket(this.recvBuf);
|
||||
if(lpPacket==null){
|
||||
return null;
|
||||
}
|
||||
return lpPacket;
|
||||
} catch (IOException e) {
|
||||
throw new LogicFaceException("UdpTransport.doReceive: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LpPacket receive() {
|
||||
return null;
|
||||
public LpPacket receive() throws LogicFaceException {
|
||||
return this.doReceive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setReadTimeout(long duration) {
|
||||
return false;
|
||||
public boolean setReadTimeout(long duration) throws LogicFaceException {
|
||||
try {
|
||||
if (duration <= 0) {
|
||||
this.channel.socket().setSoTimeout(0);
|
||||
return true;
|
||||
}
|
||||
this.channel.socket().setSoTimeout((int)(duration));
|
||||
return false;
|
||||
} catch (SocketException e) {
|
||||
throw new LogicFaceException("UdpTransport.setReadTimeout"+e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user