Connecting through access servers
Access servers are the public entry points of a ViiSync server. A program connects to an access server, and the access server carries its connection to the ViiSync server. They speak the same protocol as the server itself, so nothing about trading, events or return codes changes. What changes is how the library finds and reaches them. This page covers library version 2.2.0.
Your broker gives you the access-point addresses. The examples on this page use example.com names.
Addresses
| Address | Transport | When to use it |
|---|---|---|
tls://host:port |
TCP with TLS | The fastest transport and the one to use from .NET. Access servers listen on their own port, for example 4431. |
wss://host/ws |
WebSocket over TLS, port 443 | When only port 443 is open, for example behind a corporate firewall. |
host with port 443 |
same as wss://host/ws |
Programs written for 2.0 or 2.1: change only the host name. |
tcp://host:port, ws://host:port/ws |
no encryption | Development and internal networks only. |
Pass a tls:// address the way you pass a WebSocket URL:
// TCP+TLS straight to one access server: the lowest-latency transport for a .NET client.
var api = new ViiSyncAccountApi(login, password, new Uri("tls://access-1.example.com:4431"));
api.Connect();
The (host, port) constructor keeps its meaning: port 443 connects to wss://host/ws and any other port to
ws://host:port/ws. An existing program can use an access server by changing only the host name:
// A 2.0 / 2.1 program needs no change to use an access server: give its host name. Port 443 → wss://host/ws.
var api = new ViiSyncAccountApi(login, password, "access-1.example.com", 443);
api.Connect();
Host also accepts a tls:// or tcp:// address. If the address has no
port, Port is used.
Access-point lists and failover
Give the constructor a list of access points instead of a single address:
// Several access points: each Connect (and each automatic reconnect) measures the TCP connect time of every
// address in parallel, connects to the fastest and fails over to the next if that connect fails.
var api = new ViiSyncAccountApi(login, password, new[]
{
new Uri("tls://access-1.example.com:4431"),
new Uri("tls://access-2.example.com:4432"),
});
api.OnConnectProgress += (_, e) => Console.WriteLine($"connect: {e.Progress}");
api.Connect();
Console.WriteLine($"connected through {api.CurrentAccessPoint}");
On every Connect, and on every automatic reconnect, the library:
- Measures how long a TCP connect to each access point takes. All of them are probed at once, for at most about 300 ms in total.
- Tries the access points that answered, fastest first. Access points that did not answer in time, or refused the connection, are tried last, in list order.
- Moves on to the next access point when a connect fails because the access point could not be reached, its TLS
handshake failed, it closed the connection before answering the login, or it did not answer within
ConnectTimeout. These failures have the code
NO_CONNECTIONorOPERATION_TIMEOUT. - Does not move on when the server answered.
INVALID_ACCOUNT(credentials refused),OLD_VERSION(protocol not supported) andTOO_FREQUENT_REQUEST(the login is locked out) come from the ViiSync server behind every access point, soConnectthrows them at once, exactly as with a single address.
ConnectTimeout applies to each attempt. When every access point fails,
Connect throws one ConnectException. Its Code is the code of the last attempt, and its
message names every access point that was tried. OnConnectProgress reports
Exception once, for the whole connect, and not for each access point that was skipped. Each failover is written to
OnLog.
CurrentAccessPoint is the access point of the current connection, and Server shows it too. With AutoReconnect on, a connection lost with one access point reconnects through the fastest one that still answers.
The list may mix transports. A list of wss:// addresses works the same way:
// The same over WebSocket (port 443 only, e.g. behind a firewall that blocks other ports).
var api = new ViiSyncAccountApi(login, password, new[]
{
new Uri("wss://access-1.example.com/ws"),
new Uri("wss://access-2.example.com/ws"),
});
api.Connect();
A single address is connected to directly, without probing or failover, and the connect behaves exactly as in 2.0:
a server that closes before answering the login is reported after the login timeout, as OPERATION_TIMEOUT. With a
Proxy set, the library does not probe either: it tries the access points in
list order.
Access points the server advertises
Servers that run access servers send their list to the library after login, and again whenever an access server comes or goes. AccessPointPolicy decides whether the library uses it:
| Policy | The advertised access points are added |
|---|---|
Auto (default) |
Only when the program connects through access servers: it gave an access-point list or a tls:// address, or the address it connected to (host and port) is one of the advertised access points. |
Always |
Always, also when the program connected straight to the server. |
Never |
Never. The library connects only to the addresses you gave. |
So under the default, a program that connects straight to the server, for example to wss://server.example.com/ws,
keeps exactly that address and reconnects to it, as with 2.1. A program that connects through an access server learns
the other access servers and can fail over to them, even to one it was never given.
When the list is used, AccessPoints holds your addresses followed by the advertised ones, and the next reconnect chooses among all of them. An access point that goes away is dropped from the list again. The library keeps the transport you gave it:
- A client given
tls://addresses adds the TLS address of each advertised access point. - A client given
wss://orws://addresses, or a host name, adds each WebSocket URL. - A client given
tcp://addresses adds nothing, because the advertised ports are TLS ports.
To connect only to the addresses you gave, set the policy to Never:
// Only the addresses given here: ignore the access points the server advertises.
var api = new ViiSyncAccountApi(login, password, new[] { new Uri("tls://access-1.example.com:4431") })
{
AccessPointPolicy = AccessPointPolicy.Never,
};
api.Connect();
foreach (var ap in api.AccessPoints) Console.WriteLine(ap);
To let a program that connects straight to the server also reconnect through the access servers, set it to Always:
// Connected straight to the server, but let reconnects use the access servers it advertises too.
var api = new ViiSyncAccountApi(login, password, new Uri("wss://server.example.com/ws"))
{
AccessPointPolicy = AccessPointPolicy.Always,
};
api.Connect();
UseServerAccessPoints is the same setting as a switch: false is Never,
true is Auto.
TLS certificates
A tls:// connection checks the server's certificate the way a browser does. The certificate must chain to a root your
machine trusts, and it must be issued for the host name in the address. That host name is also sent as the TLS server
name (SNI). TLS 1.2 and 1.3 are allowed. Access servers use publicly trusted certificates, so most programs need no
setup.
For a server with a private certificate, such as a test server or an internal certificate authority, ConfigureTls lets you validate the certificate yourself. It is the TLS counterpart of ConfigureWebSocket. The hook runs before every handshake. This one accepts a valid certificate or one known certificate, identified by its SHA-256 thumbprint:
// A server with a private (self-signed or internal-CA) certificate: accept exactly that certificate. Without a
// hook the certificate must chain to a trusted root and match the host name.
var api = new ViiSyncAccountApi(login, password, new Uri("tls://access-1.internal.example:4431"))
{
ConfigureTls = options => options.RemoteCertificateValidationCallback = (_, certificate, _, errors) =>
errors == SslPolicyErrors.None
|| (certificate is not null
&& string.Equals(certificate.GetCertHashString(HashAlgorithmName.SHA256), sha256Thumbprint, StringComparison.OrdinalIgnoreCase)),
};
api.Connect();
The hook can set any TLS client option, such as a client certificate or a revocation check:
// The hook sees every TLS option before the handshake: client certificate, protocols, revocation checks.
var api = new ViiSyncAccountApi(login, password, new Uri("tls://access-1.example.com:4431"))
{
ConfigureTls = options =>
{
options.ClientCertificates = new X509CertificateCollection { clientCertificate };
options.CertificateRevocationCheckMode = X509RevocationMode.Online;
},
};
api.Connect();
Warning
Do not write a hook that accepts every certificate. It turns off the protection TLS gives the login frame, which carries the account password.
What stays the same
- The API: every type, member, event and return code works as before. Trading, history, quotes and account state do not depend on the transport.
- OrderClientSafe: when a connection drops in the middle of a trade, it waits for the reconnect, which may go through another access point, and settles the outcome from the server's state as before.
- The protocol: the same bytes travel over
tls://,tcp://,wss://andws://. A 2.0 or 2.1 library can use an access server overwss://. It just does not get the access-point list or failover.