|
1 | 1 | package info.xiaomo.gengine.network.handler; |
2 | 2 |
|
| 3 | +import com.google.protobuf.AbstractMessage; |
| 4 | +import java.lang.reflect.Method; |
3 | 5 | import info.xiaomo.gengine.network.INetworkConsumer; |
4 | 6 | import info.xiaomo.gengine.network.INetworkEventListener; |
5 | 7 | import info.xiaomo.gengine.network.Message; |
| 8 | +import info.xiaomo.gengine.network.pool.MessageAndHandlerPool; |
| 9 | +import info.xiaomo.gengine.utils.ClassUtil; |
6 | 10 | import io.netty.channel.ChannelHandlerContext; |
7 | 11 | import io.netty.channel.ChannelInboundHandlerAdapter; |
8 | 12 |
|
|
11 | 15 | */ |
12 | 16 | public class MessageExecutor extends ChannelInboundHandlerAdapter { |
13 | 17 |
|
14 | | - private final INetworkConsumer consumer; |
| 18 | + private final INetworkConsumer consumer; |
15 | 19 |
|
16 | | - protected final INetworkEventListener listener; |
| 20 | + protected final INetworkEventListener listener; |
17 | 21 |
|
18 | 22 |
|
19 | | - public MessageExecutor(INetworkConsumer consumer, INetworkEventListener listener) { |
20 | | - this.consumer = consumer; |
21 | | - this.listener = listener; |
22 | | - } |
| 23 | + public MessageExecutor(INetworkConsumer consumer, INetworkEventListener listener) { |
| 24 | + this.consumer = consumer; |
| 25 | + this.listener = listener; |
| 26 | + } |
23 | 27 |
|
24 | 28 |
|
25 | | - @Override |
26 | | - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
27 | | - listener.onExceptionOccur(ctx, cause); |
28 | | - } |
| 29 | + @Override |
| 30 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| 31 | + listener.onExceptionOccur(ctx, cause); |
| 32 | + } |
29 | 33 |
|
30 | | - @Override |
31 | | - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { |
32 | | - consumer.consume((Message) msg, ctx.channel()); |
33 | | - } |
| 34 | + @Override |
| 35 | + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { |
| 36 | + if (msg instanceof Message) { |
| 37 | + Message packet = (Message) msg; |
| 38 | + System.out.println("\n<<<<<<<<<<<<收到服务端协议:" + packet.getMsgId() + "<<<<<<<<<<<<"); |
34 | 39 |
|
35 | | - @Override |
36 | | - public void channelActive(ChannelHandlerContext ctx) { |
37 | | - if (this.listener != null) { |
38 | | - this.listener.onConnected(ctx); |
39 | | - } |
40 | | - } |
| 40 | + AbstractMessage clazz = MessageAndHandlerPool.messages.get(packet.getMsgId()); |
41 | 41 |
|
42 | | - @Override |
43 | | - public void channelInactive(ChannelHandlerContext ctx) { |
44 | | - if (this.listener != null) { |
45 | | - this.listener.onDisconnected(ctx); |
46 | | - } |
47 | | - } |
| 42 | + Method m = ClassUtil.findMethod(clazz.getClass(), "getDefaultInstance"); |
| 43 | + if (m != null) { |
| 44 | + AbstractMessage message = (AbstractMessage) m.invoke(null); |
| 45 | + msg = message.newBuilderForType().mergeFrom(packet.getBytes()).build(); |
| 46 | + consumer.consume((Message) msg, ctx.channel()); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void channelActive(ChannelHandlerContext ctx) { |
| 53 | + if (this.listener != null) { |
| 54 | + this.listener.onConnected(ctx); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void channelInactive(ChannelHandlerContext ctx) { |
| 60 | + if (this.listener != null) { |
| 61 | + this.listener.onDisconnected(ctx); |
| 62 | + } |
| 63 | + } |
48 | 64 | } |
0 commit comments