PBCaGw.Workers.WorkerChain.Dispose C# (CSharp) Méthode

Dispose() public méthode

Dispose the current chain.
public Dispose ( ) : void
Résultat void
        public void Dispose()
        {
            knownChains = new ConcurrentBag<WorkerChain>(knownChains.Where(row => row != this));
            if (IsDisposed || IsDisposing)
                return;
            IsDisposing = true;

            if (workers[0] is TcpReceiver)
            {
                if (Side == ChainSide.SERVER_CONN)
                    TcpManager.DropServerConnection(this);
                else
                {
                    try
                    {
                        IPEndPoint addr = ((TcpReceiver)workers[0]).RemoteEndPoint;
                        TcpManager.DropClientConnection(addr);
                    }
                    catch
                    {
                    }
                }
            }

            foreach (var i in workers)
                i.Dispose();
            //((IDisposable)workers[0]).Dispose();

            //List<DataPacket> packets = new List<DataPacket>();
            /*lock (lockChainManagement)
            {*/
            // Unsubscribe all monitors used by a given client (used only by client chains)
            foreach (var monitor in Subscriptions)
            {
                Handlers.EventAdd.Unsubscribe(monitor.Value);
            }
            //}

            // Remove channel subscriptions
            foreach (var i in ChannelSubscriptions)
            {
                Record r = InfoService.SubscribedChannel[i.Key];
                if (r == null)
                    continue;
                foreach (var j in r.SubscriptionList)
                {
                    InfoService.ChannelSubscription.Remove(j);
                    CidGenerator.ReleaseCid(j);
                }
                Debug.Assert(r.GWCID != null, "r.GWCID != null");
                InfoService.ChannelSubscription.Remove(r.GWCID.Value);
                CidGenerator.ReleaseCid(r.GWCID.Value);
                InfoService.SubscribedChannel.Remove(i.Key);
            }

            // Remove all channels linked to this chain (used only by IOC chains)
            foreach (string channel in Channels)
            {
                //InfoService.SearchChannelEndPoint.Remove(channel);

                if (Log.WillDisplay(System.Diagnostics.TraceEventType.Verbose))
                    Log.TraceEvent(System.Diagnostics.TraceEventType.Verbose, ChainId, "Dropping channel " + channel);
                List<WorkerChain> channelsToDrop;
                /*lock (lockChainManagement)
                {
                    channelsToDrop = knownChains
                        .Where(row => row.Side != ChainSide.SERVER_CONN && row.ChannelCid.ContainsKey(channel)).ToList();
                }*/
                channelsToDrop = knownChains
                        .Where(row => row.Side != ChainSide.SERVER_CONN && row.ChannelCid.ContainsKey(channel)).ToList();
                // Disonnect all clients which use a channel
                foreach (WorkerChain chain in channelsToDrop)
                    chain.Dispose();

                /*// Send CA_PROTO_SERVER_DISCONN to all clients which use a channel
                foreach (WorkerChain chain in channelsToDrop)
                {
                    DataPacket newPacket = DataPacket.Create(0, this);
                    newPacket.Command = 27;
                    newPacket.Parameter1 = chain.ChannelCid[channel];
                    newPacket.Destination = chain.ClientEndPoint;
                    TcpManager.SendClientPacket(newPacket);
                    InfoService.ChannelCid.Remove(chain.ChannelCid[channel]);
                    CidGenerator.ReleaseCid(chain.ChannelCid[channel]);
                }*/
                try
                {
                    Record record = InfoService.ChannelEndPoint[channel];
                    if (record != null && record.GWCID != null)
                    {
                        InfoService.ChannelCid.Remove(record.GWCID.Value);
                        CidGenerator.ReleaseCid(record.GWCID.Value);
                    }
                    InfoService.ChannelEndPoint.Remove(channel);
                }
                catch
                {
                }
                if (Log.WillDisplay(System.Diagnostics.TraceEventType.Verbose))
                    Log.TraceEvent(System.Diagnostics.TraceEventType.Verbose, ChainId, "NB known channels: " + InfoService.ChannelEndPoint.Count);
            }

            /*lock (lockChainManagement)
            {
                // Remove the chain from the known chains
                knownChains.Remove(this);
            }*/

            //knownChains = new ConcurrentBag<WorkerChain>(knownChains.Where(row => row != this));

            // Remove the chain from the other chains
            WorkerChain outValue;
            foreach (WorkerChain chain in knownChains)
                chain.usedBy.TryRemove(this, out outValue);

            // Mark all chains which may need to be disconnected
            foreach (var i in knownChains.Where(row => row.Side == ChainSide.SERVER_CONN
                        && row.usedBy.Count == 0 && row.lastNonUsed == null))
                i.lastNonUsed = Gateway.Now;
            /*IEnumerable<WorkerChain> markAsUnUsed;
            lock (lockChainManagement)
            {
                markAsUnUsed = knownChains
                    .Where(row => row.Side == ChainSide.SERVER_CONN
                        && row.usedBy.Count == 0 && row.lastNonUsed == null).ToList();
            }
            foreach (WorkerChain chain in markAsUnUsed)
                chain.lastNonUsed = Gateway.Now;*/

            IsDisposed = true;
        }

