TCP Client
A MasterChannel
presents the same interface once you create it, regardless of the underlying transport. You can create a TCP client channel using the runtime,
configuration data, and a callback to observe the status of the connection.
- Rust
- C
- C++
- Java
- C#
let channel = spawn_master_tcp_client(
LinkErrorMode::Close,
get_master_channel_config()?,
EndpointList::new("127.0.0.1:20000".to_owned(), &[]),
ConnectStrategy::default(),
NullListener::create(),
);
The function is called within the context of the Tokio runtime, and is therefore implicit in Rust.
dnp3_master_channel_t* channel = NULL;
dnp3_endpoint_list_t* endpoints = dnp3_endpoint_list_create("127.0.0.1:20000");
dnp3_param_error_t err = dnp3_master_channel_create_tcp(
runtime,
DNP3_LINK_ERROR_MODE_CLOSE,
get_master_channel_config(),
endpoints,
dnp3_connect_strategy_init(),
get_client_state_listener(),
&channel
);
dnp3_endpoint_list_destroy(endpoints);
// check error
dnp3::EndpointList endpoints(std::string("127.0.0.1:20000"));
auto channel = dnp3::MasterChannel::create_tcp_channel(
runtime,
dnp3::LinkErrorMode::close,
get_master_channel_config(), endpoints,
dnp3::ConnectStrategy(),
std::make_unique<ClientStateListener>()
);
MasterChannel channel =
MasterChannel.createTcpChannel(
runtime,
LinkErrorMode.CLOSE,
getMasterChannelConfig(),
new EndpointList("127.0.0.1:20000"),
new ConnectStrategy(),
new TestClientStateListener());
var channel = MasterChannel.CreateTcpChannel(
runtime,
LinkErrorMode.Close,
GetMasterChannelConfig(),
new EndpointList("127.0.0.1:20000"),
new ConnectStrategy(),
new TestClientStateListener()
);
Enabling
A MasterChannel
won't start communicating until you call the enable
method. This gives you the opportunity to configure all the associations on the channel, as discussed in the next section. You can also disable
the channel to put it in an inactive state.
LinkErrorMode
The LinkErrorMode
controls what happens if it detects a framing error at the link-layer. TCP is a lossless transport, so the default behavior is to
close the connection. However, you can treat a TCP channel like a serial port by changing the setting to LinkErrorMode::Discard
.
EndpointList
An EndpointList
is a list of remote endpoints that the master will try to connect to. It must contain at least one entry, with the option to include additional backup
addresses. The channel will round-robin through this list until it establishes a connection. The list may contain endpoints consisting of a <host>:<port>
tuple where "host" is one of the following:
- IPv4 address
- IPv6 address
- Domain name
ConnectStrategy
The ConnectStrategy
controls the rate at which the master retries failed connection attempts. The master uses exponential backoff when attempting to establish
a connection. The delay between attempts doubles from minConnectDelay
up to maxConnectDelay
. If a connection fails after previously connecting, it will
wait reconnectDelay
before the next attempt.
ClientStateListener
The ClientStateListener
interface has a single callback to inform the application about the state of connection.