From b61ac6db33840823b6d4a297f93fa41f82ff4e69 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 3 Feb 2023 15:28:00 -0800 Subject: [PATCH] [ntcore] Add client disconnect function (#5022) As setServer doesn't disconnect, it's useful to have a function that disconnects without needing to completely stop the client. --- .../generate/java/NetworkTableInstance.java.jinja | 8 ++++++++ ntcore/src/generate/java/NetworkTablesJNI.java.jinja | 2 ++ ntcore/src/main/native/cpp/INetworkClient.h | 1 + ntcore/src/main/native/cpp/NetworkClient.cpp | 10 ++++++++++ ntcore/src/main/native/cpp/NetworkClient.h | 2 ++ ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp | 12 ++++++++++++ ntcore/src/main/native/cpp/ntcore_c.cpp | 4 ++++ ntcore/src/main/native/cpp/ntcore_cpp.cpp | 8 ++++++++ .../include/networktables/NetworkTableInstance.h | 6 ++++++ .../include/networktables/NetworkTableInstance.inc | 4 ++++ ntcore/src/main/native/include/ntcore_c.h | 8 ++++++++ ntcore/src/main/native/include/ntcore_cpp.h | 8 ++++++++ 12 files changed, 73 insertions(+) diff --git a/ntcore/src/generate/java/NetworkTableInstance.java.jinja b/ntcore/src/generate/java/NetworkTableInstance.java.jinja index ba5e33f841..ff1b40734c 100644 --- a/ntcore/src/generate/java/NetworkTableInstance.java.jinja +++ b/ntcore/src/generate/java/NetworkTableInstance.java.jinja @@ -911,6 +911,14 @@ public final class NetworkTableInstance implements AutoCloseable { NetworkTablesJNI.setServerTeam(m_handle, team, port); } + /** + * Disconnects the client if it's running and connected. This will automatically start + * reconnection attempts to the current server list. + */ + public void disconnect() { + NetworkTablesJNI.disconnect(m_handle); + } + /** * Starts requesting server address from Driver Station. This connects to the Driver Station * running on localhost to obtain the server IP address, and connects with the default port. diff --git a/ntcore/src/generate/java/NetworkTablesJNI.java.jinja b/ntcore/src/generate/java/NetworkTablesJNI.java.jinja index 2f119fe13f..bc748c60e4 100644 --- a/ntcore/src/generate/java/NetworkTablesJNI.java.jinja +++ b/ntcore/src/generate/java/NetworkTablesJNI.java.jinja @@ -251,6 +251,8 @@ public final class NetworkTablesJNI { public static native void setServerTeam(int inst, int team, int port); + public static native void disconnect(int inst); + public static native void startDSClient(int inst, int port); public static native void stopDSClient(int inst); diff --git a/ntcore/src/main/native/cpp/INetworkClient.h b/ntcore/src/main/native/cpp/INetworkClient.h index 986f43ab84..e9523c8d66 100644 --- a/ntcore/src/main/native/cpp/INetworkClient.h +++ b/ntcore/src/main/native/cpp/INetworkClient.h @@ -18,6 +18,7 @@ class INetworkClient { virtual void SetServers( std::span> servers) = 0; + virtual void Disconnect() = 0; virtual void StartDSClient(unsigned int port) = 0; virtual void StopDSClient() = 0; diff --git a/ntcore/src/main/native/cpp/NetworkClient.cpp b/ntcore/src/main/native/cpp/NetworkClient.cpp index 56d98a84bc..e3a18e0ef6 100644 --- a/ntcore/src/main/native/cpp/NetworkClient.cpp +++ b/ntcore/src/main/native/cpp/NetworkClient.cpp @@ -492,6 +492,11 @@ void NetworkClient::SetServers( m_impl->SetServers(servers, NT_DEFAULT_PORT4); } +void NetworkClient::Disconnect() { + m_impl->m_loopRunner.ExecAsync( + [this](auto&) { m_impl->Disconnect("requested by application"); }); +} + void NetworkClient::StartDSClient(unsigned int port) { m_impl->StartDSClient(port); } @@ -535,6 +540,11 @@ void NetworkClient3::SetServers( m_impl->SetServers(servers, NT_DEFAULT_PORT3); } +void NetworkClient3::Disconnect() { + m_impl->m_loopRunner.ExecAsync( + [this](auto&) { m_impl->Disconnect("requested by application"); }); +} + void NetworkClient3::StartDSClient(unsigned int port) { m_impl->StartDSClient(port); } diff --git a/ntcore/src/main/native/cpp/NetworkClient.h b/ntcore/src/main/native/cpp/NetworkClient.h index 34bf37931d..f2a7e652d4 100644 --- a/ntcore/src/main/native/cpp/NetworkClient.h +++ b/ntcore/src/main/native/cpp/NetworkClient.h @@ -38,6 +38,7 @@ class NetworkClient final : public INetworkClient { void SetServers( std::span> servers) final; + void Disconnect() final; void StartDSClient(unsigned int port) final; void StopDSClient() final; @@ -59,6 +60,7 @@ class NetworkClient3 final : public INetworkClient { void SetServers( std::span> servers) final; + void Disconnect() final; void StartDSClient(unsigned int port) final; void StopDSClient() final; diff --git a/ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp b/ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp index b8686044cf..22e5bbf181 100644 --- a/ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp +++ b/ntcore/src/main/native/cpp/jni/NetworkTablesJNI.cpp @@ -1298,6 +1298,18 @@ Java_edu_wpi_first_networktables_NetworkTablesJNI_setServerTeam nt::SetServerTeam(inst, team, port); } +/* + * Class: edu_wpi_first_networktables_NetworkTablesJNI + * Method: disconnect + * Signature: (I)V + */ +JNIEXPORT void JNICALL +Java_edu_wpi_first_networktables_NetworkTablesJNI_disconnect + (JNIEnv* env, jclass, jint inst) +{ + nt::Disconnect(inst); +} + /* * Class: edu_wpi_first_networktables_NetworkTablesJNI * Method: startDSClient diff --git a/ntcore/src/main/native/cpp/ntcore_c.cpp b/ntcore/src/main/native/cpp/ntcore_c.cpp index 151e0f1c64..8837fbfff9 100644 --- a/ntcore/src/main/native/cpp/ntcore_c.cpp +++ b/ntcore/src/main/native/cpp/ntcore_c.cpp @@ -537,6 +537,10 @@ void NT_SetServerTeam(NT_Inst inst, unsigned int team, unsigned int port) { nt::SetServerTeam(inst, team, port); } +void NT_Disconnect(NT_Inst inst) { + nt::Disconnect(inst); +} + void NT_StartDSClient(NT_Inst inst, unsigned int port) { nt::StartDSClient(inst, port); } diff --git a/ntcore/src/main/native/cpp/ntcore_cpp.cpp b/ntcore/src/main/native/cpp/ntcore_cpp.cpp index cfb5af2014..24f1f89991 100644 --- a/ntcore/src/main/native/cpp/ntcore_cpp.cpp +++ b/ntcore/src/main/native/cpp/ntcore_cpp.cpp @@ -685,6 +685,14 @@ void SetServerTeam(NT_Inst inst, unsigned int team, unsigned int port) { } } +void Disconnect(NT_Inst inst) { + if (auto ii = InstanceImpl::GetTyped(inst, Handle::kInstance)) { + if (auto client = ii->GetClient()) { + client->Disconnect(); + } + } +} + void StartDSClient(NT_Inst inst, unsigned int port) { if (auto ii = InstanceImpl::GetTyped(inst, Handle::kInstance)) { if (auto client = ii->GetClient()) { diff --git a/ntcore/src/main/native/include/networktables/NetworkTableInstance.h b/ntcore/src/main/native/include/networktables/NetworkTableInstance.h index fabc63421c..bcd30a9891 100644 --- a/ntcore/src/main/native/include/networktables/NetworkTableInstance.h +++ b/ntcore/src/main/native/include/networktables/NetworkTableInstance.h @@ -586,6 +586,12 @@ class NetworkTableInstance final { */ void SetServerTeam(unsigned int team, unsigned int port = 0); + /** + * Disconnects the client if it's running and connected. This will + * automatically start reconnection attempts to the current server list. + */ + void Disconnect(); + /** * Starts requesting server address from Driver Station. * This connects to the Driver Station running on localhost to obtain the diff --git a/ntcore/src/main/native/include/networktables/NetworkTableInstance.inc b/ntcore/src/main/native/include/networktables/NetworkTableInstance.inc index 9b712ebc89..ee1f23c2b9 100644 --- a/ntcore/src/main/native/include/networktables/NetworkTableInstance.inc +++ b/ntcore/src/main/native/include/networktables/NetworkTableInstance.inc @@ -163,6 +163,10 @@ inline void NetworkTableInstance::SetServerTeam(unsigned int team, ::nt::SetServerTeam(m_handle, team, port); } +inline void NetworkTableInstance::Disconnect() { + ::nt::Disconnect(m_handle); +} + inline void NetworkTableInstance::StartDSClient(unsigned int port) { ::nt::StartDSClient(m_handle, port); } diff --git a/ntcore/src/main/native/include/ntcore_c.h b/ntcore/src/main/native/include/ntcore_c.h index e9e9f81d5d..b0fb058660 100644 --- a/ntcore/src/main/native/include/ntcore_c.h +++ b/ntcore/src/main/native/include/ntcore_c.h @@ -1147,6 +1147,14 @@ void NT_SetServerMulti(NT_Inst inst, size_t count, const char** server_names, */ void NT_SetServerTeam(NT_Inst inst, unsigned int team, unsigned int port); +/** + * Disconnects the client if it's running and connected. This will automatically + * start reconnection attempts to the current server list. + * + * @param inst instance handle + */ +void NT_Disconnect(NT_Inst inst); + /** * Starts requesting server address from Driver Station. * This connects to the Driver Station running on localhost to obtain the diff --git a/ntcore/src/main/native/include/ntcore_cpp.h b/ntcore/src/main/native/include/ntcore_cpp.h index cbb541bbd2..5cb77a6916 100644 --- a/ntcore/src/main/native/include/ntcore_cpp.h +++ b/ntcore/src/main/native/include/ntcore_cpp.h @@ -1087,6 +1087,14 @@ void SetServer( */ void SetServerTeam(NT_Inst inst, unsigned int team, unsigned int port); +/** + * Disconnects the client if it's running and connected. This will automatically + * start reconnection attempts to the current server list. + * + * @param inst instance handle + */ +void Disconnect(NT_Inst inst); + /** * Starts requesting server address from Driver Station. * This connects to the Driver Station running on localhost to obtain the