-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathZSocketSetup.cs
172 lines (142 loc) · 3.97 KB
/
ZSocketSetup.cs
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
namespace ZeroMQ
{
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
/// <summary>
/// Defines a fluent interface for configuring device sockets.
/// </summary>
public class ZSocketSetup
{
readonly ZSocket _socket;
readonly List<Action<ZSocket>> _socketInitializers;
readonly List<string> _bindings;
readonly List<string> _connections;
bool _isConfigured;
public ZSocketSetup(ZSocket socket)
{
if (socket == null)
{
throw new ArgumentNullException("socket");
}
_socket = socket;
_socketInitializers = new List<Action<ZSocket>>();
_bindings = new List<string>();
_connections = new List<string>();
}
/// <summary>
/// Configure the socket to bind to a given endpoint. See <see cref="ZSocket.Bind"/> for details.
/// </summary>
/// <param name="endpoint">A string representing the endpoint to which the socket will bind.</param>
/// <returns>The current <see cref="ZSocketSetup"/> object.</returns>
public ZSocketSetup Bind(string endpoint)
{
if (endpoint == null)
{
throw new ArgumentNullException("endpoint");
}
_bindings.Add(endpoint);
return this;
}
/// <summary>
/// Configure the socket to connect to a given endpoint. See <see cref="ZSocket.Connect"/> for details.
/// </summary>
/// <param name="endpoint">A string representing the endpoint to which the socket will connect.</param>
/// <returns>The current <see cref="ZSocketSetup"/> object.</returns>
public ZSocketSetup Connect(string endpoint)
{
if (endpoint == null)
{
throw new ArgumentNullException("endpoint");
}
_connections.Add(endpoint);
return this;
}
public ZSocketSetup SetSocketOption<T>(Expression<Func<ZSocket, T>> property, T value)
{
PropertyInfo propertyInfo;
if (property.Body is MemberExpression)
{
propertyInfo = ((MemberExpression)property.Body).Member as PropertyInfo;
}
else
{
propertyInfo = ((MemberExpression)((UnaryExpression)property.Body).Operand).Member as PropertyInfo;
}
if (propertyInfo == null)
{
throw new InvalidOperationException("The specified ZSocket member is not a property: " + property.Body);
}
_socketInitializers.Add(s => propertyInfo.SetValue(s, value, null));
return this;
}
private byte[] _subscription;
/// <summary>
/// Configure the socket to subscribe to a specific prefix. See <see cref="ZSocket.Subscribe"/> for details.
/// </summary>
/// <param name="prefix">A byte array containing the prefix to which the socket will subscribe.</param>
/// <returns>The current <see cref="ZSocketSetup"/> object.</returns>
public ZSocketSetup Subscribe(byte[] prefix)
{
_subscription = prefix;
return this;
}
/// <summary>
/// Configure the socket to subscribe to all incoming messages. See <see cref="ZSocket.SubscribeAll"/> for details.
/// </summary>
/// <returns>The current <see cref="ZSocketSetup"/> object.</returns>
public ZSocketSetup SubscribeAll()
{
_subscription = new byte[2] { 0x01, 0x00 };
return this;
}
public void Configure()
{
if (_isConfigured)
{
return;
}
foreach (Action<ZSocket> initializer in _socketInitializers)
{
initializer.Invoke(_socket);
}
_isConfigured = true;
}
public void BindConnect()
{
foreach (string endpoint in _bindings)
{
_socket.Bind(endpoint);
}
foreach (string endpoint in _connections)
{
_socket.Connect(endpoint);
}
if (_subscription != null)
{
// _socket.Subscribe(_subscription);
using (var subscription = new ZFrame(_subscription))
{
_socket.Send(subscription);
}
}
}
public void UnbindDisconnect()
{
/* if (_subscription != null)
{
_socket.Unsubscribe(_subscription);
} */
ZError error;
foreach (string endpoint in _bindings)
{
_socket.Unbind(endpoint, out error);
}
foreach (string endpoint in _connections)
{
_socket.Disconnect(endpoint, out error);
}
}
}
}