// 获得一个连接 public static async Task <OpenConnectionResult> OpenConnectionAsync(string userNameAndUrl) { using (var releaser = await _limit.EnterAsync()) { P2PConnection connection = _table[userNameAndUrl] as P2PConnection; if (connection != null) { // 尝试连接一次 if (connection.IsDisconnected == true) { await EnsureConnectMessageServerAsync( connection, userNameAndUrl); } return(new OpenConnectionResult { Value = 0, Connection = connection }); } var accounts = MessageAccountForm.GetAccounts(); var account = FindAccount(accounts, userNameAndUrl); if (account == null) { return(new OpenConnectionResult { Value = -1, ErrorInfo = $"用户名 '{userNameAndUrl}' 没有找到" }); } connection = new P2PConnection(); _table[userNameAndUrl] = connection; var result = await connection.ConnectAsync(account.ServerUrl, account.UserName, account.Password, ""); if (result.Value == -1) { return new OpenConnectionResult { Value = -1, ErrorInfo = result.ErrorInfo, ErrorCode = result.ErrorCode } } ; return(new OpenConnectionResult { Value = 0, Connection = connection }); } }
void FillMyAccountList() { if (this.comboBox_query_myAccount.Items.Count == 0) { var accounts = MessageAccountForm.GetAccounts(); foreach (var account in accounts) { this.comboBox_query_myAccount.Items.Add(account.UserName + "@" + account.ServerUrl); } } }
// 确保连接到消息服务器 public static async Task EnsureConnectMessageServerAsync( P2PConnection connection, string userNameAndUrl) { if (connection.IsDisconnected) { var accounts = MessageAccountForm.GetAccounts(); var account = FindAccount(accounts, userNameAndUrl); if (account == null) { return; } var result = await connection.ConnectAsync(account.ServerUrl, account.UserName, account.Password, ""); } }