Usage Example

        public DebugPortWorker(WorkerChain chain, IPEndPoint client, IPEndPoint server)
        {
            Chain = chain;
            socket = ((TcpReceiver)Chain[0]).Socket;
            base.ClientEndPoint = client;
            base.ServerEndPoint = server;

            sendStream = new BufferedStream(new NetworkStream(socket));

            try
            {
                lock (sendStream)
                {
                    Send((int)DebugDataType.GW_NAME);
                    Send(Chain.Gateway.Configuration.GatewayName);

                    Send((int)DebugDataType.FULL_IOC);
                    Send(Chain.Gateway.KnownIocs.Count);
                    foreach (var i in Chain.Gateway.KnownIocs)
                    {
                        Send(i.Key);
                        Send(i.Value.Count);
                        foreach (var j in i.Value)
                        {
                            Send(j);
                        }
                    }

                    Send((int)DebugDataType.FULL_CLIENT);
                    Send(Chain.Gateway.KnownClients.Count);
                    foreach (var i in Chain.Gateway.KnownClients)
                    {
                        Send(i.Key);
                        Send(i.Value.Count);
                        foreach (var j in i.Value)
                        {
                            Send(j);
                        }
                    }

                    if (PBCaGw.Services.DebugTraceListener.TraceAll)
                        Send((int)DebugDataType.FULL_LOGS);
                    else
                        Send((int)DebugDataType.CRITICAL_LOGS);
                    Flush();

                    PBCaGw.Services.LogEntry[] logs = PBCaGw.Services.DebugTraceListener.LastEntries;
                    foreach (var i in logs)
                    {
                        Send((int)DebugDataType.LOG);
                        Send(i.Source);
                        Send((int)i.EventType);
                        Send(i.Id);
                        Send(i.Message);
                        Flush();
                    }
                }
            }
            catch
            {
                Chain.Dispose();
            }

            Chain.Gateway.NewIocChannel += new NewIocChannelDelegate(GatewayNewIocChannel);
            Chain.Gateway.DropIoc += new DropIocDelegate(GatewayDropIoc);
            Chain.Gateway.NewClientChannel += new NewClientChannelDelegate(GatewayNewClientChannel);
            Chain.Gateway.DropClient += new DropClientDelegate(GatewayDropClient);

            PBCaGw.Services.DebugTraceListener.LogEntry += GatewayLogEntry;
            PBCaGw.Services.DebugTraceListener.TraceLevelChanged += new System.EventHandler(DebugTraceListenerTraceLevelChanged);
        }
All Usage Examples Of PBCaGw.Workers.WorkerChain::Dispose