-
Notifications
You must be signed in to change notification settings - Fork 112
/
ZPollItems.cs
223 lines (182 loc) · 5.31 KB
/
ZPollItems.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
using System;
namespace ZeroMQ
{
using lib;
using System;
using System.Collections.Generic;
using System.Linq;
public static partial class ZPollItems
{
#pragma warning disable 649
// unsafe native delegate
internal delegate bool PollManyDelegate(IEnumerable<ZSocket> sockets, IEnumerable<ZPollItem> items, ZPoll pollFirst, out ZError error, TimeSpan? timeoutMs);
internal static readonly PollManyDelegate PollMany;
// unsafe native delegate
internal delegate bool PollSingleDelegate(ZSocket socket, ZPollItem item, ZPoll pollFirst, out ZError error, TimeSpan? timeout);
internal static readonly PollSingleDelegate PollSingle;
#pragma warning restore 649
static ZPollItems()
{
// Initialize static Fields
Platform.SetupImplementation(typeof(ZPollItems));
}
public static bool PollIn(this ZSocket socket, ZPollItem item, out ZMessage incoming, out ZError error, TimeSpan? timeout = null)
{
incoming = null;
return Poll(socket, item, ZPoll.In, ref incoming, out error, timeout);
}
public static bool PollOut(this ZSocket socket, ZPollItem item, ZMessage outgoing, out ZError error, TimeSpan? timeout = null)
{
if (outgoing == null)
{
throw new ArgumentNullException("outgoing");
}
return Poll(socket, item, ZPoll.Out, ref outgoing, out error, timeout);
}
public static bool Poll(this ZSocket socket, ZPollItem item, ZPoll pollEvents, ref ZMessage message, out ZError error, TimeSpan? timeout = null)
{
if (PollSingle(socket, item, pollEvents, out error, timeout))
{
if (PollSingleResult(socket, item, pollEvents, ref message))
{
return true;
}
error = ZError.EAGAIN;
}
return false;
}
internal static bool PollSingleResult(ZSocket socket, ZPollItem item, ZPoll pollEvents, ref ZMessage message)
{
bool shouldReceive = item.ReceiveMessage != null && ((pollEvents & ZPoll.In) == ZPoll.In);
bool shouldSend = item.SendMessage != null && ((pollEvents & ZPoll.Out) == ZPoll.Out);
if (pollEvents == ZPoll.In)
{
if (!shouldReceive)
{
throw new InvalidOperationException("No ReceiveMessage delegate set for Poll.In");
}
if (OnReceiveMessage(socket, item, out message))
{
if (!shouldSend)
{
return true;
}
if (OnSendMessage(socket, item, message))
{
return true;
}
}
}
else if (pollEvents == ZPoll.Out)
{
if (!shouldSend)
{
throw new InvalidOperationException("No SendMessage delegate set for Poll.Out");
}
if (OnSendMessage(socket, item, message))
{
if (!shouldReceive)
{
return true;
}
if (OnReceiveMessage(socket, item, out message))
{
return true;
}
}
}
return false;
}
internal static bool OnReceiveMessage(ZSocket socket, ZPollItem item, out ZMessage message)
{
message = null;
if (((ZPoll)item.ReadyEvents & ZPoll.In) == ZPoll.In)
{
ZError recvWorkerE;
if (item.ReceiveMessage == null)
{
// throw?
}
else if (item.ReceiveMessage(socket, out message, out recvWorkerE))
{
// what to do?
return true;
}
}
return false;
}
internal static bool OnSendMessage(ZSocket socket, ZPollItem item, ZMessage message)
{
if (((ZPoll)item.ReadyEvents & ZPoll.Out) == ZPoll.Out)
{
ZError sendWorkerE;
if (item.SendMessage == null)
{
// throw?
}
else if (item.SendMessage(socket, message, out sendWorkerE))
{
// what to do?
return true;
}
}
return false;
}
public static bool PollIn(this IEnumerable<ZSocket> sockets, IEnumerable<ZPollItem> items, out ZMessage[] incoming, out ZError error, TimeSpan? timeout = null)
{
incoming = null;
return Poll(sockets, items, ZPoll.In, ref incoming, out error, timeout);
}
public static bool PollOut(this IEnumerable<ZSocket> sockets, IEnumerable<ZPollItem> items, ZMessage[] outgoing, out ZError error, TimeSpan? timeout = null)
{
if (outgoing == null)
{
throw new ArgumentNullException("outgoing");
}
return Poll(sockets, items, ZPoll.Out, ref outgoing, out error, timeout);
}
public static bool Poll(this IEnumerable<ZSocket> sockets, IEnumerable<ZPollItem> items, ZPoll pollEvents, ref ZMessage[] messages, out ZError error, TimeSpan? timeout = null)
{
if (PollMany(sockets, items, pollEvents, out error, timeout))
{
if (PollManyResult(sockets, items, pollEvents, ref messages))
{
return true;
}
error = ZError.EAGAIN;
}
return false;
}
internal static bool PollManyResult(IEnumerable<ZSocket> sockets, IEnumerable<ZPollItem> items, ZPoll pollEvents, ref ZMessage[] messages)
{
int count = items.Count();
int readyCount = 0;
bool send = messages != null && ((pollEvents & ZPoll.Out) == ZPoll.Out);
bool receive = ((pollEvents & ZPoll.In) == ZPoll.In);
ZMessage[] incoming = null;
if (receive)
{
incoming = new ZMessage[count];
}
for (int i = 0; i < count; ++i)
{
ZSocket socket = sockets.ElementAt(i);
ZPollItem item = items.ElementAt(i);
ZMessage message = send ? messages[i] : null;
if (ZPollItems.PollSingleResult(socket, item, pollEvents, ref message))
{
++readyCount;
}
if (receive)
{
incoming[i] = message;
}
}
if (receive)
{
messages = incoming;
}
return readyCount > 0;
}
}
}