示例#1
0
        /// <summary>
        /// Handles the <see cref="E:ServerConnectionDropped" /> event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="ServerConnectionEventArgs" /> instance containing the event data.</param>
        public void OnServerConnectionDropped(object sender, ServerConnectionEventArgs args)
        {
            try
            {
                args.ConnectionInfo = CurrentServerInfo;
                PurgeClient();

                ServerConnectionDropped?.Invoke(this, args);
            }
            catch
            {
                // ignored
            }
        }
示例#2
0
        /// <summary>
        /// Processes the packet stream.
        /// </summary>
        /// <returns>Task.</returns>
        private async Task ProcessPacketStream()
        {
            try
            {
                while (IsConnected && (OutgoingPackets.Count != 0 || TcpClient.Available != 0))
                {
                    while (IsConnected && TcpClient.Available != 0)
                    {
                        ReceivedPacket?.Invoke(this, new PacketEventArgs {
                            Packet = await Packet.ReadFromStreamAsync(TcpClientStream)
                        });

                        IncomingPacketReceieved = true;
                    }


                    if (!IsConnected || !CanSendPacket || OutgoingPackets.Count == 0)
                    {
                        continue;
                    }

                    await OutgoingPackets.Dequeue().WriteToStreamAsync(TcpClientStream);

                    OutgoingPacketCooldown.Restart();
                    IncomingPacketReceieved = false;

                    //// We've successfully sent or recieved data so the keepalive can be pushed back.
                    Keepalive.Reset();
                }
            }
            catch
            {
                // Lost connection with the server
                // No handling is necessary here as the TCPClient will set Connected to false.
                ServerConnectionDropped?.Invoke(this, new ServerConnectionEventArgs {
                    Message = "Connection to the server has been lost.", Status = ServerConnectionStatusEnum.Disconnected, Timestamp = DateTime.Now
                });
            }
        }