From e99030b9192c6da6e1d6b08cba1a237d913f979f Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Sat, 17 Jun 2023 15:30:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8C=85=E5=BC=95=E7=94=A8=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=9B=E6=B7=BB=E5=8A=A0rtu=E7=9B=B8=E5=85=B3=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- connection/connection.go | 19 +-- connection/group.go | 2 +- connection/rtu.go | 39 ++++++ connection/rtu_addr.go | 35 +++++ engine.go | 3 +- go.mod | 17 +-- go.sum | 11 ++ middleware/heartbeat.go | 2 +- packet/packet.go | 2 + protocol/modbus/crc.go | 43 ------ protocol/modbus/crc_test.go | 14 -- protocol/modbus/internal/assert.go | 167 ------------------------ protocol/modbus/internal/assert_test.go | 50 ------- protocol/nnet/client_pipeline_nnet.go | 2 +- protocol/nnet/nnet.go | 2 +- protocol/nnet/pipeline_nnet.go | 2 +- protocol/nnet/router_nnet.go | 2 +- protocol/plain/router_plain.go | 2 +- router/router.go | 2 +- scheduler/scheduler.go | 2 +- scheduler/timer.go | 2 +- serialize/json/json.go | 4 +- serialize/protobuf/protobuf.go | 4 +- serialize/serializer.go | 19 --- test/test_nnet.go | 8 +- 25 files changed, 127 insertions(+), 328 deletions(-) create mode 100644 connection/rtu.go create mode 100644 connection/rtu_addr.go delete mode 100644 protocol/modbus/crc.go delete mode 100644 protocol/modbus/crc_test.go delete mode 100644 protocol/modbus/internal/assert.go delete mode 100644 protocol/modbus/internal/assert_test.go delete mode 100644 serialize/serializer.go diff --git a/connection/connection.go b/connection/connection.go index a9b1f1c..3eb72ba 100644 --- a/connection/connection.go +++ b/connection/connection.go @@ -4,10 +4,10 @@ import ( "errors" "fmt" "git.noahlan.cn/noahlan/nnet/packet" - "git.noahlan.cn/noahlan/nnet/serialize" "git.noahlan.cn/noahlan/nnet/session" - "git.noahlan.cn/noahlan/ntools-go/core/nlog" - "git.noahlan.cn/noahlan/ntools-go/core/pool" + "git.noahlan.cn/noahlan/ntool/ndef" + "git.noahlan.cn/noahlan/ntool/nlog" + "github.com/panjf2000/ants/v2" "net" "sync/atomic" ) @@ -42,13 +42,14 @@ type ( Connection struct { conf Config // 配置 session *session.Session // Session + pool *ants.Pool // 连接池 status int32 // 连接状态 conn net.Conn // low-level conn fd typ ConnType // 连接类型 packer packet.Packer // 封包、拆包器 - serializer serialize.Serializer // 消息序列化/反序列化器 + serializer ndef.Serializer // 消息序列化/反序列化器 pipeline Pipeline // 连接生命周期管理 handleFn func(conn *Connection, pkg packet.IPacket) // 消息处理方法 @@ -75,14 +76,16 @@ type ( func NewConnection( id int64, conn net.Conn, + pool *ants.Pool, conf Config, packerBuilder packet.PackerBuilder, - serializer serialize.Serializer, + serializer ndef.Serializer, pipeline Pipeline, handleFn packetFn) *Connection { r := &Connection{ conf: conf, session: session.NewSession(id), + pool: pool, status: StatusStart, conn: conn, @@ -158,11 +161,11 @@ func (r *Connection) SetLastMID(mid uint64) { } func (r *Connection) Serve() { - _ = pool.Submit(func() { + _ = r.pool.Submit(func() { r.write() }) - _ = pool.Submit(func() { + _ = r.pool.Submit(func() { r.read() }) } @@ -278,7 +281,7 @@ func (r *Connection) processPacket(packet packet.IPacket) error { if r.Status() == StatusWorking { // 处理包消息 - _ = pool.Submit(func() { + _ = r.pool.Submit(func() { r.handleFn(r, packet) }) } diff --git a/connection/group.go b/connection/group.go index 0228e6a..e7c2b28 100644 --- a/connection/group.go +++ b/connection/group.go @@ -2,7 +2,7 @@ package connection import ( "errors" - "git.noahlan.cn/noahlan/ntools-go/core/nlog" + "git.noahlan.cn/noahlan/ntool/nlog" "sync" "sync/atomic" ) diff --git a/connection/rtu.go b/connection/rtu.go new file mode 100644 index 0000000..65ea790 --- /dev/null +++ b/connection/rtu.go @@ -0,0 +1,39 @@ +package connection + +import ( + "github.com/goburrow/serial" + "net" + "time" +) + +// RTUConn rtu connection wrapper +type RTUConn struct { + serial.Port + config *serial.Config +} + +// NewRTUConn 新建 RTUConn +func NewRTUConn(port serial.Port, cfg *serial.Config) net.Conn { + return &RTUConn{Port: port, config: cfg} +} + +func (c *RTUConn) LocalAddr() net.Addr { + cfg := c.config + return NewRTUAddr(cfg.Address, cfg.BaudRate, cfg.DataBits, cfg.StopBits, cfg.Parity) +} + +func (c *RTUConn) RemoteAddr() net.Addr { + return c.LocalAddr() +} + +func (c *RTUConn) SetDeadline(_ time.Time) error { + return nil +} + +func (c *RTUConn) SetReadDeadline(_ time.Time) error { + return nil +} + +func (c *RTUConn) SetWriteDeadline(_ time.Time) error { + return nil +} diff --git a/connection/rtu_addr.go b/connection/rtu_addr.go new file mode 100644 index 0000000..eabaa6b --- /dev/null +++ b/connection/rtu_addr.go @@ -0,0 +1,35 @@ +package connection + +import ( + "fmt" + "net" +) + +const RTUNetwork = "rtu" + +type RTUAddr struct { + Address string + BaudRate int + DataBits int + StopBits int + Parity string +} + +func NewRTUAddr(addr string, rate int, dataBits int, stopBits int, parity string) net.Addr { + return &RTUAddr{ + Address: addr, + BaudRate: rate, + DataBits: dataBits, + StopBits: stopBits, + Parity: parity, + } +} + +func (r *RTUAddr) Network() string { + return RTUNetwork +} + +// String form of address (for example, "/dev/ttyS0|19200|8|1|N", "com1|19200|8|1|N") +func (r *RTUAddr) String() string { + return fmt.Sprintf("%s|%d|%d|%d", r.Address, r.BaudRate, r.DataBits, r.StopBits) +} diff --git a/engine.go b/engine.go index 6e1205c..4586778 100644 --- a/engine.go +++ b/engine.go @@ -9,7 +9,7 @@ import ( "git.noahlan.cn/noahlan/nnet/scheduler" "git.noahlan.cn/noahlan/nnet/serialize" "git.noahlan.cn/noahlan/nnet/session" - "git.noahlan.cn/noahlan/ntools-go/core/nlog" + "git.noahlan.cn/noahlan/ntool/nlog" "github.com/panjf2000/ants/v2" "math" "net" @@ -109,6 +109,7 @@ func (ngin *Engine) handle(conn net.Conn) *connection.Connection { nc := connection.NewConnection( ngin.sessIdMgr.SessionID(), conn, + ngin.goPool, connection.Config{LogDebug: ngin.ShallLogDebug(), LogPrefix: ngin.LogPrefix()}, ngin.packerBuilder, ngin.serializer, ngin.pipeline, ngin.router.Handle, diff --git a/go.mod b/go.mod index b42d2e0..b3e566b 100644 --- a/go.mod +++ b/go.mod @@ -4,20 +4,21 @@ go 1.20 require ( github.com/gorilla/websocket v1.5.0 - github.com/panjf2000/ants/v2 v2.7.3 // indirect + github.com/panjf2000/ants/v2 v2.7.5 // indirect ) require google.golang.org/protobuf v1.28.2-0.20220831092852-f930b1dc76e8 require ( - git.noahlan.cn/noahlan/ntools-go/core v1.1.3 + git.noahlan.cn/noahlan/ntool v1.0.1 // indirect github.com/fatih/color v1.15.0 // indirect + github.com/goburrow/serial v0.1.0 // indirect github.com/gofrs/uuid/v5 v5.0.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.17 // indirect - go.opentelemetry.io/otel v1.14.0 // indirect - go.opentelemetry.io/otel/trace v1.14.0 // indirect - golang.org/x/crypto v0.7.0 // indirect - golang.org/x/sys v0.6.0 // indirect - golang.org/x/text v0.8.0 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + go.opentelemetry.io/otel v1.16.0 // indirect + go.opentelemetry.io/otel/trace v1.16.0 // indirect + golang.org/x/crypto v0.10.0 // indirect + golang.org/x/sys v0.9.0 // indirect + golang.org/x/text v0.10.0 // indirect ) diff --git a/go.sum b/go.sum index c6aa715..c101ffe 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +git.noahlan.cn/noahlan/ntool v1.0.1 h1:c2sw65BmyqQHKn3+s2Bjdkz9AtSKI4e1WAkRi/CG1oc= +git.noahlan.cn/noahlan/ntool v1.0.1/go.mod h1:nLp9j4lz203kNpVFrNbT25A2QHbx235gXv+Ut1bX8E8= git.noahlan.cn/noahlan/ntools-go/core v1.1.3 h1:n4z0KaXmX/fmobavxCMc2vGJDoStbhNbm8AZugPEPGg= git.noahlan.cn/noahlan/ntools-go/core v1.1.3/go.mod h1:pmwee9V76Cyp6nVr3dPj5TpePLvRpc8C0ZgAzFIFAKU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -7,6 +9,8 @@ github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/goburrow/serial v0.1.0 h1:v2T1SQa/dlUqQiYIT8+Cu7YolfqAi3K96UmhwYyuSrA= +github.com/goburrow/serial v0.1.0/go.mod h1:sAiqG0nRVswsm1C97xsttiYCzSLBmUZ/VSlVLZJ8haA= github.com/gofrs/uuid/v5 v5.0.0 h1:p544++a97kEL+svbcFbCQVM9KFu0Yo25UoISXGNNH9M= github.com/gofrs/uuid/v5 v5.0.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= @@ -19,8 +23,10 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/panjf2000/ants/v2 v2.7.3 h1:rHQ0hH0DQvuNUqqlWIMJtkMcDuL1uQAfpX2mIhQ5/s0= github.com/panjf2000/ants/v2 v2.7.3/go.mod h1:KIBmYG9QQX5U2qzFP/yQJaq/nSb6rahS9iEHkrCMgM8= +github.com/panjf2000/ants/v2 v2.7.5/go.mod h1:KIBmYG9QQX5U2qzFP/yQJaq/nSb6rahS9iEHkrCMgM8= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -32,18 +38,23 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= go.opentelemetry.io/otel v1.14.0 h1:/79Huy8wbf5DnIPhemGB+zEPVwnN6fuQybr/SRXa6hM= go.opentelemetry.io/otel v1.14.0/go.mod h1:o4buv+dJzx8rohcUeRmWUZhqupFvzWis188WlggnNeU= +go.opentelemetry.io/otel v1.16.0/go.mod h1:vl0h9NUa1D5s1nv3A5vZOYWn8av4K8Ml6JDeHrT/bx4= go.opentelemetry.io/otel/sdk v1.14.0 h1:PDCppFRDq8A1jL9v6KMI6dYesaq+DFcDZvjsoGvxGzY= go.opentelemetry.io/otel/trace v1.14.0 h1:wp2Mmvj41tDsyAJXiWDWpfNsOiIyd38fy85pyKcFq/M= go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+gozhnZjy/rw9G8= +go.opentelemetry.io/otel/trace v1.16.0/go.mod h1:Yt9vYq1SdNz3xdjZZK7wcXv1qv2pwLkqr2QVwea0ef0= golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= +golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.28.2-0.20220831092852-f930b1dc76e8 h1:KR8+MyP7/qOlV+8Af01LtjL04bu7on42eVsxT4EyBQk= diff --git a/middleware/heartbeat.go b/middleware/heartbeat.go index 17a87cf..00a1ea9 100644 --- a/middleware/heartbeat.go +++ b/middleware/heartbeat.go @@ -5,7 +5,7 @@ import ( "git.noahlan.cn/noahlan/nnet/connection" "git.noahlan.cn/noahlan/nnet/packet" rt "git.noahlan.cn/noahlan/nnet/router" - "git.noahlan.cn/noahlan/ntools-go/core/nlog" + "git.noahlan.cn/noahlan/ntool/nlog" "sync/atomic" "time" ) diff --git a/packet/packet.go b/packet/packet.go index b24fcf0..b0ff85c 100644 --- a/packet/packet.go +++ b/packet/packet.go @@ -5,6 +5,8 @@ import "errors" var ( // ErrWrongPacketType represents a wrong packet type. ErrWrongPacketType = errors.New("wrong packet type") + // ErrPacketSizeExceed represents a wrong packet size. + ErrPacketSizeExceed = errors.New("packet size exceed") ) type ( diff --git a/protocol/modbus/crc.go b/protocol/modbus/crc.go deleted file mode 100644 index 1ef332b..0000000 --- a/protocol/modbus/crc.go +++ /dev/null @@ -1,43 +0,0 @@ -package modbus - -import "sync" - -var crcTable []uint16 -var mu sync.Mutex - -// crcModbus 计算modbus的crc -func crcModbus(data []byte) (crc uint16) { - if crcTable == nil { - mu.Lock() - if crcTable == nil { - initCrcTable() - } - mu.Unlock() - } - crc = 0xffff - for _, v := range data { - crc = (crc >> 8) ^ crcTable[(crc^uint16(v))&0x00FF] - } - return crc -} - -// initCrcTable 初始化crcTable -func initCrcTable() { - crc16IBM := uint16(0xA001) - crcTable = make([]uint16, 256) - - for i := uint16(0); i < 256; i++ { - crc := uint16(0) - c := i - - for j := uint16(0); j < 8; j++ { - if ((crc ^ c) & 0x0001) > 0 { - crc = (crc >> 1) ^ crc16IBM - } else { - crc = crc >> 1 - } - c = c >> 1 - } - crcTable[i] = crc - } -} diff --git a/protocol/modbus/crc_test.go b/protocol/modbus/crc_test.go deleted file mode 100644 index b91bdf9..0000000 --- a/protocol/modbus/crc_test.go +++ /dev/null @@ -1,14 +0,0 @@ -package modbus - -import ( - "git.noahlan.cn/noahlan/nnet/protocol/modbus/internal" - "testing" -) - -func TestCRC(t *testing.T) { - got := crcModbus([]byte{0x01, 0x04, 0x02, 0xFF, 0xFF}) - expect := uint16(0x80B8) - - assert := internal.NewAssert(t, "TestCRC") - assert.Equal(expect, got) -} diff --git a/protocol/modbus/internal/assert.go b/protocol/modbus/internal/assert.go deleted file mode 100644 index e6a490e..0000000 --- a/protocol/modbus/internal/assert.go +++ /dev/null @@ -1,167 +0,0 @@ -package internal - -import ( - "fmt" - "reflect" - "runtime" - "testing" -) - -const ( - compareNotEqual int = iota - 2 - compareLess - compareEqual - compareGreater -) - -// Assert is a simple implementation of assertion, only for internal usage -type Assert struct { - T *testing.T - CaseName string -} - -// NewAssert return instance of Assert -func NewAssert(t *testing.T, caseName string) *Assert { - return &Assert{T: t, CaseName: caseName} -} - -// Equal check if expected is equal with actual -func (a *Assert) Equal(expected, actual any) { - if compare(expected, actual) != compareEqual { - makeTestFailed(a.T, a.CaseName, expected, actual) - } -} - -// NotEqual check if expected is not equal with actual -func (a *Assert) NotEqual(expected, actual any) { - if compare(expected, actual) == compareEqual { - expectedInfo := fmt.Sprintf("not %v", expected) - makeTestFailed(a.T, a.CaseName, expectedInfo, actual) - } -} - -// Greater check if expected is greate than actual -func (a *Assert) Greater(expected, actual any) { - if compare(expected, actual) != compareGreater { - expectedInfo := fmt.Sprintf("> %v", expected) - makeTestFailed(a.T, a.CaseName, expectedInfo, actual) - } -} - -// GreaterOrEqual check if expected is greate than or equal with actual -func (a *Assert) GreaterOrEqual(expected, actual any) { - isGreatOrEqual := compare(expected, actual) == compareGreater || compare(expected, actual) == compareEqual - if !isGreatOrEqual { - expectedInfo := fmt.Sprintf(">= %v", expected) - makeTestFailed(a.T, a.CaseName, expectedInfo, actual) - } -} - -// Less check if expected is less than actual -func (a *Assert) Less(expected, actual any) { - if compare(expected, actual) != compareLess { - expectedInfo := fmt.Sprintf("< %v", expected) - makeTestFailed(a.T, a.CaseName, expectedInfo, actual) - } -} - -// LessOrEqual check if expected is less than or equal with actual -func (a *Assert) LessOrEqual(expected, actual any) { - isLessOrEqual := compare(expected, actual) == compareLess || compare(expected, actual) == compareEqual - if !isLessOrEqual { - expectedInfo := fmt.Sprintf("<= %v", expected) - makeTestFailed(a.T, a.CaseName, expectedInfo, actual) - } -} - -// IsNil check if value is nil -func (a *Assert) IsNil(value any) { - if value != nil { - makeTestFailed(a.T, a.CaseName, nil, value) - } -} - -// IsNotNil check if value is not nil -func (a *Assert) IsNotNil(value any) { - if value == nil { - makeTestFailed(a.T, a.CaseName, "not nil", value) - } -} - -// compare x and y return : -// x > y -> 1, x < y -> -1, x == y -> 0, x != y -> -2 -func compare(x, y any) int { - vx := reflect.ValueOf(x) - vy := reflect.ValueOf(y) - - if vx.Type() != vy.Type() { - return compareNotEqual - } - - switch vx.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - xInt := vx.Int() - yInt := vy.Int() - if xInt > yInt { - return compareGreater - } - if xInt == yInt { - return compareEqual - } - if xInt < yInt { - return compareLess - } - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - xUint := vx.Uint() - yUint := vy.Uint() - if xUint > yUint { - return compareGreater - } - if xUint == yUint { - return compareEqual - } - if xUint < yUint { - return compareLess - } - case reflect.Float32, reflect.Float64: - xFloat := vx.Float() - yFloat := vy.Float() - if xFloat > yFloat { - return compareGreater - } - if xFloat == yFloat { - return compareEqual - } - if xFloat < yFloat { - return compareLess - } - case reflect.String: - xString := vx.String() - yString := vy.String() - if xString > yString { - return compareGreater - } - if xString == yString { - return compareEqual - } - if xString < yString { - return compareLess - } - default: - if reflect.DeepEqual(x, y) { - return compareEqual - } - return compareNotEqual - } - - return compareNotEqual - -} - -// logFailedInfo make test failed and log error info -func makeTestFailed(t *testing.T, caseName string, expected, actual any) { - _, file, line, _ := runtime.Caller(2) - errInfo := fmt.Sprintf("Case %v failed. file: %v, line: %v, expected: %v, actual: %v.", caseName, file, line, expected, actual) - t.Error(errInfo) - t.FailNow() -} diff --git a/protocol/modbus/internal/assert_test.go b/protocol/modbus/internal/assert_test.go deleted file mode 100644 index ae0bcfa..0000000 --- a/protocol/modbus/internal/assert_test.go +++ /dev/null @@ -1,50 +0,0 @@ -package internal - -import ( - "testing" -) - -func TestAssert(t *testing.T) { - assert := NewAssert(t, "TestAssert") - assert.Equal(0, 0) - assert.NotEqual(1, 0) - - assert.NotEqual("1", 1) - var uInt1 uint - var uInt2 uint - var uInt8 uint8 - var uInt16 uint16 - var uInt32 uint32 - var uInt64 uint64 - assert.NotEqual(uInt1, uInt8) - assert.NotEqual(uInt8, uInt16) - assert.NotEqual(uInt16, uInt32) - assert.NotEqual(uInt32, uInt64) - - assert.Equal(uInt1, uInt2) - - uInt1 = 1 - uInt2 = 2 - assert.Less(uInt1, uInt2) - - assert.Greater(1, 0) - assert.GreaterOrEqual(1, 1) - assert.Less(0, 1) - assert.LessOrEqual(0, 0) - - assert.Equal(0.1, 0.1) - assert.Greater(1.1, 0.1) - assert.Less(0.1, 1.1) - - assert.Equal("abc", "abc") - assert.NotEqual("abc", "abd") - assert.Less("abc", "abd") - assert.Greater("abd", "abc") - - assert.Equal([]int{1, 2, 3}, []int{1, 2, 3}) - assert.NotEqual([]int{1, 2, 3}, []int{1, 2}) - - assert.IsNil(nil) - assert.IsNotNil("abc") - -} diff --git a/protocol/nnet/client_pipeline_nnet.go b/protocol/nnet/client_pipeline_nnet.go index ab83d81..a381a44 100644 --- a/protocol/nnet/client_pipeline_nnet.go +++ b/protocol/nnet/client_pipeline_nnet.go @@ -7,7 +7,7 @@ import ( "git.noahlan.cn/noahlan/nnet" "git.noahlan.cn/noahlan/nnet/connection" "git.noahlan.cn/noahlan/nnet/packet" - "git.noahlan.cn/noahlan/ntools-go/core/nlog" + "git.noahlan.cn/noahlan/ntool/nlog" ) type OnReadyFunc func() diff --git a/protocol/nnet/nnet.go b/protocol/nnet/nnet.go index de8c224..37a8c15 100644 --- a/protocol/nnet/nnet.go +++ b/protocol/nnet/nnet.go @@ -5,7 +5,7 @@ import ( "git.noahlan.cn/noahlan/nnet/connection" "git.noahlan.cn/noahlan/nnet/middleware" "git.noahlan.cn/noahlan/nnet/packet" - "git.noahlan.cn/noahlan/ntools-go/core/nlog" + "git.noahlan.cn/noahlan/ntool/nlog" "time" ) diff --git a/protocol/nnet/pipeline_nnet.go b/protocol/nnet/pipeline_nnet.go index 67d3d0c..f2ac1ab 100644 --- a/protocol/nnet/pipeline_nnet.go +++ b/protocol/nnet/pipeline_nnet.go @@ -7,7 +7,7 @@ import ( "git.noahlan.cn/noahlan/nnet" "git.noahlan.cn/noahlan/nnet/connection" "git.noahlan.cn/noahlan/nnet/packet" - "git.noahlan.cn/noahlan/ntools-go/core/nlog" + "git.noahlan.cn/noahlan/ntool/nlog" ) type ( diff --git a/protocol/nnet/router_nnet.go b/protocol/nnet/router_nnet.go index 5c2a1a2..7b6e7ad 100644 --- a/protocol/nnet/router_nnet.go +++ b/protocol/nnet/router_nnet.go @@ -6,7 +6,7 @@ import ( "git.noahlan.cn/noahlan/nnet/connection" "git.noahlan.cn/noahlan/nnet/packet" rt "git.noahlan.cn/noahlan/nnet/router" - "git.noahlan.cn/noahlan/ntools-go/core/nlog" + "git.noahlan.cn/noahlan/ntool/nlog" ) type ( diff --git a/protocol/plain/router_plain.go b/protocol/plain/router_plain.go index 3416a0c..a108b1a 100644 --- a/protocol/plain/router_plain.go +++ b/protocol/plain/router_plain.go @@ -4,7 +4,7 @@ import ( "git.noahlan.cn/noahlan/nnet/connection" "git.noahlan.cn/noahlan/nnet/packet" "git.noahlan.cn/noahlan/nnet/router" - "git.noahlan.cn/noahlan/ntools-go/core/nlog" + "git.noahlan.cn/noahlan/ntool/nlog" ) type Router struct { diff --git a/router/router.go b/router/router.go index d5746c9..0b22606 100644 --- a/router/router.go +++ b/router/router.go @@ -3,7 +3,7 @@ package router import ( "git.noahlan.cn/noahlan/nnet/connection" "git.noahlan.cn/noahlan/nnet/packet" - "git.noahlan.cn/noahlan/ntools-go/core/nlog" + "git.noahlan.cn/noahlan/ntool/nlog" ) type ( diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index fb1ed8a..4fc3f28 100644 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -1,7 +1,7 @@ package scheduler import ( - "git.noahlan.cn/noahlan/ntools-go/core/nlog" + "git.noahlan.cn/noahlan/ntool/nlog" "runtime/debug" "sync/atomic" "time" diff --git a/scheduler/timer.go b/scheduler/timer.go index e0ca9e0..ebb5b14 100644 --- a/scheduler/timer.go +++ b/scheduler/timer.go @@ -1,7 +1,7 @@ package scheduler import ( - "git.noahlan.cn/noahlan/ntools-go/core/nlog" + "git.noahlan.cn/noahlan/ntool/nlog" "math" "runtime/debug" "sync" diff --git a/serialize/json/json.go b/serialize/json/json.go index f026877..c48a798 100644 --- a/serialize/json/json.go +++ b/serialize/json/json.go @@ -2,12 +2,12 @@ package json import ( "encoding/json" - "git.noahlan.cn/noahlan/nnet/serialize" + "git.noahlan.cn/noahlan/ntool/ndef" ) type Serializer struct{} -func NewSerializer() serialize.Serializer { +func NewSerializer() ndef.Serializer { return &Serializer{} } diff --git a/serialize/protobuf/protobuf.go b/serialize/protobuf/protobuf.go index 707dd52..0a5cccb 100644 --- a/serialize/protobuf/protobuf.go +++ b/serialize/protobuf/protobuf.go @@ -2,7 +2,7 @@ package protobuf import ( "errors" - "git.noahlan.cn/noahlan/nnet/serialize" + "git.noahlan.cn/noahlan/ntool/ndef" "google.golang.org/protobuf/proto" ) @@ -11,7 +11,7 @@ var ErrWrongValueType = errors.New("protobuf: convert on wrong type value") type Serializer struct{} -func NewSerializer() serialize.Serializer { +func NewSerializer() ndef.Serializer { return &Serializer{} } diff --git a/serialize/serializer.go b/serialize/serializer.go deleted file mode 100644 index a99ecab..0000000 --- a/serialize/serializer.go +++ /dev/null @@ -1,19 +0,0 @@ -package serialize - -type ( - // Marshaler 序列化 - Marshaler interface { - Marshal(v interface{}) ([]byte, error) - } - - // Unmarshaler 反序列化 - Unmarshaler interface { - Unmarshal(data []byte, v interface{}) error - } - - // Serializer 消息 序列化/反序列化,仅针对payload - Serializer interface { - Marshaler - Unmarshaler - } -) diff --git a/test/test_nnet.go b/test/test_nnet.go index 177a112..3d5e533 100644 --- a/test/test_nnet.go +++ b/test/test_nnet.go @@ -8,15 +8,15 @@ import ( "git.noahlan.cn/noahlan/nnet/packet" protocol_nnet "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/ntools-go/core/pool" + "git.noahlan.cn/noahlan/ntool/nlog" + "git.noahlan.cn/noahlan/ntool/npool" "math" "time" ) func runServer(addr string) { nginOpts := make([]nnet.RunOption, 0) - nginOpts = append(nginOpts, nnet.WithPoolCfg(pool.Config{ + nginOpts = append(nginOpts, nnet.WithPoolCfg(npool.Config{ PoolSize: math.MaxInt32, ExpiryDuration: time.Second, PreAlloc: false, @@ -68,7 +68,7 @@ func runClient(addr string) (*nnet.Engine, *connection.Connection) { chReady := make(chan struct{}) nginOpts := make([]nnet.RunOption, 0) - nginOpts = append(nginOpts, nnet.WithPoolCfg(pool.Config{ + nginOpts = append(nginOpts, nnet.WithPoolCfg(npool.Config{ PoolSize: math.MaxInt32, ExpiryDuration: time.Second, PreAlloc: false,