コード例 #1
0
        private System.Windows.Forms.ToolStripButton BuildConnectionToolStripButton(string text, IConnectable connectable, Func <IAccesoUPVService, UserControls.ConnectionEventArgs, Task> connectHandler, Func <UserControls.ConnectionEventArgs, Task> disconnectHandler = null)
        {
            async void ConnectionHandler(object sender, EventArgs e)
            {
                System.Windows.Forms.ToolStripButton button = (sender as System.Windows.Forms.ToolStripButton);
                button.Enabled = false;
                try
                {
                    bool isChecked = button.Checked;

                    if (isChecked)
                    {
                        if (disconnectHandler != null)
                        {
                            await disconnectHandler.Invoke(UserControls.ConnectionEventArgs.CreateFrom(connectable, false));
                        }
                        else
                        {
                            await connectable.DisconnectAsync();
                        }
                    }
                    else
                    {
                        if (connectHandler != null)
                        {
                            await connectHandler.Invoke(_service, UserControls.ConnectionEventArgs.CreateFrom(connectable, true));
                        }
                        else
                        {
                            await connectable.ConnectAsync();
                        }

                        if (connectable is IOpenable openable)
                        {
                            openable.Open();
                        }
                    }

                    button.Checked = !isChecked;
                }
                catch (OperationCanceledException)
                {
                    // El usuario canceló algo, así que no importa
                }
                finally
                {
                    button.Enabled = true;
                }
            }

            System.Windows.Forms.ToolStripButton toolStripButton = new System.Windows.Forms.ToolStripButton(text, null, ConnectionHandler);

            void updateConnectionStatusHandler(object s, EventArgs e) => toolStripButton.Checked = connectable.IsConnected;

            updateHandlers.Add(updateConnectionStatusHandler);
            UpdateConnectionStatus += updateConnectionStatusHandler;

            return(toolStripButton);
        }
コード例 #2
0
        /// <summary>
        /// Connects to the provided <see cref="ip"/> with on the given <see cref="port"/>.
        /// </summary>
        /// <param name="connectable"></param>
        /// <param name="ip">The ip.</param>
        /// <param name="port">The port.</param>
        /// <returns>True if connection was successful.</returns>
        public static bool Connect(this IConnectable connectable, string ip, int port)
        {
            if (connectable == null)
            {
                throw new ArgumentNullException(nameof(connectable));
            }
            if (string.IsNullOrWhiteSpace(ip))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(ip));
            }
            if (port < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }

            return(connectable.ConnectAsync(ip, port).Result);
        }
コード例 #3
0
        /// <summary>
        /// Connects to the provided <see cref="address"/> with on the given <see cref="port"/>.
        /// </summary>
        /// <param name="connectable"></param>
        /// <param name="address">The ip.</param>
        /// <param name="port">The port.</param>
        /// <returns>True if connection was successful.</returns>
        public static Task <bool> ConnectAsync(this IConnectable connectable, IPAddress address, int port)
        {
            if (connectable == null)
            {
                throw new ArgumentNullException(nameof(connectable));
            }
            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }
            if (port < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }

            return(connectable.ConnectAsync(address.ToString(), port));
        }