コード例 #1
0
        /// <summary>
        ///     request asynchronous
        ///     ordered by "serial"
        /// </summary>
        /// <param name="serial"></param>
        /// <param name="pack"></param>
        /// <returns></returns>
        private async Task <RpcResult> RequestAsync(ushort serial, RpcPackage pack)
        {
            var tcs = new TaskCompletionSource <RpcResult>();

            //  call back in logic service
            Action <bool, byte[]> cb =
                (b, bytes) => GlobalVarPool.Ins.Service.Perform(() =>
            {
                tcs.SetResult(new RpcResult(b, bytes));
            });

            // callback pool
            if (!_requestPool.TryAdd(serial, cb))
            {
                Logger.Ins.Error("Request of serial {0} already processing!", serial);
                return(RpcResult.Failure);
            }

            // send
            var sendRet = await SendAsync(PiSerializer.Serialize(pack));

            if (!sendRet)
            {
                Action <bool, byte[]> _;
                _requestPool.TryRemove(serial, out _);
                _ = _ ?? cb;

                _(false, null);
            }

            return(await tcs.Task);
        }
コード例 #2
0
        private async Task <RpcResult> Response(bool success, byte[] data, ushort serial)
        {
            var pack = new RpcPackage
            {
                Type    = ERpc.Response,
                Serial  = serial,
                Success = success,
                Data    = data,
            };

            return(await SendAsync(PiSerializer.Serialize(pack)));
        }