Beispiel #1
0
        private void OnReceive(IAsyncResult asyncResult)
        {
            try
            {
                Socket s = (Socket)asyncResult.AsyncState;

                int byteCount = s.EndReceive(asyncResult);

                if (byteCount > 0)
                {
                    m_NextCheckActivity = Core.TickCount + 90000;

                    byte[] buffer;

                    lock (m_AsyncLock)
                        buffer = m_RecvBuffer;

                    if (PacketEncryptor != null)
                    {
                        PacketEncryptor.DecryptIncomingPacket(this, ref buffer, ref byteCount);
                    }

                    if (PacketEncoder != null)
                    {
                        PacketEncoder.DecodeIncomingPacket(this, ref buffer, ref byteCount);
                    }

                    lock (m_Buffer)
                        m_Buffer.Enqueue(buffer, 0, byteCount);

                    m_MessagePump.OnReceive(this);

                    lock (m_AsyncLock)
                    {
                        m_AsyncState &= ~AsyncState.Pending;

                        if ((m_AsyncState & AsyncState.Paused) == 0)
                        {
                            try
                            {
                                InternalBeginReceive();
                            }
                            catch (Exception ex)
                            {
                                TraceException(ex);
                                Dispose(false);
                            }
                        }
                    }
                }
                else
                {
                    Dispose(false);
                }
            }
            catch
            {
                Dispose(false);
            }
        }
        public Packet ReceivePacket(PacketEncryptor packetEncryptor)
        {
            if (this.incomingPackets.Count > 0)
            {
                return(this.incomingPackets.Dequeue());
            }

            try
            {
                var tlvData   = this.WebClient.DownloadData(this.Config.Uri);
                var delay     = 0;
                var failCount = 0;

                while (tlvData.Length == 0)
                {
                    delay = 10 * failCount;
                    ++failCount;
                    System.Threading.Thread.Sleep(Math.Min(10000, delay));
                    tlvData = this.WebClient.DownloadData(this.Config.Uri);
                }

                using (var tlvStream = new MemoryStream(tlvData))
                    using (var reader = new BinaryReader(tlvStream))
                    {
                        return(new Packet(reader, packetEncryptor));
                    }
            }
            catch (Exception e)
            {
                // something went wrong, bail out
            }

            return(null);
        }
Beispiel #3
0
        private Server()
        {
            this.pluginManager   = new PluginManager(this.DispatchPacket);
            this.Transports      = new List <ITransport>();
            this.commandHandler  = new CommandHandler();
            this.packetEncryptor = new PacketEncryptor();

            this.commandHandler.Register(this.pluginManager);
        }
Beispiel #4
0
        private Server()
        {
            this.channelManager  = new ChannelManager(this);
            this.Transports      = new List <ITransport>();
            this.commandHandler  = new CommandHandler();
            this.packetEncryptor = new PacketEncryptor();
            this.pluginManager   = new PluginManager(this, this.channelManager);
            this.pivotManager    = new PivotManager(this);

            this.commandHandler.Register(this.pluginManager);
        }
        public override byte[] Construct(PacketEncryptor encryptor)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream))
                {
                    writer.Write(0u);
                    writer.WritePackedUInt32((uint)flags);
                    writer.Write(NetworkObject.Pack(networkObject));

                    int length = (int)stream.Length - sizeof(int);

                    writer.Seek(0, SeekOrigin.Begin);
                    writer.Write(length);

                    return(stream.ToArray());
                }
            }
        }
        public Packet ReceivePacket(PacketEncryptor packetEncryptor)
        {
            try
            {
                var peekBuffer = new byte[4];
                if (this.tcpClient.Client.Receive(peekBuffer, SocketFlags.Peek) == peekBuffer.Length)
                {
                    if (peekBuffer[peekBuffer.Length - 1] == 0)
                    {
                        FlushStage();
                    }
                }

                return(new Packet(this.tcpReader, packetEncryptor));
            }
            catch
            {
                // The transport may have bailed while we were trying to read, so return null
                // to indicate a transport error.
            }
            return(null);
        }
