Skip to content

Commit ace601b

Browse files
committed
Remove reflection invoke.
1 parent 0644214 commit ace601b

File tree

9 files changed

+116
-76
lines changed

9 files changed

+116
-76
lines changed

Quick.Protocol.Pipeline/QpPipelineClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Quick.Protocol.Pipeline
1212
[DisplayName("命名管道")]
1313
public class QpPipelineClient : QpClient
1414
{
15-
private QpPipelineClientOptions options;
15+
private readonly QpPipelineClientOptions options;
1616
private NamedPipeClientStream pipeClientStream;
1717
public QpPipelineClient(QpPipelineClientOptions options) : base(options)
1818
{

Quick.Protocol.SerialPort/QpSerialPortClientOptions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,28 @@ public override QpClient CreateClient()
5757
return new QpSerialPortClient(this);
5858
}
5959

60+
protected override void LoadFromQueryString(string key, string value)
61+
{
62+
switch (key)
63+
{
64+
case nameof(BaudRate):
65+
BaudRate = int.Parse(value);
66+
break;
67+
case nameof(Parity):
68+
Parity = Enum.Parse<Parity>(value);
69+
break;
70+
case nameof(DataBits):
71+
DataBits = int.Parse(value);
72+
break;
73+
case nameof(StopBits):
74+
StopBits = Enum.Parse<StopBits>(value);
75+
break;
76+
default:
77+
base.LoadFromQueryString(key, value);
78+
break;
79+
}
80+
}
81+
6082
protected override void LoadFromUri(Uri uri)
6183
{
6284
PortName = uri.Host;

Quick.Protocol.Tcp/QpTcpClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Quick.Protocol.Tcp
1515
public class QpTcpClient : QpClient
1616
{
1717
private TcpClient tcpClient;
18-
private QpTcpClientOptions options;
18+
private readonly QpTcpClientOptions options;
1919

2020
public QpTcpClient(QpTcpClientOptions options) : base(options)
2121
{
@@ -27,10 +27,10 @@ protected override async Task<Stream> InnerConnectAsync()
2727
if (tcpClient != null)
2828
Close();
2929
//开始连接
30-
if (string.IsNullOrEmpty(options.LocalHost))
31-
tcpClient = new TcpClient();
30+
if (!string.IsNullOrEmpty(options.LocalHost) && options.LocalPort != null)
31+
tcpClient = new TcpClient(new IPEndPoint(IPAddress.Parse(options.LocalHost), options.LocalPort.Value));
3232
else
33-
tcpClient = new TcpClient(new IPEndPoint(IPAddress.Parse(options.LocalHost), options.LocalPort));
33+
tcpClient = new TcpClient();
3434

3535
CancellationTokenSource cts = new CancellationTokenSource();
3636
var connectTask = tcpClient.ConnectAsync(Dns.GetHostAddresses(options.Host), options.Port, cts.Token).AsTask();

Quick.Protocol.Tcp/QpTcpClientOptions.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class QpTcpClientOptions : QpClientOptions
3737
/// </summary>
3838
[DisplayName("本地端口")]
3939
[Category("高级")]
40-
public int LocalPort { get; set; } = 0;
40+
public int? LocalPort { get; set; }
4141

4242
public override void Check()
4343
{
@@ -53,6 +53,22 @@ public override QpClient CreateClient()
5353
return new QpTcpClient(this);
5454
}
5555

56+
protected override void LoadFromQueryString(string key, string value)
57+
{
58+
switch (key)
59+
{
60+
case nameof(LocalHost):
61+
LocalHost = value;
62+
break;
63+
case nameof(LocalPort):
64+
LocalPort = int.Parse(value);
65+
break;
66+
default:
67+
base.LoadFromQueryString(key, value);
68+
break;
69+
}
70+
}
71+
5672
protected override void LoadFromUri(Uri uri)
5773
{
5874
Host = uri.Host;

Quick.Protocol/QpChannel.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Threading;
1313
using System.Threading.Tasks;
1414
using System.Text.Json.Serialization;
15+
using System.Collections.ObjectModel;
1516

1617
namespace Quick.Protocol
1718
{
@@ -33,8 +34,8 @@ public abstract class QpChannel
3334
/// <summary>
3435
/// 心跳包
3536
/// </summary>
36-
private static byte[] HEARTBEAT_PACKAGHE = new byte[] { 0, 0, 0, 5, 0 };
37-
private static ArraySegment<byte> nullArraySegment = new ArraySegment<byte>();
37+
private static readonly byte[] HEARTBEAT_PACKAGHE = new byte[] { 0, 0, 0, 5, 0 };
38+
private static readonly ArraySegment<byte> nullArraySegment = new ArraySegment<byte>();
3839
//接收缓存
3940
private byte[] recvBuffer;
4041
private byte[] recvBuffer2;
@@ -43,26 +44,26 @@ public abstract class QpChannel
4344
private byte[] sendBuffer2;
4445

4546
private Stream QpPackageHandler_Stream;
46-
private QpChannelOptions options;
47+
private readonly QpChannelOptions options;
4748
private DateTime lastSendPackageTime = DateTime.MinValue;
4849

49-
private byte[] passwordMd5Buffer;
50-
private ICryptoTransform enc;
51-
private ICryptoTransform dec;
52-
private Encoding encoding = Encoding.UTF8;
50+
private readonly byte[] passwordMd5Buffer;
51+
private readonly ICryptoTransform enc;
52+
private readonly ICryptoTransform dec;
53+
private readonly Encoding encoding = Encoding.UTF8;
5354

5455
private Task sendPackageTask = Task.CompletedTask;
5556
//发送包锁对象
56-
private object SEND_PACKAGE_LOCK_OBJ = new object();
57+
private readonly object SEND_PACKAGE_LOCK_OBJ = new object();
5758
//断开连接锁对象
58-
private object DISCONNECT_LOCK_OBJ = new object();
59+
private readonly object DISCONNECT_LOCK_OBJ = new object();
5960

60-
private Dictionary<Type, JsonSerializerContext> typeSerializerContextDict = new Dictionary<Type, JsonSerializerContext>();
61-
private Dictionary<string, Type> commandRequestTypeDict = new Dictionary<string, Type>();
62-
private Dictionary<string, Type> commandResponseTypeDict = new Dictionary<string, Type>();
63-
private Dictionary<Type, Type> commandRequestTypeResponseTypeDict = new Dictionary<Type, Type>();
61+
private readonly Dictionary<Type, JsonSerializerContext> typeSerializerContextDict = new Dictionary<Type, JsonSerializerContext>();
62+
private readonly Dictionary<string, Type> commandRequestTypeDict = new Dictionary<string, Type>();
63+
private readonly Dictionary<string, Type> commandResponseTypeDict = new Dictionary<string, Type>();
64+
private readonly Dictionary<Type, Type> commandRequestTypeResponseTypeDict = new Dictionary<Type, Type>();
6465

65-
private ConcurrentDictionary<string, CommandContext> commandDict = new ConcurrentDictionary<string, CommandContext>();
66+
private readonly ConcurrentDictionary<string, CommandContext> commandDict = new ConcurrentDictionary<string, CommandContext>();
6667

6768
private bool _IsConnected = false;
6869
/// <summary>
@@ -94,7 +95,7 @@ protected set
9495
public string AuthenticateQuestion { get; protected set; }
9596

9697
//长整型数字的一半,统计大于这个数时,统计计数归零,防止溢出
97-
private long LONG_HALF_MAX_VALUE = long.MaxValue / 2;
98+
private readonly long LONG_HALF_MAX_VALUE = long.MaxValue / 2;
9899
/// <summary>
99100
/// 总共接收到的字节数量
100101
/// </summary>
@@ -217,7 +218,7 @@ private JsonSerializerContext getTypeSerializerContext(Type type)
217218
/// </summary>
218219
public object Tag { get; set; }
219220

220-
private Dictionary<string, Type> noticeTypeDict = new Dictionary<string, Type>();
221+
private readonly Dictionary<string, Type> noticeTypeDict = new Dictionary<string, Type>();
221222

222223
public QpChannel(QpChannelOptions options)
223224
{
@@ -295,7 +296,7 @@ protected virtual void OnReadError(Exception exception)
295296
}
296297

297298
//获取空闲的缓存
298-
private byte[] getFreeBuffer(byte[] usingBuffer, params byte[][] bufferArray)
299+
private static byte[] getFreeBuffer(byte[] usingBuffer, params byte[][] bufferArray)
299300
{
300301
foreach (var buffer in bufferArray)
301302
{
@@ -374,7 +375,7 @@ await writeTask
374375
await stream.FlushAsync().ConfigureAwait(false);
375376
}
376377

377-
private void writePackageTotalLengthToBuffer(byte[] buffer, int offset, int packageTotalLength)
378+
private static void writePackageTotalLengthToBuffer(byte[] buffer, int offset, int packageTotalLength)
378379
{
379380
//构造包头
380381
var ret = BitConverter.GetBytes(packageTotalLength);

Quick.Protocol/QpClientOptions.cs

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
34
using System.ComponentModel;
45
using System.Reflection;
56
using System.Text;
@@ -67,47 +68,46 @@ public virtual QpClient CreateClient()
6768
throw new NotImplementedException();
6869
}
6970

71+
protected virtual void LoadFromQueryString(string key, string value)
72+
{
73+
switch (key)
74+
{
75+
case nameof(ConnectionTimeout):
76+
ConnectionTimeout = int.Parse(value);
77+
break;
78+
case nameof(EnableCompress):
79+
EnableCompress = bool.Parse(value);
80+
break;
81+
case nameof(EnableEncrypt):
82+
EnableEncrypt = bool.Parse(value);
83+
break;
84+
case nameof(EnableNetstat):
85+
EnableNetstat = bool.Parse(value);
86+
break;
87+
case nameof(MaxPackageSize):
88+
MaxPackageSize = int.Parse(value);
89+
break;
90+
case nameof(Password):
91+
Password = value;
92+
break;
93+
case nameof(RaiseNoticePackageReceivedEvent):
94+
RaiseNoticePackageReceivedEvent = bool.Parse(value);
95+
break;
96+
case nameof(TransportTimeout):
97+
TransportTimeout = int.Parse(value);
98+
break;
99+
}
100+
}
101+
70102
protected virtual void LoadFromUri(Uri uri)
71103
{
72104
if (string.IsNullOrEmpty(uri.Query))
73105
return;
74106
var queryString = System.Web.HttpUtility.ParseQueryString(uri.Query);
75-
var type = this.GetType();
76107
foreach (var key in queryString.AllKeys)
77108
{
78-
var pi = type.GetProperty(key);
79-
if (pi == null)
80-
continue;
81-
var propertyType = pi.PropertyType;
82-
var stringValue = queryString[key];
83-
object propertyValue = stringValue;
84-
if (propertyType == typeof(bool))
85-
propertyValue = Convert.ToBoolean(stringValue);
86-
else if (propertyType == typeof(byte))
87-
propertyValue = Convert.ToByte(stringValue);
88-
else if (propertyType == typeof(sbyte))
89-
propertyValue = Convert.ToSByte(stringValue);
90-
else if (propertyType == typeof(short))
91-
propertyValue = Convert.ToInt16(stringValue);
92-
else if (propertyType == typeof(ushort))
93-
propertyValue = Convert.ToUInt16(stringValue);
94-
else if (propertyType == typeof(int))
95-
propertyValue = Convert.ToInt32(stringValue);
96-
else if (propertyType == typeof(uint))
97-
propertyValue = Convert.ToUInt32(stringValue);
98-
else if (propertyType == typeof(long))
99-
propertyValue = Convert.ToInt64(stringValue);
100-
else if (propertyType == typeof(ulong))
101-
propertyValue = Convert.ToUInt64(stringValue);
102-
else if (propertyType == typeof(float))
103-
propertyValue = Convert.ToSingle(stringValue);
104-
else if (propertyType == typeof(double))
105-
propertyValue = Convert.ToDouble(stringValue);
106-
else if (propertyType == typeof(decimal))
107-
propertyValue = Convert.ToDecimal(stringValue);
108-
else if (propertyType == typeof(DateTime))
109-
propertyValue = Convert.ToDateTime(stringValue);
110-
pi.SetValue(this, propertyValue);
109+
var value = queryString[key];
110+
LoadFromQueryString(key, value);
111111
}
112112
}
113113

@@ -118,14 +118,13 @@ public Uri ToUri(bool includePassword = false, bool includeOtherProperty = false
118118
HashSet<string> ignorePropertyNames = new HashSet<string>();
119119
ignorePropertyNames.Add(nameof(HeartBeatInterval));
120120
if (!includePassword)
121-
ignorePropertyNames.Add(nameof(Password));
122-
121+
ignorePropertyNames.Add(nameof(Password));
123122
string baseUrl = ToUriBasic(ignorePropertyNames);
124123
if (includePassword || includeOtherProperty)
125124
{
126125
StringBuilder sb = new StringBuilder(baseUrl);
127126
int currentIndex = 0;
128-
var jObj = JsonNode.Parse(JsonSerializer.Serialize(this,this.GetType(), GetJsonSerializerContext())).AsObject();
127+
var jObj = JsonNode.Parse(JsonSerializer.Serialize(this, GetType(), GetJsonSerializerContext())).AsObject();
129128
foreach (var property in jObj)
130129
{
131130
var key = property.Key;
@@ -134,12 +133,14 @@ public Uri ToUri(bool includePassword = false, bool includeOtherProperty = false
134133
if (!includeOtherProperty && key != nameof(Password))
135134
continue;
136135
if (currentIndex == 0)
137-
sb.Append("?");
136+
sb.Append('?');
138137
if (currentIndex > 0)
139-
sb.Append("&");
138+
sb.Append('&');
140139
currentIndex++;
141140

142-
var value = property.Value.ToString();
141+
var value = property.Value?.ToString();
142+
if (string.IsNullOrEmpty(value))
143+
continue;
143144
value = System.Web.HttpUtility.UrlEncode(value);
144145
sb.Append($"{key}={value}");
145146
}
@@ -151,7 +152,7 @@ public Uri ToUri(bool includePassword = false, bool includeOtherProperty = false
151152

152153
public override string ToString() => ToUri().ToString();
153154

154-
private static Dictionary<string, Func<QpClientOptions>> schemaQpClientOptionsFactoryDict = new Dictionary<string, Func<QpClientOptions>>();
155+
private static readonly Dictionary<string, Func<QpClientOptions>> schemaQpClientOptionsFactoryDict = new Dictionary<string, Func<QpClientOptions>>();
155156

156157
public static void RegisterUriSchema(string schema, Func<QpClientOptions> optionsFactory)
157158
{

Quick.Protocol/QpCommandInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public class QpCommandInfo
4848
[DisplayName("响应示例")]
4949
[ReadOnly(true)]
5050
public string ResponseTypeSchemaSample { get; set; }
51-
private JsonSerializerContext jsonSerializerContext;
51+
private readonly JsonSerializerContext jsonSerializerContext;
5252
public JsonSerializerContext GetJsonSerializerContext() => jsonSerializerContext;
5353

54-
private Type requestType;
54+
private readonly Type requestType;
5555

56-
private Type responseType;
56+
private readonly Type responseType;
5757

5858
public QpCommandInfo() { }
5959
public QpCommandInfo(string name, string description,

Quick.Protocol/QpNoticeInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Quick.Protocol
1414
/// </summary>
1515
public class QpNoticeInfo
1616
{
17-
private Type noticeType;
17+
private readonly Type noticeType;
1818

1919
/// <summary>
2020
/// 名称
@@ -35,7 +35,7 @@ public class QpNoticeInfo
3535
[ReadOnly(true)]
3636
public string NoticeTypeName { get; set; }
3737

38-
private JsonSerializerContext jsonSerializerContext;
38+
private readonly JsonSerializerContext jsonSerializerContext;
3939
public JsonSerializerContext GetJsonSerializerContext() => jsonSerializerContext;
4040

4141
public QpNoticeInfo() { }

Quick.Protocol/QpServerChannel.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ namespace Quick.Protocol
1111
{
1212
public class QpServerChannel : QpChannel
1313
{
14-
private Stream stream;
15-
private CancellationTokenSource cts;
16-
private QpServerOptions options;
17-
private string channelName;
14+
private readonly Stream stream;
15+
private readonly CancellationTokenSource cts;
16+
private readonly QpServerOptions options;
17+
private readonly string channelName;
1818
//通过认证后,才允许使用的命令执行管理器列表
19-
private List<CommandExecuterManager> authedCommandExecuterManagerList = null;
19+
private readonly List<CommandExecuterManager> authedCommandExecuterManagerList = null;
2020
//通过认证后,才允许使用的通知处理器管理器列表
21-
private List<NoticeHandlerManager> authedNoticeHandlerManagerList = null;
21+
private readonly List<NoticeHandlerManager> authedNoticeHandlerManagerList = null;
2222

2323
public override string ChannelName => channelName;
2424

0 commit comments

Comments
 (0)