Exemple #1
0
        protected virtual async void BinaryInputHandler(ISockAsyncEventAsServer socketAsync)
        {
            var fiberRw = await GetFiberRw(socketAsync);


            if (fiberRw == null)
            {
                socketAsync.Disconnect();
                return;
            }


            for (; ;)
            {
                try
                {
                    if (!await DataOnByLine(fiberRw))
                    {
                        break;
                    }
                }
                catch (System.Net.Sockets.SocketException) { break; }
                catch (Exception er)
                {
                    this.Log.Error("read error", er);
                    break;
                }
            }

            socketAsync.Disconnect();
        }
Exemple #2
0
        static async void BinaryInputHandler(ISockAsyncEventAsServer socketAsync)
        {
            var fiberW = await socketAsync.GetFiberRw <UserInfo>((input, output) =>  //我们在这地方使用GZIP 压缩发送流 解压读取流
            {
                var gzip_input  = new GZipStream(input, CompressionMode.Decompress); //将读取流解压
                var gzip_output = new GZipStream(output, CompressionMode.Compress);  //将输出流压缩
                return(new GetFiberRwResult(gzip_input, gzip_output));               //这里顺序不要搞反 (input,output)的顺序
            });

            if (fiberW is null) //如果获取失败 那么断开连接
            {
                socketAsync.Disconnect();
                return;
            }

            for (; ;)  //循环读取处理数据表 类似于 协程
            {
                try
                {
                    await ReadCommand(fiberW);
                }
                catch (System.Net.Sockets.SocketException)
                {
                    break;
                }
                catch (Exception er)
                {
                    Console.WriteLine(er.ToString()); //出现异常 打印,并且结束循环,断开连接
                    break;
                }
            }

            socketAsync.Disconnect();
        }
Exemple #3
0
        static async void BinaryInputHandler(ISockAsyncEventAsServer socketAsync)
        {
            var(fiberW, errMsg) = await socketAsync.GetFiberRwSSL <UserInfo>(certificate); //获取一个异步基础流



            if (fiberW is null) //如果获取失败 那么断开连接
            {
                Console.WriteLine(errMsg);
                socketAsync.Disconnect();
                return;
            }

            for (; ;)  //循环读取处理数据表 类似于 协程
            {
                try
                {
                    await ReadCommand(fiberW);
                }
                catch (System.Net.Sockets.SocketException)
                {
                    break;
                }
                catch (Exception er)
                {
                    Console.WriteLine(er.ToString()); //出现异常 打印,并且结束循环,断开连接
                    break;
                }
            }

            socketAsync.Disconnect();
        }
Exemple #4
0
        /// <summary>
        /// 数据包输入
        /// </summary>
        /// <param name="data">输入数据</param>
        /// <param name="socketAsync">该数据包的通讯SOCKET</param>
        static async void BinaryInputHandler(ISockAsyncEventAsServer socketAsync)
        {
            //USE SSL+GZIP
            var(fiberRw, errMsg) = await socketAsync.GetFiberRwSSL <string>(certificate, (input, output) =>
            {
                //var gzip_input = new GZipStream(input, CompressionMode.Decompress, true);
                //var gzip_output = new GZipStream(output, CompressionMode.Compress, true);
                //return new GetFiberRwResult(gzip_input, gzip_output); //return gzip mode


                var lz4_input  = K4os.Compression.LZ4.AsyncStreams.LZ4Stream.Decode(input, leaveOpen: true);
                var lz4_output = K4os.Compression.LZ4.AsyncStreams.LZ4Stream.Encode(output, leaveOpen: true);
                return(new GetFiberRwResult(lz4_input, lz4_output));  //return lz4 mode
            });

            if (fiberRw is null)
            {
                Console.WriteLine(errMsg);
                socketAsync.Disconnect();
                return;
            }

            //var fiberRw = await socketAsync.GetFiberRw<string>((input, output) =>
            //{
            //    var lz4_input = K4os.Compression.LZ4.Streams.LZ4Stream.Decode(input, leaveOpen: true);
            //    var lz4_output = K4os.Compression.LZ4.Streams.LZ4Stream.Encode(output, leaveOpen: true);
            //    return new GetFiberRwResult(lz4_input, lz4_output); //return lz4 mode
            //});

            //var fiberRw = await socketAsync.GetFiberRw<string>();

            fiberRw.UserToken = "my is ttk";

            for (; ;)
            {
                try
                {
                    //提供2种数据 读取写入方式
                    ReadBytes readBytes = await new ReadBytes(fiberRw).Init();
                    DataOn(ref readBytes, fiberRw);

                    await DataOnByLine(fiberRw);
                }
                catch (System.IO.IOException)
                {
                    break;
                }
                catch (System.Net.Sockets.SocketException)
                {
                    break;
                }
                catch (Exception er)
                {
                    Console.WriteLine(er.ToString());
                    break;
                }
            }

            socketAsync.Disconnect();
        }