Beispiel #7
0
        public override byte[] Construct(PacketEncryptor encryptor)
        {
            var packet = new NetworkObject();

            packet.AddField(0, NetworkObjectField.CreateIntField((int)opcode));
            packet.AddField(1, NetworkObjectField.CreateObjectField(networkObject));

            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream))
                {
                    writer.Write(0u);
                    writer.WritePackedUInt32((uint)flags);
                    writer.Write(NetworkObject.Pack(packet));

                    int length = (int)stream.Length - sizeof(int);
                    writer.Seek(0, SeekOrigin.Begin);
                    writer.Write(length);

                    return(stream.ToArray());
                }
            }
        }
Beispiel #8
0
        public virtual void Send(Packet p)
        {
            if (Socket == null || BlockAllPackets)
            {
                p.OnSend();
                return;
            }

            int length;

            var buffer = p.Compile(CompressionEnabled, out length);

            if (buffer != null)
            {
                if (buffer.Length <= 0 || length <= 0)
                {
                    p.OnSend();
                    return;
                }

                PacketSendProfile prof = null;

                if (Core.Profiling)
                {
                    prof = PacketSendProfile.Acquire(p.GetType());
                }

                if (prof != null)
                {
                    prof.Start();
                }

                var buffered = false;

                if (PacketEncoder != null || PacketEncryptor != null)
                {
                    var packetBuffer = buffer;
                    var packetLength = length;

                    if (BufferStaticPackets && p.State.HasFlag(PacketState.Acquired))
                    {
                        if (packetLength <= SendBufferSize)
                        {
                            packetBuffer = m_SendBufferPool.AcquireBuffer();
                        }
                        else
                        {
                            packetBuffer = new byte[packetLength];
                        }

                        System.Buffer.BlockCopy(buffer, 0, packetBuffer, 0, packetLength);
                    }

                    if (PacketEncoder != null)
                    {
                        PacketEncoder.EncodeOutgoingPacket(this, ref packetBuffer, ref packetLength);
                    }

                    if (PacketEncryptor != null)
                    {
                        PacketEncryptor.EncryptOutgoingPacket(this, ref packetBuffer, ref packetLength);
                    }

                    buffered = buffer != packetBuffer && packetBuffer.Length == SendBufferSize;

                    buffer = packetBuffer;
                    length = packetLength;
                }

                try
                {
                    SendQueue.Gram gram;

                    lock (_SendLock)
                    {
                        lock (m_SendQueue)
                        {
                            gram = m_SendQueue.Enqueue(buffer, length);
                        }

                        if (buffered && m_SendBufferPool.Count < SendBufferCapacity)
                        {
                            m_SendBufferPool.ReleaseBuffer(buffer);
                        }

                        if (gram != null && !_Sending)
                        {
                            _Sending = true;

                            try
                            {
                                Socket.BeginSend(gram.Buffer, 0, gram.Length, SocketFlags.None, m_OnSend, Socket);
                            }
                            catch (Exception ex)
                            {
                                TraceException(ex);
                                Dispose(false);
                            }
                        }
                    }
                }
                catch (CapacityExceededException)
                {
                    Utility.PushColor(ConsoleColor.Red);
                    Console.WriteLine("Client: {0}: Too much data pending, disconnecting...", this);
                    Utility.PopColor();

                    Dispose(false);
                }

                p.OnSend();

                if (prof != null)
                {
                    prof.Finish(length);
                }
            }
            else
            {
                Utility.PushColor(ConsoleColor.Red);
                Console.WriteLine("Client: {0}: null buffer send, disconnecting...", this);
                Utility.PopColor();

                using (var op = new StreamWriter("null_send.log", true))
                {
                    op.WriteLine("{0} Client: {1}: null buffer send, disconnecting...", DateTime.UtcNow, this);
                    op.WriteLine(new StackTrace());
                }

                Dispose();
            }
        }
Beispiel #9
0
 public Packet ReceivePacket(PacketEncryptor packetEncryptor)
 {
     return(new Packet(this.tcpReader, packetEncryptor));
 }
Beispiel #10
0
 public Packet ReceivePacket(PacketEncryptor packetEncryptor)
 {
     throw new NotImplementedException();
 }
Beispiel #11
0
 public abstract byte[] Construct(PacketEncryptor encryptor);