Beispiel #1
0
        async Task NewUser()
        {
            string strError = "";

            UserDialog dlg = new UserDialog();

            dlg.Font = this.Font;
            dlg.ShowDialog(this);

            if (dlg.DialogResult == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            List <User> users = new List <User>();
            User        user  = dlg.UserItem;

            users.Add(user);

            this.EnableControls(false);
            try
            {
                MessageResult result = await this.Connection.SetUsers("create", users);

                if (result.Value == -1)
                {
                    strError = result.ErrorInfo;
                    goto ERROR1;
                }
            }
            catch (AggregateException ex)
            {
                strError = MessageConnection.GetExceptionText(ex);
                goto ERROR1;
            }
            catch (Exception ex)
            {
                strError = "NewUser() 出现异常: " + ex.Message;
                goto ERROR1;
            }
            finally
            {
                this.EnableControls(true);
            }

            // 更新显示
            ListViewItem item = ChangeItem(null, user);

            this.listView1.SelectedItems.Clear();
            item.Selected = true;

            this.Changed = true;
            return;

ERROR1:
            this.Invoke((Action)(() =>
            {
                MessageBox.Show(this, strError);
            }
                                 ));
        }
        // parameters:
        //      strName 连接的名字。如果要针对同一 dp2mserver 使用多根连接,可以用名字区分它们。如果不想区分,可以使用空
        //      autoConnect 是否自动连接
        //      waitConnecting  是否等待连接完成后再返回?
        public MessageConnection GetConnection(string url,
                                               string remoteName,
                                               bool autoConnect    = true,
                                               bool waitConnecting = true)
        {
            MessageConnection connection = null;

            foreach (MessageConnection current_connection in _connections)
            {
                if (current_connection.ServerUrl == url && current_connection.Name == remoteName)
                {
                    connection = current_connection;
                    goto FOUND;
                }
            }

            connection           = new MessageConnection();
            connection.ServerUrl = url;
            connection.Name      = remoteName;
            connection.Container = this;
            this._connections.Add(connection);

FOUND:
            if (autoConnect && connection.IsConnected == false)
            {
                Task task = connection.ConnectAsync(url);

                if (waitConnecting)
                {
                    task.Wait();
                }
            }

            return(connection);
        }
Beispiel #3
0
        async Task ListAllUsers()
        {
            string strError = "";

            this.EnableControls(false);
            try
            {
                this.listView1.Items.Clear();

                int start = 0;
                while (true)
                {
                    var result = await this.Connection.GetUsers("*", start, 100);

                    if (result.Value == -1)
                    {
                        strError = result.ErrorInfo;
                        goto ERROR1;
                    }
                    if (result.Users.Count == 0)
                    {
                        break;
                    }
                    start += result.Users.Count;

                    foreach (User user in result.Users)
                    {
#if NO
                        ListViewItem item = new ListViewItem();
                        item.SubItems.Add(user.userName);
                        item.SubItems.Add(user.department);
                        item.SubItems.Add(user.rights);

                        this.listView1.Items.Add(item);
#endif
                        ChangeItem(null, user);
                    }
                }
                return;
            }
            catch (AggregateException ex)
            {
                strError = MessageConnection.GetExceptionText(ex);
                goto ERROR1;
            }
            catch (Exception ex)
            {
                strError = "ListAllUsers() 出现异常: " + ex.Message;
            }
            finally
            {
                this.EnableControls(true);
            }
ERROR1:
            this.Invoke((Action)(() =>
            {
                MessageBox.Show(this, strError);
            }
                                 ));
        }
Beispiel #4
0
 // 触发登录事件
 public virtual void TriggerLogin(MessageConnection connection)
 {
     if (this.Login != null)
     {
         LoginEventArgs e = new LoginEventArgs();
         this.Login(connection, e);
     }
 }
 public void ReturnConnection(MessageConnection connection)
 {
     connection.UseCount--;
     if (connection.UseCount < 0)
     {
         throw new Exception("发生了 GetConnection() 和 ReturnConnection() 调用次数不配套的错误,UseCount 小于 0 了");
     }
 }
        public virtual void TriggerClosing(MessageConnection connection,
                                           ConnectionClosingEventArgs e)
        {
            ConnectionClosingEventHandler handler = this.ConnectionClosing;

            if (handler != null)
            {
                handler(connection, e);
            }
        }
        // 触发消息通知事件
        public virtual void TriggerAddMessage(MessageConnection connection,
                                              AddMessageEventArgs e)
        {
            AddMessageEventHandler handler = this.AddMessage;

            if (handler != null)
            {
                handler(connection, e);
            }
        }
        public virtual void TriggerCreated(MessageConnection connection,
                                           ConnectionCreatedEventArgs e)
        {
            ConnectionCreatedEventHandler handler = this.ConnectionCreated;

            if (handler != null)
            {
                handler(connection, e);
            }
        }
        // 从集合中寻找一条已有的通道,或者创建一条新通道
        MessageConnection PeekConnection(string url,
                                         string strName,
                                         bool incUseCount,
                                         out bool newCreate)
        {
            newCreate = false;
            MessageConnection connection = null;

            this._lock.EnterUpgradeableReadLock();
            try
            {
                foreach (MessageConnection current_connection in _connections)
                {
                    if (current_connection.ServerUrl == url &&
                        current_connection.Name == strName &&
                        (incUseCount == false || current_connection.UseCount == 0))     // 只有 inUseCount == true 时才要求 .UseCount == 0 入选
                    {
                        connection          = current_connection;
                        connection.LastTime = DateTime.Now;
                        if (incUseCount)
                        {
                            connection.UseCount++;
                        }
                        return(connection);
                    }
                }

                connection           = new MessageConnection();
                connection.ServerUrl = url;
                connection.Name      = strName;
                connection.LastTime  = DateTime.Now;
                connection.Container = this;
                this._lock.EnterWriteLock();
                try
                {
                    this._connections.Add(connection);
                    newCreate = true;
                    if (incUseCount)
                    {
                        connection.UseCount++;
                    }
                    return(connection);
                }
                finally
                {
                    this._lock.ExitWriteLock();
                }
            }
            finally
            {
                this._lock.ExitUpgradeableReadLock();
            }
        }
        public virtual void TriggerConnectionStateChange(MessageConnection connection,
                                                         string strAction)
        {
            ConnectionEventHandler handler = this.ConnectionStateChange;

            if (handler != null)
            {
                ConnectionEventArgs e = new ConnectionEventArgs();
                e.Action = strAction;
                handler(connection, e);
            }
        }
        public void RemoveConnection(MessageConnection connection)
        {
            // 触发 Closing 事件
            TriggerClosing(connection, new ConnectionClosingEventArgs());
            connection.CloseConnection();

            this._lock.EnterWriteLock();
            try
            {
                this._connections.Remove(connection);
            }
            finally
            {
                this._lock.ExitWriteLock();
            }
        }
Beispiel #12
0
        // parameters:
        //      strName 连接的名字。如果要针对同一 dp2mserver 使用多根连接,可以用名字区分它们。如果不想区分,可以使用空
        public Task <MessageConnection> GetConnectionAsync(string url,
                                                           string strName,
                                                           bool autoConnect = true)
        {
            MessageConnection connection = null;

            foreach (MessageConnection current_connection in _connections)
            {
                if (current_connection.ServerUrl == url && current_connection.Name == strName)
                {
                    connection = current_connection;
                    goto FOUND;
                }
            }

            connection           = new MessageConnection();
            connection.ServerUrl = url;
            connection.Name      = strName;
            connection.Container = this;
            this._connections.Add(connection);

FOUND:
            if (autoConnect && connection.IsConnected == false)
            {
                Task <MessageConnection> task = new Task <MessageConnection>(() =>
                {
                    connection.ConnectAsync(url).Wait();
                    return(connection);
                });
                task.Start();
                return(task);
            }

            {
                var task = new Task <MessageConnection>(() =>
                {
                    return(connection);
                });
                task.Start();
                return(task);
            }
        }
        // 触发登录事件
        public virtual void TriggerLogin(MessageConnection connection)
        {
            LoginEventHandler handler = this.Login;

            if (handler != null)
            {
                LoginEventArgs e = new LoginEventArgs();
                // 注: 在触发事件以前, MessageConnection 对象的 ServerUrl 和 Name 成员已经准备好了,可以利用
                //e.ServerUrl = connection.ServerUrl;
                //e.Name = connection.Name;
                handler(connection, e);
                if (string.IsNullOrEmpty(e.ErrorInfo) == false)
                {
                    throw new Exception(e.ErrorInfo);
                }

                connection.UserName   = e.UserName;
                connection.Password   = e.Password;
                connection.Parameters = e.Parameters;
            }
        }
Beispiel #14
0
 // 触发消息通知事件
 public virtual void TriggerAddMessage(MessageConnection connection,
     AddMessageEventArgs e)
 {
     AddMessageEventHandler handler = this.AddMessage;
     if (handler != null)
     {
         handler(connection, e);
     }
 }
Beispiel #15
0
        async Task DeleteUser()
        {
            string strError = "";

            if (this.listView1.SelectedItems.Count == 0)
            {
                strError = "尚未选定要删除的事项";
                goto ERROR1;
            }

            {
                DialogResult result = MessageBox.Show(this,
                                                      "确实要删除选定的 " + this.listView1.SelectedItems.Count.ToString() + " 个用户?",
                                                      "UserManageDialog",
                                                      MessageBoxButtons.YesNo,
                                                      MessageBoxIcon.Question,
                                                      MessageBoxDefaultButton.Button2);
                if (result != DialogResult.Yes)
                {
                    return;
                }
            }

            List <User> users = new List <User>();

            foreach (ListViewItem item in this.listView1.SelectedItems)
            {
                User user = (User)item.Tag;
                users.Add(user);
            }

            this.EnableControls(false);
            try
            {
                MessageResult result = await this.Connection.SetUsers("delete", users);

                if (result.Value == -1)
                {
                    strError = result.ErrorInfo;
                    goto ERROR1;
                }
            }
            catch (AggregateException ex)
            {
                strError = MessageConnection.GetExceptionText(ex);
                goto ERROR1;
            }
            catch (Exception ex)
            {
                strError = "DeleteUser() 出现异常: " + ex.Message;
                goto ERROR1;
            }
            finally
            {
                this.EnableControls(true);
            }

            foreach (ListViewItem item in this.listView1.SelectedItems)
            {
                this.listView1.Items.Remove(item);
            }

            this.Changed = true;
            return;

ERROR1:
            this.Invoke((Action)(() =>
            {
                MessageBox.Show(this, strError);
            }
                                 ));
        }
Beispiel #16
0
        async Task ModifyUser()
        {
            string strError = "";

            if (this.listView1.SelectedItems.Count == 0)
            {
                strError = "尚未选定要修改的事项";
                goto ERROR1;
            }

            ListViewItem item = this.listView1.SelectedItems[0];
            UserDialog   dlg  = new UserDialog();

            dlg.Font       = this.Font;
            dlg.ChangeMode = true;
            dlg.UserItem   = (User)item.Tag;
            dlg.ShowDialog(this);

            if (dlg.DialogResult == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }

            if (dlg.Changed == false && dlg.ChangePassword == false)
            {
                MessageBox.Show(this, "没有发生修改");
                return;
            }

            List <User> users = new List <User>();
            User        user  = dlg.UserItem;

            users.Add(user);

            this.EnableControls(false);
            try
            {
                if (dlg.Changed == true)
                {
                    MessageResult result = await this.Connection.SetUsers("change", users);

                    if (result.Value == -1)
                    {
                        strError = result.ErrorInfo;
                        // 如果这里返回出错仅仅是因为权限不够,还需要尝试继续执行后面的修改密码的操作
                        if (result.String != "Denied")
                        {
                            goto ERROR1;
                        }
                    }
                }

                if (dlg.ChangePassword)
                {
                    MessageResult result = await this.Connection.SetUsers("changePassword", users);

                    if (result.Value == -1)
                    {
                        if (string.IsNullOrEmpty(strError) == false)
                        {
                            strError += "; ";
                        }
                        strError += result.ErrorInfo;
                        goto ERROR1;
                    }
                }

                if (string.IsNullOrEmpty(strError) == false)
                {
                    goto ERROR1;
                }
            }
            catch (AggregateException ex)
            {
                strError = MessageConnection.GetExceptionText(ex);
                goto ERROR1;
            }
            catch (Exception ex)
            {
                strError = "ModifyUser() 出现异常: " + ex.Message;
                goto ERROR1;
            }
            finally
            {
                this.EnableControls(true);
            }

            ChangeItem(item, dlg.UserItem);

            this.Changed = true;
            return;

ERROR1:
            this.Invoke((Action)(() =>
            {
                MessageBox.Show(this, strError);
            }
                                 ));
        }
Beispiel #17
0
 public void DeleteConnection(MessageConnection channel)
 {
     this._connections.Remove(channel);
 }
        // parameters:
        //      strName 连接的名字。如果要针对同一 dp2mserver 使用多根连接,可以用名字区分它们。如果不想区分,可以使用空
        public Task <MessageConnection> GetConnectionTaskAsync(string url,
                                                               string strName,
                                                               bool autoConnect = true,
                                                               bool incUseCount = false)
        {
            MessageConnection connection = null;

#if NO
            this._lock.EnterUpgradeableReadLock();
            try
            {
                foreach (MessageConnection current_connection in _connections)
                {
                    if (current_connection.ServerUrl == url && current_connection.Name == strName)
                    {
                        connection          = current_connection;
                        connection.LastTime = DateTime.Now;
                        goto FOUND;
                    }
                }

                connection           = new MessageConnection();
                connection.ServerUrl = url;
                connection.Name      = strName;
                connection.LastTime  = DateTime.Now;
                connection.Container = this;
                this._lock.EnterWriteLock();
                try
                {
                    this._connections.Add(connection);
                }
                finally
                {
                    this._lock.ExitWriteLock();
                }
            }
            finally
            {
                this._lock.ExitUpgradeableReadLock();
            }
#endif
            bool newCreate = false;
            connection = PeekConnection(url,
                                        strName,
                                        incUseCount,
                                        out newCreate);

            if (newCreate == true)
            {
                // 触发 Created 事件
                this.TriggerCreated(connection, new ConnectionCreatedEventArgs());
            }

            // FOUND:
#if NO
            LoginEventArgs e = new LoginEventArgs();
            e.ServerUrl = url;
            e.Name      = strName;
            LoginEventHandler handler = this.Login;
            if (handler != null)
            {
                handler(connection, e); // TODO: 是否在真正连接前再触发?
            }
            if (string.IsNullOrEmpty(e.ErrorInfo) == false)
            {
                throw new Exception(e.ErrorInfo);
            }

            connection.UserName   = e.UserName;
            connection.Password   = e.Password;
            connection.Parameters = e.Parameters;
#endif

            if (autoConnect && connection.ConnectState == Microsoft.AspNet.SignalR.Client.ConnectionState.Disconnected)
            {
                return(Task.Run <MessageConnection>(async() =>
                {
                    // TODO: 建议抛出原有 Exception
                    MessageResult result = await connection.ConnectAsync();
                    if (result.Value == -1)
                    {
                        throw new MessageException(result.String, connection.UserName, result.ErrorInfo);
                    }
                    return connection;
                }));

#if NO
                Task <MessageConnection> task = new Task <MessageConnection>(async() =>
                {
                    // TODO: 建议抛出原有 Exception
                    MessageResult result = await connection.ConnectAsync();
                    if (result.Value == -1)
                    {
                        throw new Exception(result.ErrorInfo);
                    }
                    return(connection);
                });
                task.Start();
                return(task);
#endif
            }

#if NO
            {
                var task = new Task <MessageConnection>(() =>
                {
                    return(connection);
                });
                task.Start();
                return(task);
            }
#endif
            return(Task.Run(() =>
            {
                return connection;
            }));
        }
        public async Task <MessageConnection> GetConnectionAsyncLite(string url,
                                                                     string strName,
                                                                     bool autoConnect = true,
                                                                     bool incUseCount = false)
        {
            MessageConnection connection = null;

#if NO
            this._lock.EnterUpgradeableReadLock();
            try
            {
                foreach (MessageConnection current_connection in _connections)
                {
                    if (current_connection.ServerUrl == url && current_connection.Name == strName)
                    {
                        connection          = current_connection;
                        connection.LastTime = DateTime.Now;
                        goto FOUND;
                    }
                }

                connection           = new MessageConnection();
                connection.ServerUrl = url;
                connection.Name      = strName;
                connection.LastTime  = DateTime.Now;
                connection.Container = this;
                this._lock.EnterWriteLock();
                try
                {
                    this._connections.Add(connection);
                }
                finally
                {
                    this._lock.ExitWriteLock();
                }
            }
            finally
            {
                this._lock.ExitUpgradeableReadLock();
            }
#endif
            bool newCreate = false;
            connection = PeekConnection(url,
                                        strName,
                                        incUseCount,
                                        out newCreate);

            if (newCreate == true)
            {
                // 触发 Created 事件
                this.TriggerCreated(connection, new ConnectionCreatedEventArgs());
            }

            // FOUND:
            if (autoConnect && connection.ConnectState == Microsoft.AspNet.SignalR.Client.ConnectionState.Disconnected)
            {
                MessageResult result = await connection.ConnectAsync();

                if (result.Value == -1)
                {
                    throw new MessageException(result.String, connection.UserName, result.ErrorInfo);
                }
                return(connection);
            }

            return(connection);
        }