-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConnectionCreator.cs
More file actions
114 lines (96 loc) · 3.33 KB
/
ConnectionCreator.cs
File metadata and controls
114 lines (96 loc) · 3.33 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.IO;
using System.Net;
using System.Reflection;
using ZusiTcpInterface.Converters;
using ZusiTcpInterface.TypeDescriptors;
namespace ZusiTcpInterface
{
public class ConnectionCreator
{
private DescriptorCollection _descriptors;
private RootNodeConverter _rootNodeConverter;
private string _clientName = String.Empty;
private string _clientVersion = String.Empty;
private NeededDataCollection _neededData;
private IPEndPoint _endPoint = new IPEndPoint(IPAddress.Loopback, 1436);
public DescriptorCollection Descriptors
{
get { return _descriptors; }
}
public ConnectionCreator(string cabInfoTypeDescriptorFilename = "CabInfoTypes.xml")
{
using (var commandSetFileStream = File.OpenRead(cabInfoTypeDescriptorFilename))
{
InitialiseFrom(commandSetFileStream);
}
}
public ConnectionCreator(Stream commandsetFileStream)
{
InitialiseFrom(commandsetFileStream);
}
public ConnectionCreator(IEnumerable<AttributeDescriptor> descriptors)
{
InitialiseFrom(descriptors);
}
private void InitialiseFrom(Stream fileStream)
{
var descriptors = DescriptorReader.ReadCommandsetFrom(fileStream);
InitialiseFrom(descriptors);
}
private void InitialiseFrom(IEnumerable<AttributeDescriptor> descriptors)
{
var descriptorCollection = new DescriptorCollection(descriptors);
_descriptors = descriptorCollection;
SetupNodeConverters();
_neededData = new NeededDataCollection(_descriptors);
var entryAssembly = Assembly.GetEntryAssembly();
var assemblyName = entryAssembly.GetName();
ClientName = assemblyName.Name;
ClientVersion = assemblyName.Version.ToString();
}
private void SetupNodeConverters()
{
var handshakeConverter = new NodeConverter();
var ackHelloConverter = new AckHelloConverter();
var ackNeededDataConverter = new AckNeededDataConverter();
handshakeConverter.SubNodeConverters[0x02] = ackHelloConverter;
var cabDataConverter = GenerateNodeConverter(_descriptors);
var userDataConverter = new NodeConverter();
userDataConverter.SubNodeConverters[0x04] = ackNeededDataConverter;
userDataConverter.SubNodeConverters[0x0A] = cabDataConverter;
_rootNodeConverter = new RootNodeConverter();
_rootNodeConverter[0x01] = handshakeConverter;
_rootNodeConverter[0x02] = userDataConverter;
}
private INodeConverter GenerateNodeConverter(DescriptorCollection descriptors)
{
var attributeConverters = AttributeConverters.MapToDescriptors(descriptors);
return new FlatteningNodeConverter { ConversionFunctions = attributeConverters };
}
public Connection CreateConnection()
{
return new Connection(ClientName, ClientVersion, NeededData.GetRequestedAddresses(), EndPoint, _rootNodeConverter);
}
public IPEndPoint EndPoint
{
get { return _endPoint; }
set { _endPoint = value; }
}
public NeededDataCollection NeededData
{
get { return _neededData; }
}
public string ClientVersion
{
get { return _clientVersion; }
set { _clientVersion = value; }
}
public string ClientName
{
get { return _clientName; }
set { _clientName = value; }
}
}
}