Ejemplo n.º 1
0
        private void StartListening()
        {
            Logger.Debug("Starting with listening");
            try
            {
                var buffer       = new byte[RxBufferSize];
                var bufferOffset = 0;
                while (tcpClient?.Connected ?? false)
                {
                    if (tcpClient?.Available > 0)
                    {
                        var read             = stream.Read(buffer, bufferOffset, buffer.Length - bufferOffset);
                        var packetCollection = packetsFactory.GetPackets(buffer, 0, read + bufferOffset);
                        foreach (var packet in packetCollection)
                        {
                            Logger.Debug("Received packet from server: {packet}", packet);
                            PacketReceived?.Invoke(this, packet);
                        }

                        var leftover = read + bufferOffset - packetCollection.BytesUsed;
                        if (leftover > 0)
                        {
                            Array.Copy(buffer, packetCollection.BytesUsed, buffer, 0, leftover);
                            bufferOffset = leftover;
                        }
                        else
                        {
                            bufferOffset = 0;
                        }
                    }
                    else
                    {
                        Thread.Sleep(TimeSpan.FromTicks(100));
                    }
                }

                stream = null;
                if (tcpClient != null && tcpClient.Connected)
                {
                    tcpClient.Close();
                }
                Disconnected?.Invoke(this, EventArgs.Empty);
            }
            catch (Exception ex)
            {
                Logger.Error("Got an exceptions while listening to the server");
                Logger.Error(ex);
                Logger.Warn("Disconnecting");
                stream?.Close();
                stream = null;
                if (tcpClient != null && tcpClient.Connected)
                {
                    tcpClient.Close();
                    Disconnected?.Invoke(this, EventArgs.Empty);
                }
            }
        }
Ejemplo n.º 2
0
        private void ListenToClient(int index, ServerClient client)
        {
            Logger.Debug("Starting with listening to client on slot {index}", index);
            clients[index].IsListening = true;

            try
            {
                var buffer       = new byte[RxBufferSize];
                var bufferOffset = 0;
                while (client.IsConnected)
                {
                    if (client.Tcp?.Available >= 0)
                    {
                        var read             = client.Stream.Read(buffer, bufferOffset, buffer.Length - bufferOffset);
                        var packetCollection = packetsFactory.GetPackets(buffer, 0, read + bufferOffset);
                        foreach (var packet in packetCollection)
                        {
                            Logger.Debug("Received packet from {client}: {packet}", client.EndPoint, packet);
                            PacketReceived?.Invoke(this, new PacketReceivedArgs(packet, client));
                        }

                        var leftover = read + bufferOffset - packetCollection.BytesUsed;
                        if (leftover > 0)
                        {
                            Array.Copy(buffer, packetCollection.BytesUsed, buffer, 0, leftover);
                            bufferOffset = leftover;
                        }
                        else
                        {
                            bufferOffset = 0;
                        }
                    }
                    else
                    {
                        Thread.Sleep(TimeSpan.FromTicks(100));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Got an exception while listening to client {client}, disconnecting", client.EndPoint);
                Logger.Error(ex);
            }

            clients[index].IsListening = false;
            clients[index].Ping        = -1;
            clients[index].Tcp?.Close();
            clients[index].Tcp = null;

            Logger.Info("Client {client} on slot {slot} disconnected", client.EndPoint, index);
        }
Ejemplo n.º 3
0
        public void GetMultiplePackets()
        {
            var random  = Randomizer.CreateRandomizer();
            var packets = new List <TestPacket>();

            for (var i = 0; i < 10; i++)
            {
                packets.Add(new TestPacket(random.GetString()));
            }

            var bytes           = packets.SelectMany(x => x.Serialize()).ToArray();
            var returnedPackets = _factory.GetPackets(bytes, 0, bytes.Length).ToArray();

            Assert.NotNull(returnedPackets);
            Assert.NotZero(returnedPackets.Length);
            Assert.AreEqual(packets.Count, returnedPackets.Length);
            for (var i = 0; i < returnedPackets.Length; i++)
            {
                Assert.IsInstanceOf <TestPacket>(returnedPackets[i]);
                var p = (TestPacket)returnedPackets[i];
                Assert.AreEqual(packets[i].Data, p.Data);
            }
        }