Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
R
RoboPLC
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄新宇
RoboPLC
Commits
d404109e
提交
d404109e
authored
4月 11, 2024
作者:
Serhij S
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
reader moved to comm
上级
2017434a
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
37 行增加
和
21 行删除
+37
-21
mod.rs
src/comm/mod.rs
+20
-1
tcp.rs
src/comm/tcp.rs
+17
-20
没有找到文件。
src/comm/mod.rs
浏览文件 @
d404109e
use
parking_lot
::
MutexGuard
;
use
std
::{
io
::{
Read
,
Write
},
net
::
SocketAddr
,
sync
::
Arc
,
time
::
Duration
,
};
use
crate
::
Result
;
use
crate
::
{
Result
,
DataDeliveryPolicy
}
;
pub
mod
serial
;
// Serial communications
pub
mod
tcp
;
// TCP communications
...
...
@@ -35,6 +36,9 @@ impl Client {
pub
fn
protocol
(
&
self
)
->
Protocol
{
self
.
0
.protocol
()
}
pub
fn
local_ip_addr
(
&
self
)
->
Result
<
Option
<
SocketAddr
>>
{
self
.
0
.local_ip_addr
()
}
}
pub
enum
Protocol
{
...
...
@@ -51,6 +55,9 @@ trait Communicator {
fn
read_exact
(
&
self
,
buf
:
&
mut
[
u8
])
->
Result
<
()
>
;
fn
protocol
(
&
self
)
->
Protocol
;
fn
session_id
(
&
self
)
->
usize
;
fn
local_ip_addr
(
&
self
)
->
Result
<
Option
<
SocketAddr
>>
{
Ok
(
None
)
}
}
/// Connection Options
...
...
@@ -60,6 +67,18 @@ pub struct ConnectionOptions {
timeout
:
Duration
,
}
pub
struct
CommReader
{
reader
:
Option
<
Box
<
dyn
Read
+
Send
+
'static
>>
,
}
impl
CommReader
{
pub
fn
take
(
&
mut
self
)
->
Option
<
Box
<
dyn
Read
+
Send
+
'static
>>
{
self
.reader
.take
()
}
}
impl
DataDeliveryPolicy
for
CommReader
{}
pub
type
ChatFn
=
dyn
Fn
(
&
mut
dyn
Stream
)
->
std
::
result
::
Result
<
(),
Box
<
dyn
std
::
error
::
Error
+
Send
+
Sync
>>
+
Send
+
Sync
;
...
...
src/comm/tcp.rs
浏览文件 @
d404109e
use
crate
::
pchannel
;
use
crate
::{
DataDeliveryPolicy
,
Error
,
Result
};
use
crate
::{
Error
,
Result
};
use
super
::{
ChatFn
,
Client
,
Communicator
,
ConnectionOptions
,
Protocol
,
Stream
};
use
super
::{
ChatFn
,
Client
,
Comm
Reader
,
Comm
unicator
,
ConnectionOptions
,
Protocol
,
Stream
};
use
core
::
fmt
;
use
parking_lot
::{
Mutex
,
MutexGuard
};
use
std
::
io
::{
Read
,
Write
};
...
...
@@ -27,25 +27,13 @@ pub fn connect<A: ToSocketAddrs + fmt::Debug>(addr: A, timeout: Duration) -> Res
pub
fn
connect_with_options
<
A
:
ToSocketAddrs
+
fmt
::
Debug
>
(
addr
:
A
,
options
:
ConnectionOptions
,
)
->
Result
<
(
Client
,
pchannel
::
Receiver
<
TcpReader
>
)
>
{
let
(
tcp
,
rx
)
=
Tcp
::
create
(
addr
,
options
)
?
;
Ok
((
Client
(
tcp
),
rx
.unwrap
()
))
)
->
Result
<
(
Client
,
Option
<
pchannel
::
Receiver
<
CommReader
>
>
)
>
{
let
(
tcp
,
maybe_
rx
)
=
Tcp
::
create
(
addr
,
options
)
?
;
Ok
((
Client
(
tcp
),
maybe_rx
))
}
impl
Stream
for
TcpStream
{}
pub
struct
TcpReader
{
reader
:
Option
<
Box
<
dyn
Read
+
Send
+
'static
>>
,
}
impl
TcpReader
{
pub
fn
take
(
&
mut
self
)
->
Option
<
Box
<
dyn
Read
+
Send
+
'static
>>
{
self
.reader
.take
()
}
}
impl
DataDeliveryPolicy
for
TcpReader
{}
#[allow(clippy
::
module_name_repetitions)]
pub
struct
Tcp
{
addr
:
SocketAddr
,
...
...
@@ -53,7 +41,7 @@ pub struct Tcp {
timeout
:
Duration
,
busy
:
Mutex
<
()
>
,
session_id
:
AtomicUsize
,
reader_tx
:
Option
<
pchannel
::
Sender
<
Tcp
Reader
>>
,
reader_tx
:
Option
<
pchannel
::
Sender
<
Comm
Reader
>>
,
chat
:
Option
<
Box
<
ChatFn
>>
,
}
...
...
@@ -99,6 +87,15 @@ impl Communicator for Tcp {
.read_exact
(
buf
)
.map_err
(|
e
|
handle_tcp_stream_error!
(
self
.session_id
,
stream
,
e
,
false
))
}
fn
local_ip_addr
(
&
self
)
->
Result
<
Option
<
SocketAddr
>>
{
let
mut
stream
=
self
.get_stream
()
?
;
stream
.as_mut
()
.unwrap
()
.local_addr
()
.map
(
Some
)
.map_err
(|
e
|
handle_tcp_stream_error!
(
self
.session_id
,
stream
,
e
,
false
))
}
fn
protocol
(
&
self
)
->
Protocol
{
Protocol
::
Tcp
}
...
...
@@ -108,7 +105,7 @@ impl Tcp {
fn
create
<
A
:
ToSocketAddrs
+
fmt
::
Debug
>
(
addr
:
A
,
options
:
ConnectionOptions
,
)
->
Result
<
(
TcpClient
,
Option
<
pchannel
::
Receiver
<
Tcp
Reader
>>
)
>
{
)
->
Result
<
(
TcpClient
,
Option
<
pchannel
::
Receiver
<
Comm
Reader
>>
)
>
{
let
(
tx
,
rx
)
=
if
options
.with_reader
{
let
(
tx
,
rx
)
=
pchannel
::
bounded
(
READER_CHANNEL_CAPACITY
);
(
Some
(
tx
),
Some
(
rx
))
...
...
@@ -140,7 +137,7 @@ impl Tcp {
chat
(
&
mut
stream
)
.map_err
(
Error
::
io
)
?
;
}
if
let
Some
(
ref
tx
)
=
self
.reader_tx
{
tx
.send
(
Tcp
Reader
{
tx
.send
(
Comm
Reader
{
reader
:
Some
(
Box
::
new
(
stream
.try_clone
()
?
)),
})
?
;
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论