Exemplo n.º 1
0
        private async Task ProcessPacketStream()
        {
            try
            {
                while (IsConnected && (OutgoingPackets.Count != 0 || TcpClient.Available != 0))
                {
                    // Process incoming packets
                    while (IsConnected && TcpClient.Available != 0)
                    {
                        if (ReceivedPacket != null)
                        {
                            ReceivedPacket(this, new PacketEventArgs()
                            {
                                Packet = await Packet.ReadFromStreamAsync(TcpClientStream)
                            });
                        }

                        IncomingPacketReceieved = true;
                        //CanSendPacket = true;
                    }


                    // Send an outgoing packet
                    if (IsConnected && CanSendPacket && OutgoingPackets.Count != 0)
                    {
                        await OutgoingPackets.Dequeue().WriteToStreamAsync(TcpClientStream);

                        OutgoingPacketCooldown.Restart();
                        IncomingPacketReceieved = false;
                        //CanSendPacket = 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.
                if (ServerConnectionDropped != null)
                {
                    ServerConnectionDropped(this, new ServerConnectionEventArgs()
                    {
                        Message = "Connection to the server has been lost.", Status = ServerConnectionStatus.Disconnected, Timestamp = DateTime.Now
                    });
                }
            }
        }
Exemplo n.º 2
0
        public async Task <bool> Connect(string hostname, int port)
        {
            if (ServerConnectionStarting != null)
            {
                ServerConnectionStarting(this, new ServerConnectionEventArgs {
                    Message = "Connecting to " + hostname + ":" + port.ToString() + "...", Status = ServerConnectionStatus.Connecting, Timestamp = DateTime.Now
                });
            }

            try
            {
                TcpClient         = new TcpClient();
                TcpClient.NoDelay = true;
                await TcpClient.ConnectAsync(hostname, port);
            }
            catch
            {
                if (ServerConnectionFailed != null)
                {
                    ServerConnectionFailed(this, new ServerConnectionEventArgs {
                        Message = "Failed to connect!  Make sure the server is running and that your hostname and port are correct.", Status = ServerConnectionStatus.Disconnected, Timestamp = DateTime.Now
                    });
                }
            }
            if (IsConnected)
            {
                TcpClientStream = TcpClient.GetStream();

                if (ServerConnectionSucceeded != null)
                {
                    ServerConnectionSucceeded(this, new ServerConnectionEventArgs {
                        Message = "Successfully connected.", Status = ServerConnectionStatus.Connected, Timestamp = DateTime.Now
                    });
                }

                Keepalive.Reset();
                return(true);
            }
            else
            {
                return(false);
            }
        }