/// <summary> /// Does the broad cast. /// </summary> /// <param name="reqMsg">The req MSG.</param> /// <param name="handlers">The handlers.</param> /// <returns>Is needed to clean null handlers</returns> protected virtual bool DoBroadCast(RequestMessage reqMsg, Dictionary <Guid, ServiceRequestNotifyHandler> handlers) { bool needCleanHandlers = false; List <Guid> clientIdList = new List <Guid>(handlers.Keys); Random random = new Random(); int start = random.Next(handlers.Count); for (int i = 0; i < handlers.Count; i++) { Guid tempClientId = clientIdList[(i + start) % handlers.Count]; ServiceRequestNotifyHandler tempHandler = handlers[tempClientId]; if (tempHandler != null) { try { IService service = ((Services.MessageRequestCallbackHandler)tempHandler.Target).Service; if (OnLog != null) { OnLog("[" + DateTime.Now.ToString() + "] Notify service host: " + reqMsg.ServiceName + "[" + tempClientId.ToString() + "]"); } tempHandler(reqMsg); //if calling ok, skip other subscribers, easily exit loop break; } catch (Exception ex) { if (OnLog != null) { OnLog("[" + DateTime.Now.ToString() + "] Service host: " + reqMsg.ServiceName + "[" + tempClientId.ToString() + "] shutdown! Reason: " + ex.ToString()); } handlers[tempClientId] = null; needCleanHandlers = true; } } else { needCleanHandlers = true; } } return(needCleanHandlers); }
/// <summary> /// Subscribes the service request. /// </summary> /// <param name="serviceName">Name of the service.</param> /// <param name="clientId">The client id.</param> /// <param name="handler">The handler.</param> public void SubscribeServiceRequest(string serviceName, Guid clientId, ServiceRequestNotifyHandler handler) { if (handler == null) { return; } lock (onServiceRequests) { if (!onServiceRequests.ContainsKey(serviceName)) { onServiceRequests.Add(serviceName, new Dictionary <Guid, ServiceRequestNotifyHandler>()); } } onServiceRequests[serviceName].Add(clientId, handler); if (OnLog != null) { OnLog(string.Format("[" + DateTime.Now.ToString() + "] Added new service reqMsg subscribing: {0}[{1}]", serviceName, clientId)); } }