Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
R
RoboPLC
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄新宇
RoboPLC
Commits
75e96170
提交
75e96170
authored
7月 02, 2024
作者:
Serhij S
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
chat replaced with ConnectionHandler object. connct function
上级
cf414f4b
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
36 行增加
和
20 行删除
+36
-20
Cargo.toml
Cargo.toml
+1
-1
mod.rs
src/comm/mod.rs
+20
-13
serial.rs
src/comm/serial.rs
+3
-0
tcp.rs
src/comm/tcp.rs
+12
-6
没有找到文件。
Cargo.toml
浏览文件 @
75e96170
[package]
name
=
"roboplc"
version
=
"0.
3.2
"
version
=
"0.
4.0
"
edition
=
"2021"
authors
=
[
"Serhij S. <div@altertech.com>"
]
license
=
"Apache-2.0"
...
...
src/comm/mod.rs
浏览文件 @
75e96170
...
...
@@ -21,6 +21,11 @@ impl Client {
pub
fn
lock
(
&
self
)
->
MutexGuard
<
()
>
{
self
.
0
.lock
()
}
/// Connect the client. Does not need to be called for request/response protocols as the client
/// is automatically connected when the first request is made.
pub
fn
connect
(
&
self
)
->
Result
<
()
>
{
self
.
0
.connect
()
}
/// Reconnect the client in case of read/write problems
pub
fn
reconnect
(
&
self
)
{
self
.
0
.reconnect
();
...
...
@@ -81,6 +86,7 @@ pub trait Stream: Read + Write + Send {}
trait
Communicator
{
fn
lock
(
&
self
)
->
MutexGuard
<
()
>
;
fn
connect
(
&
self
)
->
Result
<
()
>
;
fn
reconnect
(
&
self
);
fn
write
(
&
self
,
buf
:
&
[
u8
])
->
Result
<
()
>
;
fn
read_exact
(
&
self
,
buf
:
&
mut
[
u8
])
->
Result
<
()
>
;
...
...
@@ -130,14 +136,18 @@ impl Timeouts {
}
}
pub
type
ChatFn
=
dyn
Fn
(
&
mut
dyn
Stream
)
->
std
::
result
::
Result
<
(),
Box
<
dyn
std
::
error
::
Error
+
Send
+
Sync
>>
+
Send
+
Sync
;
pub
trait
ConnectionHandler
{
/// called right after the connection is established
fn
on_connect
(
&
self
,
stream
:
&
mut
dyn
Stream
,
)
->
std
::
result
::
Result
<
(),
Box
<
dyn
std
::
error
::
Error
+
Send
+
Sync
>>
;
}
/// Connection Options
pub
struct
ConnectionOptions
{
with_reader
:
bool
,
c
hat
:
Option
<
Box
<
ChatFn
>>
,
c
onnection_handler
:
Option
<
Box
<
dyn
ConnectionHandler
+
Send
+
Sync
>>
,
timeouts
:
Timeouts
,
}
...
...
@@ -146,7 +156,7 @@ impl ConnectionOptions {
pub
fn
new
(
timeout
:
Duration
)
->
Self
{
Self
{
with_reader
:
false
,
c
hat
:
None
,
c
onnection_handler
:
None
,
timeouts
:
Timeouts
{
connect
:
timeout
,
read
:
timeout
,
...
...
@@ -161,16 +171,13 @@ impl ConnectionOptions {
self
.with_reader
=
true
;
self
}
/// Set the c
hat function. The chat function is called after the connection is established. The
///
chat function can be used to implement custom protocols that require additional setup
.
pub
fn
c
hat
<
F
>
(
mut
self
,
chat
:
F
)
->
Self
/// Set the c
onnection handler. The connection handler is used to implement custom protocols
///
that require additional setup/handling. Replaces "chat" function
.
pub
fn
c
onnection_handler
<
T
>
(
mut
self
,
connection_handler
:
T
)
->
Self
where
F
:
Fn
(
&
mut
dyn
Stream
)
->
std
::
result
::
Result
<
(),
Box
<
dyn
std
::
error
::
Error
+
Send
+
Sync
>>
+
Send
+
Sync
+
'static
,
T
:
ConnectionHandler
+
Send
+
Sync
+
'static
,
{
self
.c
hat
=
Some
(
Box
::
new
(
chat
));
self
.c
onnection_handler
=
Some
(
Box
::
new
(
connection_handler
));
self
}
/// Set timeouts
...
...
src/comm/serial.rs
浏览文件 @
75e96170
...
...
@@ -153,6 +153,9 @@ impl Communicator for Serial {
fn
session_id
(
&
self
)
->
usize
{
self
.session_id
.load
(
Ordering
::
Acquire
)
}
fn
connect
(
&
self
)
->
Result
<
()
>
{
self
.get_port
()
.map
(|
_
|
())
}
fn
reconnect
(
&
self
)
{
let
mut
port
=
self
.port
.lock
();
port
.system_port
.take
();
...
...
src/comm/tcp.rs
浏览文件 @
75e96170
...
...
@@ -2,7 +2,8 @@ use crate::pchannel;
use
crate
::{
Error
,
Result
};
use
super
::{
ChatFn
,
Client
,
CommReader
,
Communicator
,
ConnectionOptions
,
Protocol
,
Stream
,
Timeouts
,
Client
,
CommReader
,
Communicator
,
ConnectionHandler
,
ConnectionOptions
,
Protocol
,
Stream
,
Timeouts
,
};
use
core
::
fmt
;
use
parking_lot_rt
::{
Mutex
,
MutexGuard
};
...
...
@@ -46,7 +47,7 @@ pub struct Tcp {
session_id
:
AtomicUsize
,
allow_reconnect
:
AtomicBool
,
reader_tx
:
Option
<
pchannel
::
Sender
<
CommReader
>>
,
c
hat
:
Option
<
Box
<
ChatFn
>>
,
c
onnection_handler
:
Option
<
Box
<
dyn
ConnectionHandler
+
Send
+
Sync
>>
,
}
#[allow(clippy
::
module_name_repetitions)]
...
...
@@ -68,6 +69,9 @@ impl Communicator for Tcp {
fn
session_id
(
&
self
)
->
usize
{
self
.session_id
.load
(
Ordering
::
Acquire
)
}
fn
connect
(
&
self
)
->
Result
<
()
>
{
self
.get_stream
()
.map
(|
_
|
())
}
fn
reconnect
(
&
self
)
{
self
.stream
.lock
()
...
...
@@ -136,7 +140,7 @@ impl Tcp {
session_id
:
<
_
>
::
default
(),
allow_reconnect
:
AtomicBool
::
new
(
true
),
reader_tx
:
tx
,
c
hat
:
options
.chat
,
c
onnection_handler
:
options
.connection_handler
,
};
Ok
((
client
.into
(),
rx
))
}
...
...
@@ -160,9 +164,11 @@ impl Tcp {
stream
.set_write_timeout
(
Some
(
self
.timeouts.write
))
?
;
}
stream
.set_nodelay
(
true
)
?
;
if
let
Some
(
ref
chat
)
=
self
.chat
{
trace!
(
"chatting with the server"
);
chat
(
&
mut
stream
)
.map_err
(
Error
::
io
)
?
;
if
let
Some
(
ref
connection_handler
)
=
self
.connection_handler
{
trace!
(
"starting connection handler"
);
connection_handler
.on_connect
(
&
mut
stream
)
.map_err
(
Error
::
io
)
?
;
}
self
.session_id
.fetch_add
(
1
,
Ordering
::
Release
);
trace!
(
addr
=%
self
.addr
,
session_id
=
self
.session_id
(),
"TCP session started"
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论