/// <summary> /// Read multi-bulk response from Redis server /// </summary> /// <param name="response"></param> /// <returns></returns> public static RedisSubscriptionResponse ReadResponse(object[] response) { RedisSubscriptionResponseType type = ParseType(response[0] as String); RedisSubscriptionResponse obj; switch (type) { case RedisSubscriptionResponseType.Subscribe: case RedisSubscriptionResponseType.Unsubscribe: case RedisSubscriptionResponseType.PSubscribe: case RedisSubscriptionResponseType.PUnsubscribe: obj = new RedisSubscriptionChannel(type, response); break; case RedisSubscriptionResponseType.Message: case RedisSubscriptionResponseType.PMessage: obj = new RedisSubscriptionMessage(type, response); break; default: throw new RedisProtocolException("Unexpected response type: " + type); } obj.Type = type; return(obj); }
private void Read_Task() { RedisSubscriptionResponse response; using (new ActivityTracer("Handle subscriptions")) { while (true) { if (_readCancel.IsCancellationRequested) { break; } response = TryReadResponse(); if (response == null) { continue; } switch (response.Type) { case RedisSubscriptionResponseType.Subscribe: case RedisSubscriptionResponseType.PSubscribe: case RedisSubscriptionResponseType.Unsubscribe: case RedisSubscriptionResponseType.PUnsubscribe: RedisSubscriptionChannel channel = response as RedisSubscriptionChannel; Interlocked.Exchange(ref _count, channel.Count); if (SubscriptionChanged != null) { SubscriptionChanged(this, new RedisSubscriptionChangedEventArgs(channel)); } break; case RedisSubscriptionResponseType.Message: case RedisSubscriptionResponseType.PMessage: RedisSubscriptionMessage message = response as RedisSubscriptionMessage; if (SubscriptionReceived != null) { SubscriptionReceived(this, new RedisSubscriptionReceivedEventArgs(message)); } if (message.Pattern != null && _callbackDispatchers.ContainsKey(message.Pattern) && _callbackDispatchers[message.Pattern] != null) { _callbackDispatchers[message.Pattern].OnMsgReceived(message); } else if (_callbackDispatchers.ContainsKey(message.Channel) && _callbackDispatchers[message.Channel] != null) { _callbackDispatchers[message.Channel].OnMsgReceived(message); } break; } } } }
public void HandleSubscription(RedisSubscription command) { _connection.Write(command.Command, command.Arguments); if (!IsSubscribed) { using (new ActivityTracer("Handle subscriptions")) { IsSubscribed = true; while (true) { var resp = _connection.Read(command.Parser); switch (resp.Type) { case RedisSubscriptionResponseType.Subscribe: case RedisSubscriptionResponseType.PSubscribe: case RedisSubscriptionResponseType.Unsubscribe: case RedisSubscriptionResponseType.PUnsubscribe: RedisSubscriptionChannel channel = resp as RedisSubscriptionChannel; Count = channel.Count; if (SubscriptionChanged != null) { SubscriptionChanged(this, new RedisSubscriptionChangedEventArgs(channel)); } break; case RedisSubscriptionResponseType.Message: case RedisSubscriptionResponseType.PMessage: RedisSubscriptionMessage message = resp as RedisSubscriptionMessage; if (SubscriptionReceived != null) { SubscriptionReceived(this, new RedisSubscriptionReceivedEventArgs(message)); } break; } if (Count == 0) { break; } } IsSubscribed = false; } } }
/// <summary> /// Read multi-bulk response from Redis server /// </summary> /// <param name="response"></param> /// <returns></returns> public static RedisSubscriptionResponse ReadResponse(object[] response) { RedisSubscriptionResponseType type = ParseType(response[0] as String); RedisSubscriptionResponse obj; switch (type) { case RedisSubscriptionResponseType.Subscribe: case RedisSubscriptionResponseType.Unsubscribe: case RedisSubscriptionResponseType.PSubscribe: case RedisSubscriptionResponseType.PUnsubscribe: obj = new RedisSubscriptionChannel(type, response); break; case RedisSubscriptionResponseType.Message: case RedisSubscriptionResponseType.PMessage: obj = new RedisSubscriptionMessage(type, response); break; default: throw new RedisProtocolException("Unexpected response type: " + type); } obj.Type = type; return obj; }
/// <summary> /// Instantiate new instance of the RedisSubscriptionChangedEventArgs class /// </summary> /// <param name="response">The Redis server response</param> public RedisSubscriptionChangedEventArgs(RedisSubscriptionChannel response) { Response = response; }