update
parent
195d636b20
commit
60588b54d1
|
@ -16,6 +16,8 @@ import io.netty.handler.timeout.IdleStateEvent;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author yezhihao
|
||||
* home https://gitee.com/yezhihao/jt808-server
|
||||
|
@ -97,7 +99,10 @@ public class TCPServerHandler extends ChannelInboundHandlerAdapter {
|
|||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) {
|
||||
Session session = ctx.channel().attr(Session.KEY).get();
|
||||
log.warn(">>>>>消息处理异常" + session, e);
|
||||
if (e instanceof IOException)
|
||||
log.warn(">>>>>终端主动断开连接{},{}", e.getMessage(), session);
|
||||
else
|
||||
log.warn(">>>>>消息处理异常" + session, e);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package io.github.yezhihao.netmc.codec;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.github.yezhihao.netmc.session.Session;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
/**
|
||||
* 基础消息解码
|
||||
|
@ -10,8 +10,6 @@ import io.github.yezhihao.netmc.session.Session;
|
|||
*/
|
||||
public interface MessageDecoder<T> {
|
||||
|
||||
T decode(ByteBuf buf);
|
||||
|
||||
T decode(ByteBuf buf, Session session);
|
||||
|
||||
}
|
|
@ -10,8 +10,6 @@ import io.netty.buffer.ByteBuf;
|
|||
*/
|
||||
public interface MessageEncoder<T> {
|
||||
|
||||
ByteBuf encode(T message);
|
||||
|
||||
ByteBuf encode(T message, Session session);
|
||||
|
||||
}
|
|
@ -4,6 +4,10 @@ import java.util.AbstractList;
|
|||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* @author yezhihao
|
||||
* home https://gitee.com/yezhihao/jt808-server
|
||||
*/
|
||||
public final class AdapterList<S, T> extends AbstractList<T> {
|
||||
|
||||
private final List<S> src;
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package io.github.yezhihao.netmc.util;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* @author yezhihao
|
||||
* home https://gitee.com/yezhihao/jt808-server
|
||||
*/
|
||||
public final class AdapterMap<K, S, T> extends AbstractMap<K, T> {
|
||||
|
||||
private final Map<K, S> src;
|
||||
private final Set<Entry<K, T>> entries;
|
||||
|
||||
public AdapterMap(Map<K, S> src, Function<S, T> function) {
|
||||
this.src = src;
|
||||
this.entries = new AdapterSet(src.entrySet(), (Function<Entry<K, S>, Entry<K, T>>) e -> new SimpleEntry(e.getKey(), function.apply(e.getValue())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Entry<K, T>> entrySet() {
|
||||
return entries;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return src.size();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package io.github.yezhihao.netmc.util;
|
||||
|
||||
import java.util.AbstractSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* @author yezhihao
|
||||
* home https://gitee.com/yezhihao/jt808-server
|
||||
*/
|
||||
public final class AdapterSet<S, T> extends AbstractSet<T> {
|
||||
|
||||
private final Set<S> src;
|
||||
private final Iterator<T> iterator;
|
||||
|
||||
public AdapterSet(Set<S> src, Function<S, T> function) {
|
||||
this.src = src;
|
||||
this.iterator = new Iterator<T>() {
|
||||
|
||||
private final Function<S, T> f = function;
|
||||
private final Iterator<S> it = src.iterator();
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return it.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
return f.apply(it.next());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
it.remove();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return iterator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return src.size();
|
||||
}
|
||||
}
|
|
@ -9,11 +9,6 @@ import java.nio.charset.StandardCharsets;
|
|||
|
||||
public class MyMessageDecoder implements MessageDecoder {
|
||||
|
||||
@Override
|
||||
public Object decode(ByteBuf buf) {
|
||||
return decode(buf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object decode(ByteBuf buf, Session session) {
|
||||
String msgStr = buf.readCharSequence(buf.readableBytes(), StandardCharsets.UTF_8).toString();
|
||||
|
|
|
@ -2,6 +2,7 @@ package io.github.yezhihao.netmc.codec;
|
|||
|
||||
import io.github.yezhihao.netmc.model.MyHeader;
|
||||
import io.github.yezhihao.netmc.model.MyMessage;
|
||||
import io.github.yezhihao.netmc.session.Session;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
|
||||
|
@ -10,7 +11,7 @@ import java.nio.charset.StandardCharsets;
|
|||
public class MyMessageEncoder implements MessageEncoder<MyMessage> {
|
||||
|
||||
@Override
|
||||
public ByteBuf encode(MyMessage message) {
|
||||
public ByteBuf encode(MyMessage message, Session session) {
|
||||
MyHeader header = message.getHeader();
|
||||
|
||||
StringBuilder msg = new StringBuilder();
|
||||
|
|
Loading…
Reference in New Issue