feat: 添加rtu监听开启.

main v0.7.1
NoahLan 1 year ago
parent e99030b919
commit e1d3529e22

@ -2,7 +2,7 @@ package nnet
import ( import (
"git.noahlan.cn/noahlan/nnet/connection" "git.noahlan.cn/noahlan/nnet/connection"
"git.noahlan.cn/noahlan/ntools-go/core/nlog" "git.noahlan.cn/noahlan/ntool/nlog"
"net" "net"
) )

@ -7,8 +7,8 @@ import (
"git.noahlan.cn/noahlan/nnet/packet" "git.noahlan.cn/noahlan/nnet/packet"
rt "git.noahlan.cn/noahlan/nnet/router" rt "git.noahlan.cn/noahlan/nnet/router"
"git.noahlan.cn/noahlan/nnet/scheduler" "git.noahlan.cn/noahlan/nnet/scheduler"
"git.noahlan.cn/noahlan/nnet/serialize"
"git.noahlan.cn/noahlan/nnet/session" "git.noahlan.cn/noahlan/nnet/session"
"git.noahlan.cn/noahlan/ntool/ndef"
"git.noahlan.cn/noahlan/ntool/nlog" "git.noahlan.cn/noahlan/ntool/nlog"
"github.com/panjf2000/ants/v2" "github.com/panjf2000/ants/v2"
"math" "math"
@ -24,7 +24,7 @@ type Engine struct {
dieChan chan struct{} // 应用程序退出信号 dieChan chan struct{} // 应用程序退出信号
pipeline connection.Pipeline // 消息管道 pipeline connection.Pipeline // 消息管道
packerBuilder packet.PackerBuilder // 封包、拆包器 packerBuilder packet.PackerBuilder // 封包、拆包器
serializer serialize.Serializer // 消息 序列化/反序列化 serializer ndef.Serializer // 消息 序列化/反序列化
goPool *ants.Pool // goroutine池 goPool *ants.Pool // goroutine池
connManager *connection.Manager // 连接管理器 connManager *connection.Manager // 连接管理器
lifetime *lifetime.Mgr // 生命周期 lifetime *lifetime.Mgr // 生命周期

@ -5,8 +5,8 @@ import (
"git.noahlan.cn/noahlan/nnet/lifetime" "git.noahlan.cn/noahlan/nnet/lifetime"
"git.noahlan.cn/noahlan/nnet/packet" "git.noahlan.cn/noahlan/nnet/packet"
rt "git.noahlan.cn/noahlan/nnet/router" rt "git.noahlan.cn/noahlan/nnet/router"
"git.noahlan.cn/noahlan/nnet/serialize" "git.noahlan.cn/noahlan/ntool/ndef"
"git.noahlan.cn/noahlan/ntools-go/core/pool" "git.noahlan.cn/noahlan/ntool/npool"
"github.com/panjf2000/ants/v2" "github.com/panjf2000/ants/v2"
"time" "time"
) )
@ -73,7 +73,7 @@ func WithPackerBuilder(fn packet.PackerBuilder) RunOption {
} }
// WithSerializer 设置消息的 序列化/反序列化 方式 // WithSerializer 设置消息的 序列化/反序列化 方式
func WithSerializer(s serialize.Serializer) RunOption { func WithSerializer(s ndef.Serializer) RunOption {
return func(ngin *Engine) { return func(ngin *Engine) {
ngin.serializer = s ngin.serializer = s
} }
@ -87,7 +87,7 @@ func WithPool(pl *ants.Pool) RunOption {
} }
// WithPoolCfg 设置工作池配置 // WithPoolCfg 设置工作池配置
func WithPoolCfg(cfg pool.Config) RunOption { func WithPoolCfg(cfg npool.Config) RunOption {
return func(ngin *Engine) { return func(ngin *Engine) {
ngin.goPool, _ = ants.NewPool(cfg.PoolSize, ants.WithOptions(cfg.Options())) ngin.goPool, _ = ants.NewPool(cfg.PoolSize, ants.WithOptions(cfg.Options()))
} }

@ -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
}

@ -3,7 +3,7 @@ package nnet
import ( import (
"errors" "errors"
"git.noahlan.cn/noahlan/nnet/config" "git.noahlan.cn/noahlan/nnet/config"
"git.noahlan.cn/noahlan/ntools-go/core/nlog" "git.noahlan.cn/noahlan/ntool/nlog"
"net" "net"
) )

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"git.noahlan.cn/noahlan/nnet/config" "git.noahlan.cn/noahlan/nnet/config"
"git.noahlan.cn/noahlan/nnet/connection" "git.noahlan.cn/noahlan/nnet/connection"
"git.noahlan.cn/noahlan/ntools-go/core/nlog" "git.noahlan.cn/noahlan/ntool/nlog"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"net/http" "net/http"
"os" "os"

@ -5,7 +5,7 @@ import (
"git.noahlan.cn/noahlan/nnet/packet" "git.noahlan.cn/noahlan/nnet/packet"
"git.noahlan.cn/noahlan/nnet/protocol/nnet" "git.noahlan.cn/noahlan/nnet/protocol/nnet"
rt "git.noahlan.cn/noahlan/nnet/router" rt "git.noahlan.cn/noahlan/nnet/router"
"git.noahlan.cn/noahlan/ntools-go/core/nlog" "git.noahlan.cn/noahlan/ntool/nlog"
"sync" "sync"
"testing" "testing"
) )

Loading…
Cancel
Save