Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
R
RoboPLC
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄新宇
RoboPLC
Commits
f8603f18
提交
f8603f18
authored
4月 12, 2024
作者:
Serhij S
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
comm options timeouts
上级
f751a9ef
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
42 行增加
和
14 行删除
+42
-14
mod.rs
src/comm/mod.rs
+34
-8
tcp.rs
src/comm/tcp.rs
+8
-6
没有找到文件。
src/comm/mod.rs
浏览文件 @
f8603f18
...
@@ -60,13 +60,6 @@ trait Communicator {
...
@@ -60,13 +60,6 @@ trait Communicator {
}
}
}
}
/// Connection Options
pub
struct
ConnectionOptions
{
with_reader
:
bool
,
chat
:
Option
<
Box
<
ChatFn
>>
,
timeout
:
Duration
,
}
pub
struct
CommReader
{
pub
struct
CommReader
{
reader
:
Option
<
Box
<
dyn
Read
+
Send
+
'static
>>
,
reader
:
Option
<
Box
<
dyn
Read
+
Send
+
'static
>>
,
}
}
...
@@ -79,16 +72,34 @@ impl CommReader {
...
@@ -79,16 +72,34 @@ impl CommReader {
impl
DataDeliveryPolicy
for
CommReader
{}
impl
DataDeliveryPolicy
for
CommReader
{}
pub
(
crate
)
struct
Timeouts
{
pub
connect
:
Duration
,
pub
read
:
Duration
,
pub
write
:
Duration
,
}
pub
type
ChatFn
=
dyn
Fn
(
&
mut
dyn
Stream
)
->
std
::
result
::
Result
<
(),
Box
<
dyn
std
::
error
::
Error
+
Send
+
Sync
>>
pub
type
ChatFn
=
dyn
Fn
(
&
mut
dyn
Stream
)
->
std
::
result
::
Result
<
(),
Box
<
dyn
std
::
error
::
Error
+
Send
+
Sync
>>
+
Send
+
Send
+
Sync
;
+
Sync
;
/// Connection Options
pub
struct
ConnectionOptions
{
with_reader
:
bool
,
chat
:
Option
<
Box
<
ChatFn
>>
,
timeouts
:
Timeouts
,
}
impl
ConnectionOptions
{
impl
ConnectionOptions
{
/// timeout = the default timeout
pub
fn
new
(
timeout
:
Duration
)
->
Self
{
pub
fn
new
(
timeout
:
Duration
)
->
Self
{
Self
{
Self
{
with_reader
:
false
,
with_reader
:
false
,
chat
:
None
,
chat
:
None
,
timeout
,
timeouts
:
Timeouts
{
connect
:
timeout
,
read
:
timeout
,
write
:
timeout
,
},
}
}
}
}
/// Enable the reader channel. The reader channel allows the client to receive a clone of the
/// Enable the reader channel. The reader channel allows the client to receive a clone of the
...
@@ -110,4 +121,19 @@ impl ConnectionOptions {
...
@@ -110,4 +121,19 @@ impl ConnectionOptions {
self
.chat
=
Some
(
Box
::
new
(
chat
));
self
.chat
=
Some
(
Box
::
new
(
chat
));
self
self
}
}
/// Set the connect timeout
pub
fn
connect_timeout
(
mut
self
,
timeout
:
Duration
)
->
Self
{
self
.timeouts.connect
=
timeout
;
self
}
/// Set the read timeout
pub
fn
read_timeout
(
mut
self
,
timeout
:
Duration
)
->
Self
{
self
.timeouts.read
=
timeout
;
self
}
/// Set the write timeout
pub
fn
write_timeout
(
mut
self
,
timeout
:
Duration
)
->
Self
{
self
.timeouts.write
=
timeout
;
self
}
}
}
src/comm/tcp.rs
浏览文件 @
f8603f18
use
crate
::
pchannel
;
use
crate
::
pchannel
;
use
crate
::{
Error
,
Result
};
use
crate
::{
Error
,
Result
};
use
super
::{
ChatFn
,
Client
,
CommReader
,
Communicator
,
ConnectionOptions
,
Protocol
,
Stream
};
use
super
::{
ChatFn
,
Client
,
CommReader
,
Communicator
,
ConnectionOptions
,
Protocol
,
Stream
,
Timeouts
,
};
use
core
::
fmt
;
use
core
::
fmt
;
use
parking_lot
::{
Mutex
,
MutexGuard
};
use
parking_lot
::{
Mutex
,
MutexGuard
};
use
std
::
io
::{
Read
,
Write
};
use
std
::
io
::{
Read
,
Write
};
...
@@ -38,7 +40,7 @@ impl Stream for TcpStream {}
...
@@ -38,7 +40,7 @@ impl Stream for TcpStream {}
pub
struct
Tcp
{
pub
struct
Tcp
{
addr
:
SocketAddr
,
addr
:
SocketAddr
,
stream
:
Mutex
<
Option
<
TcpStream
>>
,
stream
:
Mutex
<
Option
<
TcpStream
>>
,
timeout
:
Duration
,
timeout
s
:
Timeouts
,
busy
:
Mutex
<
()
>
,
busy
:
Mutex
<
()
>
,
session_id
:
AtomicUsize
,
session_id
:
AtomicUsize
,
reader_tx
:
Option
<
pchannel
::
Sender
<
CommReader
>>
,
reader_tx
:
Option
<
pchannel
::
Sender
<
CommReader
>>
,
...
@@ -119,7 +121,7 @@ impl Tcp {
...
@@ -119,7 +121,7 @@ impl Tcp {
.ok_or_else
(||
Error
::
invalid_data
(
format!
(
"Invalid address: {:?}"
,
addr
)))
?
,
.ok_or_else
(||
Error
::
invalid_data
(
format!
(
"Invalid address: {:?}"
,
addr
)))
?
,
stream
:
<
_
>
::
default
(),
stream
:
<
_
>
::
default
(),
busy
:
<
_
>
::
default
(),
busy
:
<
_
>
::
default
(),
timeout
:
options
.timeout
,
timeout
s
:
options
.timeouts
,
session_id
:
<
_
>
::
default
(),
session_id
:
<
_
>
::
default
(),
reader_tx
:
tx
,
reader_tx
:
tx
,
chat
:
options
.chat
,
chat
:
options
.chat
,
...
@@ -129,9 +131,9 @@ impl Tcp {
...
@@ -129,9 +131,9 @@ impl Tcp {
fn
get_stream
(
&
self
)
->
Result
<
MutexGuard
<
Option
<
TcpStream
>>>
{
fn
get_stream
(
&
self
)
->
Result
<
MutexGuard
<
Option
<
TcpStream
>>>
{
let
mut
lock
=
self
.stream
.lock
();
let
mut
lock
=
self
.stream
.lock
();
if
lock
.as_mut
()
.is_none
()
{
if
lock
.as_mut
()
.is_none
()
{
let
mut
stream
=
TcpStream
::
connect_timeout
(
&
self
.addr
,
self
.timeout
)
?
;
let
mut
stream
=
TcpStream
::
connect_timeout
(
&
self
.addr
,
self
.timeout
s.connect
)
?
;
stream
.set_read_timeout
(
Some
(
self
.timeout
))
?
;
stream
.set_read_timeout
(
Some
(
self
.timeout
s.read
))
?
;
stream
.set_write_timeout
(
Some
(
self
.timeout
))
?
;
stream
.set_write_timeout
(
Some
(
self
.timeout
s.write
))
?
;
stream
.set_nodelay
(
true
)
?
;
stream
.set_nodelay
(
true
)
?
;
if
let
Some
(
ref
chat
)
=
self
.chat
{
if
let
Some
(
ref
chat
)
=
self
.chat
{
chat
(
&
mut
stream
)
.map_err
(
Error
::
io
)
?
;
chat
(
&
mut
stream
)
.map_err
(
Error
::
io
)
?
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论