コード例 #1
0
        /// <summary>
        /// 发送结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        protected virtual async Task SendResult(long id, object argument)
        {
            if (IWrite == null)
            {
                throw new NullReferenceException("IWrite is null!");
            }

            var packbuffer = SerializationPacker.PackSingleObject(argument);

            Task WSend()
            {
                if (mode == 0)
                {
                    IWrite !.Write(2500);
                    IWrite !.Write(id);
                    IWrite !.Write(false);
                    IWrite !.Write(1);
                    IWrite !.Write(packbuffer);
                    return(IWrite !.FlushAsync());
                }
                else
                {
                    using var buffer = new WriteBytes(IWrite);
                    buffer.WriteLen();
                    buffer.Cmd(2500);
                    buffer.Write(id);
                    buffer.Write(false);
                    buffer.Write(1);
                    buffer.Write(packbuffer);
                    return(buffer.FlushAsync());
                }
            }

            await await IWrite.Sync.Ask(WSend);
        }
コード例 #2
0
        protected virtual async Task GetSessionId()
        {
            if (IWrite == null)
            {
                throw new NullReferenceException("IWrite is null!");
            }

            Task WSend()
            {
                if (mode == 0)
                {
                    IWrite !.Write(2000);

                    return(IWrite !.FlushAsync());
                }
                else
                {
                    using var buffer = new WriteBytes(IWrite);
                    buffer.WriteLen();
                    buffer.Cmd(2000);

                    return(buffer.FlushAsync());
                }
            }

            await await IWrite.Sync.Ask(WSend);
        }
コード例 #3
0
        /// <summary>
        /// 发送错误
        /// </summary>
        /// <param name="id"></param>
        /// <param name="msg"></param>
        /// <param name="errorType"></param>
        /// <returns></returns>
        protected virtual async Task SendError(long id, string msg, ErrorType errorType)
        {
            if (IWrite == null)
            {
                throw new NullReferenceException("IWrite is null!");
            }

            Task WSend()
            {
                if (mode == 0)
                {
                    IWrite !.Write(2500);
                    IWrite !.Write(id);
                    IWrite !.Write(true);
                    IWrite !.Write((int)errorType);
                    IWrite !.Write(msg);

                    return(IWrite !.FlushAsync());
                }
                else
                {
                    using var buffer = new WriteBytes(IWrite);
                    buffer.WriteLen();
                    buffer.Cmd(2500);
                    buffer.Write(id);
                    buffer.Write(true);
                    buffer.Write((int)errorType);
                    buffer.Write(msg);

                    return(buffer.FlushAsync());
                }
            }

            await await IWrite.Sync.Ask(WSend);
        }
コード例 #4
0
        /// <summary>
        /// 发送Session
        /// </summary>
        /// <returns></returns>
        public virtual async Task SendSessionId()
        {
            try
            {
                if (FiberRw != null)
                {
                    using var wr = new WriteBytes(FiberRw);
                    Task WSend()
                    {
                        wr.WriteLen();
                        wr.Cmd(2000);
                        wr.Write(SessionId);

                        return(wr.FlushAsync());
                    }

                    await await FiberRw.Sync.Ask(WSend);
                }
                else
                {
                    Log.Error("Send fail,is not fiber");
                }
            }
            catch (Exception er)
            {
                Log.Error("SendSessionId:", er);
            }
        }
コード例 #5
0
        /// <summary>
        /// 发送结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        protected virtual async Task SendResult(long id, byte[][]?arguments = null)
        {
            if (FiberRw != null)
            {
                using var wr = new WriteBytes(FiberRw);
                Task WSend()
                {
                    wr.WriteLen();
                    wr.Cmd(2500);
                    wr.Write(id);
                    wr.Write(false);
                    if (arguments is null)
                    {
                        wr.Write(0);
                    }
                    else
                    {
                        wr.Write(arguments.Length);
                        foreach (var item in arguments)
                        {
                            wr.Write(item);
                        }
                    }
                    return(wr.FlushAsync());
                }

                await await FiberRw.Sync.Ask(WSend);
            }
            else
            {
                Log.Error("Send fail,is not fiber");
            }
        }
