0002-Add-a-default-repository.patch
rsb-cil-test/Program.cs | ||
---|---|---|
79 | 79 | |
80 | 80 |
listener.Deactivate(); |
81 | 81 | |
82 |
} |
|
82 |
Console.WriteLine("\nRepository test:"); |
|
83 |
Informer repoInformer = new Informer("/test/repo", new OutConnector(new BusClientConnection(host, port), Rsb.Converter.DefaultConverterRepository.Instance.InformerConverterSelector)); |
|
84 |
Listener repoListener = new Listener(new InPushConnector(new BusClientConnection(host, port), new Scope("/"), Rsb.Converter.DefaultConverterRepository.Instance.ListenerConverterSelector)); |
|
85 |
repoListener.EventReceived += (e) => { |
|
86 |
Console.WriteLine("Event: {0}\nData: {1}", e, e.Data); |
|
87 |
}; |
|
88 |
repoListener.Activate(); |
|
89 |
repoInformer.Activate(); |
|
90 | ||
91 |
//test string |
|
92 |
repoInformer.Send("Teest"); |
|
93 |
//test bool |
|
94 |
repoInformer.Send(true); |
|
95 |
//test int |
|
96 |
repoInformer.Send(42); |
|
97 |
//test long (int64 |
|
98 |
repoInformer.Send((long)42); |
|
99 |
//test for uint32 |
|
100 |
repoInformer.Send((uint)4242); |
|
101 |
//test for uint64 |
|
102 |
repoInformer.Send((ulong)4242); |
|
103 |
//test double |
|
104 |
repoInformer.Send(4.2); |
|
105 |
//test float |
|
106 |
repoInformer.Send((float)4.2); |
|
107 |
//test byte[] |
|
108 |
byte[] test = { 12, 13, 14, 255 }; |
|
109 |
repoInformer.Send(test); |
|
110 |
//test void |
|
111 |
repoInformer.Send(Null.Instance); |
|
112 | ||
113 |
repoInformer.Deactivate(); |
|
114 |
Console.ReadKey(); |
|
115 |
repoListener.Deactivate(); |
|
116 |
} |
|
83 | 117 | |
84 | 118 |
} |
85 | 119 |
} |
rsb-cil/Rsb/Converter/ByteConverter.cs | ||
---|---|---|
1 |
using System; |
|
2 | ||
3 |
namespace Rsb.Converter |
|
4 |
{ |
|
5 |
internal class ByteConverter : IConverter |
|
6 |
{ |
|
7 | ||
8 |
private static readonly string WIRE_SCHEMA = "bytes"; |
|
9 | ||
10 |
public object Deserialize(Tuple<string, byte[]> data) |
|
11 |
{ |
|
12 |
if (data.Item1 != WIRE_SCHEMA) |
|
13 |
{ |
|
14 |
throw new ConversionException("Unexpected wire schema for deserialization: " + data.Item1); |
|
15 |
} |
|
16 |
return data.Item2; |
|
17 |
} |
|
18 | ||
19 |
public Tuple<string, byte[]> Serialize(object data) |
|
20 |
{ |
|
21 |
return new Tuple<string, byte[]>(WIRE_SCHEMA, (byte[]) data); |
|
22 |
} |
|
23 |
} |
|
24 |
} |
rsb-cil/Rsb/Converter/DefaultConverterRepository.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 | ||
4 |
namespace Rsb.Converter |
|
5 |
{ |
|
6 |
public class DefaultConverterRepository |
|
7 |
{ |
|
8 |
private static readonly DefaultConverterRepository instance = new DefaultConverterRepository(); |
|
9 | ||
10 |
private List<Tuple<PredicateConverterSelection<string>.Predicate, IConverter>> listenerConverterList = new List<Tuple<PredicateConverterSelection<string>.Predicate, IConverter>>(); |
|
11 |
private PredicateConverterSelection<string> listenerConverterSelector = null; |
|
12 | ||
13 |
public PredicateConverterSelection<string> ListenerConverterSelector { get => listenerConverterSelector; } |
|
14 | ||
15 |
private List<Tuple<PredicateConverterSelection<Type>.Predicate, IConverter>> informerConverterList = new List<Tuple<PredicateConverterSelection<Type>.Predicate, IConverter>>(); |
|
16 |
private PredicateConverterSelection<Type> informerConverterSelector = null; |
|
17 | ||
18 |
public PredicateConverterSelection<Type> InformerConverterSelector { get => informerConverterSelector; } |
|
19 | ||
20 |
private readonly Tuple<string, IConverter>[] defaultConverter = { |
|
21 |
new Tuple<string, IConverter>("void", new NullConverter()), |
|
22 |
new Tuple<string, IConverter>("double", new DoubleConverter()), |
|
23 |
new Tuple<string, IConverter>("float", new FloatConverter()), |
|
24 |
new Tuple<string, IConverter>("int32", new Int32Converter()), |
|
25 |
new Tuple<string, IConverter>("int64", new Int64Converter()), |
|
26 |
new Tuple<string, IConverter>("uint32", new UInt32Converter()), |
|
27 |
new Tuple<string, IConverter>("uint64", new UInt64Converter()), |
|
28 |
new Tuple<string, IConverter>("bool", new BoolConverter()), |
|
29 |
new Tuple<string, IConverter>("ascii-string", new StringConverter(false)), |
|
30 |
new Tuple<string, IConverter>("utf-8-string", new StringConverter()), |
|
31 |
new Tuple<string, IConverter>("bytes", new ByteConverter()), |
|
32 |
//new Tuple<string, IConverter>("scope", new NullConverter()), |
|
33 |
}; |
|
34 | ||
35 |
private readonly Dictionary<string, Type> defaultTypeNames = new Dictionary<string, Type> |
|
36 |
{ |
|
37 |
{"void", typeof(Null)}, |
|
38 |
{"double", typeof(double)}, |
|
39 |
{"float", typeof(float)}, |
|
40 |
{"int32", typeof(int)}, |
|
41 |
{"int64", typeof(long)}, |
|
42 |
{"uint32", typeof(uint)}, |
|
43 |
{"uint64", typeof(ulong)}, |
|
44 |
{ "bool", typeof(bool)}, |
|
45 |
{"utf-8-string", typeof(string)}, |
|
46 |
{"bytes", typeof(byte[])}, |
|
47 |
{"scope", typeof(Scope)} |
|
48 |
}; |
|
49 |
|
|
50 |
// Explicit static constructor to tell C# compiler |
|
51 |
// not to mark type as beforefieldinit |
|
52 |
static DefaultConverterRepository() |
|
53 |
{ |
|
54 |
} |
|
55 | ||
56 |
private DefaultConverterRepository() |
|
57 |
{ |
|
58 |
foreach (var type in defaultConverter) { |
|
59 |
listenerConverterList.Add( |
|
60 |
new Tuple<PredicateConverterSelection<string>.Predicate, IConverter>( |
|
61 |
new IsExactMatch<string>(type.Item1), |
|
62 |
type.Item2 |
|
63 |
)); |
|
64 |
// will just send utf8 |
|
65 |
if (type.Item1.Equals("ascii-string")) { |
|
66 |
continue; |
|
67 |
} |
|
68 |
informerConverterList.Add( |
|
69 |
new Tuple<PredicateConverterSelection<Type>.Predicate, IConverter>( |
|
70 |
new IsExactMatch<Type>(defaultTypeNames[type.Item1]), |
|
71 |
type.Item2 |
|
72 |
)); |
|
73 |
} |
|
74 | ||
75 |
listenerConverterSelector = new PredicateConverterSelection<string>(listenerConverterList); |
|
76 |
informerConverterSelector = new PredicateConverterSelection<Type>(informerConverterList); |
|
77 |
} |
|
78 | ||
79 |
public static DefaultConverterRepository Instance |
|
80 |
{ |
|
81 |
get |
|
82 |
{ |
|
83 |
return instance; |
|
84 |
} |
|
85 |
} |
|
86 |
} |
|
87 |
} |
rsb-cil/Rsb/Converter/DoubleConverter.cs | ||
---|---|---|
1 |
using System; |
|
2 | ||
3 |
namespace Rsb.Converter |
|
4 |
{ |
|
5 |
internal class DoubleConverter : IConverter |
|
6 |
{ |
|
7 |
private static readonly string WIRE_SCHEMA = "double"; |
|
8 | ||
9 |
public object Deserialize(Tuple<string, byte[]> data) |
|
10 |
{ |
|
11 |
if (data.Item1 != WIRE_SCHEMA) |
|
12 |
{ |
|
13 |
throw new ConversionException("Unexpected wire schema for deserialization: " + data.Item1); |
|
14 |
} |
|
15 |
if (data.Item2.Length != 8) |
|
16 |
{ |
|
17 |
throw new ConversionException("Data of type double must have length 8. Received: " + data.Item2.Length); |
|
18 |
} |
|
19 |
return BitConverter.ToDouble(data.Item2, 0); |
|
20 |
} |
|
21 | ||
22 |
public Tuple<string, byte[]> Serialize(object data) |
|
23 |
{ |
|
24 |
return new Tuple<string, byte[]>(WIRE_SCHEMA, BitConverter.GetBytes((double)data)); |
|
25 |
} |
|
26 |
} |
|
27 |
} |
rsb-cil/Rsb/Converter/FloatConverter.cs | ||
---|---|---|
1 |
using System; |
|
2 | ||
3 |
namespace Rsb.Converter |
|
4 |
{ |
|
5 |
internal class FloatConverter : IConverter |
|
6 |
{ |
|
7 |
private static readonly string WIRE_SCHEMA = "float"; |
|
8 | ||
9 |
public object Deserialize(Tuple<string, byte[]> data) |
|
10 |
{ |
|
11 |
if (data.Item1 != WIRE_SCHEMA) |
|
12 |
{ |
|
13 |
throw new ConversionException("Unexpected wire schema for deserialization: " + data.Item1); |
|
14 |
} |
|
15 |
if (data.Item2.Length != 4) |
|
16 |
{ |
|
17 |
throw new ConversionException("Data of type float must have length 4. Received: " + data.Item2.Length); |
|
18 |
} |
|
19 |
return BitConverter.ToSingle(data.Item2, 0); |
|
20 |
} |
|
21 | ||
22 |
public Tuple<string, byte[]> Serialize(object data) |
|
23 |
{ |
|
24 |
return new Tuple<string, byte[]>(WIRE_SCHEMA, BitConverter.GetBytes((float)data)); |
|
25 |
} |
|
26 |
} |
|
27 |
} |
rsb-cil/Rsb/Converter/Int32Converter.cs | ||
---|---|---|
1 |
using System; |
|
2 | ||
3 |
namespace Rsb.Converter |
|
4 |
{ |
|
5 |
internal class Int32Converter : IConverter |
|
6 |
{ |
|
7 | ||
8 |
private static readonly string WIRE_SCHEMA = "int32"; |
|
9 | ||
10 |
public object Deserialize(Tuple<string, byte[]> data) |
|
11 |
{ |
|
12 |
if (data.Item1 != WIRE_SCHEMA) |
|
13 |
{ |
|
14 |
throw new ConversionException("Unexpected wire schema for deserialization: " + data.Item1); |
|
15 |
} |
|
16 |
if (data.Item2.Length != 4) |
|
17 |
{ |
|
18 |
throw new ConversionException("Data of type int32 must have length 4. Received: " + data.Item2.Length); |
|
19 |
} |
|
20 |
return BitConverter.ToInt32(data.Item2,0); |
|
21 |
} |
|
22 | ||
23 |
public Tuple<string, byte[]> Serialize(object data) |
|
24 |
{ |
|
25 |
return new Tuple<string, byte[]>(WIRE_SCHEMA, BitConverter.GetBytes((int) data)); |
|
26 |
} |
|
27 |
} |
|
28 |
} |
rsb-cil/Rsb/Converter/Int64Converter.cs | ||
---|---|---|
1 |
using System; |
|
2 | ||
3 |
namespace Rsb.Converter |
|
4 |
{ |
|
5 |
internal class Int64Converter : IConverter |
|
6 |
{ |
|
7 |
private static readonly string WIRE_SCHEMA = "int64"; |
|
8 | ||
9 |
public object Deserialize(Tuple<string, byte[]> data) |
|
10 |
{ |
|
11 |
if (data.Item1 != WIRE_SCHEMA) |
|
12 |
{ |
|
13 |
throw new ConversionException("Unexpected wire schema for deserialization: " + data.Item1); |
|
14 |
} |
|
15 |
if (data.Item2.Length != 8) |
|
16 |
{ |
|
17 |
throw new ConversionException("Data of type int64 must have length 8. Received: " + data.Item2.Length); |
|
18 |
} |
|
19 |
return BitConverter.ToInt64(data.Item2, 0); |
|
20 |
} |
|
21 | ||
22 |
public Tuple<string, byte[]> Serialize(object data) |
|
23 |
{ |
|
24 |
return new Tuple<string, byte[]>(WIRE_SCHEMA, BitConverter.GetBytes((long)data)); |
|
25 |
} |
|
26 |
} |
|
27 |
} |
rsb-cil/Rsb/Converter/NullConverter.cs | ||
---|---|---|
1 |
using System; |
|
2 | ||
3 |
namespace Rsb.Converter |
|
4 |
{ |
|
5 |
internal class NullConverter : IConverter |
|
6 |
{ |
|
7 |
private static readonly string WIRE_SCHEMA = "void"; |
|
8 | ||
9 |
public object Deserialize(Tuple<string, byte[]> data) |
|
10 |
{ |
|
11 |
if (data.Item1 != WIRE_SCHEMA) |
|
12 |
{ |
|
13 |
throw new ConversionException("Unexpected wire schema for deserialization: " + data.Item1); |
|
14 |
} |
|
15 |
return null; |
|
16 |
} |
|
17 | ||
18 |
public Tuple<string, byte[]> Serialize(object data) |
|
19 |
{ |
|
20 |
return new Tuple<string, byte[]>(WIRE_SCHEMA, new byte[0]); |
|
21 |
} |
|
22 |
} |
|
23 | ||
24 |
public sealed class Null { |
|
25 |
private static readonly Null instance = new Null(); |
|
26 | ||
27 |
// Explicit static constructor to tell C# compiler |
|
28 |
// not to mark type as beforefieldinit |
|
29 |
static Null() |
|
30 |
{ |
|
31 |
} |
|
32 | ||
33 |
private Null() |
|
34 |
{ |
|
35 |
} |
|
36 | ||
37 |
public static Null Instance |
|
38 |
{ |
|
39 |
get |
|
40 |
{ |
|
41 |
return instance; |
|
42 |
} |
|
43 |
} |
|
44 |
} |
|
45 | ||
46 |
} |
rsb-cil/Rsb/Converter/StringConverter.cs | ||
---|---|---|
1 |
using System; |
|
2 | ||
3 |
namespace Rsb.Converter |
|
4 |
{ |
|
5 |
internal class StringConverter : IConverter |
|
6 |
{ |
|
7 |
|
|
8 |
private System.Text.Encoding Encoding; |
|
9 |
private bool isUTF8; |
|
10 | ||
11 |
private static readonly string WIRE_SCHEMA_UTF8 = "utf-8-string"; |
|
12 |
private static readonly string WIRE_SCHEMA_ASCII = "ascii-string"; |
|
13 | ||
14 |
public StringConverter(bool utf8 = true) { |
|
15 |
this.isUTF8 = utf8; |
|
16 |
if (utf8) |
|
17 |
{ |
|
18 |
Encoding = new System.Text.UTF8Encoding(); |
|
19 |
} |
|
20 |
else { |
|
21 |
Encoding = new System.Text.ASCIIEncoding(); |
|
22 |
} |
|
23 |
} |
|
24 | ||
25 |
public object Deserialize(Tuple<string, byte[]> data) |
|
26 |
{ |
|
27 |
if ( |
|
28 |
(isUTF8 && data.Item1 != WIRE_SCHEMA_UTF8) || |
|
29 |
(!isUTF8 && data.Item1 != WIRE_SCHEMA_ASCII) ) |
|
30 |
{ |
|
31 |
throw new ConversionException("Unexpected wire schema for deserialization: " + data.Item1); |
|
32 |
} |
|
33 |
return Encoding.GetString(data.Item2); |
|
34 |
} |
|
35 | ||
36 |
public Tuple<string, byte[]> Serialize(object data) |
|
37 |
{ |
|
38 |
return new Tuple<string, byte[]>(isUTF8 ? WIRE_SCHEMA_UTF8 : WIRE_SCHEMA_ASCII, Encoding.GetBytes(data.ToString())); |
|
39 |
} |
|
40 |
} |
|
41 |
} |
rsb-cil/Rsb/Converter/UInt32Converter.cs | ||
---|---|---|
1 |
using System; |
|
2 | ||
3 |
namespace Rsb.Converter |
|
4 |
{ |
|
5 |
internal class UInt32Converter : IConverter |
|
6 |
{ |
|
7 |
private static readonly string WIRE_SCHEMA = "uint32"; |
|
8 | ||
9 |
public object Deserialize(Tuple<string, byte[]> data) |
|
10 |
{ |
|
11 |
if (data.Item1 != WIRE_SCHEMA) |
|
12 |
{ |
|
13 |
throw new ConversionException("Unexpected wire schema for deserialization: " + data.Item1); |
|
14 |
} |
|
15 |
if (data.Item2.Length != 4) |
|
16 |
{ |
|
17 |
throw new ConversionException("Data of type uint32 must have length 4. Received: " + data.Item2.Length); |
|
18 |
} |
|
19 |
return BitConverter.ToUInt32(data.Item2, 0); |
|
20 |
} |
|
21 | ||
22 |
public Tuple<string, byte[]> Serialize(object data) |
|
23 |
{ |
|
24 |
return new Tuple<string, byte[]>(WIRE_SCHEMA, BitConverter.GetBytes((uint)data)); |
|
25 |
} |
|
26 |
} |
|
27 |
} |
rsb-cil/Rsb/Converter/UInt64Converter.cs | ||
---|---|---|
1 |
using System; |
|
2 | ||
3 |
namespace Rsb.Converter |
|
4 |
{ |
|
5 |
internal class UInt64Converter : IConverter |
|
6 |
{ |
|
7 |
private static readonly string WIRE_SCHEMA = "uint64"; |
|
8 | ||
9 |
public object Deserialize(Tuple<string, byte[]> data) |
|
10 |
{ |
|
11 |
if (data.Item1 != WIRE_SCHEMA) |
|
12 |
{ |
|
13 |
throw new ConversionException("Unexpected wire schema for deserialization: " + data.Item1); |
|
14 |
} |
|
15 |
if (data.Item2.Length != 8) |
|
16 |
{ |
|
17 |
throw new ConversionException("Data of type uint64 must have length 8. Received: " + data.Item2.Length); |
|
18 |
} |
|
19 |
return BitConverter.ToUInt64(data.Item2, 0); |
|
20 |
} |
|
21 | ||
22 |
public Tuple<string, byte[]> Serialize(object data) |
|
23 |
{ |
|
24 |
return new Tuple<string, byte[]>(WIRE_SCHEMA, BitConverter.GetBytes((ulong)data)); |
|
25 |
} |
|
26 |
} |
|
27 |
} |
rsb-cil/rsb-cil.csproj | ||
---|---|---|
40 | 40 |
</ItemGroup> |
41 | 41 |
<ItemGroup> |
42 | 42 |
<Compile Include="Properties\AssemblyInfo.cs" /> |
43 |
<Compile Include="Rsb\Converter\ByteConverter.cs" /> |
|
44 |
<Compile Include="Rsb\Converter\DefaultConverterRepository.cs" /> |
|
45 |
<Compile Include="Rsb\Converter\DoubleConverter.cs" /> |
|
46 |
<Compile Include="Rsb\Converter\FloatConverter.cs" /> |
|
47 |
<Compile Include="Rsb\Converter\Int32Converter.cs" /> |
|
48 |
<Compile Include="Rsb\Converter\Int64Converter.cs" /> |
|
49 |
<Compile Include="Rsb\Converter\NullConverter.cs" /> |
|
50 |
<Compile Include="Rsb\Converter\StringConverter.cs" /> |
|
51 |
<Compile Include="Rsb\Converter\UInt32Converter.cs" /> |
|
52 |
<Compile Include="Rsb\Converter\UInt64Converter.cs" /> |
|
43 | 53 |
<Compile Include="Rsb\Protocol\EventId.cs" /> |
44 | 54 |
<Compile Include="Rsb\Protocol\EventMetaData.cs" /> |
45 | 55 |
<Compile Include="Rsb\Protocol\Notification.cs" /> |
46 |
- |