예제 #1
0
 /// <summary>
 /// Instantiates a new Client object.
 /// </summary>
 /// <param name="server">The Server to associate with the entity.</param>
 /// <param name="sessionId">The entity ID for the client.</param>
 /// <param name="tcp">The TCP client to be used for communication.</param>
 internal Client(Socket socket, Player player)
 {
     _Socket = socket;
     _Player = player;
     _Player.Client = this;
     _CurrentBuffer = new ByteQueue();
     _ProcessedBuffer = new ByteQueue();
     _FragPackets = new ByteQueue();
     //PacketHandler = new PacketHandler(Server, socket);
 }
예제 #2
0
 public TestClient(string name)
 {
     _userName = name;
     _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);
 }
예제 #3
0
        internal ByteQueue GetBufferToProcess()
        {
            lock (_QueueSwapLock)
            {
                ByteQueue temp = _currentBuffer;
                _currentBuffer   = _processedBuffer;
                _processedBuffer = temp;
            }

            return(_processedBuffer);
        }
예제 #4
0
파일: Client.cs 프로젝트: IdentErr/c-raft
 /// <summary>
 /// Instantiates a new Client object.
 /// </summary>
 internal Client(int sessionId, Server server, Socket socket)
 {
     _socket = socket;
     _currentBuffer = new ByteQueue();
     _processedBuffer = new ByteQueue();
     _fragPackets = new ByteQueue();
     _nextActivityCheck = DateTime.Now + TimeSpan.FromSeconds(5.0);
     SessionID = sessionId;
     Server = server;
     //PacketHandler = new PacketHandler(Server, socket);
 }
예제 #5
0
 /// <summary>
 /// Instantiates a new Client object.
 /// </summary>
 internal Client(int sessionId, Server server, Socket socket)
 {
     _socket            = socket;
     _currentBuffer     = new ByteQueue();
     _processedBuffer   = new ByteQueue();
     _fragPackets       = new ByteQueue();
     _nextActivityCheck = DateTime.Now + TimeSpan.FromSeconds(10);
     SessionID          = sessionId;
     Server             = server;
     _chunkSendTimer    = new Timer(SendChunks, null, Timeout.Infinite, Timeout.Infinite);
     //PacketHandler = new PacketHandler(Server, socket);
 }
예제 #6
0
파일: Client.cs 프로젝트: dekema2/c-raft
 /// <summary>
 /// Instantiates a new Client object.
 /// </summary>
 internal Client(int sessionId, Server server, Socket socket)
 {
     _socket = socket;
     _currentBuffer = new ByteQueue();
     _processedBuffer = new ByteQueue();
     _fragPackets = new ByteQueue();
     _nextActivityCheck = DateTime.Now + TimeSpan.FromSeconds(10);
     SessionID = sessionId;
     Server = server;
     _chunkSendTimer = new Timer(SendChunks, null, Timeout.Infinite, Timeout.Infinite);
     //PacketHandler = new PacketHandler(Server, socket);
 }
예제 #7
0
        /// <summary>
        /// Instantiates a new Client object.
        /// </summary>
        internal Client(int sessionId, Server server, Socket socket)
        {
            _socket            = socket;
            _currentBuffer     = new ByteQueue();
            _processedBuffer   = new ByteQueue();
            _fragPackets       = new ByteQueue();
            _nextActivityCheck = DateTime.Now + TimeSpan.FromSeconds(10);
            SessionID          = sessionId;
            Server             = server;

            // Generate a unique ServerId for each client
            byte[] bytes = new byte[8];
            Server.Rand.NextBytes(bytes);
            ConnectionId = BitConverter.ToString(bytes).Replace("-", "");

            _chunkSendTimer = new Timer(SendChunks, null, Timeout.Infinite, Timeout.Infinite);
            //PacketHandler = new PacketHandler(Server, socket);
        }
예제 #8
0
파일: Server.cs 프로젝트: TheaP/c-raft
        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;
        }
