//消息处理 private void ProcessData() { //沾包分包处理 if (buffCount < sizeof(Int32)) { return; } //包体长度 Array.Copy(readBuff, lenBytes, sizeof(Int32)); msgLength = BitConverter.ToInt32(lenBytes, 0); if (buffCount < msgLength + sizeof(Int32)) { return; } //协议解码 Protocol.ProtocolBase protocol = proto.Decode(readBuff, sizeof(Int32), msgLength); //Debug.Log("收到消息 " + protocol.GetDesc()); lock (msgDist.msgList) { msgDist.msgList.Add(protocol); } //清除已处理的消息 int count = buffCount - msgLength - sizeof(Int32); Array.Copy(readBuff, sizeof(Int32) + msgLength, readBuff, 0, count); buffCount = count; if (buffCount > 0) { ProcessData(); } }
public bool Send(Protocol.ProtocolBase protocol, string cbName, MsgDistribution.Delegate cb) { if (status != Status.Connected) { return(false); } msgDist.AddOnceListener(cbName, cb); return(Send(protocol)); }
public bool Send(Protocol.ProtocolBase protocol) { if (status != Status.Connected) { Debug.LogError("[Connection]还没链接就发送数据是不好的"); return(true); } byte[] b = protocol.Encode(); byte[] length = BitConverter.GetBytes(b.Length); byte[] sendbuff = length.Concat(b).ToArray(); socket.Send(sendbuff); return(true); }
//消息分发 public void DispatchMsgEvent(Protocol.ProtocolBase protocol) { string name = protocol.GetName(); //Debug.Log("分发处理消息 " + name); if (eventDict.ContainsKey(name)) { eventDict[name](protocol); } if (onceDict.ContainsKey(name)) { onceDict[name](protocol); onceDict[name] = null; onceDict.Remove(name); } }
public bool Send(Protocol.ProtocolBase protocol, MsgDistribution.Delegate cb) { string cbName = protocol.GetName(); return(Send(protocol, cbName, cb)); }