/// <summary>
        /// Dispose.
        /// </summary>
        public override void Dispose()
        {
            List <ClientEventsHandler>   clients;
            List <EventSubscriptionInfo> subscriptions;

            lock (_syncRoot)
            {
                subscriptions = GeneralHelper.EnumerableToList <EventSubscriptionInfo>(_eventSubscriptions.Values);

                // Clear methods first, thus removing clients is faster.
                _eventSubscriptions.Clear();

                // Take care to do an optimized hot swap teardown.
                clients = GeneralHelper.EnumerableToList(_clients.Values);

                _clients.Clear();
            }

            foreach (EventSubscriptionInfo methodSubscription in subscriptions)
            {
                methodSubscription.Dispose();
            }

            foreach (ClientEventsHandler clientInfo in clients)
            {
                clientInfo.Dispose();
            }

            base.Dispose();
        }
示例#2
0
        protected void ReleaseCurrentClientSource()
        {
            object currentSource = _clientSource;

            _clientSource = null;
            if (currentSource == null)
            {
                return;
            }

            lock (this)
            {
                // Release all current associations.
                foreach (KeyValuePair <int, EventHandlingInformation> pair in _eventsMethods)
                {
                    pair.Value.EventInfo.RemoveEventHandler(currentSource, pair.Value.DelegateInstance);
                }

                _eventsMethods.Clear();
            }
        }
示例#3
0
        /// <summary>
        /// Dispose.
        /// </summary>
        public void Dispose()
        {
            // Stop the main accept socket.
            Stop(null);

            ClientConnectedEvent    = null;
            ClientDisconnectedEvent = null;

            ClientMessageReceivedEvent  = null;
            ClientAsyncMessageSendEvent = null;

            // Dispose all clients.
            SocketCommunicatorEx[] clients = CommonHelper.EnumerableToArray <SocketCommunicatorEx>(_clientsHotSwap.Values);
            _clientsHotSwap.Clear();

            _serializer = null;

            foreach (SocketCommunicatorEx client in clients)
            {
                client.Dispose();
            }
        }
示例#4
0
        void _messageClient_MessageReceivedEvent(SocketCommunicator helper, object message)
        {
            if (message is EnvelopeMessage)
            {
                EnvelopeMessage envelopeMessage = (EnvelopeMessage)message;

                // Remove the remote message bus index association.
                envelopeMessage.Sender.LocalMessageBusIndex = ClientId.InvalidMessageBusClientIndex;

                foreach (ClientId id in envelopeMessage.Receivers)
                {
                    // Decode the id.
                    id.LocalMessageBusIndex = base.GetClientIndexByGuid(id.Guid);

                    if (id.IsMessageBusIndexValid)
                    {
                        // Assign as a part of the local bus.
                        id.MessageBus = this;
                        if (DoSendToClient(envelopeMessage.Sender, id, envelopeMessage.Envelope, null) != SendToClientResultEnum.Success)
                        {
#if Matrix_Diagnostics
                            InstanceMonitor.OperationError(string.Format("Failed to accept envelope message [{0}].", envelopeMessage.ToString()));
#endif
                        }
                    }
                    else
                    {
#if Matrix_Diagnostics
                        InstanceMonitor.OperationError(string.Format("Failed to accept envelope message [{0}] due to unrecognized receiver id.", envelopeMessage.ToString()));
#endif
                    }
                }
            }
            else if (message is ClientsListMessage)
            {// Received client update from server.
                ClientsListMessage listMessage = (ClientsListMessage)message;

                int jef = 0;
                foreach (var client in listMessage.Ids)
                {
                    Console.WriteLine("Incoming client id: " + client + " Source type: " + listMessage.SourcesTypes[jef]);
                    jef++;
                }

                List <ClientId> existingIds = new List <ClientId>();
                lock (_syncRoot)
                {
                    existingIds.AddRange(_originalServerClientsHotSwap.Values);

                    _originalServerClientsHotSwap.Clear();
                    _originalServerClientsTypesHotSwap.Clear();
                    _originalServerClientsSourcesTypesHotNamesSwap.Clear();

                    // Preprocess Ids, by assigning them new indeces and adding to the local message bus register.
                    for (int i = 0; i < listMessage.Ids.Count; i++)
                    {
                        // Add an original copy to the list.
                        _originalServerClientsHotSwap.Add(listMessage.Ids[i].Guid, listMessage.Ids[i]);

                        _originalServerClientsTypesHotSwap.Add(listMessage.Ids[i].Guid, listMessage.Types[i]);
                        _originalServerClientsSourcesTypesHotNamesSwap.Add(listMessage.Ids[i].Guid, listMessage.SourcesTypes[i]);

                        // Add the client to a new spot.
                        //_clientsHotSwap.Add(null);
                        //int messageBusIndex = _clientsHotSwap.Count - 1;

                        // This type of assignment will also work with multiple entries.
                        // This performs an internal hotswap.
                        //_guidToIndexHotSwap[id.Guid] = messageBusIndex;

                        // Also add to this classes collection.
                        //_localToRemoteId[messageBusIndex] = id;
                    }
                }

                foreach (ClientId id in listMessage.Ids)
                {
                    existingIds.Remove(id);
                    RaiseClientAddedEvent(id);
                }

                // Raise for any that were removed.
                foreach (ClientId id in existingIds)
                {
                    RaiseClientRemovedEvent(id, true);
                }
            }
            else if (message is RequestClientListUpdateMessage)
            {
                SendClientsUpdate();
            }
            else if (message is ClientUpdateMessage)
            {
                ClientUpdateMessage updateMessage = (ClientUpdateMessage)message;

                if (_originalServerClientsHotSwap.ContainsKey(updateMessage.ClientId.Guid))
                {
                    RaiseClientUpdateEvent(updateMessage.ClientId);
                }
                else
                {
#if Matrix_Diagnostics
                    InstanceMonitor.OperationError(string.Format("Failed to raise update event for client [{0}], since client not found.", updateMessage.ClientId.ToString()));
#endif
                }
            }
            else if (message is StateUpdateMessage)
            {
                RaiseCounterPartyUpdateEvent("Server", ((StateUpdateMessage)message).State.ToString());
            }
            else
            {
#if Matrix_Diagnostics
                InstanceMonitor.Warning(string.Format("Message [{0}] not recognized.", message.GetType().Name));
#endif
            }
        }
 /// <summary>
 ///
 /// </summary>
 public void Dispose()
 {
     _subscriptionsHotSwap.Clear();
 }