[编织消息框架][设计协议]opCode

OpCode(Operation Code) 操作码的意思。

OpCode 有几种域组成,不同领域格式组成不同

1.指令号

2.数据范围

3.数据内容

如 {code}{addr range}{data}

{1}{2-5}{2至5地址内容为 "你好"}

{2}{6-10}{6至10地址内容为 "hello"}

这种编码可以应用到其它领域,本项目用的格式是

{data model}{data}

源码解读

QOpCode 类维护所有opCode

 1 /**
 2  * @author solq
 3  **/
 4 @Target(TYPE)
 5 @Retention(RUNTIME)
 6 public @interface QOpCode {
 7     short value();
 8     
 9     public static final short QPRODUCE = 1;
10     public static final short QCONSUME = 2;
11     public static final short QSUBSCIBE = 3;
12     public static final short QCODE = 4;
13     public static final short QRPC = 5;
14 
15

 QDispenseHandler.class

 1 private void doReceive0(QPacket packet, ChannelHandlerContext ctx) {
 2 
 3     boolean response = packet.hasStatus(QPacket.MASK_RESPONSE);
 4             // 业务处理
 5     final short c = packet.getC();
 6     switch (c) {
 7         case QOpCode.QPRODUCE:
 8         if (produceHandler != null) {
 9             QProduce produce = packet.toProduce();
10             produceHandler.onReceive(ctx, packet, produce);
11         }
12         break;
13         case QOpCode.QCONSUME:
14         if (consumeHandler != null) {
15             QConsume qConsume = packet.toConsume();
16             consumeHandler.onReceive(ctx, packet, qConsume);
17         }
18         break;
19         case QOpCode.QSUBSCIBE:
20         if (subscribeHandler != null) {
21             Collection<QSubscribe> qSubscribe = packet.toSubscribe();
22             subscribeHandler.onReceive(ctx, packet, qSubscribe);
23         }
24         break;
25         case QOpCode.QRPC:
26         response = false;
27         // 业务回写请求
28         if (rpcHandler != null) {
29             QRpc qRpc = packet.toRpc();
30             rpcHandler.onReceive(ctx, packet, qRpc);
31         }
32         break;
33         case QOpCode.QCODE:
34         response = false;
35         // 响应请求
36         if (responseHandler != null) {
37             short code = packet.toCode();
38             responseHandler.onReceive(ctx, packet, code);
39         }
40         break;
41         default:
42         throw new QSocketException(QCode.SOCKET_UNKNOWN_OPCODE, " opCode : " + c);
43     }
44      
45 }

根据不同的model code 进行解释,然后调用应用层处理

其中应用层处理统一实现 IQReceiveHandler 接口规范处理参数同处理行为

 1     //应用层逻辑
 2     protected IQReceiveHandler<QProduce> produceHandler;
 3     protected IQReceiveHandler<QConsume> consumeHandler;
 4     protected IQReceiveHandler<Collection<QSubscribe>> subscribeHandler;
 5     protected IQReceiveHandler<Short> responseHandler;
 6     protected IQReceiveHandler<QRpc> rpcHandler;
 7 
 8     //通信层逻辑
 9     protected IQReceiveHandler<Void> acceptHandler;
10     protected IQReceiveHandler<Void> closeHandler;
11 
12     protected IQReceiveHandler<Void> beforeHandler;
13     protected IQReceiveHandler<Void> afterHandler;
 1 /**
 2  * 
 3  * 响应端 业务逻辑
 4  * 
 5  * @author solq
 6  **/
 7 public interface IQReceiveHandler<T> {
 8 
 9     void onReceive(ChannelHandlerContext ctx,QPacket packet, T body);
10 
11 }

 

posted @ 2017-03-16 11:15  solq321  阅读(393)  评论(0编辑  收藏  举报