0003-Add-converter-repository.patch

S. Barut, 09/24/2018 11:56 AM

Download (16.8 KB)

View differences:

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
39 39
  </ItemGroup>
40 40
  <ItemGroup>
41 41
    <Compile Include="Properties\AssemblyInfo.cs" />
42
    <Compile Include="Rsb\Converter\ByteConverter.cs" />
43
    <Compile Include="Rsb\Converter\DefaultConverterRepository.cs" />
44
    <Compile Include="Rsb\Converter\DoubleConverter.cs" />
45
    <Compile Include="Rsb\Converter\FloatConverter.cs" />
46
    <Compile Include="Rsb\Converter\Int32Converter.cs" />
47
    <Compile Include="Rsb\Converter\Int64Converter.cs" />
48
    <Compile Include="Rsb\Converter\NullConverter.cs" />
49
    <Compile Include="Rsb\Converter\StringConverter.cs" />
50
    <Compile Include="Rsb\Converter\UInt32Converter.cs" />
51
    <Compile Include="Rsb\Converter\UInt64Converter.cs" />
42 52
    <Compile Include="Rsb\Patterns\ICallback.cs" />
43 53
    <Compile Include="Rsb\Patterns\DataCallback.cs" />
44 54
    <Compile Include="Rsb\Patterns\LocalServer.cs" />
45
-