Exemple #1
0
        // 获得一个连接
        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
                });
            }
        }
Exemple #2
0
        // 触发消息通知事件
        public virtual void TriggerAddMessage(P2PConnection connection,
                                              AddMessageEventArgs e)
        {
            AddMessageEventHandler handler = this.AddMessage;

            if (handler != null)
            {
                handler(connection, e);
            }
        }
Exemple #3
0
        // 确保连接到消息服务器
        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,
                                                           "");
            }
        }
Exemple #4
0
        // 继续填充余下的命中记录
        async Task <NormalResult> FillRestRecordsAsync(
            P2PConnection connection,
            string remoteUserName,
            string resultsetName,
            int start,
            CancellationToken token)
        {
            try
            {
                // string remoteUserName = this.comboBox_query_shelfAccount.Text;
                while (token.IsCancellationRequested == false)
                {
                    SearchRequest request = new SearchRequest(Guid.NewGuid().ToString(),
                                                              null,            // loginInfo,
                                                              "searchHistory", // "searchBiblio",
                                                              "",              // "dbNameList",
                                                              "!getResult",
                                                              "",              // this.comboBox_query_from.Text,
                                                              "",              // this.comboBox_query_matchStyle.Text,
                                                              resultsetName,
                                                              "json",
                                                              -1,
                                                              start,
                                                              10);
                    var result = await connection.SearchAsyncLite(remoteUserName,
                                                                  request,
                                                                  TimeSpan.FromSeconds(60),
                                                                  token);

                    if (result.ResultCount == -1)
                    {
                        return(new NormalResult
                        {
                            Value = -1,
                            ErrorInfo = result.ErrorInfo
                        });
                    }
                    if (result.ResultCount == 0)
                    {
                        // strError = "没有命中";
                        return(new NormalResult
                        {
                            Value = -1,
                            ErrorInfo = "检索没有命中结果。无法获得结果"
                        });
                    }
                    if (start >= result.ResultCount)
                    {
                        break;
                    }

                    FillRecords(result.Records, false, token);
                    start += result.Records.Count;
                    if (start >= result.ResultCount)
                    {
                        break;
                    }
                }

                return(new NormalResult());
            }
            catch (AggregateException ex)
            {
                return(new NormalResult
                {
                    Value = -1,
                    ErrorInfo = MessageConnection.GetExceptionText(ex)
                });
            }
            catch (Exception ex)
            {
                return(new NormalResult
                {
                    Value = -1,
                    ErrorInfo = ex.Message
                });
            }
        }