예제 #9
0
        internal void Send_Start()
        {
            if (!Running || !_socket.Connected)
            {
                DisposeSendSystem();
                return;
            }

            Packet packet = null;

            try
            {
                ByteQueue byteQueue = new ByteQueue();
                int       length    = 0;
                while (!PacketsToBeSent.IsEmpty && length <= 1024)
                {
                    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 (packet is DisconnectPacket)
                    {
                        ToDisconnect = true;
                        _sendSocketEvent.Completed += Disconnected;
                        break;
                    }
                }

                if (byteQueue.Length > 0)
                {
                    byte[] data = new byte[length];
                    byteQueue.Dequeue(data, 0, data.Length);

                    if (Encrypter != null)
                    {
                        byte[] toEncrypt = data;
                        data = new byte[length];
                        Encrypter.TransformBlock(toEncrypt, 0, length, data, 0);
                    }
                    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 e)
            {
                MarkToDispose();
                DisposeSendSystem();
                if (packet != null)
                {
                    Logger.Log(LogLevel.Error, "Sending packet: {0}", packet.ToString());
                }
                Logger.Log(LogLevel.Error, e.ToString());

                // TODO: log something?
            }
        }
예제 #10
0
파일: TestClient.cs 프로젝트: TheaP/c-raft
        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;

                    if (_fragPackets.Size > 0)
                        packetType = _fragPackets.GetPacketID();
                    else
                        packetType = _readingBufferQueue.GetPacketID();

                    /*using(StreamWriter sw = new StreamWriter(String.Format("recv_packets{0}.log", _userName), true))
                    {
                        byte[] enums = (byte[])Enum.GetValues(typeof (PacketType));
                        int i = Array.BinarySearch(enums, 0, enums.Length, packetType);

                        if(i != -1)
                            sw.WriteLine("{0} - Received packet {1}", DateTime.Now, ((PacketType)packetType));
                        else
                            sw.WriteLine("{0} - Received packet {1}", DateTime.Now, packetType);
                    }*/

                    //result = HandlePacket(packetType, length);

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

                    if (handler == null)
                    {
                        using (StreamWriter sw = new StreamWriter(String.Format("unhandled_packets_{0}.log", _userName), true))
                        {
                            byte[] unhandled = GetBufferToBeRead(length);

                            sw.WriteLine("{0} - PacketType: {1}", DateTime.Now, unhandled[0]);
                            sw.WriteLine(BitConverter.ToString(unhandled));
                        }

                        length = 0;
                    }
                    else if (handler.Length == 0)
                    {
                        byte[] data = GetBufferToBeRead(length);
                        /*using (StreamWriter sw = new StreamWriter(String.Format("recv_packets{0}.log", _userName), true))
                        {*/
                            if (length >= handler.MinimumLength)
                            {
                                PacketReader reader = new PacketReader(data, length);

                                handler.OnReceive(this, reader);
                                
                                // If we failed it's because the packet isn't complete
                                if (reader.Failed)
                                {
                                    //sw.WriteLine("Failed Fragmented: {0}", BitConverter.ToString(data));
                                    EnqueueFragment(data);
                                    length = 0;
                                }
                                else
                                {
                                    //sw.WriteLine("Variable length: {0}", BitConverter.ToString(data, 0, reader.Index));
                                    _readingBufferQueue.Enqueue(data, reader.Index, data.Length - reader.Index);
                                    length = _readingBufferQueue.Length;
                                }
                            }
                            else
                            {
                                //sw.WriteLine("Fragmented variable: {0}", BitConverter.ToString(data));
                                // Not enough data
                                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;
                        //}
                    }
                }
            }
        }
예제 #11
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;

                    if (_fragPackets.Size > 0)
                        packetType = _fragPackets.GetPacketID();
                    else
                        packetType = _readingBufferQueue.GetPacketID();

                    /*using(StreamWriter sw = new StreamWriter(String.Format("recv_packets{0}.log", _userName), true))
                    {
                        sw.WriteLine("{0} - Received packet {1}", DateTime.Now, ((PacketType)packetType));
                    }*/

                    bool result = false;
                    //result = HandlePacket(packetType, length);

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

                    if (handler == null)
                    {
                        using (StreamWriter sw = new StreamWriter(String.Format("unhandled_packets_{0}.log", _userName), true))
                        {
                            byte[] unhandled = GetBufferToBeRead(length);

                            sw.WriteLine("PacketType: {0}", unhandled[0]);
                            sw.WriteLine(BitConverter.ToString(unhandled));
                        }

                        length = 0;
                    }
                    else if (handler.Length == 0)
                    {
                        byte[] data = GetBufferToBeRead(length);

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

                            handler.OnReceive(this, reader);

                            // If we failed it's because the packet isn't complete
                            if (reader.Failed)
                            {
                                EnqueueFragment(data);
                                length = 0;
                            }
                            else
                            {
                                _readingBufferQueue.Enqueue(data, reader.Index, data.Length - reader.Index);
                                length = _readingBufferQueue.Length;
                            }
                        }
                        else
                            EnqueueFragment(data); // Not enough data

                    }
                    else if (length >= handler.Length)
                    {
                        byte[] data = GetBufferToBeRead(handler.Length);

                        PacketReader reader = new PacketReader(data, handler.Length, StreamRole.Client);

                        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
                    {
                        byte[] data = GetBufferToBeRead(length);
                        EnqueueFragment(data);
                        length = 0;
                    }
                }
            }
        }
예제 #12
0
파일: Client.cs 프로젝트: OBASI/c-raft
        /// <summary>
        /// Instantiates a new Client object.
        /// </summary>
        internal Client(int sessionId, Server server, Socket socket)
        {
            _socket = socket;
            _currentBuffer = new ByteQueue();
            _processedBuffer = new ByteQueue();
            _fragPackets = new ByteQueue();
            _nextActivityCheck = DateTime.Now + TimeSpan.FromSeconds(10);
            SessionID = sessionId;
            Server = server;

            // Generate a unique ServerId for each client
            byte[] bytes = new byte[8];
            Server.Rand.NextBytes(bytes);
            ConnectionId = BitConverter.ToString(bytes).Replace("-", "");

            _chunkSendTimer = new Timer(SendChunks, null, Timeout.Infinite, Timeout.Infinite);
            //PacketHandler = new PacketHandler(Server, socket);
        }