/// <summary> /// OnMessageReceived /// </summary> /// <param name="connection"></param> /// <param name="e"></param> protected override void OnMessageReceived(IConnection connection, MessageReceivedEventArgs e) { base.OnMessageReceived(connection, e); int readLength; RedisMessage message = null; try { message = this._protocol.Parse(connection, e.Buffer, out readLength); } catch (Exception ex) { base.OnConnectionError(connection, ex); connection.BeginDisconnect(ex); e.SetReadlength(e.Buffer.Count); return; } this.OnResponse(message); e.SetReadlength(readLength); }
/// <summary> /// on reponse /// </summary> /// <param name="message"></param> private void OnResponse(RedisMessage message) { if (message == null) { return; } var objMulti = message.Reply as MultiBulkReplies; if (objMulti == null || objMulti.Replies == null || objMulti.Replies.Length == 0) { return; } var objFlag = objMulti.Replies[0] as BulkReplies; if (objFlag == null || objFlag.Payload == null || objFlag.Payload.Length == 0) { return; } switch (Encoding.UTF8.GetString(objFlag.Payload)) { case "message": { if (objMulti.Replies.Length != 3) { return; } var objChannel = objMulti.Replies[1] as BulkReplies; if (objChannel == null || objChannel.Payload == null || objChannel.Payload.Length == 0) { return; } var objMsg = objMulti.Replies[2] as BulkReplies; if (objMsg == null || objMsg.Payload == null) { return; } this.OnListener(Encoding.UTF8.GetString(objChannel.Payload), objMsg.Payload); break; } case "pmessage": { if (objMulti.Replies.Length != 4) { return; } var objChannel = objMulti.Replies[2] as BulkReplies; if (objChannel == null || objChannel.Payload == null || objChannel.Payload.Length == 0) { return; } var objMsg = objMulti.Replies[3] as BulkReplies; if (objMsg == null || objMsg.Payload == null) { return; } this.OnListener(Encoding.UTF8.GetString(objChannel.Payload), objMsg.Payload); break; } default: break; } }