Пример #1
0
 private void OnWriteContentLength(WriteBytes write, HttpToken token)
 {
     write.Write(_headerContentLength.Data, 0, _headerContentLength.Length);
     token.ContentPostion = write.Allocate(LenData);
     write.Write(_2line, 0, 4);
     token.HttpHandlerPostion = (int)write.Stream.Position;
 }
Пример #2
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);
        }
Пример #3
0
        /// <summary>
        /// 发送结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        protected virtual Task SendResult(Result result)
        {
            if (FiberRw != null)
            {
                using (var wr = new WriteBytes(FiberRw))
                {
                    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);
                        foreach (var item in result.Arguments)
                        {
                            wr.Write(item);
                        }
                    }

                    return(wr.Flush());
                }
            }
            else
            {
                Log.Error("Send fail,is not fiber");
                return(Task.CompletedTask);
            }
        }
Пример #4
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);
            }
        }
Пример #5
0
        /// <summary>
        ///  重载可以让客户端使用
        /// </summary>
        /// <param name="cmdTag"></param>
        /// <param name="args"></param>
        protected override void SendAction(int cmdTag, object[] args)
        {
            if (!IsConnect)
            {
                if (!ConnectIt())
                {
                    throw new NetxException("not connect", ErrorType.Notconnect);
                }
            }

            if (FiberRw != null)
            {
                using (var wr = new WriteBytes(FiberRw))
                {
                    wr.WriteLen();
                    wr.Cmd(2400);
                    wr.Write((byte)0);
                    wr.Write(cmdTag);
                    wr.Write((long)-1);
                    wr.Write(args.Length);
                    foreach (var arg in args)
                    {
                        WriteObj(wr, arg);
                    }
                    wr.Flush();
                }
            }
            else
            {
                Log.Error("Send fail,is not fiber");
            }
        }
Пример #6
0
        /// <summary>
        /// 发送结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        protected virtual Task SendResult(long id, byte[][] arguments = null)
        {
            if (FiberRw != null)
            {
                using (var wr = new WriteBytes(FiberRw))
                {
                    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.Flush());
                }
            }
            else
            {
                Log.Error("Send fail,is not fiber");
                return(Task.CompletedTask);
            }
        }
Пример #7
0
        private static 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);
                writeBytes.Flush();
            }
        }
Пример #8
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);
            }
        }
Пример #9
0
        public async void queries(string queryString, IFiberRw <HttpToken> fiberRw, WriteBytes write)
        {
            int count = 1;

            if (!string.IsNullOrEmpty(queryString))
            {
                var values = queryString.Split('=');
                if (values.Length > 1)
                {
                    if (int.TryParse(values[1], out int size))
                    {
                        count = size;
                    }
                }
            }
            if (count > 500)
            {
                count = 500;
            }
            if (count < 1)
            {
                count = 1;
            }
            try
            {
                var data = await fiberRw.UserToken.Db.LoadMultipleQueriesRows(count);

                await JsonSerializer.NonGeneric.Utf8.SerializeAsync(data, write.Stream);
            }
            catch (Exception e_)
            {
                write.Write(e_.Message);
            }
            OnCompleted(fiberRw, write);
        }
Пример #10
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);
        }
Пример #11
0
        static void  DataOn(ReadBytes read, IFiberRw <string> fiberRw)
        {
            var cmd = read.ReadInt32();
            var p1  = read.ReadInt32();
            var p2  = read.ReadInt64();
            var p3  = read.ReadDouble();
            var p4  = read.ReadSingle();
            var p5  = read.ReadBoolean();
            var p6  = read.ReadBoolean();
            var p7  = read.ReadString();
            var p8  = read.ReadMemory();
            var p9  = read.ReadInt16();


            // var p10 = read.ReadObject<List<Guid>>();
            // read.Dispose();


            using (WriteBytes writeBytes = new WriteBytes(fiberRw))
            {
                writeBytes.WriteLen();
                writeBytes.Cmd(cmd);
                writeBytes.Write(p1);
                writeBytes.Write(p2);
                writeBytes.Write(p3);
                writeBytes.Write(p4);
                writeBytes.Write(p5);
                writeBytes.Write(p6);
                writeBytes.Write(p7);
                writeBytes.Write(p8);
                writeBytes.Write(p9);
                //writeBytes.Write(p10);
                writeBytes.Flush();
            }
        }
Пример #12
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 (FiberRw != null)
            {
                using (var wr = new WriteBytes(FiberRw))
                {
                    Task <int> WSend()
                    {
                        wr.WriteLen();
                        wr.Cmd(2500);
                        wr.Write(id);
                        wr.Write(true);
                        wr.Write((int)errorType);
                        wr.Write(msg);
                        return(wr.Flush());
                    }

                    await await FiberRw.Sync.Ask(WSend);
                }
            }
            else
            {
                Log.Error("Send fail,is not fiber");
            }
        }
Пример #13
0
 public void Json(IFiberRw<HttpToken> fiberRw,ref WriteBytes write)
 {
     JsonMessage jsonMessage = default(JsonMessage);
     jsonMessage.message = "Hello, World!";          
      JsonFormatter.SerializeObject(jsonMessage,write.Stream,System.Text.Encoding.UTF8);
     OnCompleted(fiberRw, write);
 }
