diff --git a/src/TouchSocket/Core/ByteManager/ByteBlockExtensions.cs b/src/TouchSocket/Core/ByteManager/ByteBlockExtensions.cs index a6cd0969b328776ab7601b3b79054035b95a39d5..6ec2e0544b0144c5a4f3ac340c9a6f726190154f 100644 --- a/src/TouchSocket/Core/ByteManager/ByteBlockExtensions.cs +++ b/src/TouchSocket/Core/ByteManager/ByteBlockExtensions.cs @@ -548,7 +548,7 @@ namespace TouchSocket.Core.ByteManager /// /// /// - public static T ReadObject(this IByteBlock byteBlock, SerializationType serializationType = SerializationType.RRQMBinary) + public static T ReadObject(this IByteBlock byteBlock, SerializationType serializationType = SerializationType.FastBinary) { int length = byteBlock.ReadInt32(); @@ -561,9 +561,9 @@ namespace TouchSocket.Core.ByteManager switch (serializationType) { - case SerializationType.RRQMBinary: + case SerializationType.FastBinary: { - obj = SerializeConvert.RRQMBinaryDeserialize(byteBlock.Buffer, byteBlock.Pos); + obj = SerializeConvert.FastBinaryDeserialize(byteBlock.Buffer, byteBlock.Pos); } break; @@ -588,7 +588,7 @@ namespace TouchSocket.Core.ByteManager /// /// /// - public static TByteBlock WriteObject(this TByteBlock byteBlock, object value, SerializationType serializationType = SerializationType.RRQMBinary) + public static TByteBlock WriteObject(this TByteBlock byteBlock, object value, SerializationType serializationType = SerializationType.FastBinary) where TByteBlock : IByteBlock { if (value == null) @@ -599,9 +599,9 @@ namespace TouchSocket.Core.ByteManager byte[] data; switch (serializationType) { - case SerializationType.RRQMBinary: + case SerializationType.FastBinary: { - data = SerializeConvert.RRQMBinarySerialize(value); + data = SerializeConvert.FastBinarySerialize(value); } break; diff --git a/src/TouchSocket/Core/ByteManager/ValueByteBlock.cs b/src/TouchSocket/Core/ByteManager/ValueByteBlock.cs new file mode 100644 index 0000000000000000000000000000000000000000..bbe97b1d1bc1dbc8d42b0de86801b3d38be3512d --- /dev/null +++ b/src/TouchSocket/Core/ByteManager/ValueByteBlock.cs @@ -0,0 +1,404 @@ +//using System; +//using System.Collections.Generic; +//using System.Linq; +//using System.Text; +//using System.Threading.Tasks; + +//namespace TouchSocket.Core.ByteManager +//{ +// /// +// /// 字节块流 +// /// +// [System.Diagnostics.DebuggerDisplay("Len={Len}")] +// public ref struct ValueByteBlock /*: IByteBlock*/ +// { +// internal long m_length; +// internal bool m_using; +// private static float m_ratio = 1.5f; +// private byte[] m_buffer; +// private bool m_holding; +// private object m_locker; +// private bool m_needDis; +// private long m_position; + +// /// +// /// 构造函数 +// /// +// /// +// /// +// public ValueByteBlock(int byteSize = 1024 * 64, bool equalSize = false) +// { +// this.m_needDis = true; +// this.m_buffer = BytePool.GetByteCore(byteSize, equalSize); +// this.m_using = true; +// this.m_length = 0; +// this.m_holding = false; +// this.m_position = 0; +// this.m_locker = new object(); +// } + +// /// +// /// 构造函数 +// /// +// /// +// public ValueByteBlock(byte[] bytes) +// { +// this.m_buffer = bytes ?? throw new ArgumentNullException(nameof(bytes)); +// this.m_length = bytes.Length; +// this.m_using = true; +// this.m_needDis = false; +// this.m_length = 0; +// this.m_holding = false; +// this.m_position = 0; +// this.m_locker = new object(); +// } + +// /// +// /// 扩容增长比,默认为1.5, +// /// min:1.5 +// /// +// public static float Ratio +// { +// get => m_ratio; +// set +// { +// if (value < 1.5) +// { +// value = 1.5f; +// } +// m_ratio = value; +// } +// } + +// /// +// /// 字节实例 +// /// +// public byte[] Buffer => this.m_buffer; + +// /// +// /// 仅当内存块可用,且>0时为True。 +// /// +// public bool CanRead => this.m_using && this.CanReadLen > 0; + +// /// +// /// 还能读取的长度,计算为的差值。 +// /// +// public int CanReadLen => this.Len - this.Pos; + +// /// +// /// 还能读取的长度,计算为的差值。 +// /// +// public long CanReadLength => this.m_length - this.m_position; + +// /// +// /// 容量 +// /// +// public int Capacity => this.m_buffer.Length; + +// /// +// /// 表示持续性持有,为True时,Dispose将调用无效。 +// /// +// public bool Holding => this.m_holding; + +// /// +// /// Int真实长度 +// /// +// public int Len => (int)this.m_length; + +// /// +// /// 真实长度 +// /// +// public long Length => this.m_length; + +// /// +// /// int型流位置 +// /// +// public int Pos +// { +// get => (int)this.m_position; +// set => this.m_position = value; +// } + +// /// +// /// 流位置 +// /// +// public long Position +// { +// get => this.m_position; +// set => this.m_position = value; +// } + +// /// +// /// 使用状态 +// /// +// public bool Using => this.m_using; + +// /// +// /// +// /// +// public int FreeLength => this.Capacity - this.Pos; + +// /// +// /// 直接完全释放,游离该对象,然后等待GC +// /// +// public void AbsoluteDispose() +// { +// this.m_holding = false; +// this.m_using = false; +// this.m_position = 0; +// this.m_length = 0; +// this.m_buffer = null; +// } + +// /// +// /// 清空所有内存数据 +// /// +// /// 内存块已释放 +// public void Clear() +// { +// if (!this.m_using) +// { +// throw new ObjectDisposedException(this.GetType().FullName); +// } +// Array.Clear(this.m_buffer, 0, this.m_buffer.Length); +// } + +// /// +// /// 释放 +// /// +// public void Dispose() +// { +// if (this.m_holding) +// { +// return; +// } + +// if (this.m_needDis) +// { +// lock (m_locker) +// { +// if (this.m_using) +// { +// GC.SuppressFinalize(this); +// BytePool.Recycle(this.m_buffer); +// this.AbsoluteDispose(); +// } +// } +// } +// } + +// /// +// /// 读取数据,然后递增Pos +// /// +// /// +// /// +// /// +// /// +// /// +// public int Read(byte[] buffer, int offset, int length) +// { +// if (!this.m_using) +// { +// throw new ObjectDisposedException(this.GetType().FullName); +// } +// int len = this.m_length - this.m_position > length ? length : this.CanReadLen; +// Array.Copy(this.m_buffer, this.m_position, buffer, offset, len); +// this.m_position += len; +// return len; +// } + +// /// +// /// 读取数据,然后递增Pos +// /// +// /// +// /// +// public int Read(byte[] buffer) +// { +// return this.Read(buffer, 0, buffer.Length); +// } + +// /// +// /// 读取数据,然后递增Pos +// /// +// /// +// /// +// /// +// public int Read(out byte[] buffer, int length) +// { +// buffer = new byte[length]; +// return this.Read(buffer, 0, buffer.Length); +// } + +// /// +// /// +// /// +// /// +// public int ReadByte() +// { +// byte value = this.m_buffer[this.m_position]; +// this.m_position++; +// return value; +// } + +// /// +// /// 将内存块初始化到刚申请的状态。 +// /// 仅仅重置属性。 +// /// +// /// 内存块已释放 +// public void Reset() +// { +// if (!this.m_using) +// { +// throw new ObjectDisposedException(this.GetType().FullName); +// } +// this.m_position = 0; +// this.m_length = 0; +// } + +// /// +// /// 重新设置容量 +// /// +// /// 新尺寸 +// /// 是否保留元数据 +// /// +// public void SetCapacity(int size, bool retainedData = false) +// { +// if (!this.m_using) +// { +// throw new ObjectDisposedException(this.GetType().FullName); +// } +// byte[] bytes = new byte[size]; + +// if (retainedData) +// { +// Array.Copy(this.m_buffer, 0, bytes, 0, this.m_buffer.Length); +// } +// BytePool.Recycle(this.m_buffer); +// this.m_buffer = bytes; +// } + +// /// +// /// 设置持续持有属性,当为True时,调用Dispose会失效,表示该对象将长期持有,直至设置为False。 +// /// 当为False时,会自动调用Dispose。 +// /// +// /// +// /// +// public void SetHolding(bool holding) +// { +// if (!this.m_using) +// { +// throw new ObjectDisposedException(this.GetType().FullName); +// } +// this.m_holding = holding; +// if (!holding) +// { +// this.Dispose(); +// } +// } + +// /// +// /// 设置实际长度 +// /// +// /// +// /// +// public void SetLength(long value) +// { +// if (!this.m_using) +// { +// throw new ObjectDisposedException(this.GetType().FullName); +// } +// if (value > this.m_buffer.Length) +// { +// throw new Exception("设置值超出容量"); +// } +// this.m_length = value; +// } + +// /// +// /// 从指定位置转化到指定长度的有效内存 +// /// +// /// +// /// +// /// +// public byte[] ToArray(int offset, int length) +// { +// if (!this.m_using) +// { +// throw new ObjectDisposedException(this.GetType().FullName); +// } +// byte[] buffer = new byte[length]; +// Array.Copy(this.m_buffer, offset, buffer, 0, buffer.Length); +// return buffer; +// } + +// /// +// /// 转换为UTF-8字符 +// /// +// /// +// public override string ToString() +// { +// return this.ToString(0, this.Len); +// } + +// /// +// /// 转换为UTF-8字符 +// /// +// /// 偏移量 +// /// 长度 +// /// +// public string ToString(int offset, int length) +// { +// if (!this.m_using) +// { +// throw new ObjectDisposedException(this.GetType().FullName); +// } +// return Encoding.UTF8.GetString(this.m_buffer, offset, length); +// } + +// /// +// /// 转换为UTF-8字符 +// /// +// /// 偏移量 +// /// +// public string ToString(int offset) +// { +// if (!this.m_using) +// { +// throw new ObjectDisposedException(this.GetType().FullName); +// } +// return Encoding.UTF8.GetString(this.m_buffer, offset, this.Len - offset); +// } + +// /// +// /// 写入 +// /// +// /// +// /// +// /// +// /// +// public void Write(byte[] buffer, int offset, int count) +// { +// if (count == 0) +// { +// return; +// } +// if (!this.m_using) +// { +// throw new ObjectDisposedException(this.GetType().FullName); +// } +// if (this.m_buffer.Length - this.m_position < count) +// { +// int need = this.m_buffer.Length + count - ((int)(this.m_buffer.Length - this.m_position)); +// int lend = this.m_buffer.Length; +// while (need > lend) +// { +// lend = (int)(lend * m_ratio); +// } +// this.SetCapacity(lend, true); +// } +// Array.Copy(buffer, offset, this.m_buffer, this.m_position, count); +// this.m_position += count; +// this.m_length = Math.Max(this.m_position, this.m_length); +// } +// } +//} \ No newline at end of file diff --git a/src/TouchSocket/Core/Common/Mapper.cs b/src/TouchSocket/Core/Common/Mapper.cs new file mode 100644 index 0000000000000000000000000000000000000000..e5cd4f62b9177b15f5cfb4296494a8acfcf367e3 --- /dev/null +++ b/src/TouchSocket/Core/Common/Mapper.cs @@ -0,0 +1,132 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TouchSocketPro +{ + /// + /// 映射数据 + /// + public static class Mapper + { + /// + /// 简单映射 + /// + /// + /// + /// + /// + public static T1 Map(T t)where T:class where T1:class,new () + { + if (t is null) + { + return default; + } + var source = Activator.CreateInstance(typeof(T)); + var result = Activator.CreateInstance(typeof(T1)); + if (source.GetType().Name == "List`1" || result.GetType().Name == "List`1") + { + throw new Exception("形参有误!,请使用对象。"); + } + var tpropertyInfos = source.GetType().GetProperties(); + var t1propertyInfos = result.GetType().GetProperties(); + foreach (var tinfo in tpropertyInfos) + { + foreach (var t1info in t1propertyInfos) + { + if (t1info.PropertyType.IsValueType || t1info.PropertyType.Name.StartsWith("String")) + { + if (tinfo.Name == t1info.Name) + { + try + { + object value = tinfo.GetValue(t, null); + var property = typeof(T1).GetProperty(tinfo.Name); + if (property != null && property.CanWrite && !(value is DBNull)) + { + property.SetValue(result, value, null); + } + } + catch + { + } + } + } + } + + } + return (T1)result; + } + + /// + /// 简单映射 + /// + /// + /// + /// + public static T1 Map(object t) where T1 : class, new() + { + if (t is null) + { + return default; + } + var result = Activator.CreateInstance(typeof(T1)); + if (t.GetType().Name == "List`1" || result.GetType().Name == "List`1") + { + throw new Exception("形参有误!,请使用对象。"); + } + var tpropertyInfos = t.GetType().GetProperties(); + var t1propertyInfos = result.GetType().GetProperties(); + foreach (var tinfo in tpropertyInfos) + { + foreach (var t1info in t1propertyInfos) + { + if (t1info.PropertyType.IsValueType || t1info.PropertyType.Name.StartsWith("String")) + { + if (tinfo.Name == t1info.Name) + { + try + { + object value = tinfo.GetValue(t, null); + var property = typeof(T1).GetProperty(tinfo.Name); + if (property != null && property.CanWrite && !(value is DBNull)) + { + property.SetValue(result, value, null); + } + } + catch + { + } + } + } + } + + } + return (T1)result; + } + + /// + /// 映射List + /// + /// + /// + /// + /// + public static IEnumerable MapList(IEnumerable list) where T : class where T1 : class, new() + { + if (list is null) + { + throw new ArgumentNullException(nameof(list)); + } + + List result = new List(); + foreach (var item in list) + { + result.Add(Map(item)); + } + return result; + } + } +} diff --git a/src/TouchSocket/Core/Common/Metadata.cs b/src/TouchSocket/Core/Common/Metadata.cs index 685557f18da047a688d6d110e2be62449374339e..9d5dc6d3bcf4f5981644da60c37f433df206ca97 100644 --- a/src/TouchSocket/Core/Common/Metadata.cs +++ b/src/TouchSocket/Core/Common/Metadata.cs @@ -24,7 +24,7 @@ namespace TouchSocket.Core /// /// /// - public void AddOrUpdate(string key, string value) + public Metadata AddOrUpdate(string key, string value) { if (this.ContainsKey(key)) { @@ -34,6 +34,7 @@ namespace TouchSocket.Core { this.Add(key, value); } + return this; } } } \ No newline at end of file diff --git a/src/TouchSocket/Core/IO/FileIO/FilePool.cs b/src/TouchSocket/Core/IO/FileIO/FilePool.cs index 01ca10439ba16077286f61b6d372fdc192e1ea1f..1a608d5887dc8404a6c2f2dc29e52183a5d9df10 100644 --- a/src/TouchSocket/Core/IO/FileIO/FilePool.cs +++ b/src/TouchSocket/Core/IO/FileIO/FilePool.cs @@ -213,9 +213,8 @@ namespace TouchSocket.Core.IO /// 减少引用次数,并尝试释放流。 /// /// - /// 延迟释放时间,默认5000ms。当设置为0时,立即释放。 /// - public static Result TryReleaseFile(string path, int delayTime = 5000) + public static Result TryReleaseFile(string path) { if (string.IsNullOrEmpty(path)) { @@ -227,28 +226,11 @@ namespace TouchSocket.Core.IO Interlocked.Decrement(ref fileStorage.reference); if (fileStorage.reference <= 0) { - if (delayTime > 0) + if (pathStream.TryRemove(path, out fileStorage)) { - Run.EasyAction.DelayRun(delayTime, path, (p) => - { - if (GetReferenceCount(p) == 0) - { - if (pathStream.TryRemove(p, out fileStorage)) - { - fileStorage.Dispose(); - } - } - }); - return new Result(ResultCode.Success, $"如果在{delayTime}ms后引用仍然为0的话,即被释放。"); - } - else - { - if (pathStream.TryRemove(path, out fileStorage)) - { - fileStorage.Dispose(); - } - return new Result(ResultCode.Success, "流成功释放。"); + fileStorage.Dispose(); } + return new Result(ResultCode.Success, "流成功释放。"); } else { @@ -257,7 +239,7 @@ namespace TouchSocket.Core.IO } else { - return new Result(ResultCode.Error, ResType.StreamNotFind.GetDescription(path)); + return new Result(ResultCode.Success, ResType.StreamNotFind.GetDescription(path)); } } } diff --git a/src/TouchSocket/Core/IO/FileIO/FileStorageReader.cs b/src/TouchSocket/Core/IO/FileIO/FileStorageReader.cs index a0a0445dc5d64614a57fa9f942f858a085c72035..249a908a1d82c4e586a286d4abba71a4010e30af 100644 --- a/src/TouchSocket/Core/IO/FileIO/FileStorageReader.cs +++ b/src/TouchSocket/Core/IO/FileIO/FileStorageReader.cs @@ -17,7 +17,7 @@ namespace TouchSocket.Core.IO /// /// 文件读取器 /// - public class FileStorageReader : IDisposable + public class FileStorageReader : DisposableObject { private FileStorage m_fileStorage; @@ -36,7 +36,6 @@ namespace TouchSocket.Core.IO public FileStorage FileStorage => this.m_fileStorage; private long m_position; - private bool m_disposedValue; /// /// 游标位置 @@ -71,22 +70,14 @@ namespace TouchSocket.Core.IO } /// - /// 释放资源 + /// /// /// - protected virtual void Dispose(bool disposing) + protected override void Dispose(bool disposing) { - if (!this.m_disposedValue) - { - if (disposing) - { - // TODO: 释放托管状态(托管对象) - } - - FilePool.TryReleaseFile(this.m_fileStorage?.Path); - this.m_fileStorage = null; - this.m_disposedValue = true; - } + FilePool.TryReleaseFile(this.m_fileStorage?.Path); + this.m_fileStorage = null; + base.Dispose(disposing); } /// @@ -97,26 +88,5 @@ namespace TouchSocket.Core.IO // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中 this.Dispose(disposing: false); } - - /// - /// 释放资源 - /// - public void Dispose() - { - // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中 - this.Dispose(disposing: true); - GC.SuppressFinalize(this); - } - - /// - /// 释放资源 - /// - public void Dispose(int delayTime) - { - FilePool.TryReleaseFile(this.m_fileStorage?.Path, delayTime); - // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中 - this.Dispose(disposing: true); - GC.SuppressFinalize(this); - } } } \ No newline at end of file diff --git a/src/TouchSocket/Core/IO/FileIO/FileStorageWriter.cs b/src/TouchSocket/Core/IO/FileIO/FileStorageWriter.cs index cbb20d280a306f6eb0f5e932fe802e92c4aad965..7b3b44eeb52fd6d339e3652e54c4f980f1251c90 100644 --- a/src/TouchSocket/Core/IO/FileIO/FileStorageWriter.cs +++ b/src/TouchSocket/Core/IO/FileIO/FileStorageWriter.cs @@ -17,9 +17,8 @@ namespace TouchSocket.Core.IO /// /// 文件写入器。 /// - public class FileStorageWriter : IDisposable + public class FileStorageWriter : DisposableObject { - private bool m_disposedValue; private FileStorage m_fileStorage; private readonly bool m_singleRef; private long m_position; @@ -44,6 +43,16 @@ namespace TouchSocket.Core.IO this.Dispose(disposing: false); } + /// + /// + /// + /// + protected override void Dispose(bool disposing) + { + FilePool.TryReleaseFile(this.m_fileStorage?.Path); + base.Dispose(disposing); + } + /// /// 文件存储器 /// @@ -67,16 +76,7 @@ namespace TouchSocket.Core.IO set => this.m_position = value; } - /// - /// 释放资源 - /// - public void Dispose() - { - // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中 - this.Dispose(disposing: true); - GC.SuppressFinalize(this); - } - + /// /// 读取数据到缓存区 /// @@ -90,23 +90,6 @@ namespace TouchSocket.Core.IO this.m_position += length; } - /// - /// 释放资源 - /// - /// - protected virtual void Dispose(bool disposing) - { - if (!this.m_disposedValue) - { - if (disposing) - { - // TODO: 释放托管状态(托管对象) - } - - FilePool.TryReleaseFile(this.m_fileStorage?.Path, this.m_singleRef ? 0 : 5000); - this.m_fileStorage = null; - this.m_disposedValue = true; - } - } + } } \ No newline at end of file diff --git a/src/TouchSocket/Core/Logger/FileLogger.cs b/src/TouchSocket/Core/Logger/FileLogger.cs index 148ebfb655caedc0b16cd83b1aa1fbda76f87fa5..63b2f4260431fa691a64bac06fb31f894a52a25f 100644 --- a/src/TouchSocket/Core/Logger/FileLogger.cs +++ b/src/TouchSocket/Core/Logger/FileLogger.cs @@ -74,13 +74,27 @@ namespace TouchSocket.Core.Log this.Print(stringBuilder.ToString()); } + int m_day=-1; + int m_count; private void Print(string logString) { try { lock (typeof(FileLogger)) { - string path = Path.Combine(this.rootPath, DateTime.Now.ToString("[yyyy-MM-dd]") + ".log"); + if (m_day != DateTime.Now.DayOfYear) + { + m_day = DateTime.Now.DayOfYear; + m_count = 0; + } + else + { + if (new FileInfo(Path.Combine(this.rootPath, $"{DateTime.Now:[yyyy-MM-dd]}-{m_count:00}" + ".log")).Length>1024*1024 ) + { + m_count++; + } + } + string path = Path.Combine(this.rootPath, $"{DateTime.Now:[yyyy-MM-dd]}-{m_count:00}" + ".log"); File.AppendAllText(path, logString); } } diff --git a/src/TouchSocket/Core/Plugins/PluginsManager.cs b/src/TouchSocket/Core/Plugins/PluginsManager.cs index ccadae872273b0599531bebf45e98e65e0b53a77..0b0d99fce881e7e9150305541dfc8aa25b52da3a 100644 --- a/src/TouchSocket/Core/Plugins/PluginsManager.cs +++ b/src/TouchSocket/Core/Plugins/PluginsManager.cs @@ -133,17 +133,17 @@ namespace TouchSocket.Core.Plugins { if (value.TryGetValue(name, out PluginMethod pluginMethod)) { - foreach (var item in this.m_plugins) + for (int i = 0; i < this.m_plugins.Count; i++) { if (args.Handled) { return true; } - if (pluginMethod.type.IsAssignableFrom(item.GetType())) + if (pluginMethod.type.IsAssignableFrom(this.m_plugins[i].GetType())) { try { - pluginMethod.Invoke(item, @params); + pluginMethod.Invoke(this.m_plugins[i], @params); } catch { diff --git a/src/TouchSocket/Core/Serialization/Attributes/TouchSocketNonSerializedAttribute.cs b/src/TouchSocket/Core/Serialization/Attributes/FastNonSerializedAttribute.cs similarity index 94% rename from src/TouchSocket/Core/Serialization/Attributes/TouchSocketNonSerializedAttribute.cs rename to src/TouchSocket/Core/Serialization/Attributes/FastNonSerializedAttribute.cs index 8e6aeb86cf174e4fdf4efcb19ed76f87e273312d..47e29ba299bd5b4de4f10a374783843bc366c6db 100644 --- a/src/TouchSocket/Core/Serialization/Attributes/TouchSocketNonSerializedAttribute.cs +++ b/src/TouchSocket/Core/Serialization/Attributes/FastNonSerializedAttribute.cs @@ -18,7 +18,7 @@ namespace TouchSocket.Core.Serialization /// 忽略的RRQM序列化 /// [AttributeUsage(AttributeTargets.Property)] - public class TouchSocketNonSerializedAttribute : Attribute + public class FastNonSerializedAttribute : Attribute { } } \ No newline at end of file diff --git a/src/TouchSocket/Core/Serialization/TouchSocketBinaryFormatter.cs b/src/TouchSocket/Core/Serialization/FastBinaryFormatter.cs similarity index 99% rename from src/TouchSocket/Core/Serialization/TouchSocketBinaryFormatter.cs rename to src/TouchSocket/Core/Serialization/FastBinaryFormatter.cs index b8c163fe6259fe415e79b7e2436657114bd0f9a4..484fcfe121e78460cd36b12306dac498d47edc2d 100644 --- a/src/TouchSocket/Core/Serialization/TouchSocketBinaryFormatter.cs +++ b/src/TouchSocket/Core/Serialization/FastBinaryFormatter.cs @@ -25,7 +25,7 @@ namespace TouchSocket.Core.Serialization /// /// 该序列化以二进制方式进行,但是不支持接口、抽象类、继承类等成员的序列化。 /// - public class TouchSocketBinaryFormatter + public class FastBinaryFormatter { #region Serialize @@ -182,7 +182,7 @@ namespace TouchSocket.Core.Serialization PropertyInfo[] propertyInfos = GetProperties(type); foreach (PropertyInfo property in propertyInfos) { - if (property.GetCustomAttribute() != null) + if (property.GetCustomAttribute() != null) { continue; } diff --git a/src/TouchSocket/Core/Serialization/SerializationType.cs b/src/TouchSocket/Core/Serialization/SerializationType.cs index 5d4f0be197cfc5ed68d085e46d91cf439d9a3bc3..9a883df273d71ef7693f777ad4a8778def2e8ecc 100644 --- a/src/TouchSocket/Core/Serialization/SerializationType.cs +++ b/src/TouchSocket/Core/Serialization/SerializationType.cs @@ -19,9 +19,9 @@ namespace TouchSocket.Core.Serialization public enum SerializationType : byte { /// - /// 若汝棋茗内置 + /// 内置快速二进制 /// - RRQMBinary, + FastBinary, /// /// Json diff --git a/src/TouchSocket/Core/Serialization/SerializeConvert.cs b/src/TouchSocket/Core/Serialization/SerializeConvert.cs index a04a5d030934e8b8b4d19b3e1ce2e0ed09da4b19..ffebee21333e85f42cd0e82e7ef391fcbe3e29f8 100644 --- a/src/TouchSocket/Core/Serialization/SerializeConvert.cs +++ b/src/TouchSocket/Core/Serialization/SerializeConvert.cs @@ -178,61 +178,61 @@ namespace TouchSocket.Core.Serialization #endif - #region RRQM二进制序列化 + #region Fast二进制序列化 /// - /// RRQM二进制序列化对象 + /// Fast二进制序列化对象 /// /// /// /// - public static void RRQMBinarySerialize(ByteBlock stream, object obj) + public static void FastBinarySerialize(ByteBlock stream, object obj) { - TouchSocketBinaryFormatter bf = new TouchSocketBinaryFormatter(); + FastBinaryFormatter bf = new FastBinaryFormatter(); bf.Serialize(stream, obj); } /// - /// RRQM二进制序列化对象 + /// Fast二进制序列化对象 /// /// /// - public static byte[] RRQMBinarySerialize(object obj) + public static byte[] FastBinarySerialize(object obj) { using (ByteBlock byteBlock = new ByteBlock()) { - RRQMBinarySerialize(byteBlock, obj); + FastBinarySerialize(byteBlock, obj); return byteBlock.ToArray(); } } - #endregion RRQM二进制序列化 + #endregion Fast二进制序列化 - #region RRQM二进制反序列化 + #region Fast二进制反序列化 /// - /// 反序列化 + /// Fast反序列化 /// /// /// /// /// - public static T RRQMBinaryDeserialize(byte[] data, int offset) + public static T FastBinaryDeserialize(byte[] data, int offset) { - TouchSocketBinaryFormatter bf = new TouchSocketBinaryFormatter(); + FastBinaryFormatter bf = new FastBinaryFormatter(); return (T)bf.Deserialize(data, offset, typeof(T)); } /// - /// 反序列化 + /// Fast反序列化 /// /// /// /// /// - public static object RRQMBinaryDeserialize(byte[] data, int offset, Type type) + public static object FastBinaryDeserialize(byte[] data, int offset, Type type) { - TouchSocketBinaryFormatter bf = new TouchSocketBinaryFormatter(); + FastBinaryFormatter bf = new FastBinaryFormatter(); return bf.Deserialize(data, offset, type); } @@ -242,12 +242,12 @@ namespace TouchSocket.Core.Serialization /// /// /// - public static T RRQMBinaryDeserialize(byte[] data) + public static T FastBinaryDeserialize(byte[] data) { - return RRQMBinaryDeserialize(data, 0); + return FastBinaryDeserialize(data, 0); } - #endregion RRQM二进制反序列化 + #endregion Fast二进制反序列化 #region Xml序列化和反序列化 diff --git a/src/TouchSocket/Http/Extensions/HttpExtensions.cs b/src/TouchSocket/Http/Extensions/HttpExtensions.cs index 2d2277b13830b112e5f5cf4d83ccfa50e53cc8c3..58fd2678d78b1d52c12bd814812a2bee9f29002b 100644 --- a/src/TouchSocket/Http/Extensions/HttpExtensions.cs +++ b/src/TouchSocket/Http/Extensions/HttpExtensions.cs @@ -456,7 +456,6 @@ namespace TouchSocket.Http } reader.Position = httpRange.Start; long surLen = httpRange.Length; - using (ByteBlock block = new ByteBlock(bufferLen)) { while (surLen > 0) diff --git a/src/TouchSocket/Rpc/Global/Extensions/RpcConfigExtensions.cs b/src/TouchSocket/Rpc/Global/Extensions/RpcConfigExtensions.cs index c408cee85fd6710379b4d3080f41b029808e36a5..829bf4e05e09cd050145e73ae98e8d0cff1059c2 100644 --- a/src/TouchSocket/Rpc/Global/Extensions/RpcConfigExtensions.cs +++ b/src/TouchSocket/Rpc/Global/Extensions/RpcConfigExtensions.cs @@ -34,9 +34,9 @@ namespace TouchSocket.Core.Config /// /// 当RpcStore完成配置时回调 /// 可以使用现有的值,如果赋值为null,则会重新创建。 - public static TouchSocketConfig ConfigureRpcStore(this TouchSocketConfig config, Action action, RpcStore value = default) + public static TouchSocketConfig ConfigureRpcStore(this TouchSocketConfig config, Action action, RpcStore value=default) { - if (value == default) + if (value==default) { value = new RpcStore(config.Container); } diff --git a/src/TouchSocket/Rpc/TouchRpc/Common/InvokeOption.cs b/src/TouchSocket/Rpc/TouchRpc/Common/InvokeOption.cs index 3b7cd2601ffd4edc4eaa78b78d0c4a74d8d85a3c..f437f4e68231239262aec026b350a3f9534d5a6f 100644 --- a/src/TouchSocket/Rpc/TouchRpc/Common/InvokeOption.cs +++ b/src/TouchSocket/Rpc/TouchRpc/Common/InvokeOption.cs @@ -42,7 +42,7 @@ namespace TouchSocket.Rpc.TouchRpc /// /// /// - public InvokeOption(int timeout = 5000, FeedbackType feedbackType = FeedbackType.WaitInvoke, SerializationType serializationType = SerializationType.RRQMBinary, + public InvokeOption(int timeout = 5000, FeedbackType feedbackType = FeedbackType.WaitInvoke, SerializationType serializationType = SerializationType.FastBinary, CancellationToken cancellationToken = default) : this() { this.Timeout = timeout; diff --git a/src/TouchSocket/Rpc/TouchRpc/Components/Http/HttpTouchRpcClient.cs b/src/TouchSocket/Rpc/TouchRpc/Components/Http/HttpTouchRpcClient.cs index 954622f22b217b7e6c420bdd62513daefff8aa3e..65221b0264149d50e55d6c316f05754cf1067481 100644 --- a/src/TouchSocket/Rpc/TouchRpc/Components/Http/HttpTouchRpcClient.cs +++ b/src/TouchSocket/Rpc/TouchRpc/Components/Http/HttpTouchRpcClient.cs @@ -30,11 +30,11 @@ namespace TouchSocket.Rpc.TouchRpc /// public class HttpTouchRpcClient : HttpClientBase, IHttpTouchRpcClient { + private readonly ActionMap m_actionMap; private int m_failCount = 0; - private RpcActor m_rpcActor; + private readonly RpcActor m_rpcActor; private RpcStore m_rpcStore; private Timer m_timer; - private ActionMap m_actionMap; /// /// 创建一个HttpTouchRpcClient实例。 @@ -58,19 +58,19 @@ namespace TouchSocket.Rpc.TouchRpc } /// - /// + /// 服务器映射 /// - public string ID => this.m_rpcActor.ID; + public ActionMap ActionMap { get => m_actionMap; } /// /// /// - public bool IsHandshaked => this.m_rpcActor != null && this.m_rpcActor.IsHandshaked; + public string ID => this.m_rpcActor.ID; /// - /// 服务器映射 + /// /// - public ActionMap ActionMap { get => m_actionMap; } + public bool IsHandshaked => this.m_rpcActor != null && this.m_rpcActor.IsHandshaked; /// /// @@ -118,18 +118,6 @@ namespace TouchSocket.Rpc.TouchRpc /// /// public override ITcpClient Connect(int timeout = 5000) - { - return this.Connect(null, default, timeout); - } - - /// - /// - /// - /// - /// - /// - /// - public virtual ITcpClient Connect(Metadata metadata = null, CancellationToken token = default, int timeout = 5000) { if (this.IsHandshaked) { @@ -146,7 +134,8 @@ namespace TouchSocket.Rpc.TouchRpc if (response.StatusCode == "200") { this.SwitchProtocolToTouchRpc(); - this.m_rpcActor.Handshake(this.Config.GetValue(TouchRpcConfigExtensions.VerifyTokenProperty), token, timeout, metadata); + this.m_rpcActor.Handshake(this.Config.GetValue(TouchRpcConfigExtensions.VerifyTokenProperty), default, + timeout, this.Config.GetValue(TouchRpcConfigExtensions.MetadataProperty)); return this; } else @@ -155,21 +144,6 @@ namespace TouchSocket.Rpc.TouchRpc } } - /// - /// 异步连接 - /// - /// - /// - /// - /// - public virtual Task ConnectAsync(Metadata metadata = null, CancellationToken token = default, int timeout = 5000) - { - return Task.Run(() => - { - return this.Connect(metadata, token, timeout); - }); - } - /// /// /// @@ -510,6 +484,17 @@ namespace TouchSocket.Rpc.TouchRpc return this.m_rpcActor.SendStreamAsync(stream, streamOperator, metadata); } + /// + /// + /// + /// + /// + /// + public bool TrySubscribeChannel(int id, out Channel channel) + { + return this.m_rpcActor.TrySubscribeChannel(id, out channel); + } + /// /// /// @@ -694,17 +679,6 @@ namespace TouchSocket.Rpc.TouchRpc base.OnDisconnected(e); } - /// - /// - /// - /// - /// - /// - public bool TrySubscribeChannel(int id, out Channel channel) - { - return this.m_rpcActor.TrySubscribeChannel(id, out channel); - } - #region 内部委托绑定 private MethodInstance GetInvokeMethod(string arg) diff --git a/src/TouchSocket/Rpc/TouchRpc/Components/Http/HttpTouchRpcService.cs b/src/TouchSocket/Rpc/TouchRpc/Components/Http/HttpTouchRpcService.cs index 1376f5a5c37d23386b6a88114660a5fcd6368bd5..3054fee56816b679316a71fdc5d40438333aacd7 100644 --- a/src/TouchSocket/Rpc/TouchRpc/Components/Http/HttpTouchRpcService.cs +++ b/src/TouchSocket/Rpc/TouchRpc/Components/Http/HttpTouchRpcService.cs @@ -55,13 +55,11 @@ namespace TouchSocket.Rpc.TouchRpc this.m_rpcActorGroup.OutputSend = this.RpcServiceOutputSend; } - - #region 字段 + private readonly ActionMap m_actionMap; private RpcActorGroup m_rpcActorGroup; private RpcStore m_rpcStore; - private readonly ActionMap m_actionMap; #endregion 字段 @@ -91,17 +89,6 @@ namespace TouchSocket.Rpc.TouchRpc base.ResetID(oldID, newID); } - /// - /// 客户端请求连接 - /// - /// - /// - protected override void OnConnecting(TClient socketClient, ClientOperationEventArgs e) - { - socketClient.internalOnRpcActorInit = this.PrivateOnRpcActorInit; - base.OnConnecting(socketClient, e); - } - /// /// /// @@ -120,6 +107,17 @@ namespace TouchSocket.Rpc.TouchRpc } } + /// + /// 客户端请求连接 + /// + /// + /// + protected override void OnConnecting(TClient socketClient, ClientOperationEventArgs e) + { + socketClient.internalOnRpcActorInit = this.PrivateOnRpcActorInit; + base.OnConnecting(socketClient, e); + } + #region 事件 /// diff --git a/src/TouchSocket/Rpc/TouchRpc/Components/TCP/TcpTouchRpcClient.cs b/src/TouchSocket/Rpc/TouchRpc/Components/TCP/TcpTouchRpcClient.cs index 9f5bc19964911631b3c2ead2ada0714b2ea4f8ad..4a2688dcabfeb4ac680de0068daee94649f1d286 100644 --- a/src/TouchSocket/Rpc/TouchRpc/Components/TCP/TcpTouchRpcClient.cs +++ b/src/TouchSocket/Rpc/TouchRpc/Components/TCP/TcpTouchRpcClient.cs @@ -118,18 +118,6 @@ namespace TouchSocket.Rpc.TouchRpc /// /// public override ITcpClient Connect(int timeout = 5000) - { - return this.Connect(null, default, timeout); - } - - /// - /// - /// - /// - /// - /// - /// - public virtual ITcpClient Connect(Metadata metadata = null, CancellationToken token = default, int timeout = 5000) { if (this.IsHandshaked) { @@ -140,25 +128,11 @@ namespace TouchSocket.Rpc.TouchRpc base.Connect(timeout); } - this.m_rpcActor.Handshake(this.Config.GetValue(TouchRpcConfigExtensions.VerifyTokenProperty), token, timeout, metadata); + this.m_rpcActor.Handshake(this.Config.GetValue(TouchRpcConfigExtensions.VerifyTokenProperty), default, + timeout, this.Config.GetValue(TouchRpcConfigExtensions.MetadataProperty)); return this; } - /// - /// - /// - /// - /// - /// - /// - public virtual Task ConnectAsync(Metadata metadata = null, CancellationToken token = default, int timeout = 5000) - { - return Task.Run(() => - { - return this.Connect(metadata, token, timeout); - }); - } - /// /// /// diff --git a/src/TouchSocket/Rpc/TouchRpc/Components/TCP/TcpTouchRpcService.cs b/src/TouchSocket/Rpc/TouchRpc/Components/TCP/TcpTouchRpcService.cs index 65d9b7571d6c1967ad36ef8f20032943a90645ac..e8eecbd92fcdc6c9661f7c9ab5990e6ee2be7bf1 100644 --- a/src/TouchSocket/Rpc/TouchRpc/Components/TCP/TcpTouchRpcService.cs +++ b/src/TouchSocket/Rpc/TouchRpc/Components/TCP/TcpTouchRpcService.cs @@ -54,12 +54,11 @@ namespace TouchSocket.Rpc.TouchRpc this.m_rpcActorGroup.OutputSend = this.RpcServiceOutputSend; } - #region 字段 + private readonly ActionMap m_actionMap; private RpcActorGroup m_rpcActorGroup; private RpcStore m_rpcStore; - private readonly ActionMap m_actionMap; #endregion 字段 @@ -68,7 +67,6 @@ namespace TouchSocket.Rpc.TouchRpc /// public ActionMap ActionMap { get => m_actionMap; } - /// /// /// @@ -90,17 +88,6 @@ namespace TouchSocket.Rpc.TouchRpc base.ResetID(oldID, newID); } - /// - /// 客户端请求连接 - /// - /// - /// - protected override void OnConnecting(TClient socketClient, ClientOperationEventArgs e) - { - socketClient.m_rpcActor = this.m_rpcActorGroup.CreateRpcActor(socketClient); - base.OnConnecting(socketClient, e); - } - /// /// /// @@ -119,6 +106,17 @@ namespace TouchSocket.Rpc.TouchRpc } } + /// + /// 客户端请求连接 + /// + /// + /// + protected override void OnConnecting(TClient socketClient, ClientOperationEventArgs e) + { + socketClient.m_rpcActor = this.m_rpcActorGroup.CreateRpcActor(socketClient); + base.OnConnecting(socketClient, e); + } + #region 事件 /// diff --git a/src/TouchSocket/Rpc/TouchRpc/Config/TouchRpcConfigExtensions.cs b/src/TouchSocket/Rpc/TouchRpc/Config/TouchRpcConfigExtensions.cs index 861aee24787aaea3a6c3b18fd97a703607a68659..cab12a6b599ec78f93883804f3a4167e5f5e7f25 100644 --- a/src/TouchSocket/Rpc/TouchRpc/Config/TouchRpcConfigExtensions.cs +++ b/src/TouchSocket/Rpc/TouchRpc/Config/TouchRpcConfigExtensions.cs @@ -10,6 +10,7 @@ // 感谢您的下载和使用 //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ +using TouchSocket.Core; using TouchSocket.Core.Config; using TouchSocket.Core.Dependency; @@ -45,9 +46,15 @@ namespace TouchSocket.Rpc.TouchRpc public static readonly DependencyProperty VerifyTokenProperty = DependencyProperty.Register("VerifyToken", typeof(string), typeof(TouchRpcConfigExtensions), "rrqm"); + /// + /// TouchClient连接时的元数据, 所需类型 + /// + public static readonly DependencyProperty MetadataProperty = + DependencyProperty.Register("Metadata", typeof(Metadata), typeof(TouchRpcConfigExtensions), null); + /// /// 心跳频率,默认为间隔2000ms,3次。(设置为null时禁止心跳) - /// 仅适用于及派生类 + /// 仅适用于TouchRpcClient系类 /// /// /// @@ -58,6 +65,19 @@ namespace TouchSocket.Rpc.TouchRpc return config; } + /// + /// 设置TouchClient连接时的元数据 + /// 仅适用于TouchRpcClient系类 + /// + /// + /// + /// + public static TouchSocketConfig SetMetadata(this TouchSocketConfig config, Metadata value) + { + config.SetValue(MetadataProperty, value); + return config; + } + /// /// 设置序列化转换器 /// @@ -71,7 +91,8 @@ namespace TouchSocket.Rpc.TouchRpc } /// - /// 验证超时时间,默认为3000ms + /// 验证超时时间,默认为3000ms. + /// 该配置仅有效 /// /// /// diff --git a/src/TouchSocket/Rpc/TouchRpc/File/FileTool.cs b/src/TouchSocket/Rpc/TouchRpc/File/FileTool.cs index 3a3e3a90baa9f910fb4fe24fe5b3973feb62d30d..3a63629d1a0cc4ab34937d9f5891f89c43ca9acc 100644 --- a/src/TouchSocket/Rpc/TouchRpc/File/FileTool.cs +++ b/src/TouchSocket/Rpc/TouchRpc/File/FileTool.cs @@ -13,6 +13,7 @@ using System.IO; using TouchSocket.Core.IO; using TouchSocket.Core.Serialization; +using TouchSocket.Core.XREF.Newtonsoft.Json; namespace TouchSocket.Rpc.TouchRpc { @@ -60,7 +61,7 @@ namespace TouchSocket.Rpc.TouchRpc try { FileInfo fileInfo = new FileInfo(filePath); - TouchRpcFileInfo tempInfo = SerializeConvert.XmlDeserializeFromFile(tempPath); + TouchRpcFileInfo tempInfo = JsonConvert.DeserializeObject(File.ReadAllText(tempPath)); if (tempInfo.MD5 == info.MD5 && tempInfo.FileLength == info.FileLength) { info.Position = tempInfo.Position; diff --git a/src/TouchSocket/Rpc/TouchRpc/File/TouchRpcFileStream.cs b/src/TouchSocket/Rpc/TouchRpc/File/TouchRpcFileStream.cs index 6c5d63f9f873bc165e53c515304bfcfa0d0c588c..1bc6c8ff55e948f616e7ac4e2b7aba51c0cebac6 100644 --- a/src/TouchSocket/Rpc/TouchRpc/File/TouchRpcFileStream.cs +++ b/src/TouchSocket/Rpc/TouchRpc/File/TouchRpcFileStream.cs @@ -13,44 +13,18 @@ using System; using System.IO; using System.Threading; - -/* 项目“TouchSocketPro (net5)”的未合并的更改 -在此之前: -using System; -using System.IO; -在此之后: -using TouchSocket.Core; -using TouchSocket.Core.IO; -*/ - -/* 项目“TouchSocketPro (netcoreapp3.1)”的未合并的更改 -在此之前: -using System; -using System.IO; -在此之后: using TouchSocket.Core; +using TouchSocket.Core.Extensions; using TouchSocket.Core.IO; -*/ - -/* 项目“TouchSocketPro (netstandard2.0)”的未合并的更改 -在此之前: -using System; -using System.IO; -在此之后: -using TouchSocket.Core; -using TouchSocket.Core.IO; -*/ -using TouchSocket.Core.IO; -using TouchSocket.Core.Serialization; namespace TouchSocket.Rpc.TouchRpc { /// /// 文件流 /// - internal class TouchRpcFileStream : TouchSocket.Core.DisposableObject + internal class TouchRpcFileStream : DisposableObject { - private static int saveInterval = 1000; + private static int m_saveInterval = 1000; private TouchRpcFileInfo m_fileInfo; @@ -60,11 +34,11 @@ namespace TouchSocket.Rpc.TouchRpc private bool m_resume; - private FileStorageWriter fileWriter; + private FileStorageWriter m_fileWriter; protected override void Dispose(bool disposing) { - this.fileWriter.SafeDispose(); + this.m_fileWriter.SafeDispose(); base.Dispose(disposing); } @@ -73,25 +47,25 @@ namespace TouchSocket.Rpc.TouchRpc /// public static int SaveInterval { - get => saveInterval; + get => m_saveInterval; set { if (value < 0) { value = 0; } - saveInterval = value; + m_saveInterval = value; } } - public FileStorageWriter FileWriter => this.fileWriter; + public FileStorageWriter FileWriter => this.m_fileWriter; public static TouchRpcFileStream Create(string path, ref TouchRpcFileInfo fileInfo, bool resume) { TouchRpcFileStream stream = new TouchRpcFileStream(); FileTool.TryReadTempInfo(path, ref fileInfo); - stream.fileWriter = FilePool.GetWriter(path + ".rrqm", true); - stream.fileWriter.Position = fileInfo.Position; + stream.m_fileWriter = FilePool.GetWriter(path + ".rrqm", true); + stream.m_fileWriter.Position = fileInfo.Position; stream.m_fileInfo = fileInfo; stream.m_path = path; stream.m_resume = resume; @@ -100,13 +74,13 @@ namespace TouchSocket.Rpc.TouchRpc public void Write(byte[] buffer, int offset, int length) { - this.fileWriter.Write(buffer, offset, length); + this.m_fileWriter.Write(buffer, offset, length); this.SaveProgress(); } public void FinishStream() { - if (this.fileWriter.Position != this.m_fileInfo.FileLength) + if (this.m_fileWriter.Position != this.m_fileInfo.FileLength) { throw new Exception("已完成传输,但是文件长度不对。"); } @@ -114,7 +88,7 @@ namespace TouchSocket.Rpc.TouchRpc { File.Delete(this.m_path); } - this.fileWriter.SafeDispose(); + this.m_fileWriter.SafeDispose(); string rrqmPath = this.m_path + ".rrqm"; string tempPath = this.m_path + ".temp"; @@ -145,11 +119,11 @@ namespace TouchSocket.Rpc.TouchRpc { if (this.m_resume) { - if (DateTime.Now.TimeOfDay - this.m_lastTime > TimeSpan.FromMilliseconds(saveInterval)) + if (DateTime.Now.TimeOfDay - this.m_lastTime > TimeSpan.FromMilliseconds(m_saveInterval)) { try { - SerializeConvert.XmlSerializeToFile(this.m_fileInfo, this.m_path + ".temp"); + File.WriteAllText(this.m_path + ".temp", this.m_fileInfo.ToJsonString()); this.m_lastTime = DateTime.Now.TimeOfDay; } catch diff --git a/src/TouchSocket/Rpc/TouchRpc/Interface/Http/IHttpTouchRpcClient.cs b/src/TouchSocket/Rpc/TouchRpc/Interface/Http/IHttpTouchRpcClient.cs index aab794192e75f0683e1fb59c8a554c94b4654f4c..a9afd86922b364939016ad916300531eeadcb3a8 100644 --- a/src/TouchSocket/Rpc/TouchRpc/Interface/Http/IHttpTouchRpcClient.cs +++ b/src/TouchSocket/Rpc/TouchRpc/Interface/Http/IHttpTouchRpcClient.cs @@ -23,23 +23,6 @@ namespace TouchSocket.Rpc.TouchRpc /// public interface IHttpTouchRpcClient : IHttpClient, IHttpRpcClientBase, IRpcParser { - /// - /// 连接 - /// - /// 元数据 - /// 可取消操作令箭 - /// 验证超时时间 - /// - ITcpClient Connect(Metadata metadata = null, CancellationToken token = default, int timeout = 5000); - - /// - /// 异步连接 - /// - /// 元数据 - /// 可取消操作令箭 - /// 验证超时时间 - /// - Task ConnectAsync(Metadata metadata = null, CancellationToken token = default, int timeout = 5000); } /// diff --git a/src/TouchSocket/Rpc/TouchRpc/Interface/TCP/ITcpTouchRpcClient.cs b/src/TouchSocket/Rpc/TouchRpc/Interface/TCP/ITcpTouchRpcClient.cs index bab1426224a1e26916d46474c609c6d26ed28bb3..db87c21dbeb46ee48750d7ca09d7ac1da726e1cf 100644 --- a/src/TouchSocket/Rpc/TouchRpc/Interface/TCP/ITcpTouchRpcClient.cs +++ b/src/TouchSocket/Rpc/TouchRpc/Interface/TCP/ITcpTouchRpcClient.cs @@ -23,23 +23,6 @@ namespace TouchSocket.Rpc.TouchRpc /// public interface ITcpTouchRpcClient : ITcpRpcClientBase, ITcpClient, IRpcParser { - /// - /// 连接 - /// - /// 元数据 - /// 可取消操作令箭 - /// 验证超时时间 - /// - ITcpClient Connect(Metadata metadata = null, CancellationToken token = default, int timeout = 5000); - - /// - /// 异步连接 - /// - /// 元数据 - /// 可取消操作令箭 - /// 验证超时时间 - /// - Task ConnectAsync(Metadata metadata = null, CancellationToken token = default, int timeout = 5000); } /// diff --git a/src/TouchSocket/Rpc/TouchRpc/Serialization/DefaultSerializationSelector.cs b/src/TouchSocket/Rpc/TouchRpc/Serialization/DefaultSerializationSelector.cs index eaccdaa7ccc48c75a796497d3e6a91c45e6f751f..25c8d61ddacbb2b5f4ad8c829bbeaed862955eaa 100644 --- a/src/TouchSocket/Rpc/TouchRpc/Serialization/DefaultSerializationSelector.cs +++ b/src/TouchSocket/Rpc/TouchRpc/Serialization/DefaultSerializationSelector.cs @@ -38,9 +38,9 @@ namespace TouchSocket.Rpc.TouchRpc } switch (serializationType) { - case SerializationType.RRQMBinary: + case SerializationType.FastBinary: { - return SerializeConvert.RRQMBinaryDeserialize(parameterBytes, 0, parameterType); + return SerializeConvert.FastBinaryDeserialize(parameterBytes, 0, parameterType); } case SerializationType.Json: { @@ -69,9 +69,9 @@ namespace TouchSocket.Rpc.TouchRpc } switch (serializationType) { - case SerializationType.RRQMBinary: + case SerializationType.FastBinary: { - return SerializeConvert.RRQMBinarySerialize(parameter); + return SerializeConvert.FastBinarySerialize(parameter); } case SerializationType.Json: { diff --git a/src/TouchSocket/Rpc/TouchRpc/Stream/StreamOperator.cs b/src/TouchSocket/Rpc/TouchRpc/Stream/StreamOperator.cs index a9c87241582e77c9771fc5b22971c0b9ca995ceb..674b0aedf6335abd338d78def3c5b82e92b0c340 100644 --- a/src/TouchSocket/Rpc/TouchRpc/Stream/StreamOperator.cs +++ b/src/TouchSocket/Rpc/TouchRpc/Stream/StreamOperator.cs @@ -52,7 +52,7 @@ namespace TouchSocket.Rpc.TouchRpc /// /// 最大传输速度(企业版默认1024*1024字节,开源版不限速,所以此值无效。) /// - public int MaxSpeed => int.MaxValue; + public int MaxSpeed =>int.MaxValue; /// /// 超时时间,默认10*1000ms。 diff --git a/src/TouchSocket/Rpc/WebApi/Plugins/WebApiParserPlugin.cs b/src/TouchSocket/Rpc/WebApi/Plugins/WebApiParserPlugin.cs index cfd7fc3e282e1598dc7e49dfa10618b9437c217d..56526d35d56fb3d64f89b2be09ea636632ba5f92 100644 --- a/src/TouchSocket/Rpc/WebApi/Plugins/WebApiParserPlugin.cs +++ b/src/TouchSocket/Rpc/WebApi/Plugins/WebApiParserPlugin.cs @@ -27,9 +27,10 @@ namespace TouchSocket.Rpc.WebApi /// public class WebApiParserPlugin : HttpPluginBase, IRpcParser { - private readonly StringConverter m_converter; private readonly ActionMap m_actionMap; + private readonly StringConverter m_converter; private RpcStore m_rpcStore; + /// /// 构造函数 /// @@ -326,6 +327,7 @@ namespace TouchSocket.Rpc.WebApi { this.m_rpcStore = rpcService; } + #endregion RPC解析器 } } \ No newline at end of file diff --git a/src/TouchSocket/Sockets/Components/TCP/TcpClient.cs b/src/TouchSocket/Sockets/Components/TCP/TcpClient.cs index f319473d54629d1fdc7ef4d2befc0e3dd55a1bd1..09fe6acb1bd0f29a7402123da20c2132dea5ae97 100644 --- a/src/TouchSocket/Sockets/Components/TCP/TcpClient.cs +++ b/src/TouchSocket/Sockets/Components/TCP/TcpClient.cs @@ -335,6 +335,10 @@ namespace TouchSocket.Sockets /// public void Shutdown(SocketShutdown how) { + if (this.m_mainSocket == null) + { + return; + } this.m_mainSocket.Shutdown(how); } diff --git a/src/TouchSocket/Sockets/DelegateCollection.cs b/src/TouchSocket/Sockets/DelegateCollection.cs index a6c6e827155ab7c421fa3ae2f29cf89323284eea..cc3a74c1cd030f657992328753d585a6b42f018e 100644 --- a/src/TouchSocket/Sockets/DelegateCollection.cs +++ b/src/TouchSocket/Sockets/DelegateCollection.cs @@ -44,7 +44,7 @@ public delegate void ClientConnectingEventHandler(TClient client, Clien /// /// /// -public delegate void ClientDisconnectedEventHandler(TClient client, ClientDisconnectedEventArgs e); +public delegate void ClientDisconnectedEventHandler(TClient client, ClientDisconnectedEventArgs e); /// /// 正在连接事件 diff --git a/src/TouchSocket/Sockets/Extensions/ClientExtension.cs b/src/TouchSocket/Sockets/Extensions/ClientExtension.cs index 61efe9cc3df9fa4b1ec15d69b0a7c0715a341ac3..a5b9dc082464b4d344594f8d222001c4506dd84f 100644 --- a/src/TouchSocket/Sockets/Extensions/ClientExtension.cs +++ b/src/TouchSocket/Sockets/Extensions/ClientExtension.cs @@ -24,20 +24,6 @@ namespace TouchSocket.Sockets /// public static class ClientExtension { - /// - /// 使用断线重连。 - /// - /// 客户端 - /// 成功回调函数 - /// 尝试重连次数,设为-1时则永远尝试连接 - /// 是否输出日志。 - /// 失败时,停留时间 - public static T UseReconnection(this T client, int tryCount = 10, bool printLog = false, int sleepTime = 1000, Action successCallback = null) where T : ITcpClient, IPlguinObject - { - client.PluginsManager.Add(new ReconnectionPlugin(tryCount, printLog, sleepTime, successCallback)); - return client; - } - /// /// 获取相关信息。格式: ///IPPort=IP:Port,ID=id,Protocol=Protocol @@ -60,6 +46,10 @@ namespace TouchSocket.Sockets { try { + if (client==null||!client.Online) + { + return; + } client.Shutdown(how); } catch diff --git a/src/TouchSocket/Sockets/Interface/IClientSender.cs b/src/TouchSocket/Sockets/Interface/IClientSender.cs new file mode 100644 index 0000000000000000000000000000000000000000..6322dc0a39a60bab625727df1215f1c465eeb66f --- /dev/null +++ b/src/TouchSocket/Sockets/Interface/IClientSender.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace TouchSocket.Sockets +{ + /// + /// 客户端发送接口 + /// + public interface IClientSender : ISend + { + /// + /// 同步组合发送数据。 + /// 内部已经封装Ssl和发送长度检测,即:调用完成即表示数据全部发送完毕。 + /// 该发送会经过适配器封装,具体封装内容由适配器决定。 + /// + /// 组合数据 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void Send(IList transferBytes); + + /// + /// 异步组合发送数据。 + /// 时,如果使用独立线程发送,则不会触发异常。 + /// 时,相当于 + /// 该发送会经过适配器封装,具体封装内容由适配器决定。 + /// + /// 组合数据 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void SendAsync(IList transferBytes); + } + +} diff --git a/src/TouchSocket/Sockets/Interface/IDefaultSender.cs b/src/TouchSocket/Sockets/Interface/IDefaultSender.cs new file mode 100644 index 0000000000000000000000000000000000000000..23896dd2c6cd587d8d32c5c9ab3fc28d67f01a26 --- /dev/null +++ b/src/TouchSocket/Sockets/Interface/IDefaultSender.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TouchSocket.Core.ByteManager; + +namespace TouchSocket.Sockets +{ + /// + /// 具有直接发送功能 + /// + public interface IDefaultSender : ISendBase + { + #region 默认发送 + /// + /// 绕过适配器,直接发送字节流 + /// + /// 数据缓存区 + /// 偏移量 + /// 数据长度 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void DefaultSend(byte[] buffer, int offset, int length); + + /// + /// 绕过适配器,直接发送字节流 + /// + /// 数据缓存区 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void DefaultSend(byte[] buffer); + + /// + /// 绕过适配器,直接发送字节流 + /// + /// 数据块载体 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void DefaultSend(ByteBlock byteBlock); + #endregion + + #region 默认发送 + /// + /// 绕过适配器,直接发送字节流 + /// + /// 数据缓存区 + /// 偏移量 + /// 数据长度 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void DefaultSendAsync(byte[] buffer, int offset, int length); + + /// + /// 绕过适配器,直接发送字节流 + /// + /// 数据缓存区 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void DefaultSendAsync(byte[] buffer); + + /// + /// 绕过适配器,直接发送字节流 + /// + /// 数据块载体 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void DefaultSendAsync(ByteBlock byteBlock); + #endregion + } +} diff --git a/src/TouchSocket/Sockets/Interface/IIDSender.cs b/src/TouchSocket/Sockets/Interface/IIDSender.cs new file mode 100644 index 0000000000000000000000000000000000000000..0dbc690371abd8a30c6f18b58fcd64ad521f34ac --- /dev/null +++ b/src/TouchSocket/Sockets/Interface/IIDSender.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TouchSocket.Core.ByteManager; + +namespace TouchSocket.Sockets +{ + /// + /// 通过ID发送 + /// + public interface IIDSender + { + /// + /// 向对应ID的客户端发送 + /// + /// 目标ID + /// 数据 + /// 偏移 + /// 长度 + /// 未连接异常 + /// 未找到ID对应的客户端 + /// 其他异常 + void Send(string id, byte[] buffer, int offset, int length); + + /// + /// 向对应ID的客户端发送 + /// + /// 目标ID + /// 数据 + /// 未连接异常 + /// 未找到ID对应的客户端 + /// 其他异常 + void Send(string id, byte[] buffer); + + /// + /// 向对应ID的客户端发送 + /// + /// 目标ID + /// 数据 + /// 未连接异常 + /// 未找到ID对应的客户端 + /// 其他异常 + void Send(string id, ByteBlock byteBlock); + + /// + /// 向对应ID的客户端发送 + /// + /// 目标ID + /// 数据 + /// 偏移 + /// 长度 + /// 未连接异常 + /// 未找到ID对应的客户端 + /// 其他异常 + void SendAsync(string id, byte[] buffer, int offset, int length); + + /// + /// 向对应ID的客户端发送 + /// + /// 目标ID + /// 数据 + /// 未连接异常 + /// 未找到ID对应的客户端 + /// 其他异常 + void SendAsync(string id, byte[] buffer); + + /// + /// 向对应ID的客户端发送 + /// + /// 目标ID + /// 数据 + /// 未连接异常 + /// 未找到ID对应的客户端 + /// 其他异常 + void SendAsync(string id, ByteBlock byteBlock); + } +} diff --git a/src/TouchSocket/Sockets/Interface/ISend.cs b/src/TouchSocket/Sockets/Interface/ISend.cs new file mode 100644 index 0000000000000000000000000000000000000000..f68cae9ca627dad42f52f4dc706731e6d8c35d28 --- /dev/null +++ b/src/TouchSocket/Sockets/Interface/ISend.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; +using TouchSocket.Core.ByteManager; + +namespace TouchSocket.Sockets +{ + /// + /// 具有发送功能的接口 + /// + public interface ISend : ISendBase + { + /// + /// 同步发送数据。 + /// 内部已经封装Ssl和发送长度检测,即:调用完成即表示数据全部发送完毕。 + /// 该发送会经过适配器封装,具体封装内容由适配器决定。 + /// + /// 数据缓存区 + /// 偏移量 + /// 数据长度 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void Send(byte[] buffer, int offset, int length); + + /// + /// 同步发送数据。 + /// 内部已经封装Ssl和发送长度检测,即:调用完成即表示数据全部发送完毕。 + /// 该发送会经过适配器封装,具体封装内容由适配器决定。 + /// + /// 数据缓存区 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void Send(byte[] buffer); + + /// + /// 同步发送数据。 + /// 内部已经封装Ssl和发送长度检测,即:调用完成即表示数据全部发送完毕。 + /// 该发送会经过适配器封装,具体封装内容由适配器决定。 + /// + /// 数据块 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void Send(ByteBlock byteBlock); + + /// + /// 异步发送数据。 + /// 时,如果使用独立线程发送,则不会触发异常。 + /// 时,相当于 + /// 该发送会经过适配器封装,具体封装内容由适配器决定。 + /// + /// 数据缓存区 + /// 偏移量 + /// 数据长度 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void SendAsync(byte[] buffer, int offset, int length); + + /// + /// 异步发送数据。 + /// 时,如果使用独立线程发送,则不会触发异常。 + /// 时,相当于 + /// 该发送会经过适配器封装,具体封装内容由适配器决定。 + /// + /// 数据缓存区 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void SendAsync(byte[] buffer); + + /// + /// 异步发送数据。 + /// 时,如果使用独立线程发送,则不会触发异常。 + /// 时,相当于 + /// 该发送会经过适配器封装,具体封装内容由适配器决定。 + /// + /// 数据块 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void SendAsync(ByteBlock byteBlock); + } +} diff --git a/src/TouchSocket/Sockets/Interface/ISendBase.cs b/src/TouchSocket/Sockets/Interface/ISendBase.cs new file mode 100644 index 0000000000000000000000000000000000000000..02153711328610d2fd8bece28d9115cac5e44021 --- /dev/null +++ b/src/TouchSocket/Sockets/Interface/ISendBase.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TouchSocket.Sockets +{ + /// + /// 具有发送动作的基类。 + /// + public interface ISendBase + { + /// + /// 表示对象能否顺利执行发送操作。 + /// 由于高并发,当改值为Tru时,也不一定完全能执行。 + /// + bool CanSend { get; } + } +} diff --git a/src/TouchSocket/Sockets/Interface/ISendInterface.cs b/src/TouchSocket/Sockets/Interface/ISendInterface.cs deleted file mode 100644 index 9e3ea9e594644e48ba304426f3bda3eb5dc77b1e..0000000000000000000000000000000000000000 --- a/src/TouchSocket/Sockets/Interface/ISendInterface.cs +++ /dev/null @@ -1,468 +0,0 @@ -//------------------------------------------------------------------------------ -// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有 -// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权 -// CSDN博客:https://blog.csdn.net/qq_40374647 -// 哔哩哔哩视频:https://space.bilibili.com/94253567 -// Gitee源代码仓库:https://gitee.com/RRQM_Home -// Github源代码仓库:https://github.com/RRQM -// API首页:https://www.yuque.com/rrqm/touchsocket/index -// 交流QQ群:234762506 -// 感谢您的下载和使用 -//------------------------------------------------------------------------------ -//------------------------------------------------------------------------------ -using System; -using System.Collections.Generic; -using System.Net; -using System.Net.Sockets; -using System.Threading; -using System.Threading.Tasks; -using TouchSocket.Core.ByteManager; - -namespace TouchSocket.Sockets -{ - /// - /// 客户端发送接口 - /// - public interface IClientSender : ISend - { - /// - /// 同步组合发送数据。 - /// 内部已经封装Ssl和发送长度检测,即:调用完成即表示数据全部发送完毕。 - /// 该发送会经过适配器封装,具体封装内容由适配器决定。 - /// - /// 组合数据 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void Send(IList transferBytes); - - /// - /// 异步组合发送数据。 - /// 时,如果使用独立线程发送,则不会触发异常。 - /// 时,相当于 - /// 该发送会经过适配器封装,具体封装内容由适配器决定。 - /// - /// 组合数据 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void SendAsync(IList transferBytes); - } - - /// - /// 具有Udp终结点的发送 - /// - public interface IUdpClientSender : ISend - { - /// - /// 同步组合发送数据。 - /// 内部已经封装Ssl和发送长度检测,即:调用完成即表示数据全部发送完毕。 - /// 该发送会经过适配器封装,具体封装内容由适配器决定。 - /// - /// 远程终结点 - /// 组合数据 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void Send(EndPoint endPoint, IList transferBytes); - - /// - /// 异步组合发送数据。 - /// 时,如果使用独立线程发送,则不会触发异常。 - /// 时,相当于 - /// 该发送会经过适配器封装,具体封装内容由适配器决定。 - /// - /// 远程终结点 - /// 组合数据 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void SendAsync(EndPoint endPoint, IList transferBytes); - } - - /// - /// 具有直接发送功能 - /// - public interface IDefaultSender : ISendBase - { - #region 默认发送 - /// - /// 绕过适配器,直接发送字节流 - /// - /// 数据缓存区 - /// 偏移量 - /// 数据长度 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void DefaultSend(byte[] buffer, int offset, int length); - - /// - /// 绕过适配器,直接发送字节流 - /// - /// 数据缓存区 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void DefaultSend(byte[] buffer); - - /// - /// 绕过适配器,直接发送字节流 - /// - /// 数据块载体 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void DefaultSend(ByteBlock byteBlock); - #endregion - - #region 默认发送 - /// - /// 绕过适配器,直接发送字节流 - /// - /// 数据缓存区 - /// 偏移量 - /// 数据长度 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void DefaultSendAsync(byte[] buffer, int offset, int length); - - /// - /// 绕过适配器,直接发送字节流 - /// - /// 数据缓存区 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void DefaultSendAsync(byte[] buffer); - - /// - /// 绕过适配器,直接发送字节流 - /// - /// 数据块载体 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void DefaultSendAsync(ByteBlock byteBlock); - #endregion - } - - /// - /// 具有直接发送功能 - /// - public interface IUdpDefaultSender : ISendBase - { - #region 默认发送 - /// - /// 绕过适配器,直接发送字节流 - /// - /// 目的终结点 - /// 数据缓存区 - /// 偏移量 - /// 数据长度 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void DefaultSend(EndPoint endPoint, byte[] buffer, int offset, int length); - - /// - /// 绕过适配器,直接发送字节流 - /// - /// 目的终结点 - /// 数据缓存区 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void DefaultSend(EndPoint endPoint, byte[] buffer); - - /// - /// 绕过适配器,直接发送字节流 - /// - /// 目的终结点 - /// 数据块载体 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void DefaultSend(EndPoint endPoint, ByteBlock byteBlock); - #endregion - - #region 默认发送 - /// - /// 绕过适配器,直接发送字节流 - /// - /// 目的终结点 - /// 数据缓存区 - /// 偏移量 - /// 数据长度 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void DefaultSendAsync(EndPoint endPoint, byte[] buffer, int offset, int length); - - /// - /// 绕过适配器,直接发送字节流 - /// - /// 目的终结点 - /// 数据缓存区 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void DefaultSendAsync(EndPoint endPoint, byte[] buffer); - - /// - /// 绕过适配器,直接发送字节流 - /// - /// 目的终结点 - /// 数据块载体 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void DefaultSendAsync(EndPoint endPoint, ByteBlock byteBlock); - #endregion - } - - /// - /// 具有发送动作的基类。 - /// - public interface ISendBase - { - /// - /// 表示对象能否顺利执行发送操作。 - /// 由于高并发,当改值为Tru时,也不一定完全能执行。 - /// - bool CanSend { get; } - } - - /// - /// 具有发送功能的接口 - /// - public interface ISend : ISendBase - { - /// - /// 同步发送数据。 - /// 内部已经封装Ssl和发送长度检测,即:调用完成即表示数据全部发送完毕。 - /// 该发送会经过适配器封装,具体封装内容由适配器决定。 - /// - /// 数据缓存区 - /// 偏移量 - /// 数据长度 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void Send(byte[] buffer, int offset, int length); - - /// - /// 同步发送数据。 - /// 内部已经封装Ssl和发送长度检测,即:调用完成即表示数据全部发送完毕。 - /// 该发送会经过适配器封装,具体封装内容由适配器决定。 - /// - /// 数据缓存区 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void Send(byte[] buffer); - - /// - /// 同步发送数据。 - /// 内部已经封装Ssl和发送长度检测,即:调用完成即表示数据全部发送完毕。 - /// 该发送会经过适配器封装,具体封装内容由适配器决定。 - /// - /// 数据块 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void Send(ByteBlock byteBlock); - - /// - /// 异步发送数据。 - /// 时,如果使用独立线程发送,则不会触发异常。 - /// 时,相当于 - /// 该发送会经过适配器封装,具体封装内容由适配器决定。 - /// - /// 数据缓存区 - /// 偏移量 - /// 数据长度 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void SendAsync(byte[] buffer, int offset, int length); - - /// - /// 异步发送数据。 - /// 时,如果使用独立线程发送,则不会触发异常。 - /// 时,相当于 - /// 该发送会经过适配器封装,具体封装内容由适配器决定。 - /// - /// 数据缓存区 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void SendAsync(byte[] buffer); - - /// - /// 异步发送数据。 - /// 时,如果使用独立线程发送,则不会触发异常。 - /// 时,相当于 - /// 该发送会经过适配器封装,具体封装内容由适配器决定。 - /// - /// 数据块 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - void SendAsync(ByteBlock byteBlock); - } - - /// - /// 通过ID发送 - /// - public interface IIDSender - { - /// - /// 向对应ID的客户端发送 - /// - /// 目标ID - /// 数据 - /// 偏移 - /// 长度 - /// 未连接异常 - /// 未找到ID对应的客户端 - /// 其他异常 - void Send(string id, byte[] buffer, int offset, int length); - - /// - /// 向对应ID的客户端发送 - /// - /// 目标ID - /// 数据 - /// 未连接异常 - /// 未找到ID对应的客户端 - /// 其他异常 - void Send(string id, byte[] buffer); - - /// - /// 向对应ID的客户端发送 - /// - /// 目标ID - /// 数据 - /// 未连接异常 - /// 未找到ID对应的客户端 - /// 其他异常 - void Send(string id, ByteBlock byteBlock); - - /// - /// 向对应ID的客户端发送 - /// - /// 目标ID - /// 数据 - /// 偏移 - /// 长度 - /// 未连接异常 - /// 未找到ID对应的客户端 - /// 其他异常 - void SendAsync(string id, byte[] buffer, int offset, int length); - - /// - /// 向对应ID的客户端发送 - /// - /// 目标ID - /// 数据 - /// 未连接异常 - /// 未找到ID对应的客户端 - /// 其他异常 - void SendAsync(string id, byte[] buffer); - - /// - /// 向对应ID的客户端发送 - /// - /// 目标ID - /// 数据 - /// 未连接异常 - /// 未找到ID对应的客户端 - /// 其他异常 - void SendAsync(string id, ByteBlock byteBlock); - } - - - /// - /// 发送等待接口 - /// - public interface IWaitSender : ISendBase - { - /// - /// 发送字节流 - /// - /// 数据缓存区 - /// 偏移 - /// 长度 - /// 超时时间 - /// 取消令箭 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - /// 返回的数据 - byte[] SendThenReturn(byte[] buffer, int offset, int length, int timeout = 1000 * 5, CancellationToken token = default); - - /// - /// 发送字节流 - /// - /// 数据缓存区 - /// 超时时间 - /// 取消令箭 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - /// 返回的数据 - byte[] SendThenReturn(byte[] buffer, int timeout = 1000 * 5, CancellationToken token = default); - - /// - /// 发送流中的有效数据 - /// - /// 数据块载体 - /// 超时时间 - /// 取消令箭 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - /// 返回的数据 - byte[] SendThenReturn(ByteBlock byteBlock, int timeout = 1000 * 5, CancellationToken token = default); - - /// - /// 异步发送 - /// - /// 数据缓存区 - /// 偏移 - /// 长度 - /// 超时时间 - /// 取消令箭 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - /// 返回的数据 - Task SendThenReturnAsync(byte[] buffer, int offset, int length, int timeout = 1000 * 5, CancellationToken token = default); - - /// - /// 异步发送 - /// - /// 数据缓存区 - /// 超时时间 - /// 取消令箭 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - /// 返回的数据 - Task SendThenReturnAsync(byte[] buffer, int timeout = 1000 * 5, CancellationToken token = default); - - /// - /// 异步发送 - /// - /// 数据块载体 - /// 超时时间 - /// 取消令箭 - /// 客户端没有连接 - /// 发送数据超长 - /// 其他异常 - /// 返回的数据 - Task SendThenReturnAsync(ByteBlock byteBlock, int timeout = 1000 * 5, CancellationToken token = default); - } -} \ No newline at end of file diff --git a/src/TouchSocket/Sockets/Interface/IUdpClientSender.cs b/src/TouchSocket/Sockets/Interface/IUdpClientSender.cs new file mode 100644 index 0000000000000000000000000000000000000000..7479abf980c38991a26878534f4a956543328d8c --- /dev/null +++ b/src/TouchSocket/Sockets/Interface/IUdpClientSender.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace TouchSocket.Sockets +{ + /// + /// 具有Udp终结点的发送 + /// + public interface IUdpClientSender : ISend + { + /// + /// 同步组合发送数据。 + /// 内部已经封装Ssl和发送长度检测,即:调用完成即表示数据全部发送完毕。 + /// 该发送会经过适配器封装,具体封装内容由适配器决定。 + /// + /// 远程终结点 + /// 组合数据 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void Send(EndPoint endPoint, IList transferBytes); + + /// + /// 异步组合发送数据。 + /// 时,如果使用独立线程发送,则不会触发异常。 + /// 时,相当于 + /// 该发送会经过适配器封装,具体封装内容由适配器决定。 + /// + /// 远程终结点 + /// 组合数据 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void SendAsync(EndPoint endPoint, IList transferBytes); + } +} diff --git a/src/TouchSocket/Sockets/Interface/IUdpDefaultSender.cs b/src/TouchSocket/Sockets/Interface/IUdpDefaultSender.cs new file mode 100644 index 0000000000000000000000000000000000000000..b6e0422768d9526e522adc782de13e13e8c44951 --- /dev/null +++ b/src/TouchSocket/Sockets/Interface/IUdpDefaultSender.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using TouchSocket.Core.ByteManager; + +namespace TouchSocket.Sockets +{ + /// + /// 具有直接发送功能 + /// + public interface IUdpDefaultSender : ISendBase + { + #region 默认发送 + /// + /// 绕过适配器,直接发送字节流 + /// + /// 目的终结点 + /// 数据缓存区 + /// 偏移量 + /// 数据长度 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void DefaultSend(EndPoint endPoint, byte[] buffer, int offset, int length); + + /// + /// 绕过适配器,直接发送字节流 + /// + /// 目的终结点 + /// 数据缓存区 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void DefaultSend(EndPoint endPoint, byte[] buffer); + + /// + /// 绕过适配器,直接发送字节流 + /// + /// 目的终结点 + /// 数据块载体 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void DefaultSend(EndPoint endPoint, ByteBlock byteBlock); + #endregion + + #region 默认发送 + /// + /// 绕过适配器,直接发送字节流 + /// + /// 目的终结点 + /// 数据缓存区 + /// 偏移量 + /// 数据长度 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void DefaultSendAsync(EndPoint endPoint, byte[] buffer, int offset, int length); + + /// + /// 绕过适配器,直接发送字节流 + /// + /// 目的终结点 + /// 数据缓存区 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void DefaultSendAsync(EndPoint endPoint, byte[] buffer); + + /// + /// 绕过适配器,直接发送字节流 + /// + /// 目的终结点 + /// 数据块载体 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + void DefaultSendAsync(EndPoint endPoint, ByteBlock byteBlock); + #endregion + } +} diff --git a/src/TouchSocket/Sockets/Interface/IUdpSessionInterface.cs b/src/TouchSocket/Sockets/Interface/IUdpSession.cs similarity index 100% rename from src/TouchSocket/Sockets/Interface/IUdpSessionInterface.cs rename to src/TouchSocket/Sockets/Interface/IUdpSession.cs diff --git a/src/TouchSocket/Sockets/Interface/IWaitSender.cs b/src/TouchSocket/Sockets/Interface/IWaitSender.cs new file mode 100644 index 0000000000000000000000000000000000000000..1613fd5456c5b6b1c8b1ab3eb2789835f5eff51c --- /dev/null +++ b/src/TouchSocket/Sockets/Interface/IWaitSender.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using TouchSocket.Core.ByteManager; + +namespace TouchSocket.Sockets +{ + /// + /// 发送等待接口 + /// + public interface IWaitSender : ISendBase + { + /// + /// 发送字节流 + /// + /// 数据缓存区 + /// 偏移 + /// 长度 + /// 超时时间 + /// 取消令箭 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + /// 返回的数据 + byte[] SendThenReturn(byte[] buffer, int offset, int length, int timeout = 1000 * 5, CancellationToken token = default); + + /// + /// 发送字节流 + /// + /// 数据缓存区 + /// 超时时间 + /// 取消令箭 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + /// 返回的数据 + byte[] SendThenReturn(byte[] buffer, int timeout = 1000 * 5, CancellationToken token = default); + + /// + /// 发送流中的有效数据 + /// + /// 数据块载体 + /// 超时时间 + /// 取消令箭 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + /// 返回的数据 + byte[] SendThenReturn(ByteBlock byteBlock, int timeout = 1000 * 5, CancellationToken token = default); + + /// + /// 异步发送 + /// + /// 数据缓存区 + /// 偏移 + /// 长度 + /// 超时时间 + /// 取消令箭 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + /// 返回的数据 + Task SendThenReturnAsync(byte[] buffer, int offset, int length, int timeout = 1000 * 5, CancellationToken token = default); + + /// + /// 异步发送 + /// + /// 数据缓存区 + /// 超时时间 + /// 取消令箭 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + /// 返回的数据 + Task SendThenReturnAsync(byte[] buffer, int timeout = 1000 * 5, CancellationToken token = default); + + /// + /// 异步发送 + /// + /// 数据块载体 + /// 超时时间 + /// 取消令箭 + /// 客户端没有连接 + /// 发送数据超长 + /// 其他异常 + /// 返回的数据 + Task SendThenReturnAsync(ByteBlock byteBlock, int timeout = 1000 * 5, CancellationToken token = default); + } +} diff --git a/src/TouchSocket/TouchSocket.csproj b/src/TouchSocket/TouchSocket.csproj index ccd32932ff69d1800e6d4c5e9ee85ca36fe009ba..0f54551f5f6769de04129d44d69d08635d504e0b 100644 --- a/src/TouchSocket/TouchSocket.csproj +++ b/src/TouchSocket/TouchSocket.csproj @@ -4,7 +4,7 @@ logo.ico true RRQM.pfx - 0.1.4 + 0.2.0 8.0 若汝棋茗 Copyright © 2022 若汝棋茗