Exemple #5
0
        /// <summary>
        /// 数据包输入
        /// </summary>
        /// <param name="data">输入数据</param>
        /// <param name="socketAsync">该数据包的通讯SOCKET</param>
        static async void BinaryInputHandler(ISockAsyncEventAsServer socketAsync)
        {
            //USE SSL+GZIP
            var(fiberRw, errMsg) = await socketAsync.GetFiberRwSSL <string>(certificate, (input, output) =>
            {
                var gzip_input  = new GZipStream(input, CompressionMode.Decompress, true);
                var gzip_output = new GZipStream(output, CompressionMode.Compress, true);
                return(new GetFiberRwResult(gzip_input, gzip_output)); //return gzip mode
            });

            // var (fiberRw, errMsg) = await socketAsync.GetFiberRwSSL<string>(certificate);

            if (fiberRw is null)
            {
                Console.WriteLine(errMsg);
                socketAsync.Disconnect();
                return;
            }

            // var fiberRw = await socketAsync.GetFiberRw<string>();

            fiberRw.UserToken = "my is ttk";

            byte[] data = new byte[4096];

            for (; ;)
            {
                try
                {
                    var x = System.Diagnostics.Stopwatch.StartNew();

                    var memory = await fiberRw.ReadLine(data);

                    x.Stop();
                    var str = System.Text.Encoding.Default.GetString(memory.Span);
                    Console.WriteLine(x.ElapsedTicks + ":" + str);

                    fiberRw.Write("200\r\n", false);
                    await fiberRw.FlushAsync();
                }
                catch (System.Net.Sockets.SocketException)
                {
                    break;
                }
                catch (Exception er)
                {
                    Console.WriteLine(er.ToString());
                    break;
                }
            }

            socketAsync.Disconnect();
        }
Exemple #6
0
        static async void BinaryInputHandler(ISockAsyncEventAsServer socketAsync)
        {
            var(fiberW, errMsg) = await socketAsync.GetFiberRwSSL <UserInfo>(certificate, (input, output) => //在GZIP的基础上在通过SSL 加密
            {
                var gzip_input  = new GZipStream(input, CompressionMode.Decompress, true);                   //注意第三个参数
                var gzip_output = new GZipStream(output, CompressionMode.Compress, true);
                return(new GetFiberRwResult(gzip_input, gzip_output));
            });  //我们在这地方使用SSL加密


            if (fiberW is null) //如果获取失败 那么断开连接
            {
                Console.WriteLine(errMsg);
                socketAsync.Disconnect();
                return;
            }

            for (; ;)  //循环读取处理数据表 类似于 协程
            {
                try
                {
                    await ReadCommand(fiberW);
                }
                catch (System.Net.Sockets.SocketException)
                {
                    break;
                }
                catch (Exception er)
                {
                    Console.WriteLine(er.ToString()); //出现异常 打印,并且结束循环,断开连接
                    break;
                }
            }

            socketAsync.Disconnect();
        }
Exemple #7
0
        /// <summary>
        /// 数据包输入
        /// </summary>
        /// <param name="data">输入数据</param>
        /// <param name="socketAsync">该数据包的通讯SOCKET</param>
        static async void BinaryInputHandler(ISockAsyncEventAsServer socketAsync)
        {
            var(fiberRw, errMsg) = await socketAsync.GetFiberRwSSL <string>(certificate);

            Console.WriteLine("SSL OK");

            if (fiberRw is null)
            {
                Console.WriteLine(errMsg);
                socketAsync.Disconnect();
                return;
            }

            fiberRw.UserToken = "my is ttk";


            for (; ;)
            {
                try
                {
                    using (var data = await fiberRw.ReadMemory())
                    {
                        int?x = await fiberRw.ReadInt32();

                        Console.WriteLine(data.Value.Length);
                        Console.WriteLine(x);

                        fiberRw.Write("ok");
                        await fiberRw.Flush();
                    }

                    break;
                }
                catch (System.Net.Sockets.SocketException)
                {
                    break;
                }
                catch (Exception er)
                {
                    Console.WriteLine(er.ToString());
                    break;
                }
            }

            socketAsync.AcceptSocket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
        }
Exemple #8
0
        /// <summary>
        /// 数据包输入
        /// </summary>
        /// <param name="data">输入数据</param>
        /// <param name="socketAsync">该数据包的通讯SOCKET</param>
        static async void BinaryInputHandler(ISockAsyncEventAsServer socketAsync)
        {
            // var (fiberRw, errMsg) = await socketAsync.GetFiberRwSSL<string>(certificate);
            var(fiberRw, errMsg) = await socketAsync.GetFiberRwSSL <string>(async (stream) =>
            {
                var sslstream = new SslStream(stream, false);

                try
                {
                    await sslstream.AuthenticateAsServerAsync(certificate, false, SslProtocols.Tls, true);
                }
                catch (Exception er)
                {
                    Console.WriteLine(er.Message);
                    return(null);
                }

                return(sslstream);
            });

            if (fiberRw is null)
            {
                Console.WriteLine(errMsg);
                socketAsync.Disconnect();
                return;
            }


            Console.WriteLine("SSL OK");

            fiberRw.UserToken = "my is ttk";


            for (; ;)
            {
                try
                {
                    using (var data = await fiberRw.ReadMemory())
                    {
                        int?x = await fiberRw.ReadInt32();

                        Console.WriteLine(data.Value.Length);
                        Console.WriteLine(x);

                        fiberRw.Write("ok");
                        await fiberRw.FlushAsync();
                    }

                    break;
                }
                catch (System.Net.Sockets.SocketException)
                {
                    break;
                }
                catch (Exception er)
                {
                    Console.WriteLine(er.ToString());
                    break;
                }
            }

            socketAsync.AcceptSocket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
        }