Пример #14
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 <int> WSend()
                    {
                        wr.WriteLen();
                        wr.Cmd(2500);
                        wr.Write(id);
                        wr.Write(false);
                        wr.Write(1);
                        wr.Write(SerializationPacker.PackSingleObject(argument));
                        return(wr.Flush());
                    }

                    await await FiberRw.Sync.Ask(WSend);
                }
            }
            else
            {
                Log.Error("Send fail,is not fiber");
            }
        }
Пример #15
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 <int> 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.Flush());
                }

                await await FiberRw.Sync.Ask(WSend);
            }
            else
            {
                Log.Error("Send fail,is not fiber");
            }
        }
Пример #16
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);
        }
Пример #17
0
        private void OnWriteHeader(ref WriteBytes write)
        {
            write.Write(_httpsuccess.Data, 0, _httpsuccess.Length);
            write.Write(_headerServer.Data, 0, _headerServer.Length);
            ArraySegment <byte> date = GMTDate.Default.DATE;

            write.Write(date.Array, date.Offset, date.Count);
        }
Пример #18
0
        public void Json(IFiberRw <HttpToken> fiberRw, ref WriteBytes write)
        {
            JsonMessage jsonMessage = default(JsonMessage);

            jsonMessage.message = "Hello, World!";
            JsonSerializer.NonGeneric.Utf8.SerializeAsync(jsonMessage, write.Stream);
            OnCompleted(fiberRw, write);
        }
Пример #19
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);
        }
Пример #20
0
        public async void Plaintext(IFiberRw <HttpToken> fiberRw, WriteBytes write)
        {
            write.Write(_result_plaintext.Data, 0, _result_plaintext.Length);
            var length = write.Stream.Length - fiberRw.UserToken.HttpHandlerPostion;

            write.Stream.Position = fiberRw.UserToken.ContentPostion.postion;
            write.Write(length.ToString(), false);
            await write.Flush();
        }
Пример #21
0
        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();
        }
Пример #22
0
        public void Default(IFiberRw <HttpToken> fiberRw, ref WriteBytes write)
        {
            write.Write("<b> zysocket server</b><hr/>");
            write.Write($"error not found!");

            var length = write.Stream.Length - fiberRw.UserToken.HttpHandlerPostion;

            write.Stream.Position = fiberRw.UserToken.ContentPostion.postion;
            write.Write(length.ToString(), false);
            write.Flush();
        }
Пример #23
0
        public async void Json(IFiberRw <HttpToken> fiberRw, WriteBytes write)
        {
            JsonMessage jsonMessage = default(JsonMessage);

            jsonMessage.message = "Hello, World!";
            JsonFormatter.SerializeObject(jsonMessage, write.Stream, System.Text.Encoding.UTF8);
            var length = write.Stream.Length - fiberRw.UserToken.HttpHandlerPostion;

            write.Stream.Position = fiberRw.UserToken.ContentPostion.postion;
            write.Write(length.ToString(), false);
            await write.Flush();
        }
Пример #24
0
        private async void OnCompleted(IFiberRw <HttpToken> fiberRw, WriteBytes write)
        {
            Task <int> WSend()
            {
                var length = write.Stream.Length - fiberRw.UserToken.HttpHandlerPostion;

                write.Stream.Position = fiberRw.UserToken.ContentPostion.postion;
                write.Write(length.ToString(), false);
                write.Flush(false);
                return(fiberRw.Flush());
            }

            await await fiberRw.Sync.Ask(WSend);
        }
Пример #25
0
        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);
        }
Пример #26
0
        public async void db(IFiberRw <HttpToken> fiberRw, WriteBytes write)
        {
            try
            {
                var data = await fiberRw.UserToken.Db.LoadSingleQueryRow();

                await JsonFormatter.SerializeObjectAsync(data, write.Stream, System.Text.Encoding.UTF8);
            }
            catch (Exception e_)
            {
                write.Write(e_.Message);
            }

            OnCompleted(fiberRw, write);
        }
Пример #27
0
        public async void db(IFiberRw <HttpToken> fiberRw, WriteBytes write)
        {
            try
            {
                var data = await fiberRw.UserToken.Db.LoadSingleQueryRow();

                await JsonSerializer.NonGeneric.Utf8.SerializeAsync(data, write.Stream);
            }
            catch (Exception e_)
            {
                write.Write(e_.Message);
            }

            OnCompleted(fiberRw, write);
        }
Пример #28
0
        async void BinaryInputHandler(ISockAsyncEvent socketAsync)
        {
            var fiberRw = await socketAsync.GetFiberRw <HttpToken>();

            fiberRw.UserToken = new HttpToken
            {
                Db = new RawDb(new ConcurrentRandom(), Npgsql.NpgsqlFactory.Instance)
            };

            using var data_r = fiberRw.GetMemory(1024);
            using var write  = new WriteBytes(fiberRw);
            for (; ;)
            {
                await HttpHandler.Receive(fiberRw, data_r.Memory, write);
            }
        }
Пример #29
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 <int> 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.Flush());
                    }

                    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");
            }
        }
Пример #30
0
 /// <summary>
 /// 发送Session
 /// </summary>
 /// <returns></returns>
 public virtual Task SendSessionId()
 {
     if (FiberRw != null)
     {
         using (var wr = new WriteBytes(FiberRw))
         {
             wr.WriteLen();
             wr.Cmd(2000);
             wr.Write(SessionId);
             return(wr.Flush());
         }
     }
     else
     {
         Log.Error("Send fail,is not fiber");
         return(Task.CompletedTask);
     }
 }