forked from zeromq/clrzmq4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZDevice.cs
303 lines (264 loc) · 8.28 KB
/
ZDevice.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
namespace ZeroMQ
{
using System;
using System.Threading;
using System.Collections.Generic;
/// <summary>
/// Forwards messages received by a front-end socket to a back-end socket, from which
/// they are then sent.
/// </summary>
/// <remarks>
/// The base implementation of <see cref="ZDevice"/> is <b>not</b> threadsafe. Do not construct
/// a device with sockets that were created in separate threads or separate contexts.
/// </remarks>
public abstract class ZDevice : ZThread
{
/// <summary>
/// The polling interval in milliseconds.
/// </summary>
protected readonly TimeSpan PollingInterval = TimeSpan.FromMilliseconds(500);
/// <summary>
/// The ZContext reference, to not become finalized
/// </summary>
protected readonly ZContext Context;
/// <summary>
/// The frontend socket that will normally pass messages to <see cref="BackendSocket"/>.
/// </summary>
public ZSocket FrontendSocket;
/// <summary>
/// The backend socket that will normally receive messages from (and possibly send replies to) <see cref="FrontendSocket"/>.
/// </summary>
public ZSocket BackendSocket;
/// <summary>
/// You are using ZContext.Current!
/// </summary>
protected ZDevice()
: this (ZContext.Current)
{ }
protected ZDevice(ZContext context)
: base()
{
Context = context;
}
/// <summary>
/// Initializes a new instance of the <see cref="ZDevice"/> class.
/// You are using ZContext.Current!
/// </summary>
/// <param name="frontendSocket">
/// A <see cref="ZSocket"/> that will pass incoming messages to <paramref name="backendSocket"/>.
/// </param>
/// <param name="backendSocket">
/// A <see cref="ZSocket"/> that will receive messages from (and optionally send replies to) <paramref name="frontendSocket"/>.
/// </param>
/// <param name="mode">The <see cref="DeviceMode"/> for the current device.</param>
protected ZDevice(ZSocketType frontendType, ZSocketType backendType)
: this (ZContext.Current, frontendType, backendType)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ZDevice"/> class.
/// </summary>
/// <param name="frontendSocket">
/// A <see cref="ZSocket"/> that will pass incoming messages to <paramref name="backendSocket"/>.
/// </param>
/// <param name="backendSocket">
/// A <see cref="ZSocket"/> that will receive messages from (and optionally send replies to) <paramref name="frontendSocket"/>.
/// </param>
/// <param name="mode">The <see cref="DeviceMode"/> for the current device.</param>
protected ZDevice(ZContext context, ZSocketType frontendType, ZSocketType backendType)
: base()
{
Context = context;
ZError error;
if (!Initialize(frontendType, backendType, out error))
{
throw new ZException(error);
}
}
protected virtual bool Initialize(ZSocketType frontendType, ZSocketType backendType, out ZError error)
{
error = default(ZError);
/* if (frontendType == ZSocketType.None && backendType == ZSocketType.None)
{
throw new InvalidOperationException();
} /**/
if (frontendType != ZSocketType.None)
{
if (null == (FrontendSocket = ZSocket.Create(Context, frontendType, out error)))
{
return false;
}
FrontendSetup = new ZSocketSetup(FrontendSocket);
}
if (backendType != ZSocketType.None)
{
if (null == (BackendSocket = ZSocket.Create(Context, backendType, out error)))
{
return false;
}
BackendSetup = new ZSocketSetup(BackendSocket);
}
return true;
}
/// <summary>
/// Gets a <see cref="ZSocketSetup"/> for configuring the frontend socket.
/// </summary>
public ZSocketSetup BackendSetup { get; protected set; }
/// <summary>
/// Gets a <see cref="ZSocketSetup"/> for configuring the backend socket.
/// </summary>
public ZSocketSetup FrontendSetup { get; protected set; }
/*/ <summary>
/// Gets a <see cref="ManualResetEvent"/> that can be used to block while the device is running.
/// </summary>
public ManualResetEvent DoneEvent { get; private set; } /**/
/*/ <summary>
/// Gets an <see cref="AutoResetEvent"/> that is pulsed after every Poll call.
/// </summary>
public AutoResetEvent PollerPulse
{
get { return _poller.Pulse; }
}*/
/// <summary>
/// Initializes the frontend and backend sockets. Called automatically when starting the device.
/// If called multiple times, will only execute once.
/// </summary>
public virtual void Initialize()
{
EnsureNotDisposed();
if (FrontendSetup != null) FrontendSetup.Configure();
if (BackendSetup != null) BackendSetup.Configure();
}
/// <summary>
/// Start the device in the current thread. Should be used by implementations of the <see cref="DeviceRunner.Start"/> method.
/// </summary>
/// <remarks>
/// Initializes the sockets prior to starting the device with <see cref="Initialize"/>.
/// </remarks>
protected override void Run()
{
EnsureNotDisposed();
Initialize();
ZSocket[] sockets;
ZPollItem[] polls;
if (FrontendSocket != null && BackendSocket != null)
{
sockets = new ZSocket[] {
FrontendSocket,
BackendSocket
};
polls = new ZPollItem[] {
ZPollItem.Create(FrontendHandler),
ZPollItem.Create(BackendHandler)
};
}
else if (FrontendSocket != null)
{
sockets = new ZSocket[] {
FrontendSocket
};
polls = new ZPollItem[] {
ZPollItem.Create(FrontendHandler)
};
}
else
{
sockets = new ZSocket[] {
BackendSocket
};
polls = new ZPollItem[] {
ZPollItem.Create(BackendHandler)
};
}
/* ZPollItem[] polls;
{
var pollItems = new List<ZPollItem>();
switch (FrontendSocket.SocketType)
{
case ZSocketType.Code.ROUTER:
case ZSocketType.Code.XSUB:
case ZSocketType.Code.PUSH:
// case ZSocketType.Code.STREAM:
pollItems.Add(new ZPollItem(FrontendSocket, ZPoll.In)
{
ReceiveMessage = FrontendHandler
});
break;
}
switch (BackendSocket.SocketType)
{
case ZSocketType.Code.DEALER:
// case ZSocketType.Code.STREAM:
pollItems.Add(new ZPollItem(BackendSocket, ZPoll.In)
{
ReceiveMessage = BackendHandler
});
break;
}
polls = pollItems.ToArray();
} */
// Because of using ZmqSocket.Forward, this field will always be null
ZMessage[] lastMessageFrames = null;
if (FrontendSetup != null) FrontendSetup.BindConnect();
if (BackendSetup != null) BackendSetup.BindConnect();
bool isValid = false;
var error = default(ZError);
try
{
while (!Cancellor.IsCancellationRequested)
{
if (!(isValid = sockets.Poll(polls, ZPoll.In, ref lastMessageFrames, out error, PollingInterval)))
{
if (error == ZError.EAGAIN)
{
Thread.Sleep(1);
continue;
}
if (error == ZError.ETERM)
{
break;
}
// EFAULT
throw new ZException(error);
}
}
}
catch (ZException)
{
// Swallow any exceptions thrown while stopping
if (!Cancellor.IsCancellationRequested)
{
throw;
}
}
if (FrontendSetup != null) FrontendSetup.UnbindDisconnect();
if (BackendSetup != null) BackendSetup.UnbindDisconnect();
if (error == ZError.ETERM)
{
Close();
}
}
/// <summary>
/// Invoked when a message has been received by the frontend socket.
/// </summary>
/// <param name="args">A <see cref="SocketEventArgs"/> object containing the poll event args.</param>
protected abstract bool FrontendHandler(ZSocket socket, out ZMessage message, out ZError error);
/// <summary>
/// Invoked when a message has been received by the backend socket.
/// </summary>
/// <param name="args">A <see cref="SocketEventArgs"/> object containing the poll event args.</param>
protected abstract bool BackendHandler(ZSocket args, out ZMessage message, out ZError error);
/// <summary>
/// Stops the device and releases the underlying sockets. Optionally disposes of managed resources.
/// </summary>
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (FrontendSocket != null) FrontendSocket.Dispose();
if (BackendSocket != null) BackendSocket.Dispose();
}
base.Dispose(disposing);
}
}
}