示例#1
0
        private async Task <byte[]> Call(string controller, string action, params object[] arguments)
        {
            int id      = idGenerator.NewId();
            var tSource = new TaskCompletionSource <byte[]>();

            if (!taskDict.TryAdd(id, tSource))
            {
                id = idGenerator.NewId();
                if (!taskDict.TryAdd(id, tSource))
                {
                    while (true)
                    {
                        id = idGenerator.NewId();
                        if (taskDict.TryAdd(id, tSource))
                        {
                            break;
                        }
                        await Task.Delay(100);
                    }
                }
            }
            RpcCallData transData = new RpcCallData()
            {
                Id = id, Controller = controller, Action = action, Arguments = new List <byte[]>()
            };
            MemoryStream ams = new MemoryStream();

            foreach (var arg in arguments)
            {
                Serializer.Serialize(ams, arg);
                byte[] argBytes = new byte[ams.Position];
                Buffer.BlockCopy(ams.GetBuffer(), 0, argBytes, 0, argBytes.Length);
                transData.Arguments.Add(argBytes);
                ams.Position = 0;
            }
            ams.Position = 0;
            ams.WriteByte(1);
            Serializer.Serialize(ams, transData);
            byte[] bytes = new byte[ams.Position];
            Buffer.BlockCopy(ams.GetBuffer(), 0, bytes, 0, bytes.Length);
            Session.SendAsync(bytes);
            ams.Dispose();
            var cancelsource = new CancellationTokenSource(invokeTimeOut);

            tSource.Task.Wait(cancelsource.Token);
            return(await tSource.Task);
        }