Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
若汝棋茗
RRQMSocket
提交
465e4a91
RRQMSocket
项目概览
若汝棋茗
/
RRQMSocket
大约 1 年 前同步成功
通知
20
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
RRQMSocket
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
465e4a91
编写于
7月 26, 2022
作者:
若
若汝棋茗
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
优化Mapper,新增端口复用
上级
8545b659
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
118 addition
and
78 deletion
+118
-78
src/TouchSocket/Core/Common/Mapper.cs
src/TouchSocket/Core/Common/Mapper.cs
+67
-69
src/TouchSocket/Core/Reflection/Property.cs
src/TouchSocket/Core/Reflection/Property.cs
+20
-8
src/TouchSocket/Sockets/Components/TCP/TcpClient.cs
src/TouchSocket/Sockets/Components/TCP/TcpClient.cs
+4
-0
src/TouchSocket/Sockets/Components/TCP/TcpService.cs
src/TouchSocket/Sockets/Components/TCP/TcpService.cs
+4
-0
src/TouchSocket/Sockets/Components/UDP/UdpSession.cs
src/TouchSocket/Sockets/Components/UDP/UdpSession.cs
+4
-0
src/TouchSocket/Sockets/Config/TouchSocketConfigExtension.cs
src/TouchSocket/Sockets/Config/TouchSocketConfigExtension.cs
+18
-0
src/TouchSocket/TouchSocket.csproj
src/TouchSocket/TouchSocket.csproj
+1
-1
未找到文件。
src/TouchSocket/Core/Common/Mapper.cs
浏览文件 @
465e4a91
using
System
;
using
System.Collections.Concurrent
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Reflection
;
using
System.Text
;
using
System.Threading.Tasks
;
using
TouchSocket.Core.Reflection
;
namespace
TouchSocketPro
{
...
...
@@ -11,102 +14,97 @@ namespace TouchSocketPro
/// </summary>
public
static
class
Mapper
{
private
static
readonly
ConcurrentDictionary
<
Type
,
Dictionary
<
string
,
Property
>>
m_typeToProperty
=
new
ConcurrentDictionary
<
Type
,
Dictionary
<
string
,
Property
>>();
/// <summary>
/// 简单映射
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T1"></typeparam>
/// <param name="t"></param>
/// <typeparam name="TTarget"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public
static
T
1
Map
<
T
,
T1
>(
T
t
)
where
T
:
class
where
T1
:
class
,
new
()
public
static
T
Target
Map
<
TTarget
>(
object
source
)
where
TTarget
:
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
;
return
(
TTarget
)
Map
(
source
,
typeof
(
TTarget
));
}
/// <summary>
/// 简单映射
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <param name="t"></param>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TTarget"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public
static
T
1
Map
<
T1
>(
object
t
)
where
T1
:
class
,
new
()
public
static
T
Target
Map
<
TSource
,
TTarget
>(
TSource
source
)
where
TTarget
:
class
,
new
()
{
if
(
t
is
null
)
return
(
TTarget
)
Map
(
source
,
typeof
(
TTarget
));
}
/// <summary>
/// 简单对象映射
/// </summary>
/// <param name="source"></param>
/// <param name="targetType"></param>
/// <returns></returns>
public
static
object
Map
(
object
source
,
Type
targetType
)
{
return
Map
(
source
,
Activator
.
CreateInstance
(
targetType
));
}
/// <summary>
/// 简单对象映射
/// </summary>
/// <param name="source"></param>
/// <param name="target"></param>
/// <returns></returns>
public
static
object
Map
(
object
source
,
object
target
)
{
if
(
source
is
null
)
{
return
default
;
}
var
result
=
Activator
.
CreateInstance
(
typeof
(
T1
)
);
if
(
t
.
GetType
().
Name
==
"List`1"
||
result
.
GetType
().
Name
==
"List`1"
)
var
sourceType
=
source
.
GetType
(
);
if
(
sourceType
.
IsPrimitive
||
sourceType
.
IsEnum
||
sourceType
==
TouchSocket
.
Core
.
TouchSocketCoreUtility
.
stringType
)
{
throw
new
Exception
(
"形参有误!,请使用对象。"
)
;
return
source
;
}
var
tpropertyInfos
=
t
.
GetType
().
GetProperties
();
var
t1propertyInfos
=
result
.
GetType
().
GetProperties
();
foreach
(
var
tinfo
in
tpropertyInfos
)
var
sourcePairs
=
m_typeToProperty
.
GetOrAdd
(
sourceType
,
(
k
)
=>
{
Dictionary
<
string
,
Property
>
pairs
=
new
Dictionary
<
string
,
Property
>();
var
ps
=
k
.
GetProperties
(
BindingFlags
.
Default
|
BindingFlags
.
Instance
|
BindingFlags
.
Public
|
BindingFlags
.
NonPublic
);
foreach
(
var
item
in
ps
)
{
pairs
.
Add
(
item
.
Name
,
new
Property
(
item
));
}
return
pairs
;
});
var
targetPairs
=
m_typeToProperty
.
GetOrAdd
(
target
.
GetType
(),
(
k
)
=>
{
foreach
(
var
t1info
in
t1propertyInfos
)
Dictionary
<
string
,
Property
>
pairs
=
new
Dictionary
<
string
,
Property
>();
var
ps
=
k
.
GetProperties
(
BindingFlags
.
Default
|
BindingFlags
.
Instance
|
BindingFlags
.
Public
|
BindingFlags
.
NonPublic
);
foreach
(
var
item
in
ps
)
{
if
(
t1info
.
PropertyType
.
IsValueType
||
t1info
.
PropertyType
.
Name
.
StartsWith
(
"String"
))
pairs
.
Add
(
item
.
Name
,
new
Property
(
item
));
}
return
pairs
;
});
foreach
(
var
item
in
targetPairs
)
{
if
(
item
.
Value
.
CanWrite
)
{
if
(
sourcePairs
.
TryGetValue
(
item
.
Key
,
out
Property
property
))
{
if
(
tinfo
.
Name
==
t1info
.
Name
)
if
(
property
.
CanRead
)
{
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
{
}
item
.
Value
.
SetValue
(
target
,
property
.
GetValue
(
source
));
}
}
}
}
return
(
T1
)
resul
t
;
return
targe
t
;
}
/// <summary>
/// 映射List
/// </summary>
...
...
src/TouchSocket/Core/Reflection/Property.cs
浏览文件 @
465e4a91
...
...
@@ -25,12 +25,12 @@ namespace TouchSocket.Core.Reflection
/// <summary>
/// 获取器
/// </summary>
private
readonly
PropertyGetter
geter
;
private
readonly
PropertyGetter
m_
geter
;
/// <summary>
/// 设置器
/// </summary>
private
readonly
PropertySetter
seter
;
private
readonly
PropertySetter
m_
seter
;
/// <summary>
/// 获取属性名称
...
...
@@ -53,14 +53,26 @@ namespace TouchSocket.Core.Reflection
if
(
property
.
CanRead
==
true
)
{
this
.
geter
=
new
PropertyGetter
(
property
);
this
.
CanRead
=
true
;
this
.
m_geter
=
new
PropertyGetter
(
property
);
}
if
(
property
.
CanWrite
==
true
)
{
this
.
seter
=
new
PropertySetter
(
property
);
this
.
CanWrite
=
true
;
this
.
m_seter
=
new
PropertySetter
(
property
);
}
}
/// <summary>
/// 是否可以读取
/// </summary>
public
bool
CanRead
{
get
;
private
set
;
}
/// <summary>
/// 是否可以写入
/// </summary>
public
bool
CanWrite
{
get
;
private
set
;
}
/// <summary>
/// 获取属性的值
/// </summary>
...
...
@@ -69,11 +81,11 @@ namespace TouchSocket.Core.Reflection
/// <returns></returns>
public
object
GetValue
(
object
instance
)
{
if
(
this
.
geter
==
null
)
if
(
this
.
m_
geter
==
null
)
{
throw
new
NotSupportedException
();
}
return
this
.
geter
.
Invoke
(
instance
);
return
this
.
m_
geter
.
Invoke
(
instance
);
}
/// <summary>
...
...
@@ -84,11 +96,11 @@ namespace TouchSocket.Core.Reflection
/// <exception cref="NotSupportedException"></exception>
public
void
SetValue
(
object
instance
,
object
value
)
{
if
(
this
.
seter
==
null
)
if
(
this
.
m_
seter
==
null
)
{
throw
new
NotSupportedException
(
$"
{
this
.
Name
}
不允许赋值"
);
}
this
.
seter
.
Invoke
(
instance
,
value
);
this
.
m_
seter
.
Invoke
(
instance
,
value
);
}
/// <summary>
...
...
src/TouchSocket/Sockets/Components/TCP/TcpClient.cs
浏览文件 @
465e4a91
...
...
@@ -688,6 +688,10 @@ namespace TouchSocket.Sockets
socket
.
SetSocketOption
(
SocketOptionLevel
.
Tcp
,
SocketOptionName
.
NoDelay
,
this
.
m_config
.
GetValue
<
bool
>(
TouchSocketConfigExtension
.
NoDelayProperty
));
if
(
this
.
m_config
.
GetValue
<
IPHost
>(
TouchSocketConfigExtension
.
BindIPHostProperty
)
!=
null
)
{
if
(
this
.
m_config
.
GetValue
<
bool
>(
TouchSocketConfigExtension
.
ReuseAddressProperty
))
{
socket
.
SetSocketOption
(
SocketOptionLevel
.
Socket
,
SocketOptionName
.
ReuseAddress
,
true
);
}
socket
.
Bind
(
this
.
m_config
.
GetValue
<
IPHost
>(
TouchSocketConfigExtension
.
BindIPHostProperty
).
EndPoint
);
}
return
socket
;
...
...
src/TouchSocket/Sockets/Components/TCP/TcpService.cs
浏览文件 @
465e4a91
...
...
@@ -547,6 +547,10 @@ namespace TouchSocket.Sockets
Socket
socket
=
new
Socket
(
iPHost
.
AddressFamily
,
SocketType
.
Stream
,
ProtocolType
.
Tcp
);
socket
.
ReceiveBufferSize
=
this
.
BufferLength
;
socket
.
SendBufferSize
=
this
.
BufferLength
;
if
(
this
.
m_config
.
GetValue
<
bool
>(
TouchSocketConfigExtension
.
ReuseAddressProperty
))
{
socket
.
SetSocketOption
(
SocketOptionLevel
.
Socket
,
SocketOptionName
.
ReuseAddress
,
true
);
}
this
.
PreviewBind
(
socket
);
socket
.
Bind
(
iPHost
.
EndPoint
);
socket
.
Listen
(
this
.
m_backlog
);
...
...
src/TouchSocket/Sockets/Components/UDP/UdpSession.cs
浏览文件 @
465e4a91
...
...
@@ -431,6 +431,10 @@ namespace TouchSocket.Sockets
socket
.
UseOnlyOverlappedIO
=
true
;
}
socket
.
EnableBroadcast
=
this
.
m_config
.
GetValue
<
bool
>(
TouchSocketConfigExtension
.
EnableBroadcastProperty
);
if
(
this
.
m_config
.
GetValue
<
bool
>(
TouchSocketConfigExtension
.
ReuseAddressProperty
))
{
socket
.
SetSocketOption
(
SocketOptionLevel
.
Socket
,
SocketOptionName
.
ReuseAddress
,
true
);
}
this
.
PreviewBind
(
socket
);
socket
.
Bind
(
iPHost
.
EndPoint
);
...
...
src/TouchSocket/Sockets/Config/TouchSocketConfigExtension.cs
浏览文件 @
465e4a91
...
...
@@ -385,6 +385,24 @@ namespace TouchSocket.Sockets
/// </summary>
public
static
readonly
DependencyProperty
MaxCountProperty
=
DependencyProperty
.
Register
(
"MaxCount"
,
typeof
(
int
),
typeof
(
TouchSocketConfigExtension
),
10000
);
/// <summary>
/// 端口复用,默认为false,所需类型<see cref="bool"/>
/// </summary>
public
static
readonly
DependencyProperty
ReuseAddressProperty
=
DependencyProperty
.
Register
(
"ReuseAddress"
,
typeof
(
bool
),
typeof
(
TouchSocketConfigExtension
),
false
);
/// <summary>
/// 启用端口复用。
/// <para>该配置可在服务器、或客户端在监听端口时,运行监听同一个端口。可以一定程度缓解端口来不及释放的问题</para>
/// </summary>
/// <param name="config"></param>
/// <returns></returns>
public
static
TouchSocketConfig
UseReuseAddress
(
this
TouchSocketConfig
config
)
{
config
.
SetValue
(
ReuseAddressProperty
,
true
);
return
config
;
}
/// <summary>
/// 挂起连接队列的最大长度,默认100。
...
...
src/TouchSocket/TouchSocket.csproj
浏览文件 @
465e4a91
...
...
@@ -4,7 +4,7 @@
<ApplicationIcon>logo.ico</ApplicationIcon>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>RRQM.pfx</AssemblyOriginatorKeyFile>
<Version>0.2.
0
</Version>
<Version>0.2.
1
</Version>
<LangVersion>8.0</LangVersion>
<Company>若汝棋茗</Company>
<Copyright>Copyright © 2022 若汝棋茗</Copyright>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录