Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
若汝棋茗
RRQMSocket
提交
177b7ce2
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,发现更多精彩内容 >>
提交
177b7ce2
编写于
5月 15, 2023
作者:
若
若汝棋茗
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修复FilePool异常bug
上级
4a38f1f1
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
105 addition
and
104 deletion
+105
-104
src/TouchSocket/Core/IO/FileIO/FilePool.cs
src/TouchSocket/Core/IO/FileIO/FilePool.cs
+19
-13
src/TouchSocket/Core/IO/FileIO/FileStorage.cs
src/TouchSocket/Core/IO/FileIO/FileStorage.cs
+35
-36
src/TouchSocket/Core/IO/FileIO/FileStorageReader.cs
src/TouchSocket/Core/IO/FileIO/FileStorageReader.cs
+16
-17
src/TouchSocket/Core/IO/FileIO/FileStorageStream.cs
src/TouchSocket/Core/IO/FileIO/FileStorageStream.cs
+19
-20
src/TouchSocket/Core/IO/FileIO/FileStorageWriter.cs
src/TouchSocket/Core/IO/FileIO/FileStorageWriter.cs
+15
-17
src/TouchSocket/TouchSocket.csproj
src/TouchSocket/TouchSocket.csproj
+1
-1
未找到文件。
src/TouchSocket/Core/IO/FileIO/FilePool.cs
浏览文件 @
177b7ce2
...
...
@@ -81,13 +81,17 @@ namespace TouchSocket.Core
}
lock
(
m_locker
)
{
if
(
m_pathStorage
.
TryGetValue
(
fileInfo
.
FullName
,
out
storage
))
storage
=
new
FileStorage
(
fileInfo
,
FileAccess
.
Read
);
if
(
m_pathStorage
.
TryAdd
(
fileInfo
.
FullName
,
storage
))
{
Interlocked
.
Increment
(
ref
storage
.
m_reference
);
return
storage
;
}
FileStorage
fileStorage
=
new
FileStorage
(
fileInfo
,
FileAccess
.
Read
);
m_pathStorage
.
TryAdd
(
fileInfo
.
FullName
,
fileStorage
);
return
fileStorage
;
else
{
return
GetFileStorageForRead
(
fileInfo
);
}
}
}
...
...
@@ -127,13 +131,16 @@ namespace TouchSocket.Core
}
lock
(
m_locker
)
{
if
(
m_pathStorage
.
TryGetValue
(
fileInfo
.
FullName
,
out
storage
))
storage
=
new
FileStorage
(
fileInfo
,
FileAccess
.
Write
);
if
(
m_pathStorage
.
TryAdd
(
fileInfo
.
FullName
,
storage
))
{
Interlocked
.
Increment
(
ref
storage
.
m_reference
);
return
storage
;
}
FileStorage
fileStorage
=
new
FileStorage
(
fileInfo
,
FileAccess
.
Write
);
m_pathStorage
.
TryAdd
(
fileInfo
.
FullName
,
fileStorage
);
return
fileStorage
;
else
{
return
GetFileStorageForWrite
(
fileInfo
);
}
}
}
...
...
@@ -279,8 +286,7 @@ namespace TouchSocket.Core
path
=
Path
.
GetFullPath
(
path
);
if
(
m_pathStorage
.
TryGetValue
(
path
,
out
FileStorage
fileStorage
))
{
Interlocked
.
Decrement
(
ref
fileStorage
.
m_reference
);
if
(
fileStorage
.
m_reference
<=
0
)
if
(
Interlocked
.
Decrement
(
ref
fileStorage
.
m_reference
)
<=
0
)
{
if
(
delayTime
>
0
)
{
...
...
@@ -318,15 +324,15 @@ namespace TouchSocket.Core
private
static
void
OnTimer
(
object
state
)
{
List
<
string
>
keys
=
new
List
<
string
>();
foreach
(
var
item
in
m_pathStorage
)
var
keys
=
new
List
<
string
>();
foreach
(
KeyValuePair
<
string
,
FileStorage
>
item
in
m_pathStorage
)
{
if
(
DateTime
.
Now
-
item
.
Value
.
AccessTime
>
item
.
Value
.
AccessTimeout
)
{
keys
.
Add
(
item
.
Key
);
}
}
foreach
(
var
item
in
keys
)
foreach
(
string
item
in
keys
)
{
TryReleaseFile
(
item
);
}
...
...
src/TouchSocket/Core/IO/FileIO/FileStorage.cs
浏览文件 @
177b7ce2
...
...
@@ -33,18 +33,18 @@ namespace TouchSocket.Core
/// </summary>
internal
FileStorage
(
FileInfo
fileInfo
,
FileAccess
fileAccess
)
:
this
()
{
FileAccess
=
fileAccess
;
FileInfo
=
fileInfo
;
Path
=
fileInfo
.
FullName
;
m_reference
=
0
;
FileStream
=
fileAccess
==
FileAccess
.
Read
?
fileInfo
.
OpenRead
()
:
fileInfo
.
OpenWrite
();
m_lockSlim
=
new
ReaderWriterLockSlim
();
this
.
FileAccess
=
fileAccess
;
this
.
FileInfo
=
fileInfo
;
this
.
Path
=
fileInfo
.
FullName
;
this
.
m_reference
=
0
;
this
.
FileStream
=
fileAccess
==
FileAccess
.
Read
?
fileInfo
.
OpenRead
()
:
fileInfo
.
OpenWrite
();
this
.
m_lockSlim
=
new
ReaderWriterLockSlim
();
}
private
FileStorage
()
{
AccessTime
=
DateTime
.
Now
;
AccessTimeout
=
TimeSpan
.
FromSeconds
(
60
);
this
.
AccessTime
=
DateTime
.
Now
;
this
.
AccessTimeout
=
TimeSpan
.
FromSeconds
(
60
);
}
/// <summary>
...
...
@@ -81,7 +81,7 @@ namespace TouchSocket.Core
/// <summary>
/// 文件长度
/// </summary>
public
long
Length
=>
FileStream
.
Length
;
public
long
Length
=>
this
.
FileStream
.
Length
;
/// <summary>
/// 文件路径
...
...
@@ -91,7 +91,7 @@ namespace TouchSocket.Core
/// <summary>
/// 引用次数。
/// </summary>
public
int
Reference
=>
m_reference
;
public
int
Reference
=>
this
.
m_reference
;
/// <summary>
/// 创建一个只读的、已经缓存的文件信息。该操作不会占用文件句柄。
...
...
@@ -136,8 +136,8 @@ namespace TouchSocket.Core
/// </summary>
public
void
Flush
()
{
AccessTime
=
DateTime
.
Now
;
FileStream
.
Flush
();
this
.
AccessTime
=
DateTime
.
Now
;
this
.
FileStream
.
Flush
();
}
/// <summary>
...
...
@@ -150,27 +150,27 @@ namespace TouchSocket.Core
/// <returns></returns>
public
int
Read
(
long
stratPos
,
byte
[]
buffer
,
int
offset
,
int
length
)
{
AccessTime
=
DateTime
.
Now
;
using
(
WriteLock
writeLock
=
new
WriteLock
(
m_lockSlim
))
this
.
AccessTime
=
DateTime
.
Now
;
using
(
var
writeLock
=
new
WriteLock
(
this
.
m_lockSlim
))
{
if
(
m_disposedValue
)
if
(
this
.
m_disposedValue
)
{
throw
new
ObjectDisposedException
(
GetType
().
FullName
);
throw
new
ObjectDisposedException
(
this
.
GetType
().
FullName
);
}
if
(
FileAccess
==
FileAccess
.
Write
)
if
(
this
.
FileAccess
==
FileAccess
.
Write
)
{
throw
new
Exception
(
"该流不允许读取。"
);
}
if
(
Cache
)
if
(
this
.
Cache
)
{
int
r
=
(
int
)
Math
.
Min
(
m_fileData
.
Length
-
stratPos
,
length
);
Array
.
Copy
(
m_fileData
,
stratPos
,
buffer
,
offset
,
r
);
int
r
=
(
int
)
Math
.
Min
(
this
.
m_fileData
.
Length
-
stratPos
,
length
);
Array
.
Copy
(
this
.
m_fileData
,
stratPos
,
buffer
,
offset
,
r
);
return
r
;
}
else
{
FileStream
.
Position
=
stratPos
;
return
FileStream
.
Read
(
buffer
,
offset
,
length
);
this
.
FileStream
.
Position
=
stratPos
;
return
this
.
FileStream
.
Read
(
buffer
,
offset
,
length
);
}
}
}
...
...
@@ -184,7 +184,7 @@ namespace TouchSocket.Core
/// <exception cref="Exception"></exception>
public
Result
TryReleaseFile
(
int
delayTime
=
0
)
{
return
FilePool
.
TryReleaseFile
(
Path
,
delayTime
);
return
FilePool
.
TryReleaseFile
(
this
.
Path
,
delayTime
);
}
/// <summary>
...
...
@@ -196,34 +196,33 @@ namespace TouchSocket.Core
/// <param name="length"></param>
public
void
Write
(
long
stratPos
,
byte
[]
buffer
,
int
offset
,
int
length
)
{
AccessTime
=
DateTime
.
Now
;
using
(
WriteLock
writeLock
=
new
WriteLock
(
m_lockSlim
))
this
.
AccessTime
=
DateTime
.
Now
;
using
(
var
writeLock
=
new
WriteLock
(
this
.
m_lockSlim
))
{
if
(
m_disposedValue
)
if
(
this
.
m_disposedValue
)
{
throw
new
ObjectDisposedException
(
GetType
().
FullName
);
throw
new
ObjectDisposedException
(
this
.
GetType
().
FullName
);
}
if
(
FileAccess
==
FileAccess
.
Read
)
if
(
this
.
FileAccess
==
FileAccess
.
Read
)
{
throw
new
Exception
(
"该流不允许写入。"
);
}
FileStream
.
Position
=
stratPos
;
FileStream
.
Write
(
buffer
,
offset
,
length
);
FileStream
.
Flush
();
this
.
FileStream
.
Position
=
stratPos
;
this
.
FileStream
.
Write
(
buffer
,
offset
,
length
);
}
}
internal
void
Dispose
()
{
if
(
m_disposedValue
)
if
(
this
.
m_disposedValue
)
{
return
;
}
using
(
WriteLock
writeLock
=
new
WriteLock
(
m_lockSlim
))
using
(
var
writeLock
=
new
WriteLock
(
this
.
m_lockSlim
))
{
m_disposedValue
=
true
;
FileStream
.
SafeDispose
();
m_fileData
=
null
;
this
.
m_disposedValue
=
true
;
this
.
FileStream
.
SafeDispose
();
this
.
m_fileData
=
null
;
}
}
}
...
...
src/TouchSocket/Core/IO/FileIO/FileStorageReader.cs
浏览文件 @
177b7ce2
...
...
@@ -11,6 +11,8 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using
System
;
namespace
TouchSocket.Core
{
/// <summary>
...
...
@@ -19,9 +21,6 @@ namespace TouchSocket.Core
[
IntelligentCoder
.
AsyncMethodPoster
(
Flags
=
IntelligentCoder
.
MemberFlags
.
Public
)]
public
partial
class
FileStorageReader
:
DisposableObject
{
private
FileStorage
m_fileStorage
;
private
long
m_position
;
/// <summary>
/// 构造函数
...
...
@@ -29,7 +28,7 @@ namespace TouchSocket.Core
/// <param name="fileStorage"></param>
public
FileStorageReader
(
FileStorage
fileStorage
)
{
m_f
ileStorage
=
fileStorage
??
throw
new
System
.
ArgumentNullException
(
nameof
(
fileStorage
));
this
.
F
ileStorage
=
fileStorage
??
throw
new
System
.
ArgumentNullException
(
nameof
(
fileStorage
));
}
/// <summary>
...
...
@@ -38,31 +37,27 @@ namespace TouchSocket.Core
~
FileStorageReader
()
{
// 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
Dispose
(
disposing
:
false
);
this
.
Dispose
(
disposing
:
false
);
}
/// <summary>
/// 文件存储器
/// </summary>
public
FileStorage
FileStorage
=>
m_fileStorage
;
public
FileStorage
FileStorage
{
get
;
private
set
;
}
/// <summary>
/// 游标位置
/// </summary>
public
int
Pos
{
get
=>
(
int
)
m_p
osition
;
set
=>
m_p
osition
=
value
;
get
=>
(
int
)
this
.
P
osition
;
set
=>
this
.
P
osition
=
value
;
}
/// <summary>
/// 游标位置
/// </summary>
public
long
Position
{
get
=>
m_position
;
set
=>
m_position
=
value
;
}
public
long
Position
{
get
;
set
;
}
/// <summary>
/// 读取数据到缓存区
...
...
@@ -73,8 +68,8 @@ namespace TouchSocket.Core
/// <returns></returns>
public
int
Read
(
byte
[]
buffer
,
int
offset
,
int
length
)
{
int
r
=
m_fileStorage
.
Read
(
m_p
osition
,
buffer
,
offset
,
length
);
m_p
osition
+=
r
;
int
r
=
this
.
FileStorage
.
Read
(
this
.
P
osition
,
buffer
,
offset
,
length
);
this
.
P
osition
+=
r
;
return
r
;
}
...
...
@@ -84,8 +79,12 @@ namespace TouchSocket.Core
/// <param name="disposing"></param>
protected
override
void
Dispose
(
bool
disposing
)
{
FilePool
.
TryReleaseFile
(
m_fileStorage
.
Path
);
m_fileStorage
=
null
;
if
(
this
.
DisposedValue
)
{
return
;
}
FilePool
.
TryReleaseFile
(
this
.
FileStorage
.
Path
);
this
.
FileStorage
=
null
;
base
.
Dispose
(
disposing
);
}
}
...
...
src/TouchSocket/Core/IO/FileIO/FileStorageStream.cs
浏览文件 @
177b7ce2
...
...
@@ -20,7 +20,6 @@ namespace TouchSocket.Core
[
IntelligentCoder
.
AsyncMethodPoster
(
Flags
=
IntelligentCoder
.
MemberFlags
.
Public
)]
public
partial
class
FileStorageStream
:
Stream
{
private
readonly
FileStorage
m_fileStorage
;
private
long
m_position
;
/// <summary>
...
...
@@ -29,7 +28,7 @@ namespace TouchSocket.Core
/// <param name="fileStorage"></param>
public
FileStorageStream
(
FileStorage
fileStorage
)
{
m_f
ileStorage
=
fileStorage
??
throw
new
System
.
ArgumentNullException
(
nameof
(
fileStorage
));
this
.
F
ileStorage
=
fileStorage
??
throw
new
System
.
ArgumentNullException
(
nameof
(
fileStorage
));
}
/// <summary>
...
...
@@ -38,38 +37,38 @@ namespace TouchSocket.Core
~
FileStorageStream
()
{
// 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
Dispose
(
disposing
:
false
);
this
.
Dispose
(
disposing
:
false
);
}
/// <summary>
/// <inheritdoc/>
/// </summary>
public
override
bool
CanRead
=>
m_f
ileStorage
.
FileStream
.
CanRead
;
public
override
bool
CanRead
=>
this
.
F
ileStorage
.
FileStream
.
CanRead
;
/// <summary>
/// <inheritdoc/>
/// </summary>
public
override
bool
CanSeek
=>
m_f
ileStorage
.
FileStream
.
CanSeek
;
public
override
bool
CanSeek
=>
this
.
F
ileStorage
.
FileStream
.
CanSeek
;
/// <summary>
/// <inheritdoc/>
/// </summary>
public
override
bool
CanWrite
=>
m_f
ileStorage
.
FileStream
.
CanWrite
;
public
override
bool
CanWrite
=>
this
.
F
ileStorage
.
FileStream
.
CanWrite
;
/// <summary>
/// 文件存储器
/// </summary>
public
FileStorage
FileStorage
=>
m_fileStorage
;
public
FileStorage
FileStorage
{
get
;
private
set
;
}
/// <summary>
/// <inheritdoc/>
/// </summary>
public
override
long
Length
=>
m_f
ileStorage
.
FileStream
.
Length
;
public
override
long
Length
=>
this
.
F
ileStorage
.
FileStream
.
Length
;
/// <summary>
/// <inheritdoc/>
/// </summary>
public
override
long
Position
{
get
=>
m_position
;
set
=>
m_position
=
value
;
}
public
override
long
Position
{
get
=>
this
.
m_position
;
set
=>
this
.
m_position
=
value
;
}
/// <summary>
/// <inheritdoc/>
...
...
@@ -77,7 +76,7 @@ namespace TouchSocket.Core
[
IntelligentCoder
.
AsyncMethodIgnore
]
public
override
void
Flush
()
{
m_f
ileStorage
.
Flush
();
this
.
F
ileStorage
.
Flush
();
}
/// <summary>
...
...
@@ -89,8 +88,8 @@ namespace TouchSocket.Core
/// <returns></returns>
public
override
int
Read
(
byte
[]
buffer
,
int
offset
,
int
count
)
{
int
r
=
m_fileStorage
.
Read
(
m_position
,
buffer
,
offset
,
count
);
m_position
+=
r
;
int
r
=
this
.
FileStorage
.
Read
(
this
.
m_position
,
buffer
,
offset
,
count
);
this
.
m_position
+=
r
;
return
r
;
}
...
...
@@ -105,18 +104,18 @@ namespace TouchSocket.Core
switch
(
origin
)
{
case
SeekOrigin
.
Begin
:
m_position
=
offset
;
this
.
m_position
=
offset
;
break
;
case
SeekOrigin
.
Current
:
m_position
+=
offset
;
this
.
m_position
+=
offset
;
break
;
case
SeekOrigin
.
End
:
m_position
=
Length
+
offset
;
this
.
m_position
=
this
.
Length
+
offset
;
break
;
}
return
m_position
;
return
this
.
m_position
;
}
/// <summary>
...
...
@@ -125,7 +124,7 @@ namespace TouchSocket.Core
/// <param name="value"></param>
public
override
void
SetLength
(
long
value
)
{
m_f
ileStorage
.
FileStream
.
SetLength
(
value
);
this
.
F
ileStorage
.
FileStream
.
SetLength
(
value
);
}
/// <summary>
...
...
@@ -136,8 +135,8 @@ namespace TouchSocket.Core
/// <param name="count"></param>
public
override
void
Write
(
byte
[]
buffer
,
int
offset
,
int
count
)
{
m_fileStorage
.
Write
(
m_position
,
buffer
,
offset
,
count
);
m_position
+=
count
;
this
.
FileStorage
.
Write
(
this
.
m_position
,
buffer
,
offset
,
count
);
this
.
m_position
+=
count
;
}
/// <summary>
...
...
@@ -146,7 +145,7 @@ namespace TouchSocket.Core
/// <param name="disposing"></param>
protected
override
void
Dispose
(
bool
disposing
)
{
FilePool
.
TryReleaseFile
(
m_f
ileStorage
.
Path
);
FilePool
.
TryReleaseFile
(
this
.
F
ileStorage
.
Path
);
base
.
Dispose
(
disposing
);
}
}
...
...
src/TouchSocket/Core/IO/FileIO/FileStorageWriter.cs
浏览文件 @
177b7ce2
...
...
@@ -19,8 +19,6 @@ namespace TouchSocket.Core
[
IntelligentCoder
.
AsyncMethodPoster
(
Flags
=
IntelligentCoder
.
MemberFlags
.
Public
)]
public
partial
class
FileStorageWriter
:
DisposableObject
,
IWrite
{
private
readonly
FileStorage
m_fileStorage
;
private
long
m_position
;
/// <summary>
/// 构造函数
...
...
@@ -28,7 +26,7 @@ namespace TouchSocket.Core
/// <param name="fileStorage"></param>
public
FileStorageWriter
(
FileStorage
fileStorage
)
{
m_f
ileStorage
=
fileStorage
??
throw
new
System
.
ArgumentNullException
(
nameof
(
fileStorage
));
this
.
F
ileStorage
=
fileStorage
??
throw
new
System
.
ArgumentNullException
(
nameof
(
fileStorage
));
}
/// <summary>
...
...
@@ -37,7 +35,7 @@ namespace TouchSocket.Core
/// <param name="buffer"></param>
public
virtual
void
Write
(
byte
[]
buffer
)
{
Write
(
buffer
,
0
,
buffer
.
Length
);
this
.
Write
(
buffer
,
0
,
buffer
.
Length
);
}
/// <summary>
...
...
@@ -46,31 +44,27 @@ namespace TouchSocket.Core
~
FileStorageWriter
()
{
// 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
Dispose
(
disposing
:
false
);
this
.
Dispose
(
disposing
:
false
);
}
/// <summary>
/// 文件存储器
/// </summary>
public
FileStorage
FileStorage
=>
m_fileStorage
;
public
FileStorage
FileStorage
{
get
;
private
set
;
}
/// <summary>
/// 游标位置
/// </summary>
public
int
Pos
{
get
=>
(
int
)
m_p
osition
;
set
=>
m_p
osition
=
value
;
get
=>
(
int
)
this
.
P
osition
;
set
=>
this
.
P
osition
=
value
;
}
/// <summary>
/// 游标位置
/// </summary>
public
long
Position
{
get
=>
m_position
;
set
=>
m_position
=
value
;
}
public
long
Position
{
get
;
set
;
}
/// <summary>
/// 移动Pos到流末尾
...
...
@@ -78,7 +72,7 @@ namespace TouchSocket.Core
/// <returns></returns>
public
long
SeekToEnd
()
{
return
Position
=
FileStorage
.
Length
;
return
this
.
Position
=
this
.
FileStorage
.
Length
;
}
/// <summary>
...
...
@@ -90,8 +84,8 @@ namespace TouchSocket.Core
/// <returns></returns>
public
void
Write
(
byte
[]
buffer
,
int
offset
,
int
length
)
{
m_fileStorage
.
Write
(
m_p
osition
,
buffer
,
offset
,
length
);
m_p
osition
+=
length
;
this
.
FileStorage
.
Write
(
this
.
P
osition
,
buffer
,
offset
,
length
);
this
.
P
osition
+=
length
;
}
/// <summary>
...
...
@@ -100,7 +94,11 @@ namespace TouchSocket.Core
/// <param name="disposing"></param>
protected
override
void
Dispose
(
bool
disposing
)
{
FilePool
.
TryReleaseFile
(
m_fileStorage
.
Path
);
if
(
this
.
DisposedValue
)
{
return
;
}
FilePool
.
TryReleaseFile
(
this
.
FileStorage
.
Path
);
base
.
Dispose
(
disposing
);
}
}
...
...
src/TouchSocket/TouchSocket.csproj
浏览文件 @
177b7ce2
...
...
@@ -4,7 +4,7 @@
<ApplicationIcon>logo.ico</ApplicationIcon>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>D:\MyStore\13_Doc\Keys\TouchSocket.snk</AssemblyOriginatorKeyFile>
<Version>1.3.
1
</Version>
<Version>1.3.
2
</Version>
<LangVersion>8.0</LangVersion>
<Company>若汝棋茗</Company>
<Copyright>Copyright © 2023 若汝棋茗</Copyright>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录