Exemplo n.º 1
0
        private async void tbConnect_Click(object sender, EventArgs e)
        {
            if (connected)
            {
                client.Close();
                tbConnect.Text = "Connect";
            }
            else
            {
                tbConnect.Text    = "Connecting...";
                tbConnect.Enabled = false;
                if (int.TryParse(tbPort.Text, out var port))
                {
                    bool res = false;

                    try
                    {
                        if (string.IsNullOrEmpty(tbAddress.Text))
                        {
                            res = await client.ConnectAsync(port, 0);
                        }
                        else
                        {
                            res = await client.ConnectAsync(tbAddress.Text, port, 0);
                        }
                    }
                    catch (Exception ex)
                    {
                    }

                    if (res)
                    {
                        connected      = true;
                        tbConnect.Text = "Connected";
                        logPanel.Info("Connected successfully.");
                    }
                    else
                    {
                        tbConnect.Text = "Connect";
                        logPanel.Error("Unable to connect server.");
                    }

                    tbConnect.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Given port is invalid");
                }
            }
        }
Exemplo n.º 2
0
        void Accept()
        {
            Task.Run((() =>
            {
                try
                {
                    while (socket.IsBound)
                    {
                        socket.Listen(1);
                        var sock = socket.Accept();

                        lock (this)
                        {
                            var client = new SGClient(sock);
                            client.AuthRequested += (s, e) =>
                            {
                                var args = new ClientAuthRequestedEventArgs(client, e.AuthToken, e.Response);
                                ClientAuthRequested?.Invoke(this, args);

                                e.Response = args.Response;
                                e.Reject = args.Reject || (args.Response != null && args.Response.IsError);
                                if (!e.Reject)
                                {
                                    client.Disconnected += (s1, e1) =>
                                    {
                                        lock (this)
                                        {
                                            clients.Remove(client);
                                        }

                                        ClientDisconnected?.Invoke(this, new ClientConnectionEventArgs(client, e1.Reason));
                                    };

                                    lock (this)
                                    {
                                        clients.Add(client);
                                    }

                                    ClientConnected?.Invoke(this, new ClientConnectionEventArgs(client));
                                }
                            };
                            client.Listen();
                            Task.Delay(AuthenticationTimeout).ContinueWith(t =>
                            {
                                if (!client.IsAuthorised)
                                {
                                    client.Close();
                                }
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    _ = ex;
                    Stopped?.Invoke(this, EventArgs.Empty);
                }
                finally
                {
                    foreach (var client in Clients)
                    {
                        try
                        {
                            client.Close();
                        }
                        catch (Exception ex)
                        {
                            _ = ex;
                        }
                    }
                    clients.Clear();
                }
            }));
        }