Exemplo n.º 1
0
        private void _channel_Receive(object sender, Message <TSender> e)
        {
            ProtocolMap map;

            lock (this._users)
            {
                if (!this._users.TryGetValue(e.Sender, out map))
                {
                    this._users.Add(e.Sender, map = new ProtocolMap());
                }
            }

            var m = ByteProtocolMessage.ParseMessage(e.Data);

            // ReSharper disable once AssignmentInConditionalExpression
            lock (map)
            {
                bool changed;
                var  old = map.Get(m.Id);
                if (m.Name == string.Empty)
                {
                    changed = map.Remove(m.Id);
                }
                else if (changed = map.Set(m.Id, m.Name))
                {
                    this.ProtocolDiscovered?.Invoke(this, new KeyValuePair <Message <TSender>, string>(e, m.Name));
                }

                if (changed && old != null)
                {
                    this.ProtocolDeleted?.Invoke(this, new KeyValuePair <Message <TSender>, string>(e, old));
                }
            }
        }
Exemplo n.º 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));
        }