From e1d3529e22eb2aac1275f38c81c5c2a7fb4a6427 Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Sat, 17 Jun 2023 15:39:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0rtu=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E5=BC=80=E5=90=AF.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client_tcp.go | 2 +- engine.go | 4 ++-- options.go | 8 +++---- server_rtu.go | 47 ++++++++++++++++++++++++++++++++++++++++++ server_tcp.go | 2 +- server_ws.go | 2 +- test/test_nnet_test.go | 2 +- 7 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 server_rtu.go diff --git a/client_tcp.go b/client_tcp.go index fa43f2f..b850de3 100644 --- a/client_tcp.go +++ b/client_tcp.go @@ -2,7 +2,7 @@ package nnet import ( "git.noahlan.cn/noahlan/nnet/connection" - "git.noahlan.cn/noahlan/ntools-go/core/nlog" + "git.noahlan.cn/noahlan/ntool/nlog" "net" ) diff --git a/engine.go b/engine.go index 4586778..3a1787a 100644 --- a/engine.go +++ b/engine.go @@ -7,8 +7,8 @@ import ( "git.noahlan.cn/noahlan/nnet/packet" rt "git.noahlan.cn/noahlan/nnet/router" "git.noahlan.cn/noahlan/nnet/scheduler" - "git.noahlan.cn/noahlan/nnet/serialize" "git.noahlan.cn/noahlan/nnet/session" + "git.noahlan.cn/noahlan/ntool/ndef" "git.noahlan.cn/noahlan/ntool/nlog" "github.com/panjf2000/ants/v2" "math" @@ -24,7 +24,7 @@ type Engine struct { dieChan chan struct{} // 应用程序退出信号 pipeline connection.Pipeline // 消息管道 packerBuilder packet.PackerBuilder // 封包、拆包器 - serializer serialize.Serializer // 消息 序列化/反序列化 + serializer ndef.Serializer // 消息 序列化/反序列化 goPool *ants.Pool // goroutine池 connManager *connection.Manager // 连接管理器 lifetime *lifetime.Mgr // 生命周期 diff --git a/options.go b/options.go index b9ddca1..e8ae6d6 100644 --- a/options.go +++ b/options.go @@ -5,8 +5,8 @@ import ( "git.noahlan.cn/noahlan/nnet/lifetime" "git.noahlan.cn/noahlan/nnet/packet" rt "git.noahlan.cn/noahlan/nnet/router" - "git.noahlan.cn/noahlan/nnet/serialize" - "git.noahlan.cn/noahlan/ntools-go/core/pool" + "git.noahlan.cn/noahlan/ntool/ndef" + "git.noahlan.cn/noahlan/ntool/npool" "github.com/panjf2000/ants/v2" "time" ) @@ -73,7 +73,7 @@ func WithPackerBuilder(fn packet.PackerBuilder) RunOption { } // WithSerializer 设置消息的 序列化/反序列化 方式 -func WithSerializer(s serialize.Serializer) RunOption { +func WithSerializer(s ndef.Serializer) RunOption { return func(ngin *Engine) { ngin.serializer = s } @@ -87,7 +87,7 @@ func WithPool(pl *ants.Pool) RunOption { } // WithPoolCfg 设置工作池配置 -func WithPoolCfg(cfg pool.Config) RunOption { +func WithPoolCfg(cfg npool.Config) RunOption { return func(ngin *Engine) { ngin.goPool, _ = ants.NewPool(cfg.PoolSize, ants.WithOptions(cfg.Options())) } diff --git a/server_rtu.go b/server_rtu.go new file mode 100644 index 0000000..34ef30d --- /dev/null +++ b/server_rtu.go @@ -0,0 +1,47 @@ +package nnet + +import ( + "git.noahlan.cn/noahlan/nnet/connection" + "git.noahlan.cn/noahlan/ntool/nlog" + "github.com/goburrow/serial" + "sync" +) + +func (ngin *Engine) ServerRTU(conf serial.Config) error { + err := ngin.setup() + if err != nil { + nlog.Errorf("%s failed to setup server, err:%v", ngin.LogPrefix(), err) + return err + } + + port, err := serial.Open(&conf) + if err != nil { + nlog.Errorf("%s failed to open %s, err:%v", ngin.LogPrefix(), conf.Address, err) + return err + } + + nlog.Infof("%s now open %s at rate:%d ...", ngin.LogPrefix(), conf.Address, conf.BaudRate) + defer func() { + _ = port.Close() + ngin.Stop() + }() + + var wg sync.WaitGroup + wg.Add(1) + + ngin.handle(connection.NewRTUConn(port, &conf)) + + go func() { + for { + select { + case <-ngin.dieChan: + wg.Done() + default: + } + } + }() + + wg.Wait() + + return nil +} diff --git a/server_tcp.go b/server_tcp.go index a889f30..073e5dd 100644 --- a/server_tcp.go +++ b/server_tcp.go @@ -3,7 +3,7 @@ package nnet import ( "errors" "git.noahlan.cn/noahlan/nnet/config" - "git.noahlan.cn/noahlan/ntools-go/core/nlog" + "git.noahlan.cn/noahlan/ntool/nlog" "net" ) diff --git a/server_ws.go b/server_ws.go index 584721b..f7e12fc 100644 --- a/server_ws.go +++ b/server_ws.go @@ -4,7 +4,7 @@ import ( "fmt" "git.noahlan.cn/noahlan/nnet/config" "git.noahlan.cn/noahlan/nnet/connection" - "git.noahlan.cn/noahlan/ntools-go/core/nlog" + "git.noahlan.cn/noahlan/ntool/nlog" "github.com/gorilla/websocket" "net/http" "os" diff --git a/test/test_nnet_test.go b/test/test_nnet_test.go index 001b1a3..543a131 100644 --- a/test/test_nnet_test.go +++ b/test/test_nnet_test.go @@ -5,7 +5,7 @@ import ( "git.noahlan.cn/noahlan/nnet/packet" "git.noahlan.cn/noahlan/nnet/protocol/nnet" rt "git.noahlan.cn/noahlan/nnet/router" - "git.noahlan.cn/noahlan/ntools-go/core/nlog" + "git.noahlan.cn/noahlan/ntool/nlog" "sync" "testing" )