/// <summary> /// 处理接收数据,如果处理完成就直接回调函数给应用层 /// </summary> private void processReceive() { isReceiveProcess = true; //处理数据 byte [] data = EncodeTool.DecodePacket(ref dataCache); //Console.WriteLine("接收数据成功"+ data == null); if (data == null) { isReceiveProcess = false; return; } //TODO 需要再次转成一个具体的类型功我们使用 SocketMsg value = EncodeTool.DecodeMsg(data); //Console.WriteLine((value.Value as string)); if (value != null) { //这样子调用函数 receiveCompeleted(this, value); } // //Console.WriteLine("接收数据成功"); //回调上层,尾递归,继续处理,直到为空 processReceive(); }
/// <summary> /// 发送网络消息 /// </summary> /// <param name="opCode">操作码</param> /// <param name="subCode">子操作</param> /// <param name="value">参数</param> public void Send(int opCode, int subCode, object value) { SocketMsg msg = new SocketMsg(opCode, subCode, value); byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); Send(packet); }
public void Send(int opCode, int subCode, object value) { SocketMsg msg = new SocketMsg(opCode, subCode, value); byte[] data = EncodeTool.EncodeMsg(msg); // 把需要发送的 SocketMsg 消息 转换成 字节数组 byte[] packet = EncodeTool.EncodePacket(data); // 再把字节数组 封装成 包头 + 包尾 sendQue.Enqueue(packet); if (!isSendProcess) { Send(); } }
/// <summary> /// 发送网络消息 /// </summary> /// <param name="opCode">操作码</param> /// <param name="subCode">子操作</param> /// <param name="value">参数</param> public void Send(int opCode, int subCode, object value) { SocketMsg msg = new SocketMsg(opCode, subCode, value); byte[] data = EncodeTool.EncodeMsg(msg); byte[] packet = EncodeTool.EncodePacket(data); //存入消息队列里面 sendQueue.Enqueue(packet); if (!isSendProcess) { send(); } }
/// <summary> /// 发送数据 /// </summary> /// <param name="opCode"></param> /// <param name="subCode"></param> /// <param name="value"></param> public void Send(int opCode, int subCode, object value) { SocketMsg msg = new SocketMsg(opCode, subCode, value); // 先变成 socketMsg ,再变成 包头 + 包尾 的形式 byte[] data = EncodeTool.EncodeMsg(msg); // byte[] packet = EncodeTool.EncodePacket(data); //现存好数据,然后再发送 sendQueue.Enqueue(packet); if (!isSendProcess) { // 处理一下发送消息 send(); } }
private void processRecive() { IsReciveProcess = true; //解析数据包 byte[] data = EncodeTool.DecodePacket(ref dataCache); if (data == null) { IsReciveProcess = false; return; } SocketMessage msg = EncodeTool.DecodeMsg(data); //回调给上层 reciveCompleted?.Invoke(this, msg); processRecive(); }
/// <summary> /// 处理接收的数据 /// </summary> private void ProcessReceive() { isReceiveProcess = true; byte[] data = EncodeTool.DecodePacket(ref dataCacheList); // 拆包 if (data == null) { isReceiveProcess = false; return; } // 把 data 再次转成一个具体的数据类型 , 供我们使用 SocketMsg msg = EncodeTool.DecodeMsg(data); // 回调给上层 if (rc != null) { rc(this, msg); } // 尾递归 ProcessReceive(); }
/// <summary> /// 处理接受的数据 /// </summary> private void processReceive() { isReceiveProcess = true; //解析数据包 byte[] data = EncodeTool.DecodePacket(ref dataCache); if (data == null) { isReceiveProcess = false; return; } SocketMsg msg = EncodeTool.DecodeMsg(data); //回调给上层 if (receiveCompleted != null) { receiveCompleted(this, msg); } //尾递归 processReceive(); }