private async Task CreateAndWriteData(string extraLine, bool enableTimestampOffset, int timestampOffsetInSeconds, CancellationToken cancellationToken)
        {
            byte[] data = CreateData(extraLine);

            var minimalAcceptedTimestamp = _transactionDataReceiver.MinimalAcceptedTimestamp;

            if (enableTimestampOffset)
            {
                var requestedTimestamp = DateTime.UtcNow.AddSeconds(timestampOffsetInSeconds);
                if (requestedTimestamp < minimalAcceptedTimestamp)
                {
                    await DelayNextWriteAttempt(requestedTimestamp, minimalAcceptedTimestamp, cancellationToken);

                    return;
                }

                _transactionDataReceiver.WriteRawData(data, requestedTimestamp);
            }
            else
            {
                if (DateTime.UtcNow < minimalAcceptedTimestamp)
                {
                    await DelayNextWriteAttempt(DateTime.UtcNow, minimalAcceptedTimestamp, cancellationToken);

                    return;
                }

                // Note: If an offset has been used and it was set in the future (which makes no sense in a real life scenario),
                // this call can fail with an ArgumentException if the usage of offset is disabled again. Hence the check above,
                // which would normally not be required.
                _transactionDataReceiver.WriteRawData(data);
            }

            await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
        }
Beispiel #2
0
        private void StartInstance()
        {
            if (_tcpServer != null)
            {
                Close();
            }
            var options = new TcpServerOptions
            {
                Echo                   = _properties.Echo,
                EnableKeepAlive        = _properties.EnableKeepAlives,
                LocalEndpoint          = new IPEndPoint(_properties.LocalIp, _properties.LocalPort),
                AllowedRemoteEndpoints = _properties.AllowedExternalAddresses,
                MaxConnections         = 1
            };

            _tcpServer = new TcpServer(options);
            _tcpServer.BytesReceived += (sender, bytes) => _txnReceiver.WriteRawData(bytes);
            _tcpServer.ErrorMessage  += (sender, exception) =>
                                        Util.Log(true, "Cascadia.Net.TcpServer", exception.ToString());
            _tcpServer.InfoMessage += (sender, s) => Util.Log(false, "Cascadia.Net.TcpServer", s);

            Util.Log(
                false,
                $"{GetType().FullName}.{System.Reflection.MethodBase.GetCurrentMethod().Name} [{_instanceId}]",
                $"Starting {Resources.CascadiaTransactionServer} listener on {options.LocalEndpoint}");

            _tcpServer.Start();
        }