Ejemplo n.º 1
0
        public Client(GameScene gameScene)
        {
            GameScene = gameScene;
            PacketMap.Initialize();

            _userName = gameScene.Engine.Username;
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _receiveBufferQueue = new ByteQueue();
            _readingBufferQueue = new ByteQueue();
            _fragPackets = new ByteQueue();
            _socketAsyncArgs = new SocketAsyncEventArgs();
            _receiveQueueReader = new Thread(ProcessReadQueue);
        }
Ejemplo n.º 2
0
        public Client(int clientId, int dbId, Server server, Socket socket)
        {
            IsLoggingIn = false;

            ClientId = clientId;
            DbId = dbId;
            Server = server;
            _socket = socket;

            _currentBuffer = new ByteQueue();
            _processedBuffer = new ByteQueue();
            FragPackets = new ByteQueue();

            _nextActivityCheck = DateTime.Now + TimeSpan.FromSeconds(30);
        }
Ejemplo n.º 3
0
        internal void SendStart()
        {
            if (!Running || !_socket.Connected)
            {
                DisposeSendSystem();
                return;
            }

            try
            {
                ByteQueue byteQueue = new ByteQueue();
                int length = 0;
                while (!PacketsToBeSent.IsEmpty && length <= 1024)
                {
                    Packet packet;
                    if (!PacketsToBeSent.TryDequeue(out packet))
                    {
                        Interlocked.Exchange(ref _timesEnqueuedForSend, 0);
                        return;
                    }

                    if (!packet.Shared)
                        packet.Write();

                    byte[] packetBuffer = packet.GetBuffer();
                    length += packetBuffer.Length;

                    byteQueue.Enqueue(packetBuffer, 0, packetBuffer.Length);
                    packet.Release();
                }

                if (byteQueue.Length > 0)
                {
                    byte[] data = new byte[length];
                    byteQueue.Dequeue(data, 0, data.Length);
                    Send_Async(data);
                }
                else
                {
                    Interlocked.Exchange(ref _timesEnqueuedForSend, 0);

                    if (!PacketsToBeSent.IsEmpty)
                    {
                        int newValue = Interlocked.Increment(ref _timesEnqueuedForSend);

                        if (newValue == 1)
                        {
                            Server.SendClientQueue.Enqueue(this);
                            Server.NetworkSignal.Set();
                        }
                    }
                }
            }
            catch (Exception)
            {
                MarkToDispose();
                DisposeSendSystem();
                //if (packet != null)
                //Logger.Log(LogLevel.Error, "Sending packet: {0}", packet.ToString());
                //Logger.Log(LogLevel.Error, e.ToString());

                // TODO: log something?
            }
        }
Ejemplo n.º 4
0
        private static byte[] GetBufferToBeRead(ByteQueue processedBuffer, Client client, int length)
        {
            int availableData = client.FragPackets.Size + processedBuffer.Size;

            if (length > availableData)
                return null;

            int fromFrag;

            byte[] data = new byte[length];

            if (length >= client.FragPackets.Size)
                fromFrag = client.FragPackets.Size;
            else
                fromFrag = length;

            client.FragPackets.Dequeue(data, 0, fromFrag);

            int fromProcessed = length - fromFrag;

            processedBuffer.Dequeue(data, fromFrag, fromProcessed);

            return data;
        }
Ejemplo n.º 5
0
        private void ProcessReadQueue()
        {
            while (_recv.WaitOne())
            {
                ByteQueue temp;
                lock (_queueLock)
                {
                    temp = _receiveBufferQueue;
                    _receiveBufferQueue = _readingBufferQueue;
                }

                _readingBufferQueue = temp;

                int length = _fragPackets.Size + _readingBufferQueue.Size;

                while (length > 0)
                {
                    byte packetType = _fragPackets.Size > 0
                                          ? _fragPackets.GetPacketId()
                                          : _readingBufferQueue.GetPacketId();

                    ClientPacketHandler handler = PacketHandlers.GetHandler((PacketType) packetType);

                    if (handler == null)
                    {
                        byte[] unhandled = GetBufferToBeRead(length);
                        Console.WriteLine("Received unknown packet! Id:{0}", packetType);
                        Console.WriteLine("Fehler {0}", BitConverter.ToString(unhandled));
                        length = 0;
                    }
                    else if (handler.Length == 0)
                    {
                        byte[] data = GetBufferToBeRead(length);
                        Console.WriteLine("Klappt {0}", BitConverter.ToString(data));

                        if (length >= handler.MinimumLength)
                        {
                            PacketReader reader = new PacketReader(data, length);

                            handler.OnReceive(this, reader);
                            if (reader.Failed)
                            {
                                EnqueueFragment(data);
                                length = 0;
                            }
                            else
                            {
                                _readingBufferQueue.Enqueue(data, reader.Index, data.Length - reader.Index);
                                length = _readingBufferQueue.Length;
                            }
                        }
                        else
                        {
                            EnqueueFragment(data);
                            length = 0;
                        }
                    }
                    else if (length >= handler.Length)
                    {
                        byte[] data = GetBufferToBeRead(handler.Length);
                        /*using (StreamWriter sw = new StreamWriter(String.Format("recv_packets{0}.log", _userName), true))
                        {
                            sw.WriteLine("Fixed length: {0}", BitConverter.ToString(data));
                        }*/
                        PacketReader reader = new PacketReader(data, handler.Length);

                        handler.OnReceive(this, reader);

                        // If we failed it's because the packet is wrong
                        if (reader.Failed)
                        {
                            Dispose();
                            length = 0;
                        }
                        else
                        {
                            if (_fragPackets.Length > 0)
                                throw new Exception("Fragpackets must be empy here!");
                            length = _readingBufferQueue.Length;
                        }
                    }
                    else
                    {
                        /*using (StreamWriter sw = new StreamWriter(String.Format("recv_packets{0}.log", _userName), true))
                        { */
                        byte[] data = GetBufferToBeRead(length);
                        //sw.WriteLine("Fragmented fixed: {0}", BitConverter.ToString(data));
                        EnqueueFragment(data);
                        length = 0;
                        //}
                    }
                }
            }
        }