mirror of
https://github.com/RRQM/TouchSocket.git
synced 2025-12-20 02:16:42 +08:00
更新文档
This commit is contained in:
@@ -143,7 +143,7 @@ namespace AdapterConsoleApp
|
||||
{
|
||||
//使用拼接模式发送,在发送流式数据之前
|
||||
|
||||
int dataLen = 0;
|
||||
var dataLen = 0;
|
||||
foreach (var item in transferBytes)
|
||||
{
|
||||
dataLen += item.Count;
|
||||
@@ -238,7 +238,7 @@ namespace AdapterConsoleApp
|
||||
}
|
||||
}
|
||||
|
||||
class MyClass : IRequestInfo
|
||||
internal class MyClass : IRequestInfo
|
||||
{
|
||||
public OrderType OrderType { get; set; }
|
||||
public DataType DataType { get; set; }
|
||||
@@ -246,13 +246,13 @@ namespace AdapterConsoleApp
|
||||
public byte[] Data { get; set; }
|
||||
}
|
||||
|
||||
enum DataType : byte
|
||||
internal enum DataType : byte
|
||||
{
|
||||
Down = 0,
|
||||
Up = 1
|
||||
}
|
||||
|
||||
enum OrderType : byte
|
||||
internal enum OrderType : byte
|
||||
{
|
||||
Hold = 0,
|
||||
Go = 1
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using TouchSocket.Core;
|
||||
using TouchSocket.Sockets;
|
||||
|
||||
namespace AdapterTesterConsoleApp
|
||||
{
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace CustomAdapterConsoleApp
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
var service = CreateService();
|
||||
var client = CreateClient();
|
||||
@@ -27,7 +27,7 @@ namespace CustomAdapterConsoleApp
|
||||
//构建发送数据
|
||||
using (var byteBlock = new ByteBlock(1024))
|
||||
{
|
||||
byteBlock.Write((byte)(myRequestInfo.Body.Length+2));//先写长度,因为该长度还包含数据类型和指令类型,所以+2
|
||||
byteBlock.Write((byte)(myRequestInfo.Body.Length + 2));//先写长度,因为该长度还包含数据类型和指令类型,所以+2
|
||||
byteBlock.Write((byte)myRequestInfo.DataType);//然后数据类型
|
||||
byteBlock.Write((byte)myRequestInfo.OrderType);//然后指令类型
|
||||
byteBlock.Write(myRequestInfo.Body);//再写数据
|
||||
@@ -37,7 +37,7 @@ namespace CustomAdapterConsoleApp
|
||||
}
|
||||
}
|
||||
|
||||
static TcpClient CreateClient()
|
||||
private static TcpClient CreateClient()
|
||||
{
|
||||
var client = new TcpClient();
|
||||
//载入配置
|
||||
@@ -54,7 +54,7 @@ namespace CustomAdapterConsoleApp
|
||||
return client;
|
||||
}
|
||||
|
||||
static TcpService CreateService()
|
||||
private static TcpService CreateService()
|
||||
{
|
||||
var service = new TcpService();
|
||||
service.Received = (client, e) =>
|
||||
@@ -79,7 +79,7 @@ namespace CustomAdapterConsoleApp
|
||||
{
|
||||
//a.Add();//此处可以添加插件
|
||||
}));
|
||||
service.Start();//启动
|
||||
service.Start();//启动
|
||||
service.Logger.Info("服务器已启动");
|
||||
return service;
|
||||
}
|
||||
@@ -87,7 +87,6 @@ namespace CustomAdapterConsoleApp
|
||||
|
||||
internal class MyCustomDataHandlingAdapter : CustomDataHandlingAdapter<MyRequestInfo>
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 筛选解析数据。实例化的TRequest会一直保存,直至解析成功,或手动清除。
|
||||
/// <para>当不满足解析条件时,请返回<see cref="FilterResult.Cache"/>,此时会保存<see cref="ByteBlock.CanReadLen"/>的数据</para>
|
||||
@@ -108,18 +107,18 @@ namespace CustomAdapterConsoleApp
|
||||
return FilterResult.Cache;//当头部都无法解析时,直接缓存
|
||||
}
|
||||
|
||||
int pos = byteBlock.Pos;//记录初始游标位置,防止本次无法解析时,回退游标。
|
||||
var pos = byteBlock.Pos;//记录初始游标位置,防止本次无法解析时,回退游标。
|
||||
|
||||
MyRequestInfo myRequestInfo = new MyRequestInfo();
|
||||
var myRequestInfo = new MyRequestInfo();
|
||||
|
||||
//此操作实际上有两个作用,
|
||||
//1.填充header
|
||||
//2.将byteBlock.Pos递增3的长度。
|
||||
byteBlock.Read(out byte[] header, 3);//填充header
|
||||
byteBlock.Read(out var header, 3);//填充header
|
||||
|
||||
//因为第一个字节表示所有长度,而DataType、OrderType已经包含在了header里面。
|
||||
//所有只需呀再读取header[0]-2个长度即可。
|
||||
byte bodyLength = (byte)(header[0] - 2);
|
||||
var bodyLength = (byte)(header[0] - 2);
|
||||
|
||||
if (bodyLength > byteBlock.CanReadLen)
|
||||
{
|
||||
@@ -132,7 +131,7 @@ namespace CustomAdapterConsoleApp
|
||||
//此操作实际上有两个作用,
|
||||
//1.填充body
|
||||
//2.将byteBlock.Pos递增bodyLength的长度。
|
||||
byteBlock.Read(out byte[] body, bodyLength);
|
||||
byteBlock.Read(out var body, bodyLength);
|
||||
|
||||
myRequestInfo.DataType = header[1];
|
||||
myRequestInfo.OrderType = header[2];
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace CustomFixedHeaderConsoleApp
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
var service = CreateService();
|
||||
var client = CreateClient();
|
||||
@@ -37,7 +37,7 @@ namespace CustomFixedHeaderConsoleApp
|
||||
}
|
||||
}
|
||||
|
||||
static TcpClient CreateClient()
|
||||
private static TcpClient CreateClient()
|
||||
{
|
||||
var client = new TcpClient();
|
||||
//载入配置
|
||||
@@ -54,7 +54,7 @@ namespace CustomFixedHeaderConsoleApp
|
||||
return client;
|
||||
}
|
||||
|
||||
static TcpService CreateService()
|
||||
private static TcpService CreateService()
|
||||
{
|
||||
var service = new TcpService();
|
||||
service.Received = (client, e) =>
|
||||
@@ -124,7 +124,6 @@ namespace CustomFixedHeaderConsoleApp
|
||||
/// </summary>
|
||||
public byte[] Body { get; set; }
|
||||
|
||||
|
||||
public bool OnParsingBody(byte[] body)
|
||||
{
|
||||
if (body.Length == this.BodyLength)
|
||||
@@ -135,7 +134,6 @@ namespace CustomFixedHeaderConsoleApp
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public bool OnParsingHeader(byte[] header)
|
||||
{
|
||||
//在该示例中,第一个字节表示后续的所有数据长度,但是header设置的是3,所以后续还应当接收length-2个长度。
|
||||
@@ -145,5 +143,4 @@ namespace CustomFixedHeaderConsoleApp
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,7 +6,7 @@ namespace PackageAdapterConsoleApp
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
var service = CreateService();
|
||||
var client = CreateClient();
|
||||
@@ -22,13 +22,13 @@ namespace PackageAdapterConsoleApp
|
||||
}
|
||||
}
|
||||
|
||||
static SingleStreamDataHandlingAdapter GetAdapter()
|
||||
private static SingleStreamDataHandlingAdapter GetAdapter()
|
||||
{
|
||||
return new TerminatorPackageAdapter("\r\n");//使用换行终止字符
|
||||
//return new PeriodPackageAdapter() { CacheTimeout=TimeSpan.FromMilliseconds(100) };//使用周期适配器。
|
||||
}
|
||||
|
||||
static TcpClient CreateClient()
|
||||
private static TcpClient CreateClient()
|
||||
{
|
||||
var client = new TcpClient();
|
||||
//载入配置
|
||||
@@ -45,13 +45,13 @@ namespace PackageAdapterConsoleApp
|
||||
return client;
|
||||
}
|
||||
|
||||
static TcpService CreateService()
|
||||
private static TcpService CreateService()
|
||||
{
|
||||
var service = new TcpService();
|
||||
service.Received = (client, e) =>
|
||||
{
|
||||
//从客户端收到信息
|
||||
string mes = Encoding.UTF8.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意:数据长度是byteBlock.Len
|
||||
var mes = Encoding.UTF8.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意:数据长度是byteBlock.Len
|
||||
client.Logger.Info($"已从{client.Id}接收到信息:{mes}");
|
||||
return EasyTask.CompletedTask;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user