/// <summary> /// 返回FiberRw /// </summary> /// <param name="socketAsync"></param> /// <returns></returns> protected virtual async Task <IFiberRw?> GetFiberRw(ISockAsyncEventAsClient socketAsync) { if (is_use_ssl) //SSL Config { var result = decodeType switch { CompressType.None => await socketAsync.GetFiberRwSSL(Certificate !), CompressType.gzip => await socketAsync.GetFiberRwSSL(Certificate !, init : (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 }), CompressType.lz4 => await socketAsync.GetFiberRwSSL(Certificate !, init : (input, output) => { 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 }), _ => throw new NotImplementedException() }; if (result.IsError) { if (result.ErrMsg != null) { Log.Error(result.ErrMsg); } return(null); } else { return(result.FiberRw); } } else { return(decodeType switch { CompressType.None => await socketAsync.GetFiberRw(), CompressType.gzip => await socketAsync.GetFiberRw((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 }), CompressType.lz4 => await socketAsync.GetFiberRw((input, output) => { 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 }), _ => throw new NotImplementedException() });
private static async void Client_BinaryInput(ISocketClient client, ISockAsyncEventAsClient socketAsync) { var fiberRw = await socketAsync.GetFiberRw(); if (fiberRw == null) { client.ShutdownBoth(true); return; } client.SetConnected(); for (; ;) //我们可以在这里处理数据或者在上面 { try { await ReadCommand(fiberRw); } catch (Exception er) { Console.WriteLine(er); break; } } client.ShutdownBoth(true); }
private static async void Client_BinaryInput(ISocketClient client, ISockAsyncEventAsClient socketAsync) { var fiberRw = await socketAsync.GetFiberRw((input, output) => //我们在这地方使用GZIP 压缩发送流 解压读取流 { var gzip_input = new GZipStream(input, CompressionMode.Decompress, false); //将读取流解压 var gzip_output = new GZipStream(output, CompressionMode.Compress, false); //将输出流压缩 return(new GetFiberRwResult(gzip_input, gzip_output)); //这里顺序不要搞反 (input,output)的顺序 }); if (fiberRw == null) { client.ShutdownBoth(true); return; } client.SetConnected(); for (; ;) //我们可以在这里处理数据或者在上面 { try { await ReadCommand(fiberRw); } catch (Exception er) { Console.WriteLine(er); break; } } client.ShutdownBoth(true); }
private static async void Client_BinaryInput(ISocketClient client, ISockAsyncEventAsClient socketAsync) { //USE SSL+GZIP //var res = await socketAsync.GetFiberRwSSL<string>(null, "localhost", (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 //}); //if (res.IsError) //{ // Console.WriteLine(res.ErrMsg); // client.ShutdownBoth(true); // return; //} //var fiberRw = res.FiberRw; var fiberRw = await socketAsync.GetFiberRw(); client.SetConnected(); while (true) { try { await DataOnByLine(fiberRw); } catch { break; } } client.ShutdownBoth(true); }
/// <summary> /// 返回FiberRw /// </summary> /// <param name="socketAsync"></param> /// <returns></returns> protected virtual async Task <IFiberRw> GetFiberRw(ISockAsyncEventAsClient socketAsync) { if (is_use_ssl) //SSL Config { var result = await socketAsync.GetFiberRwSSL(Certificate); if (result.IsError) { Log.Error(result.ErrMsg); return(null); } else { return(result.FiberRw); } } else { return(await socketAsync.GetFiberRw()); } }
private static async void Client_BinaryInput(ISocketClient client, ISockAsyncEventAsClient socketAsync) { var fiberRw = await socketAsync.GetFiberRw((input,output) => //我们在这地方使用GZIP 压缩发送流 解压读取流 { var lz4_input = K4os.Compression.LZ4.AsyncStreams.LZ4Stream.Decode(input); var lz4_output = K4os.Compression.LZ4.AsyncStreams.LZ4Stream.Encode(output); return new GetFiberRwResult(lz4_input, lz4_output); //这里顺序不要搞反 (input,output)的顺序 }); if(fiberRw==null) { client.ShutdownBoth(true); return; } client.SetConnected(); for (; ; ) //我们可以在这里处理数据或者在上面 { try { await ReadCommand(fiberRw); } catch (Exception er) { Console.WriteLine(er); break; } } client.ShutdownBoth(true); }