forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageExtensions.cs
More file actions
114 lines (93 loc) · 3.67 KB
/
MessageExtensions.cs
File metadata and controls
114 lines (93 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
using System.Threading;
using ServiceStack.Messaging;
using ServiceStack.Text;
namespace ServiceStack
{
public static class MessageExtensions
{
public static string ToString(byte[] bytes)
{
return System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
}
private static Dictionary<Type, ToMessageDelegate> ToMessageFnCache = new Dictionary<Type, ToMessageDelegate>();
internal static ToMessageDelegate GetToMessageFn(Type type)
{
ToMessageDelegate toMessageFn;
ToMessageFnCache.TryGetValue(type, out toMessageFn);
if (toMessageFn != null) return toMessageFn;
var genericType = typeof(MessageExtensions<>).MakeGenericType(type);
var mi = genericType.GetStaticMethod("ConvertToMessage");
toMessageFn = (ToMessageDelegate)mi.MakeDelegate(typeof(ToMessageDelegate));
Dictionary<Type, ToMessageDelegate> snapshot, newCache;
do
{
snapshot = ToMessageFnCache;
newCache = new Dictionary<Type, ToMessageDelegate>(ToMessageFnCache);
newCache[type] = toMessageFn;
} while (!ReferenceEquals(
Interlocked.CompareExchange(ref ToMessageFnCache, newCache, snapshot), snapshot));
return toMessageFn;
}
public static IMessage ToMessage(this byte[] bytes, Type ofType)
{
if (bytes == null)
return null;
var msgFn = GetToMessageFn(ofType);
var msg = msgFn(bytes);
return msg;
}
public static Message<T> ToMessage<T>(this byte[] bytes)
{
if (bytes == null)
return null;
var messageText = ToString(bytes);
return JsonSerializer.DeserializeFromString<Message<T>>(messageText);
}
public static byte[] ToBytes(this IMessage message)
{
var serializedMessage = JsonSerializer.SerializeToString((object)message);
return System.Text.Encoding.UTF8.GetBytes(serializedMessage);
}
public static byte[] ToBytes<T>(this IMessage<T> message)
{
var serializedMessage = JsonSerializer.SerializeToString(message);
return System.Text.Encoding.UTF8.GetBytes(serializedMessage);
}
public static string ToInQueueName(this IMessage message)
{
var queueName = message.Priority > 0
? new QueueNames(message.Body.GetType()).Priority
: new QueueNames(message.Body.GetType()).In;
return queueName;
}
public static string ToDlqQueueName(this IMessage message)
{
return new QueueNames(message.Body.GetType()).Dlq;
}
public static string ToInQueueName<T>(this IMessage<T> message)
{
return message.Priority > 0
? QueueNames<T>.Priority
: QueueNames<T>.In;
}
public static IMessageQueueClient CreateMessageQueueClient(this IMessageService mqServer)
{
return mqServer.MessageFactory.CreateMessageQueueClient();
}
public static IMessageProducer CreateMessageProducer(this IMessageService mqServer)
{
return mqServer.MessageFactory.CreateMessageProducer();
}
}
internal delegate IMessage ToMessageDelegate(object param);
internal static class MessageExtensions<T>
{
public static IMessage ConvertToMessage(object oBytes)
{
var bytes = (byte[]) oBytes;
return bytes.ToMessage<T>();
}
}
}