Example #1
0
        void RecvUnconnectedPacket(UdpStream buffer, UdpEndPoint ep)
        {
            UdpAssert.Assert(buffer.Ptr == 0);
            buffer.Ptr = HeaderBitSize;

            if (buffer.ReadByte(8) == (byte)UdpCommandType.Connect)
            {
                if (Config.AllowIncommingConnections && ((connLookup.Count + pendingConnections.Count) < Config.ConnectionLimit || Config.ConnectionLimit == -1))
                {
                    if (Config.AutoAcceptIncommingConnections)
                    {
                        AcceptConnection(ep);
                    }
                    else
                    {
                        if (pendingConnections.Add(ep))
                        {
                            Raise(UdpEvent.PUBLIC_CONNECT_REQUEST, ep);
                        }
                    }
                }
                else
                {
                    SendRefusedCommand(ep);
                }
            }
        }
Example #2
0
        public UdpStream Acquire()
        {
            UdpStream stream = null;

            lock (pool) {
                if (pool.Count > 0)
                {
                    stream = pool.Pop();
                }
            }

            if (stream == null)
            {
                stream      = new UdpStream(new byte[PacketSize * 2]);
                stream.Pool = this;
            }

            UdpAssert.Assert(stream.IsPooled);

            stream.IsPooled = false;
            stream.Position = 0;
            //stream.Size = (PacketSize - UdpMath.BytesRequired(UdpSocket.HeaderBitSize)) << 3;
            stream.Size = (PacketSize - 0) << 3; //we're not using this for it's intended purpose,
            //just hijacking it to use it's bitpacking methods.

            return(stream);
        }
Example #3
0
        void RecvUnconnectedPacket(UdpStream buffer, UdpEndPoint ep)
        {
            UdpAssert.Assert(buffer.Ptr == 0);
            buffer.Ptr = HeaderBitSize;

            if (buffer.ReadByte(8) == (byte)UdpCommandType.Connect)
            {
                if (Config.AllowIncommingConnections && ((connLookup.Count + pendingConnections.Count) < Config.ConnectionLimit || Config.ConnectionLimit == -1))
                {
                    if (Config.AutoAcceptIncommingConnections)
                    {
                        AcceptConnection(ep, null);
                    }
                    else
                    {
                        if (pendingConnections.Add(ep))
                        {
                            object obj = null;
                            if (buffer.ReadByte(8) == UdpEvent.INTERNAL_COMMAND_HASOBJECT)
                            {
                                //they've also sent an object along with their connection request
                                serializer.Unpack(buffer, ref obj);
                            }
                            Raise(UdpEvent.PUBLIC_CONNECT_REQUEST, ep, obj);
                        }
                    }
                }
                else
                {
                    SendRefusedCommand(ep);
                }
            }
        }
        public UdpStream Acquire()
        {
            UdpStream stream = null;

            lock (pool) {
                if (pool.Count > 0)
                {
                    stream = pool.Pop();
                }
            }

            if (stream == null)
            {
                stream      = new UdpStream(new byte[socket.Config.PacketSize * 2]);
                stream.Pool = this;
            }

            UdpAssert.Assert(stream.IsPooled);

            stream.IsPooled = false;
            stream.Position = 0;
            stream.Size     = (socket.Config.PacketSize - UdpMath.BytesRequired(UdpSocket.HeaderBitSize)) << 3;

            return(stream);
        }
Example #5
0
        internal void Release(UdpStream stream)
        {
            UdpAssert.Assert(stream.IsPooled == false);

            lock (pool) {
                stream.Size     = 0;
                stream.Position = 0;
                stream.IsPooled = true;

                pool.Push(stream);
            }
        }
Example #6
0
        bool ParseHeader(UdpStream stream)
        {
            // we should always start at ptr 0
            UdpAssert.Assert(stream.Ptr == 0);

            UdpHeader header = new UdpHeader();

            header.Unpack(stream, socket);

            // after unpacking the header, the pointer should be at the header size
            UdpAssert.Assert(stream.Ptr == UdpSocket.HeaderBitSize);

            int seqDistance = UdpMath.SeqDistance(header.ObjSequence, recvSequence, UdpHeader.SEQ_PADD);

            // we have to be within window size
            if (seqDistance > socket.Config.PacketWindow || seqDistance < -socket.Config.PacketWindow)
            {
                ConnectionError(UdpConnectionError.SequenceOutOfBounds);
                return(false);
            }

            // this is an old packet
            if (seqDistance <= 0)
            {
                return(false);
            }

            // update receive history
            if (seqDistance >= UdpSocket.AckRedundancy)
            {
                recvHistory = 1UL;
            }
            else
            {
                recvHistory = (recvHistory << seqDistance) | 1UL;
            }

            // update our receive stats
            recvSequence       = header.ObjSequence;
            recvSinceLastSend += 1;

            // ack sent objects
            AckHandles(header, true);

            return(true);
        }