Skip to content

Commit

Permalink
Pass initiator and reason to connection::close
Browse files Browse the repository at this point in the history
  • Loading branch information
bmalinowsky committed Aug 12, 2024
1 parent f4838a0 commit 3665432
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/io/calimero/knxnetip/TcpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.io.IOException;
import java.io.InterruptedIOException;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.math.BigInteger;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
Expand Down Expand Up @@ -81,6 +82,7 @@
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

import io.calimero.CloseEvent;
import io.calimero.KNXException;
import io.calimero.KNXFormatException;
import io.calimero.KNXIllegalArgumentException;
Expand Down Expand Up @@ -319,8 +321,9 @@ private void setupSecureSession()
}
catch (final IOException e) {
close();
conn.close();
throw new KNXConnectionClosedException("I/O error establishing secure session with " + hostPort, e);
final String reason = "I/O error establishing secure session with " + hostPort;
conn.close(CloseEvent.INTERNAL, reason);
throw new KNXConnectionClosedException(reason, e);
}
finally {
conn.sessionRequestLock.unlock();
Expand Down Expand Up @@ -471,7 +474,7 @@ private void sendKeepAlive() {
if (sessionState == SessionState.Authenticated && !conn.socket.isClosed()) {
logger.log(WARNING, "error sending keep-alive: {0}", e.getMessage());
close();
conn.close();
conn.close(CloseEvent.INTERNAL, "error sending keep-alive");
}
}
}
Expand Down Expand Up @@ -710,7 +713,11 @@ public boolean isConnected() {
*/
@Override
public void close() {
unsecuredConnections.values().forEach(ClientConnection::close);
close(CloseEvent.USER_REQUEST, "user request");
}

void close(final int initiator, final String reason) {
unsecuredConnections.values().forEach(t -> t.close(initiator, reason, Level.DEBUG, null));
unsecuredConnections.clear();

sessions.values().forEach(SecureSession::close);
Expand Down Expand Up @@ -766,6 +773,8 @@ private void runReceiveLoop() {
final byte[] data = new byte[rcvBufferSize];
int offset = 0;

int initiator = CloseEvent.USER_REQUEST;
String reason = "user request";
try {
final var in = socket.getInputStream();
while (!socket.isClosed()) {
Expand Down Expand Up @@ -801,20 +810,26 @@ else if (header.getTotalLength() > rcvBufferSize) {
}

final int read = in.read(data, offset, data.length - offset);
if (read == -1)
if (read == -1) {
initiator = CloseEvent.SERVER_REQUEST;
reason = "server request";
return;
}
offset += read;
}
}
catch (final InterruptedIOException e) {
Thread.currentThread().interrupt();
}
catch (IOException | RuntimeException e) {
if (!socket.isClosed())
if (!socket.isClosed()) {
initiator = CloseEvent.INTERNAL;
reason = e.getMessage();
logger.log(ERROR, "receiver communication failure", e);
}
}
finally {
close();
close(initiator, reason);
}
}

Expand Down

0 comments on commit 3665432

Please sign in to comment.