master
剑器近 2021-07-01 16:31:57 +08:00
parent 195d636b20
commit 60588b54d1
8 changed files with 94 additions and 12 deletions

View File

@ -16,6 +16,8 @@ import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException;
/** /**
* @author yezhihao * @author yezhihao
* home https://gitee.com/yezhihao/jt808-server * home https://gitee.com/yezhihao/jt808-server
@ -97,7 +99,10 @@ public class TCPServerHandler extends ChannelInboundHandlerAdapter {
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) { public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) {
Session session = ctx.channel().attr(Session.KEY).get(); 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 @Override

View File

@ -1,7 +1,7 @@
package io.github.yezhihao.netmc.codec; package io.github.yezhihao.netmc.codec;
import io.netty.buffer.ByteBuf;
import io.github.yezhihao.netmc.session.Session; 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> { public interface MessageDecoder<T> {
T decode(ByteBuf buf);
T decode(ByteBuf buf, Session session); T decode(ByteBuf buf, Session session);
} }

View File

@ -10,8 +10,6 @@ import io.netty.buffer.ByteBuf;
*/ */
public interface MessageEncoder<T> { public interface MessageEncoder<T> {
ByteBuf encode(T message);
ByteBuf encode(T message, Session session); ByteBuf encode(T message, Session session);
} }

View File

@ -4,6 +4,10 @@ import java.util.AbstractList;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Function;
/**
* @author yezhihao
* home https://gitee.com/yezhihao/jt808-server
*/
public final class AdapterList<S, T> extends AbstractList<T> { public final class AdapterList<S, T> extends AbstractList<T> {
private final List<S> src; private final List<S> src;

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -9,11 +9,6 @@ import java.nio.charset.StandardCharsets;
public class MyMessageDecoder implements MessageDecoder { public class MyMessageDecoder implements MessageDecoder {
@Override
public Object decode(ByteBuf buf) {
return decode(buf);
}
@Override @Override
public Object decode(ByteBuf buf, Session session) { public Object decode(ByteBuf buf, Session session) {
String msgStr = buf.readCharSequence(buf.readableBytes(), StandardCharsets.UTF_8).toString(); String msgStr = buf.readCharSequence(buf.readableBytes(), StandardCharsets.UTF_8).toString();

View File

@ -2,6 +2,7 @@ package io.github.yezhihao.netmc.codec;
import io.github.yezhihao.netmc.model.MyHeader; import io.github.yezhihao.netmc.model.MyHeader;
import io.github.yezhihao.netmc.model.MyMessage; import io.github.yezhihao.netmc.model.MyMessage;
import io.github.yezhihao.netmc.session.Session;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
@ -10,7 +11,7 @@ import java.nio.charset.StandardCharsets;
public class MyMessageEncoder implements MessageEncoder<MyMessage> { public class MyMessageEncoder implements MessageEncoder<MyMessage> {
@Override @Override
public ByteBuf encode(MyMessage message) { public ByteBuf encode(MyMessage message, Session session) {
MyHeader header = message.getHeader(); MyHeader header = message.getHeader();
StringBuilder msg = new StringBuilder(); StringBuilder msg = new StringBuilder();