Skip to content

Commit

Permalink
ModbusTCPAssembler is now public
Browse files Browse the repository at this point in the history
  • Loading branch information
aldas committed Jul 27, 2023
1 parent f5f23d7 commit 8d278e4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions server/modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"github.com/aldas/go-modbus-client/packet"
)

// modbusTCPAssembler assembles read data into complete packets and calls ModbusHandler with assembled packet
type modbusTCPAssembler struct {
// ModbusTCPAssembler assembles read data into complete packets and calls ModbusHandler with assembled packet
type ModbusTCPAssembler struct {
handler ModbusHandler
received bytes.Buffer
}

func (m *modbusTCPAssembler) ReceiveRead(ctx context.Context, received []byte, bytesRead int) (response []byte, closeConnection bool) {
// ReceiveRead assembles read byte until full TCP packet is formed or return an error when received data does not look like TCP packet
func (m *ModbusTCPAssembler) ReceiveRead(ctx context.Context, received []byte, bytesRead int) (response []byte, closeConnection bool) {
m.received.Write(received)

n, err := packet.LooksLikeModbusTCP(m.received.Bytes(), false)
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ type ContextRemoteAddr struct{}
func (s *Server) serve(ctx context.Context, listener net.Listener, handler ModbusHandler) error {
if s.AssemblerCreatorFunc == nil {
s.AssemblerCreatorFunc = func(handler ModbusHandler) PacketAssembler {
return &modbusTCPAssembler{handler: handler}
return &ModbusTCPAssembler{handler: handler}
}
}
onErrorFunc := s.OnErrorFunc
Expand Down
2 changes: 1 addition & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestRequestToServer(t *testing.T) {

// we start the server and listen for incoming connections/data in separate goroutine. ListenAndServe is blocking call.
go func() {
err := s.ListenAndServe(ctx, "localhost:5020", mbs)
err := s.ListenAndServe(ctx, "localhost:0", mbs)
if err != nil && !errors.Is(err, ErrServerClosed) {
assert.NoError(t, err)
}
Expand Down

0 comments on commit 8d278e4

Please sign in to comment.