Skip to content

Commit

Permalink
Wrap all messages saved to the store in WrappedValue so that stores t…
Browse files Browse the repository at this point in the history
…hat need to serialize the input can do so using ToString.
  • Loading branch information
davidfowl committed Feb 9, 2012
1 parent 892f7af commit ebfbef6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
8 changes: 4 additions & 4 deletions SignalR/Connection.cs
Expand Up @@ -100,14 +100,14 @@ private List<object> ProcessResults(IList<Message> source)
var messageValues = new List<object>();
foreach (var message in source)
{
SignalCommand command;
if (SignalCommand.TryGetCommand(message, _serializer, out command))
if (SignalCommand.IsCommand(message))
{
var command = WrappedValue.Unwrap<SignalCommand>(message.Value, _serializer);
ProcessCommand(command);
}
else
{
messageValues.Add(message.Value);
messageValues.Add(WrappedValue.Unwrap(message.Value, _serializer));
}
}
return messageValues;
Expand All @@ -131,7 +131,7 @@ private void ProcessCommand(SignalCommand command)

private Task SendMessage(string key, object value)
{
return _messageBus.Send(key, value).Catch();
return _messageBus.Send(key, new WrappedValue(value, _serializer)).Catch();
}

private void PopulateResponseState(PersistentResponse response)
Expand Down
54 changes: 54 additions & 0 deletions SignalR/MessageBus/WrappedValue.cs
@@ -0,0 +1,54 @@
namespace SignalR.MessageBus
{
/// <summary>
/// All values saved to the messages store are wrapped by this type.
/// If a store needs to save values in a serializable way then it just needs to call
/// ToString() and we'll unwrap it when it comes back (if needed).
/// </summary>
internal class WrappedValue
{
private readonly IJsonSerializer _serializer;
private readonly object _value;

public WrappedValue(object value, IJsonSerializer serializer)
{
_value = value;
_serializer = serializer;
}

public object Value
{
get
{
return _value;
}
}

public static T Unwrap<T>(object value, IJsonSerializer serializer)
{
var wrappedValue = value as WrappedValue;
if (wrappedValue != null)
{
return (T)wrappedValue.Value;
}

return serializer.Parse<T>((string)value);
}

public static object Unwrap(object value, IJsonSerializer serializer)
{
var wrappedValue = value as WrappedValue;
if (wrappedValue != null)
{
return wrappedValue.Value;
}

return serializer.Parse((string)value);
}

public override string ToString()
{
return _serializer.Stringify(_value);
}
}
}
27 changes: 3 additions & 24 deletions SignalR/SignalCommand.cs
@@ -1,4 +1,5 @@
using System;
using SignalR.MessageBus;

namespace SignalR
{
Expand All @@ -11,31 +12,9 @@ internal static string AddCommandSuffix(string eventKey)
return eventKey + "." + SignalrCommand;
}

internal static bool TryGetCommand(Message message, IJsonSerializer serializer, out SignalCommand command)
public static bool IsCommand(Message message)
{
command = null;
if (!message.SignalKey.EndsWith(SignalrCommand, StringComparison.OrdinalIgnoreCase))
{
return false;
}

command = message.Value as SignalCommand;

// Optimization for in memory message store
if (command != null)
{
return true;
}

// Otherwise deserialize the message value
string rawValue = message.Value as string;
if (rawValue == null)
{
return false;
}

command = serializer.Parse<SignalCommand>(rawValue);
return true;
return message.SignalKey.EndsWith(SignalrCommand, StringComparison.OrdinalIgnoreCase);
}

public CommandType Type { get; set; }
Expand Down
1 change: 1 addition & 0 deletions SignalR/SignalR.csproj
Expand Up @@ -59,6 +59,7 @@
<Compile Include="Infrastructure\DefaultDependencyResolver.cs" />
<Compile Include="MessageBus\InMemoryMessage.cs" />
<Compile Include="MessageBus\MessageResult.cs" />
<Compile Include="MessageBus\WrappedValue.cs" />
<Compile Include="PersistentConnectionFactory.cs" />
<Compile Include="GuidConnectionIdFactory.cs" />
<Compile Include="Hubs\ActionInfo.cs" />
Expand Down

0 comments on commit ebfbef6

Please sign in to comment.