Пример #1
0
        private bool QueueReceive(PortalBuffer buffer)
        {
            if (buffer == null)
            {
                return(false);
            }

            if (_ReceiveQueue == null || buffer.Size < PortalPacket.MinSize)
            {
                return(false);
            }

            if (_Buffered + buffer.Size > PortalPacket.MaxSize * 8L)
            {
                if (_DisplayRecvOutput)
                {
                    ToConsole("Recv: Too much data pending: {0} + {1} bytes", _Buffered, buffer.Size);
                }

                Dispose();
                return(false);
            }

            _ReceiveQueue.Enqueue(buffer);
            _Buffered += buffer.Size;

            return(true);
        }
Пример #2
0
 public PortalPacketReader(PortalBuffer buffer)
     : base(new PortalStream(buffer), Encoding.UTF8)
 {
     PacketID = ReadUInt16();
     ServerID = ReadUInt16();
     Length   = ReadInt32();
 }
Пример #3
0
        private void ProcessReceiveBuffer(PortalBuffer buffer)
        {
            if (buffer == null || buffer.Size < PortalPacket.MinSize)
            {
                return;
            }

            var pid = BitConverter.ToUInt16(buffer.Join(0, 2), 0);

            PortalPacketHandler handler;

            if (!Handlers.TryGetValue(pid, out handler) || handler == null)
            {
                if (_DisplayRecvOutput)
                {
                    ToConsole("Recv: Missing Handler for {0}", pid);
                }

                return;
            }

            if (handler.Context == PortalContext.Disabled)
            {
                if (_DisplayRecvOutput)
                {
                    ToConsole("Recv: Ignoring Packet {0}", pid);
                }

                return;
            }

            if (handler.Context != PortalContext.Any && ((handler.Context == PortalContext.Server && !IsRemoteClient) ||
                                                         (handler.Context == PortalContext.Client && !IsLocalClient)))
            {
                if (_DisplayRecvOutput)
                {
                    ToConsole("Recv: Out Of Context Packet {0} requires {1}", pid, handler.Context);
                }

                return;
            }

            if (_DisplayRecvOutput)
            {
                ToConsole("Recv: Packet {0} at {1} bytes", pid, buffer.Size);
            }

            using (var p = new PortalPacketReader(buffer))
            {
                handler.OnReceive(this, p);
            }
        }
Пример #4
0
        public PortalStream(PortalBuffer buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            if (buffer.IsDisposed)
            {
                throw new ObjectDisposedException("buffer");
            }

            _Buffer = buffer;
        }
Пример #5
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    _Readable = false;
                    _Writable = false;
                    _Seekable = false;

                    _Buffer = null;
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
Пример #6
0
 protected virtual void OnCompile(ref PortalBuffer buffer)
 {
 }
Пример #7
0
        private bool Receive(bool wait)
        {
            try
            {
                if ((!wait && _NoReceive) || !IsAlive)
                {
                    return(false);
                }

                if (wait)
                {
                    var timer   = new Stopwatch();
                    var timeout = _Client.ReceiveTimeout;

                    timer.Start();

                    while (Pending < Peek.Size)
                    {
                        if (!IsAlive || (Portal.Ticks % 1000 == 0 && !IsConnected))
                        {
                            timer.Stop();
                            return(false);
                        }

                        if (Pending < 0 || Pending >= Peek.Size)
                        {
                            timer.Reset();
                            break;
                        }

                        if (timeout > 0 && timer.ElapsedMilliseconds >= timeout)
                        {
                            timer.Stop();
                            break;
                        }

                        Thread.Sleep(1);
                    }
                }

                if ((!wait && _NoReceive) || !IsAlive || Pending < Peek.Size)
                {
                    return(false);
                }

                var peek = Peek.Acquire();
                var head = 0;

                while (head < peek.Length && _Client != null)
                {
                    head += _Client.Receive(peek, head, peek.Length - head, SocketFlags.None);

                    if (_Client == null || !_Client.Connected || head >= peek.Length)
                    {
                        break;
                    }
                }

                if (head < peek.Length)
                {
                    if (_DisplayRecvOutput)
                    {
                        ToConsole("Recv: Peek Failed at {0} / {1} bytes", head, peek.Length);
                    }

                    Peek.Free(ref peek);

                    Dispose();
                    return(false);
                }

                var pid = BitConverter.ToUInt16(peek, 0);

                if (GetHandler(pid) == null)
                {
                    if (_DisplayRecvOutput)
                    {
                        ToConsole("Recv: Unknown Packet ({0})", pid);
                    }

                    Peek.Free(ref peek);

                    Dispose();
                    return(false);
                }

                var sid = BitConverter.ToUInt16(peek, 2);

                if (IsRemoteClient)
                {
                    if (!_IsSeeded || !_ServerID.HasValue)
                    {
                        _ServerID = sid;
                        _IsSeeded = true;
                    }
                    else if (_ServerID != sid)
                    {
                        if (_DisplayRecvOutput)
                        {
                            ToConsole("Recv: Server ID Spoofed ({0})", sid);
                        }

                        Peek.Free(ref peek);

                        Dispose();
                        return(false);
                    }
                }
                else if (IsLocalClient)
                {
                    if (!_IsSeeded)
                    {
                        _IsSeeded = true;
                    }

                    if (!_ServerID.HasValue)
                    {
                        _ServerID = sid;
                    }
                }

                var size = BitConverter.ToInt32(peek, 4);

                if (size < PortalPacket.MinSize || size > PortalPacket.MaxSize)
                {
                    if (_DisplayRecvOutput)
                    {
                        ToConsole(
                            "Recv: Size Out Of Range for {0} at {1} / {2} - {3} bytes",
                            pid,
                            size,
                            PortalPacket.MinSize,
                            PortalPacket.MaxSize);
                    }

                    Peek.Free(ref peek);

                    Dispose();
                    return(false);
                }

                var buffer = new PortalBuffer(size);

                for (var i = 0; i < peek.Length; i++)
                {
                    buffer[i] = peek[i];
                }

                Peek.Free(ref peek);

                if (size > head)
                {
                    var length = head + buffer.Receive(_Client, head, size - head);

                    if (length < size)
                    {
                        if (_DisplayRecvOutput)
                        {
                            ToConsole("Recv: Failed for {0} at {1} / {2} bytes", pid, length, size);
                        }

                        buffer.Dispose();

                        Dispose();
                        return(false);
                    }
                }

                if (!QueueReceive(buffer))
                {
                    buffer.Dispose();
                    return(false);
                }

                return(true);
            }
            catch (Exception e)
            {
                ToConsole("Recv: Exception Thrown", e);

                Dispose();
                return(false);
            }
        }
Пример #8
0
 public PortalStream(long size, int coalesce)
 {
     _Buffer = new PortalBuffer(size, coalesce);
 }
Пример #9
0
 public PortalStream(long size)
 {
     _Buffer = new PortalBuffer(size);
 }
Пример #10
0
 public PortalStream()
 {
     _Buffer = new PortalBuffer();
 }