Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: switch buffer to libgox/buffer #25

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (

"github.com/protocol-laboratory/opcua-go/opcua"
"github.com/protocol-laboratory/opcua-go/opcua/ua"
"github.com/shoothzj/gox/netx"
)

func main() {
logger := slog.Default()

config := &opcua.ClientConfig{
Address: netx.Address{
Address: opcua.Address{
Host: "localhost",
Port: 4840,
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/protocol-laboratory/opcua-go
go 1.21

require (
github.com/shoothzj/gox v0.0.4-0.20240916105254-02a0a7039d5d
github.com/libgox/buffer v0.0.0-20241010235618-d8b51aafaff0
github.com/stretchr/testify v1.9.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/libgox/buffer v0.0.0-20241010235618-d8b51aafaff0 h1:b13ZQECXGS1YxtFlDTBJbJKM4LgtsOyARasu0H620tY=
github.com/libgox/buffer v0.0.0-20241010235618-d8b51aafaff0/go.mod h1:VAuNNs9Mp5NttnP8etLB4FI4ZZ67sRUnk/jTb/BnSqo=
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/shoothzj/gox v0.0.4-0.20240916105254-02a0a7039d5d h1:oUVTx1ezGfBFkfRb192K+FFRAjjBX5u58qkbXEHfTdo=
github.com/shoothzj/gox v0.0.4-0.20240916105254-02a0a7039d5d/go.mod h1:JrD7u6Tem/9Ut4o0kQNK+aZkMWCba/d9VeabBZSEDT4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
17 changes: 17 additions & 0 deletions opcua/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package opcua

import (
"net"
"strconv"
)

type Address struct {
// Host domain name or ipv4, ipv6 address
Host string
// Port service port
Port int
}

func (addr Address) Addr() string {
return net.JoinHostPort(addr.Host, strconv.Itoa(addr.Port))
}
13 changes: 9 additions & 4 deletions opcua/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
"net"
"sync"

"github.com/shoothzj/gox/buffer"
"github.com/shoothzj/gox/netx"
"github.com/libgox/buffer"

"github.com/protocol-laboratory/opcua-go/opcua/ua"
)

type ClientConfig struct {
Address netx.Address
Address Address
BufferMax int
SendQueueSize int
PendingQueueSize int
Expand Down Expand Up @@ -169,7 +168,13 @@
}

func NewClient(config *ClientConfig) (*Client, error) {
conn, err := netx.Dial(config.Address, config.TlsConfig)
var conn net.Conn
var err error
if config.TlsConfig != nil {
conn, err = tls.Dial("tcp", config.Address.Addr(), config.TlsConfig)

Check warning on line 174 in opcua/client.go

View check run for this annotation

Codecov / codecov/patch

opcua/client.go#L174

Added line #L174 was not covered by tests
} else {
conn, err = net.Dial("tcp", config.Address.Addr())
}

if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion opcua/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"sync"
"time"

"github.com/shoothzj/gox/buffer"
"github.com/libgox/buffer"

"github.com/protocol-laboratory/opcua-go/opcua/ua"
)
Expand Down
3 changes: 1 addition & 2 deletions opcua/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/protocol-laboratory/opcua-go/opcua/ua"
"github.com/shoothzj/gox/netx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -27,7 +26,7 @@ func newTestSimpleClientServer(t *testing.T, serverConfig *ServerConfig, clientC
require.NoError(t, err, "Server should start without error")

clientConfig.Logger = testLogger
clientConfig.Address = netx.Address{
clientConfig.Address = Address{
Host: serverConfig.Host,
Port: port,
}
Expand Down
14 changes: 7 additions & 7 deletions opcua/ua/message_acknowledge.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ua

import (
"github.com/shoothzj/gox/buffer"
"github.com/libgox/buffer"
)

type MessageAcknowledge struct {
Expand Down Expand Up @@ -58,22 +58,22 @@ func (m *MessageAcknowledge) Buffer() (*buffer.Buffer, error) {
if _, err := buf.Write([]byte{'F'}); err != nil {
return nil, err
}
if err := buf.PutUInt32Le(uint32(m.Length())); err != nil {
if err := buf.WriteUInt32Le(uint32(m.Length())); err != nil {
return nil, err
}
if err := buf.PutUInt32Le(m.Version); err != nil {
if err := buf.WriteUInt32Le(m.Version); err != nil {
return nil, err
}
if err := buf.PutUInt32Le(m.ReceiveBufferSize); err != nil {
if err := buf.WriteUInt32Le(m.ReceiveBufferSize); err != nil {
return nil, err
}
if err := buf.PutUInt32Le(m.SendBufferSize); err != nil {
if err := buf.WriteUInt32Le(m.SendBufferSize); err != nil {
return nil, err
}
if err := buf.PutUInt32Le(m.MaxMessageSize); err != nil {
if err := buf.WriteUInt32Le(m.MaxMessageSize); err != nil {
return nil, err
}
if err := buf.PutUInt32Le(m.MaxChunkCount); err != nil {
if err := buf.WriteUInt32Le(m.MaxChunkCount); err != nil {
return nil, err
}
return buf, nil
Expand Down
5 changes: 2 additions & 3 deletions opcua/ua/message_acknowledge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package ua
import (
"testing"

"github.com/shoothzj/gox/testx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDecodeMessageAcknowledge(t *testing.T) {
buffer := testx.Hex2Buffer(t, "41434b461c00000000000000ffff0000ffff00000000200040000000")
buffer := hex2Buffer(t, "41434b461c00000000000000ffff0000ffff00000000200040000000")
err := buffer.Skip(8)
require.Nil(t, err)
msg, err := DecodeMessageAcknowledge(buffer)
Expand All @@ -32,5 +31,5 @@ func TestEncodeMessageAcknowledge(t *testing.T) {
}
buffer, err := msg.Buffer()
require.Nil(t, err)
assert.Equal(t, testx.Hex2Buffer(t, "41434b461c00000000000000ffff0000ffff00000000200040000000"), buffer)
assert.Equal(t, hex2Buffer(t, "41434b461c00000000000000ffff0000ffff00000000200040000000"), buffer)
}
2 changes: 1 addition & 1 deletion opcua/ua/message_browse_req.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ua

import (
"github.com/shoothzj/gox/buffer"
"github.com/libgox/buffer"
)

type MessageBrowseReq struct {
Expand Down
3 changes: 1 addition & 2 deletions opcua/ua/message_browse_req_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package ua
import (
"testing"

"github.com/shoothzj/gox/testx"
"github.com/stretchr/testify/require"
)

func TestDecodeMessageBrowseReq(t *testing.T) {
buffer := testx.Hex2Buffer(t, "4d534746870000000200000001000000c8000000c800000001000f020500002000000048cce1823313199f88bb583a480954a576d4fae160deef9bf25daa6e50bfbc39f6c9be3815fcda01c800000000000000ffffffffa00f00000000000000000000000000000000000000000000000100000001000f3d00000000002d01000000003f000000")
buffer := hex2Buffer(t, "4d534746870000000200000001000000c8000000c800000001000f020500002000000048cce1823313199f88bb583a480954a576d4fae160deef9bf25daa6e50bfbc39f6c9be3815fcda01c800000000000000ffffffffa00f00000000000000000000000000000000000000000000000100000001000f3d00000000002d01000000003f000000")
err := buffer.Skip(8)
require.Nil(t, err)
}
Expand Down
2 changes: 1 addition & 1 deletion opcua/ua/message_browse_resp.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ua

import (
"github.com/shoothzj/gox/buffer"
"github.com/libgox/buffer"
)

type MessageBrowseResp struct {
Expand Down
3 changes: 1 addition & 2 deletions opcua/ua/message_browse_resp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package ua
import (
"testing"

"github.com/shoothzj/gox/testx"
"github.com/stretchr/testify/require"
)

func TestDecodeMessageBrowseResp(t *testing.T) {
buffer := testx.Hex2Buffer(t, "4d534746480000000200000001000000fb030000c500000001001202b0c4be3815fcda01c50000000000000000ffffffff0000000100000000000000ffffffff00000000ffffffff")
buffer := hex2Buffer(t, "4d534746480000000200000001000000fb030000c500000001001202b0c4be3815fcda01c50000000000000000ffffffff0000000100000000000000ffffffff00000000ffffffff")
err := buffer.Skip(8)
require.Nil(t, err)
}
Expand Down
4 changes: 2 additions & 2 deletions opcua/ua/message_close_secure_channel.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ua

import (
"github.com/shoothzj/gox/buffer"
"github.com/libgox/buffer"
)

type MessageCloseSecureChannel struct {
Expand Down Expand Up @@ -29,7 +29,7 @@ func (m *MessageCloseSecureChannel) Buffer() (*buffer.Buffer, error) {
if _, err := buf.Write([]byte{'F'}); err != nil {
return nil, err
}
if err := buf.PutUInt32Le(uint32(m.Length())); err != nil {
if err := buf.WriteUInt32Le(uint32(m.Length())); err != nil {
return nil, err
}
return buf, nil
Expand Down
3 changes: 1 addition & 2 deletions opcua/ua/message_close_secure_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package ua
import (
"testing"

"github.com/shoothzj/gox/testx"
"github.com/stretchr/testify/require"
)

func TestDecodeMessageCloseSecureChannel(t *testing.T) {
buffer := testx.Hex2Buffer(t, "4d5347465f0000000200000001000000e6000000e60000000100d9010500002000000048cce1823313199f88bb583a480954a576d4fae160deef9bf25daa6e50bfbc396ee1343d15fcda01e600000000000000ffffffffa00f000000000001")
buffer := hex2Buffer(t, "4d5347465f0000000200000001000000e6000000e60000000100d9010500002000000048cce1823313199f88bb583a480954a576d4fae160deef9bf25daa6e50bfbc396ee1343d15fcda01e600000000000000ffffffffa00f000000000001")
err := buffer.Skip(8)
require.Nil(t, err)
}
Expand Down
2 changes: 1 addition & 1 deletion opcua/ua/message_close_session_req.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ua

import (
"github.com/shoothzj/gox/buffer"
"github.com/libgox/buffer"
)

type MessageCloseSessionReq struct {
Expand Down
3 changes: 1 addition & 2 deletions opcua/ua/message_close_session_req_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package ua
import (
"testing"

"github.com/shoothzj/gox/testx"
"github.com/stretchr/testify/require"
)

func TestDecodeMessageCloseSessionReq(t *testing.T) {
buffer := testx.Hex2Buffer(t, "4d5347465f0000000200000001000000e6000000e60000000100d9010500002000000048cce1823313199f88bb583a480954a576d4fae160deef9bf25daa6e50bfbc396ee1343d15fcda01e600000000000000ffffffffa00f000000000001")
buffer := hex2Buffer(t, "4d5347465f0000000200000001000000e6000000e60000000100d9010500002000000048cce1823313199f88bb583a480954a576d4fae160deef9bf25daa6e50bfbc396ee1343d15fcda01e600000000000000ffffffffa00f000000000001")
err := buffer.Skip(8)
require.Nil(t, err)
}
Expand Down
2 changes: 1 addition & 1 deletion opcua/ua/message_close_session_resp.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ua

import (
"github.com/shoothzj/gox/buffer"
"github.com/libgox/buffer"
)

type MessageCloseSessionResp struct {
Expand Down
3 changes: 1 addition & 2 deletions opcua/ua/message_close_session_resp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package ua
import (
"testing"

"github.com/shoothzj/gox/testx"
"github.com/stretchr/testify/require"
)

func TestDecodeMessageCloseSessionResp(t *testing.T) {
buffer := testx.Hex2Buffer(t, "1e00000060090900005406400000000000000000000000000000000100000000000000000000000000000001d11afd7da59b38d0e146c88b801816eb005c00000101080a1f86fec7f584f6454d5347463400000002000000010000001c040000e60000000100dc01c07f353d15fcda01e60000000000000000ffffffff000000")
buffer := hex2Buffer(t, "1e00000060090900005406400000000000000000000000000000000100000000000000000000000000000001d11afd7da59b38d0e146c88b801816eb005c00000101080a1f86fec7f584f6454d5347463400000002000000010000001c040000e60000000100dc01c07f353d15fcda01e60000000000000000ffffffff000000")
err := buffer.Skip(8)
require.Nil(t, err)
}
Expand Down
2 changes: 1 addition & 1 deletion opcua/ua/message_create_session_req.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ua

import (
"github.com/shoothzj/gox/buffer"
"github.com/libgox/buffer"
)

type MessageCreateSessionReq struct {
Expand Down
3 changes: 1 addition & 2 deletions opcua/ua/message_create_session_req_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package ua
import (
"testing"

"github.com/shoothzj/gox/testx"
"github.com/stretchr/testify/require"
)

func TestDecodeMessageCreateSessionReq(t *testing.T) {
buffer := testx.Hex2Buffer(t, "4d5347464c010000020000000100000002000000020000000100cd010000700fb43815fcda010200000000000000ffffffffa00f00000000001800000075726e3a667265656f706375613a636c69656e742d6775691e00000075726e3a667265656f706375612e6769746875622e696f3a636c69656e7402190000005075726520507974686f6e204173796e632e20436c69656e7401000000ffffffffffffffff00000000ffffffff450000006f70632e7463703a2f2f68657a68616e676a69616e64654d6163426f6f6b2d50726f2e6c6f63616c3a35333533302f4f504355412f53696d756c6174696f6e536572766572220000005075726520507974686f6e204173796e632e20436c69656e742053657373696f6e3120000000a06ee593955cb82598cb5e9950a3f7634642941823761b762323ef23f9cc805effffffff0000000040774b4100000000")
buffer := hex2Buffer(t, "4d5347464c010000020000000100000002000000020000000100cd010000700fb43815fcda010200000000000000ffffffffa00f00000000001800000075726e3a667265656f706375613a636c69656e742d6775691e00000075726e3a667265656f706375612e6769746875622e696f3a636c69656e7402190000005075726520507974686f6e204173796e632e20436c69656e7401000000ffffffffffffffff00000000ffffffff450000006f70632e7463703a2f2f68657a68616e676a69616e64654d6163426f6f6b2d50726f2e6c6f63616c3a35333533302f4f504355412f53696d756c6174696f6e536572766572220000005075726520507974686f6e204173796e632e20436c69656e742053657373696f6e3120000000a06ee593955cb82598cb5e9950a3f7634642941823761b762323ef23f9cc805effffffff0000000040774b4100000000")
err := buffer.Skip(8)
require.Nil(t, err)
}
Expand Down
2 changes: 1 addition & 1 deletion opcua/ua/message_get_endpoints_req.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ua

import (
"github.com/shoothzj/gox/buffer"
"github.com/libgox/buffer"
)

type MessageGetEndpointsReq struct {
Expand Down
3 changes: 1 addition & 2 deletions opcua/ua/message_get_endpoints_req_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package ua
import (
"testing"

"github.com/shoothzj/gox/testx"
"github.com/stretchr/testify/require"
)

func TestDecodeMessageGetEndpointsReq(t *testing.T) {
buffer := testx.Hex2Buffer(t, "4d534746a8000000010000000100000002000000020000000100ac010000a018aacbf780d9010000000000000000ffffffff60ea00000000001e0000006f70632e7463703a2f2f6c6f63616c686f73743a31323638362f6d696c6f000000000100000041000000687474703a2f2f6f7063666f756e646174696f6e2e6f72672f55412d50726f66696c652f5472616e73706f72742f75617463702d756173632d756162696e617279")
buffer := hex2Buffer(t, "4d534746a8000000010000000100000002000000020000000100ac010000a018aacbf780d9010000000000000000ffffffff60ea00000000001e0000006f70632e7463703a2f2f6c6f63616c686f73743a31323638362f6d696c6f000000000100000041000000687474703a2f2f6f7063666f756e646174696f6e2e6f72672f55412d50726f66696c652f5472616e73706f72742f75617463702d756173632d756162696e617279")
err := buffer.Skip(8)
require.Nil(t, err)
}
Expand Down
2 changes: 1 addition & 1 deletion opcua/ua/message_get_endpoints_resp.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ua

import (
"github.com/shoothzj/gox/buffer"
"github.com/libgox/buffer"
)

type MessageGetEndpointsResp struct {
Expand Down
Loading