示例#1
0
 public static ByteProtocolMessage ParseMessage(byte[] data)
 {
     return(new ByteProtocolMessage
     {
         Id = data[0],
         Name = Encoding.UTF8.GetString(ByteHelper.SkipByte(data))
     });
 }
示例#2
0
        private void _channel_Receive(object sender, Message <TSender> e)
        {
            if (!e.Data.Any())
            {
                return;
            }

            var id   = e.Data[0];
            var name = this._byteProtocol.GetProtocol(e.Sender, id);

            if (name == null)
            {
                if (id != 0)
                {
                    return;
                }
                if (e.Data.Length != ProtocolName.Length + 2)
                {
                    return;
                }
            }

            var data = ByteHelper.SkipByte(e.Data);

            if (name == null)
            {
                var m = ByteProtocolMessage.ParseMessage(data);
                if (m.Id != 0 || m.Name != ProtocolName)
                {
                    return;
                }
                name = ProtocolName;
            }

            ProtocolChannel <TSender> proto;

            lock (this._protocols) this._protocols.TryGetValue(name, out proto);
            proto?.OnReceive(new Message <TSender>(e.Sender, e.IsOwnMessage, data));
        }