public override bool NetRead(NetRead read)
        {
            base.NetRead(read);

            var count = read.Read <int>();

            _internalDictionary = new Dictionary <TKey, TValue>(count);

            for (int i = 0; i < count; i++)
            {
                var key = default(TKey);

                var keyType = typeof(TKey);

                // Get key
                if (keyType == typeof(string))
                {
                    if (read.ReadClass <string>(null, null) is TKey str)
                    {
                        key = str;
                    }
                }
                else if (keyType.IsSubclassOf(typeof(Entity)))
                {
                    if (read.ReadClass <Entity>(null, null) is TKey ent)
                    {
                        key = ent;
                    }
                }
                else if (keyType.IsSubclassOf(typeof(NetworkClass)))
                {
                    if (read.ReadClass <NetworkClass>(null, null) is TKey nc)
                    {
                        key = nc;
                    }
                }
                else if (typeof(INetIdentifiable).IsAssignableFrom(keyType))
                {
                    if (read.ReadClass <INetIdentifiable>(null, null) is TKey ni)
                    {
                        key = ni;
                    }
                }
                else
                {
                    throw new NotSupportedException("You can only network NetworkClass, Entity, string or INetIdentifiable. Use a struct with NetworkedValueDictionary<T> for other types.");
                }

                // Get value
                var value = read.Read <TValue>();

                _internalDictionary.Add(key !, value);
            }

            return(true);
        }
示例#2
0
        private byte ReadFromBuffer(int bitCount)
        {
            int  currentPos = 0;
            byte current    = 0;
            int  remaining  = bitCount;

            while (remaining > 0)
            {
                int remainingInBuffer = 8 - BitPosition;
                int bitsToRead        = Math.Min(remainingInBuffer, remaining);
                int mask = (1 << bitsToRead) - 1;

                byte buf = (byte)(CurrentByte & mask);
                CurrentByte >>= bitsToRead;
                BitPosition  += bitsToRead;
                remaining    -= bitsToRead;
                current      |= (byte)(buf << currentPos);
                currentPos   += bitsToRead;

                if (BitPosition >= 8)
                {
                    CurrentBytePosition++;
                    if (CurrentBytePosition < TotalByteCount)
                    {
                        CurrentByte = Reader.Read <byte>();
                    }
                    else
                    {
                        throw new Exception();
                    }
                    BitPosition = 0;
                }
            }
            return(current);
        }
示例#3
0
        public override bool NetRead(NetRead read)
        {
            base.NetRead(read);

            var count = read.Read <int>();

            _internalDictionary = new Dictionary <TKey, TValue>(count);

            for (int i = 0; i < count; i++)
            {
                var key   = read.Read <TKey>();
                var value = read.Read <TValue>();

                _internalDictionary.Add(key, value);
            }

            return(true);
        }
示例#4
0
 public NetworkReader(NetRead Reader)
 {
     this.Reader         = Reader;
     TotalByteCount      = Reader.Remaining;
     CurrentBytePosition = 0;
     if (Reader.Remaining > 0)
     {
         CurrentByte = Reader.Read <byte>();
     }
 }
示例#5
0
    public bool OnUnconnectedMessage(int type, NetRead read, uint ip, int port)
    {
        if (this.useQueryPort)
        {
            return(false);
        }
        if (type != 255)
        {
            return(false);
        }
        if (this.queriesPerSeconTimer.Elapsed.TotalSeconds > 1)
        {
            this.queriesPerSeconTimer.Reset();
            this.queriesPerSeconTimer.Start();
            this.NumQueriesLastSecond = 0;
        }
        if (this.NumQueriesLastSecond > ConVar.Server.queriesPerSecond)
        {
            return(false);
        }
        if (read.UInt8() != 255)
        {
            return(false);
        }
        if (read.UInt8() != 255)
        {
            return(false);
        }
        if (read.UInt8() != 255)
        {
            return(false);
        }
        if (this.queryTimer.Elapsed.TotalSeconds > 60)
        {
            this.queryTimer.Reset();
            this.queryTimer.Start();
            this.unconnectedQueries.Clear();
        }
        if (!this.unconnectedQueries.ContainsKey(ip))
        {
            this.unconnectedQueries.Add(ip, 0);
        }
        int item = this.unconnectedQueries[ip] + 1;

        this.unconnectedQueries[ip] = item;
        if (item > ConVar.Server.ipQueriesPerMin)
        {
            return(true);
        }
        this.NumQueriesLastSecond++;
        read.Position = (long)0;
        int unread = read.Unread;

        if (unread > 4096)
        {
            return(true);
        }
        if (this.queryBuffer.Capacity < unread)
        {
            this.queryBuffer.Capacity = unread;
        }
        int num = read.Read(this.queryBuffer.GetBuffer(), 0, unread);

        SteamServer.HandleIncomingPacket(this.queryBuffer.GetBuffer(), num, ip, (ushort)port);
        return(true);
    }