コード例 #6
0
        /// <summary>
        /// 发送错误
        /// </summary>
        /// <param name="id"></param>
        /// <param name="msg"></param>
        /// <param name="errorType"></param>
        /// <returns></returns>
        protected virtual async void SendError(long id, string msg, ErrorType errorType)
        {
            try
            {
                if (FiberRw != null)
                {
                    using var wr = new WriteBytes(FiberRw);
                    Task WSend()
                    {
                        wr.WriteLen();
                        wr.Cmd(2500);
                        wr.Write(id);
                        wr.Write(true);
                        wr.Write((int)errorType);
                        wr.Write(msg);
                        return(wr.FlushAsync());
                    }

                    await await FiberRw.Sync.Ask(WSend);
                }
                else
                {
                    Log.Error("Send fail,is not fiber");
                }
            }
            catch (Exception er)
            {
                Log.Error("SendError", er);
            }
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: luyikk/ZYSOCKET-V
        private static async void SendTest(IFiberRw fiberRw)
        {
            using (WriteBytes writeBytes = new WriteBytes(fiberRw))
            {
                writeBytes.WriteLen();
                writeBytes.Cmd(1001);
                writeBytes.Write(2);
                writeBytes.Write(5L);
                writeBytes.Write(5.5);
                writeBytes.Write(4.3f);
                writeBytes.Write(true);
                writeBytes.Write(false);
                writeBytes.Write("AAAAAAAAAAAAAA");
                writeBytes.Write("BBBBBBBBBBBBBBBB");
                writeBytes.Write((short)111);

                List <Guid> guids = new List <Guid>();
                for (int i = 0; i < 10000; i++)
                {
                    guids.Add(Guid.NewGuid());
                }
                writeBytes.Write(guids);
                await writeBytes.FlushAsync();
            }
        }
コード例 #8
0
        /// <summary>
        /// 发送结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        protected virtual async Task SendResult(long id, byte[][]?arguments = null)
        {
            if (IWrite == null)
            {
                throw new NullReferenceException("IWrite is null!");
            }

            Task WSend()
            {
                if (mode == 0)
                {
                    IWrite !.Write(2500);
                    IWrite !.Write(id);
                    IWrite !.Write(false);
                    if (arguments is null)
                    {
                        IWrite !.Write(0);
                    }
                    else
                    {
                        IWrite !.Write(arguments.Length);
                        foreach (var item in arguments)
                        {
                            IWrite !.Write(item);
                        }
                    }


                    return(IWrite !.FlushAsync());
                }
                else
                {
                    using var buffer = new WriteBytes(IWrite);
                    buffer.WriteLen();
                    buffer.Cmd(2500);
                    buffer.Write(id);
                    buffer.Write(false);
                    if (arguments is null)
                    {
                        buffer.Write(0);
                    }
                    else
                    {
                        buffer.Write(arguments.Length);
                        foreach (var item in arguments)
                        {
                            buffer.Write(item);
                        }
                    }


                    return(buffer.FlushAsync());
                }
            }

            await await IWrite.Sync.Ask(WSend);
        }
コード例 #9
0
ファイル: HttpHandler.cs プロジェクト: luyikk/ZYSOCKET-V
        private async void OnCompleted(IFiberRw <HttpToken> fiberRw, WriteBytes write)
        {
            var token  = fiberRw.UserToken;
            var length = write.Stream.Length - token.HttpHandlerPostion;

            write.Stream.Position = token.ContentPostion.postion;
            write.Write(length.ToString(), false);
            await write.FlushAsync();
        }
コード例 #10
0
ファイル: ServiceBase.cs プロジェクト: luyikk/NetX
        protected async Task SendToMessage(IFiberRw fiberRw, string msg)
        {
            using var wrtokenerr = new WriteBytes(fiberRw);
            wrtokenerr.WriteLen();
            wrtokenerr.Cmd(1001);
            wrtokenerr.Write(msg);

            Task WSend()
            {
                return(wrtokenerr.FlushAsync());
            }

            await await fiberRw.Sync.Ask(WSend);
        }
コード例 #11
0
        /// <summary>
        ///  重载可以让客户端使用
        /// </summary>
        /// <param name="cmdTag"></param>
        /// <param name="Id"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        protected override async Task SendAsyncAction(int cmdTag, long Id, object[] args)
        {
            if (!IsConnect)
            {
                if (!ConnectIt())
                {
                    throw new NetxException("not connect", ErrorType.Notconnect);
                }
            }

            if (FiberRw != null)
            {
                using var wr = new WriteBytes(FiberRw);
                var result = GetResult(AddAsyncResult(Id));
                Task WSend()
                {
                    wr.WriteLen();
                    wr.Cmd(2400);
                    wr.Write((byte)1);
                    wr.Write(cmdTag);
                    wr.Write(Id);
                    wr.Write(args.Length);
                    foreach (var arg in args)
                    {
                        WriteObj(wr, arg);
                    }
                    return(wr.FlushAsync());
                }

                await await FiberRw.Sync.Ask(WSend);

                var res = await result;

                if (res.IsError)
                {
                    throw new NetxException(res.ErrorMsg, res.ErrorId);
                }
            }
            else
            {
                Log.Error("Send fail,is not fiber");
            }
        }
コード例 #12
0
        /// <summary>
        /// 重载可以让客户端使用
        /// </summary>
        /// <param name="cmdTag"></param>
        /// <param name="Id"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        protected override async Task <IResult> AsyncFuncSend(int cmdTag, long Id, object[] args)
        {
            if (!IsConnect)
            {
                if (!ConnectIt())
                {
                    throw new NetxException("not connect", ErrorType.Notconnect);
                }
            }

            if (FiberRw != null)
            {
                //数据包格式为0000 0 0000  00000000 0000 .....
                //功能len(int)  标识(byte) 函数标识(int) 当前ids(long) 参数长度(int) 每个参数序列化后的数组
                using var wr = new WriteBytes(FiberRw);
                Task WSend()
                {
                    wr.WriteLen();
                    wr.Cmd(2400);
                    wr.Write((byte)2);
                    wr.Write(cmdTag);
                    wr.Write(Id);
                    wr.Write(args.Length);
                    foreach (var arg in args)
                    {
                        WriteObj(wr, arg);
                    }
                    return(wr.FlushAsync());
                }

                var result = GetResult(AddAsyncResult(Id));
                await await FiberRw.Sync.Ask(WSend);

                return(await result);
            }
            else
            {
                throw new NullReferenceException("FiberRw is null!");
            }
        }
コード例 #13
0
        /// <summary>
        /// 发送结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        protected virtual async Task SendResult(Result result)
        {
            if (FiberRw != null)
            {
                using var wr = new WriteBytes(FiberRw);
                Task WSend()
                {
                    wr.WriteLen(); //为了兼容其他框架和其他的语言,还是发个长度吧
                    wr.Cmd(2500);
                    wr.Write(result.Id);

                    if (result.IsError)
                    {
                        wr.Write(true);
                        wr.Write(result.ErrorId);
                        wr.Write(result.ErrorMsg ?? "");
                    }
                    else
                    {
                        wr.Write(false);
                        wr.Write(result.Arguments?.Count ?? 0);
                        if (result.Arguments != null)
                        {
                            foreach (var item in result.Arguments)
                            {
                                wr.Write(item);
                            }
                        }
                    }
                    return(wr.FlushAsync());
                }

                await await FiberRw.Sync.Ask(WSend);
            }
            else
            {
                Log.Error("Send fail,is not fiber");
            }
        }
コード例 #14
0
ファイル: ServiceBase.cs プロジェクト: luyikk/NetX
        protected async void SendToKeyError(IFiberRw fiberRw, bool iserr = false, string msg = "success")
        {
            try
            {
                using var wrtokenerr = new WriteBytes(fiberRw);
                wrtokenerr.WriteLen();
                wrtokenerr.Cmd(1000);
                wrtokenerr.Write(iserr);
                wrtokenerr.Write(msg);

                Task WSend()
                {
                    return(wrtokenerr.FlushAsync());
                }

                await await fiberRw.Sync.Ask(WSend);
            }
            catch (Exception er)
            {
                Log.Error("SendToKeyError:", er);
            }
        }
コード例 #15
0
        /// <summary>
        /// 发送结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        protected virtual async Task SendResult(long id, object argument)
        {
            if (FiberRw != null)
            {
                using var wr = new WriteBytes(FiberRw);
                Task WSend()
                {
                    wr.WriteLen();
                    wr.Cmd(2500);
                    wr.Write(id);
                    wr.Write(false);
                    wr.Write(1);
                    wr.Write(SerializationPacker.PackSingleObject(argument));
                    return(wr.FlushAsync());
                }

                await await FiberRw.Sync.Ask(WSend);
            }
            else
            {
                Log.Error("Send fail,is not fiber");
            }
        }
コード例 #16
0
        /// <summary>
        /// 发送结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        protected virtual async Task SendResult(Result result)
        {
            if (IWrite == null)
            {
                throw new NullReferenceException("IWrite is null!");
            }

            Task WSend()
            {
                if (mode == 0)
                {
                    IWrite !.Write(2500);
                    IWrite !.Write(result.Id);

                    if (result.IsError)
                    {
                        IWrite !.Write(true);
                        IWrite !.Write(result.ErrorId);
                        IWrite !.Write(result.ErrorMsg ?? "");
                    }
                    else
                    {
                        IWrite !.Write(false);
                        IWrite !.Write(result.Arguments?.Count ?? 0);
                        if (result.Arguments != null)
                        {
                            foreach (var item in result.Arguments)
                            {
                                IWrite !.Write(item);
                            }
                        }
                    }


                    return(IWrite !.FlushAsync());
                }
                else
                {
                    using var buffer = new WriteBytes(IWrite);
                    buffer.WriteLen();
                    buffer.Cmd(2500);
                    if (result.IsError)
                    {
                        buffer.Write(true);
                        buffer.Write(result.ErrorId);
                        buffer.Write(result.ErrorMsg ?? "");
                    }
                    else
                    {
                        buffer.Write(false);
                        buffer.Write(result.Arguments?.Count ?? 0);
                        if (result.Arguments != null)
                        {
                            foreach (var item in result.Arguments)
                            {
                                buffer.Write(item);
                            }
                        }
                    }


                    return(buffer.FlushAsync());
                }
            }

            await await IWrite.Sync.Ask(WSend);
        }