[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.
This commit is contained in:
Peter Johnson
2023-02-03 15:28:00 -08:00
committed by GitHub
parent 7b828ce84f
commit b61ac6db33
12 changed files with 73 additions and 0 deletions

View File

@@ -18,6 +18,7 @@ class INetworkClient {
virtual void SetServers(
std::span<const std::pair<std::string, unsigned int>> servers) = 0;
virtual void Disconnect() = 0;
virtual void StartDSClient(unsigned int port) = 0;
virtual void StopDSClient() = 0;

View File

@@ -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);
}

View File

@@ -38,6 +38,7 @@ class NetworkClient final : public INetworkClient {
void SetServers(
std::span<const std::pair<std::string, unsigned int>> 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<const std::pair<std::string, unsigned int>> servers) final;
void Disconnect() final;
void StartDSClient(unsigned int port) final;
void StopDSClient() final;

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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()) {