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
/**
* @author solq
**/
@Target(TYPE)
@Retention(RUNTIME)
public @interface QOpCode {
short value(); public static final short QPRODUCE = 1;
public static final short QCONSUME = 2;
public static final short QSUBSCIBE = 3;
public static final short QCODE = 4;
public static final short QRPC = 5; }
QDispenseHandler.class
private void doReceive0(QPacket packet, ChannelHandlerContext ctx) { boolean response = packet.hasStatus(QPacket.MASK_RESPONSE);
// 业务处理
final short c = packet.getC();
switch (c) {
case QOpCode.QPRODUCE:
if (produceHandler != null) {
QProduce produce = packet.toProduce();
produceHandler.onReceive(ctx, packet, produce);
}
break;
case QOpCode.QCONSUME:
if (consumeHandler != null) {
QConsume qConsume = packet.toConsume();
consumeHandler.onReceive(ctx, packet, qConsume);
}
break;
case QOpCode.QSUBSCIBE:
if (subscribeHandler != null) {
Collection<QSubscribe> qSubscribe = packet.toSubscribe();
subscribeHandler.onReceive(ctx, packet, qSubscribe);
}
break;
case QOpCode.QRPC:
response = false;
// 业务回写请求
if (rpcHandler != null) {
QRpc qRpc = packet.toRpc();
rpcHandler.onReceive(ctx, packet, qRpc);
}
break;
case QOpCode.QCODE:
response = false;
// 响应请求
if (responseHandler != null) {
short code = packet.toCode();
responseHandler.onReceive(ctx, packet, code);
}
break;
default:
throw new QSocketException(QCode.SOCKET_UNKNOWN_OPCODE, " opCode : " + c);
} }
根据不同的model code 进行解释,然后调用应用层处理
其中应用层处理统一实现 IQReceiveHandler 接口规范处理参数同处理行为
//应用层逻辑
protected IQReceiveHandler<QProduce> produceHandler;
protected IQReceiveHandler<QConsume> consumeHandler;
protected IQReceiveHandler<Collection<QSubscribe>> subscribeHandler;
protected IQReceiveHandler<Short> responseHandler;
protected IQReceiveHandler<QRpc> rpcHandler; //通信层逻辑
protected IQReceiveHandler<Void> acceptHandler;
protected IQReceiveHandler<Void> closeHandler; protected IQReceiveHandler<Void> beforeHandler;
protected IQReceiveHandler<Void> afterHandler;
/**
*
* 响应端 业务逻辑
*
* @author solq
**/
public interface IQReceiveHandler<T> { void onReceive(ChannelHandlerContext ctx,QPacket packet, T body); }