Exemplo n.º 1
0
 public void OnPerf(DataLink sender, string channel, Memory <byte> message)
 {
     if (message.Length == 4)
     {
         Console.WriteLine($"Perf {BitConverter.ToInt32(message.Span) + 1}0000 message received.");
     }
 }
Exemplo n.º 2
0
 public void RegisterSyncTask(DataLink dataLink, int hash, Task <bool> syncTask)
 {
     if (_dict.TryGetValue(hash, out var trackItem))
     {
         lock (trackItem)
         {
             var list  = trackItem.Snap;
             var index = list.FindIndex(x => x.DataLink == dataLink);
             if (index == -1)
             {
                 trackItem.Snap = list.Add(new DataLinkTrackStatus {
                     DataLink = dataLink, SyncTask = syncTask
                 });
             }
             else
             {
                 var status = list[index];
                 if (status.IsSubscribed.HasValue)
                 {
                     return;
                 }
                 if (status.SyncTask.IsCanceled)
                 {
                     status.SyncTask = syncTask;
                     trackItem.Snap  = list.SetItem(index, status);
                 }
             }
         }
     }
 }
Exemplo n.º 3
0
 public void OnIsSubscribedChanged(DataLink dataLink, int hash, bool hasSub)
 {
     if (_dict.TryGetValue(hash, out var trackItem))
     {
         lock (trackItem)
         {
             var list  = trackItem.Snap;
             var index = list.FindIndex(x => x.DataLink == dataLink);
             if (index == -1)
             {
                 trackItem.Snap = list.Add(new DataLinkTrackStatus {
                     DataLink = dataLink, IsSubscribed = hasSub, SyncTask = Task.FromResult(hasSub)
                 });
             }
             else
             {
                 var status = list[index];
                 if (status.IsSubscribed == hasSub)
                 {
                     return;
                 }
                 status.IsSubscribed = hasSub;
                 status.SyncTask     = Task.FromResult(hasSub);
                 trackItem.Snap      = list.SetItem(index, status);
             }
         }
     }
 }
Exemplo n.º 4
0
        public override async void OnHealthyChanged(DataLinkCollection sender, DataLink dataLink)
        {
            if (!dataLink.IsHealthy)
            {
                await dataLink.Disconnect();

                _ = _dlf.ConnectAsync(dataLink.RemoteName, dataLink.RemoteListenPort, sender);
            }
        }
Exemplo n.º 5
0
        public static async ValueTask SendMessage(this DataLink dl, string channel, string content)
        {
            var length = Encoding.UTF8.GetByteCount(content);

            using (var owner = ExactSizeMemoryPool.Shared.Rent(length))
            {
                Encoding.UTF8.GetBytes(content, owner.Memory.Span);
                await dl.SendMessage(channel, owner.Memory);
            }
        }
Exemplo n.º 6
0
        private async Task ListenAsync()
        {
            _listener.Start();
            while (true)
            {
                DataLink dl      = null;
                Socket   socket  = null;
                bool     success = false;
                try
                {
                    socket = await _listener.AcceptSocketAsync();

                    var stream = new SslStream(new NetworkStream(socket, true));
                    await stream.AuthenticateAsServerAsync(
                        new SslServerAuthenticationOptions
                    {
                        AllowRenegotiation             = true,
                        CertificateRevocationCheckMode = X509RevocationMode.NoCheck,
                        ServerCertificate   = _certificate,
                        EnabledSslProtocols = SslProtocols.Tls12,
                        EncryptionPolicy    = EncryptionPolicy.RequireEncryption,
                        RemoteCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => certificate.Equals(_certificate),
                        ClientCertificateRequired           = true,
                    });

                    dl = await CreateDataLinkAsServerAsync(socket.RemoteEndPoint as IPEndPoint, stream);

                    success = true;
                }
                catch (Exception ex)
                {
                    _handler.OnError(dl, ex);
                }
                finally
                {
                    if (!success)
                    {
                        socket?.Close();
                    }
                }
            }
        }
Exemplo n.º 7
0
        public async Task Remove(DataLink dataLink)
        {
            await _writeLock.WaitAsync();

            try
            {
                foreach (var pair in _dict)
                {
                    var index = pair.Value.Snap.FindIndex(s => s.DataLink == dataLink);
                    if (index == -1)
                    {
                        return;
                    }
                    pair.Value.Snap = pair.Value.Snap.RemoveAt(index);
                }
            }
            finally
            {
                _writeLock.Release();
            }
        }
Exemplo n.º 8
0
 public override void OnError(DataLink sender, Exception ex)
 {
     Console.WriteLine($"Error from {sender?.ServiceName}-{sender?.RemoteName}: {ex}");
 }
Exemplo n.º 9
0
 public override void OnDisconnect(DataLink sender)
 {
     Console.WriteLine($"Remote {sender.RemoteName} disconnected.");
 }
Exemplo n.º 10
0
 private bool OnGroupMessage(DataLink sender, string channel, Memory <byte> message)
 {
     Console.WriteLine($"From {sender.RemoteName} ({channel}): {Encoding.UTF8.GetString(message.Span)}");
     return(true);
 }
Exemplo n.º 11
0
 public void OnP2P(DataLink sender, string channel, Memory <byte> message)
 {
     Console.WriteLine($"From {sender.RemoteName} (p2p): {Encoding.UTF8.GetString(message.Span)}");
 }
Exemplo n.º 12
0
 private void OnBroadcast(DataLink sender, string channel, Memory <byte> message)
 {
     Console.WriteLine($"From {sender.RemoteName} (broadcast): {Encoding.UTF8.GetString(message.Span)}");
 }
Exemplo n.º 13
0
 public virtual void OnHealthyChanged(DataLinkCollection sender, DataLink dataLink)
 {
 }
Exemplo n.º 14
0
 public virtual void OnConnect(DataLink sender)
 {
 }
Exemplo n.º 15
0
 public virtual void OnError(DataLink sender, Exception ex)
 {
 }