예제 #1
0
        SendForWait <TArgument, TResult>(
            Rpc <TArgument, TResult> rpc,
            bool autoResend = true,
            int timeout     = -1)
            where TArgument : Bean, new()
            where TResult : Bean, new()
        {
            if (timeout < 0)
            {
                timeout = RaftConfig.AppendEntriesTimeout + 1000;
            }

            var future = new TaskCompletionSource <Rpc <TArgument, TResult> >();

            if (autoResend)
            {
                var tmp = _Leader;
                if (null != tmp &&
                    tmp.IsHandshakeDone &&
                    rpc.Send(tmp.Socket, (p) => SendForWaitHandle(future, rpc), timeout))
                {
                    return(future);
                }

                rpc.ResponseHandle = (p) => SendForWaitHandle(future, rpc);
                rpc.Timeout        = timeout;
                Pending.TryAdd(rpc, rpc);
                return(future);
            }

            // 记录不要自动发送的请求。
            NotAutoResend[rpc] = rpc;
            if (false == rpc.Send(_Leader?.Socket,
                                  (p) =>
            {
                NotAutoResend.TryRemove(p, out var _);
                return(SendForWaitHandle(future, rpc));
            },
                                  timeout))
            {
                future.TrySetException(new Exception("Send Failed."));
            }
            ;
            return(future);
        }
예제 #2
0
        /// <summary>
        /// 发送Rpc请求。
        /// 如果 autoResend == true,那么总是返回成功。内部会在需要的时候重发请求。
        /// 如果 autoResend == false,那么返回结果表示是否成功。
        /// </summary>
        /// <typeparam name="TArgument"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="rpc"></param>
        /// <param name="handle"></param>
        /// <param name="autoResend"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public bool Send <TArgument, TResult>(
            Rpc <TArgument, TResult> rpc,
            Func <Protocol, int> handle,
            bool autoResend = true,
            int timeout     = -1)
            where TArgument : Bean, new()
            where TResult : Bean, new()
        {
            if (timeout < 0)
            {
                timeout = RaftConfig.AppendEntriesTimeout + 1000;
            }

            if (autoResend)
            {
                var tmp = _Leader;
                if (null != tmp &&
                    tmp.IsHandshakeDone &&
                    rpc.Send(tmp.Socket, handle, timeout))
                {
                    return(true);
                }

                rpc.ResponseHandle = handle;
                rpc.Timeout        = timeout;
                Pending.TryAdd(rpc, rpc);
                return(true);
            }

            // 记录不要自动发送的请求。
            NotAutoResend[rpc] = rpc;
            return(rpc.Send(_Leader?.Socket, (p) =>
            {
                NotAutoResend.TryRemove(p, out var _);
                return handle(p);
            },
                            timeout));
        }