Skip to content

Commit

Permalink
fix: Made the knx-driver actively close the transport in case of the …
Browse files Browse the repository at this point in the history
…connection not being successful
  • Loading branch information
chrisdutz committed Dec 16, 2021
1 parent 0fc7ff2 commit 49417d8
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions plc4go/internal/plc4go/knxnetip/Connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,14 @@ func (m *Connection) Connect() <-chan plc4go.PlcConnectionConnectResult {
// Open the UDP Connection
err := m.messageCodec.Connect()
if err != nil {
sendResult(nil, errors.Wrap(err, "error opening connection"))
m.doSomethingAndClose(func() { sendResult(nil, errors.Wrap(err, "error opening connection")) })
return
}

// Send a search request before connecting to the device.
searchResponse, err := m.sendGatewaySearchRequest()
if err != nil {
sendResult(nil, errors.Wrap(err, "error discovering device capabilities"))
m.doSomethingAndClose(func() { sendResult(nil, errors.Wrap(err, "error discovering device capabilities")) })
return
}

Expand Down Expand Up @@ -242,7 +242,7 @@ func (m *Connection) Connect() <-chan plc4go.PlcConnectionConnectResult {
// As soon as we got a successful search-response back, send a connection request.
connectionResponse, err := m.sendGatewayConnectionRequest()
if err != nil {
sendResult(nil, errors.Wrap(err, "error connecting to device"))
m.doSomethingAndClose(func() { sendResult(nil, errors.Wrap(err, "error connecting to device")) })
return
}

Expand Down Expand Up @@ -322,18 +322,26 @@ func (m *Connection) Connect() <-chan plc4go.PlcConnectionConnectResult {
// Fire the "connected" event
sendResult(m, nil)
case driverModel.Status_NO_MORE_CONNECTIONS:
sendResult(nil, errors.New("no more connections"))
m.doSomethingAndClose(func() { sendResult(nil, errors.New("no more connections")) })
default:
sendResult(nil, errors.Errorf("got a return status of: %s", connectionResponse.Status))
m.doSomethingAndClose(func() { sendResult(nil, errors.Errorf("got a return status of: %s", connectionResponse.Status)) })
}
} else {
sendResult(nil, errors.New("this device doesn't support tunneling"))
m.doSomethingAndClose(func() { sendResult(nil, errors.New("this device doesn't support tunneling")) })
}
}()

return result
}

func (m *Connection) doSomethingAndClose(something func()) {
something()
err := m.messageCodec.Disconnect()
if err != nil {
log.Warn().Msgf("error closing connection: %s", err)
}
}

func (m *Connection) BlockingClose() {
ttlTimer := time.NewTimer(m.defaultTtl)
closeResults := m.Close()
Expand Down

0 comments on commit 49417d8

Please sign in to comment.