Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Paste
P194
(An Untitled Masterwork)
Active
Public
Actions
Authored by
stwalkerster
on Mar 17 2018, 6:43 PM.
Edit Paste
Archive Paste
View Raw File
Subscribe
Mute Notifications
Tags
None
Referenced Files
F56559:
Mar 17 2018, 6:43 PM
2018-03-17 18:43:20 (UTC+0)
Subscribers
None
namespace
foo
{
using
System.Threading
;
using
ZeroMQ
;
internal
class
zmq_sender
{
public
static
void
Main
(
string
[]
args
)
{
// fancy stuff to queue up three instances of DoStuff on different threads, and wait until they're all done
// most of this is waiting-for-completion garbage, the key bits are ThreadPool.QueueUserWorkItem() which
// takes a method or lambda, and DoStuff().
int
instances
=
3
;
var
mre
=
new
WaitHandle
[
instances
];
for
(
int
i
=
0
;
i
<
instances
;
i
++)
{
mre
[
i
]
=
new
ManualResetEvent
(
false
);
var
current
=
(
ManualResetEvent
)
mre
[
i
];
int
c
=
i
;
ThreadPool
.
QueueUserWorkItem
(
x
=>
{
try
{
DoStuff
(
c
);
}
finally
{
current
.
Set
();
}
});
}
WaitHandle
.
WaitAll
(
mre
);
}
// emulates a client. Connects, sends three things, disconnects.
private
static
void
DoStuff
(
int
i
)
{
using
(
var
context
=
new
ZContext
())
{
using
(
var
sock
=
new
ZSocket
(
context
,
ZSocketType
.
PUB
){})
{
sock
.
Connect
(
"tcp://127.0.0.1:12345"
);
sock
.
Send
(
new
ZFrame
(
i
+
": test 1"
));
Thread
.
Sleep
(
1000
);
sock
.
Send
(
new
ZFrame
(
i
+
": test 2"
));
Thread
.
Sleep
(
1000
);
sock
.
Send
(
new
ZFrame
(
i
+
": test 3"
));
}
}
}
}
internal
class
zmq_receiver
{
// connects, binds to socket, and prints whatever it receives.
public
static
void
Main
(
string
[]
args
)
{
var
context
=
new
ZContext
();
var
sock
=
new
ZSocket
(
context
,
ZSocketType
.
SUB
);
sock
.
Bind
(
"tcp://*:12345"
);
sock
.
SubscribeAll
();
while
(
true
)
{
using
(
ZFrame
request
=
sock
.
ReceiveFrame
())
{
Console
.
WriteLine
(
request
.
ReadString
());
}
}
}
}
}
/*
OUTPUT
:
0
:
test
1
2
:
test
1
1
:
test
1
0
:
test
2
2
:
test
2
1
:
test
2
2
:
test
3
1
:
test
3
0
:
test
3
Event Timeline
stwalkerster
created this paste.
Mar 17 2018, 6:43 PM
2018-03-17 18:43:20 (UTC+0)
stwalkerster
created this object with visibility "Public (No Login Required)".
Log In to Comment