Exemplo n.º 1
0
        internal void _Dispose(Exception err = null)
        {
            lock (_loc)
            {
                if (_disposed)
                {
                    return;
                }
                _disposed = true;
                _msgs.Clear();
                _msglen = 0;
            }
            _cancel.Cancel();
            _cancel.Dispose();
            _socket.Dispose();
            _listen?.Dispose();
            _aes.Dispose();

            var dis = Disposed;

            if (dis == null)
            {
                return;
            }
            Task.Run(() =>
            {
                if (err == null)
                {
                    err = new OperationCanceledException("Client disposed manually or by GC.");
                }
                var arg = new LinkEventArgs <Exception>(err);
                dis.Invoke(this, arg);
            });
        }
Exemplo n.º 2
0
        internal void _Dispose(Exception err = null)
        {
            lock (_locker)
            {
                if (_disposed)
                {
                    return;
                }
                _disposed = true;
                _messageQueue.Clear();
                _messageLength = 0;
            }
            _cancellationSource.Cancel();
            _cancellationSource.Dispose();
            _socket.Dispose();
            _listen?.Dispose();
            _aes.Dispose();

            var dis = Disposed;

            if (dis == null)
            {
                return;
            }
            _ = Task.Run(() =>
            {
                if (err == null)
                {
                    err = new OperationCanceledException("Client disposed.");
                }
                var arg = new LinkEventArgs <Exception>(err);
                dis.Invoke(this, arg);
            });
        }
Exemplo n.º 3
0
        private void _ClientReceived(object sender, LinkEventArgs <LinkPacket> arg)
        {
            var obj = arg.Object;
            var src = obj.Source;
            var tar = obj.Target;
            var buf = obj.Buffer;

            if (tar == Links.Id)
            {
                if (obj.Path == "user.group")
                {
                    var lst = obj.Data.As <List <int> >().Where(r => r < Links.Id);
                    var set = new HashSet <int>(lst);
                    if (set.Count > Links.GroupLabelLimit)
                    {
                        throw new LinkException(LinkError.GroupLimited);
                    }
                    var clt = (LinkClient)sender;
                    lock (_locker)
                        _Refresh(clt, set);
                    return;
                }

                foreach (var val in _clients.Values)
                {
                    if (val != null && val._id != src)
                    {
                        val.Enqueue(buf);
                    }
                }
                return;
            }
            else if (tar > Links.Id)
            {
                // Thread safe operation
                if (_clients.TryGetValue(tar, out var val))
                {
                    val?.Enqueue(buf);
                }
                return;
            }
            else
            {
                // Thread safe operation
                if (_set.TryGetValue(tar, out var grp))
                {
                    foreach (var val in grp.Values)
                    {
                        if (val != null && val._id != src)
                        {
                            val.Enqueue(buf);
                        }
                    }
                }
                return;
            }
        }
Exemplo n.º 4
0
        internal async Task _Receiver()
        {
            while (_cancel.IsCancellationRequested == false)
            {
                var buf = await _socket.ReceiveAsyncExt();

                var rec = Received;
                if (rec == null)
                {
                    continue;
                }
                var res = _aes.Decrypt(buf);
                var pkt = new LinkPacket().LoadValue(res);
                var arg = new LinkEventArgs <LinkPacket>(pkt);
                rec.Invoke(this, arg);
            }
        }