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

View File

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

View File

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

View File

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

View File

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