/// <summary> /// 发送消息 /// </summary> /// <param name="opCode">操作码</param> /// <param name="subCode">子操作码</param> /// <param name="value">参数</param> public void SendMsg(int opCode, int subCode, object value) { msg.Change(opCode, subCode, value); byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); SendMsg(packet); }
/// <summary> /// 处理数据 /// </summary> private void ProcessData() { isProcessingReceive = true; //解析包,从缓存区里取出一个完整的包 byte[] packet = EncodeTool.DecodePacket(ref cache); if (packet == null) { isProcessingReceive = false; return; } NetMsg msg = EncodeTool.DecodeMsg(packet); if (receiveCompleted != null) { receiveCompleted(this, msg); } ProcessData(); }
/// <summary> /// 处理数据 /// </summary> private void ProcessData() { isProcessingReceive = true; // 解析包,从缓冲区里取出一个完整的包 byte[] packet = EncodeTool.DecodePacket(ref cache); if (packet == null) { isProcessingReceive = false; return; } // 反序列化 NetMsg msg = EncodeTool.DecodeMsg(packet); if (receiveCompleted != null) // 有人注册了委托 { receiveCompleted(this, msg); // 将处理完后的 msg 回调给上层 } ProcessData(); // 递归,知道缓存区的数据为 null }