-
Notifications
You must be signed in to change notification settings - Fork 112
/
ZActor.cs
114 lines (93 loc) · 2.75 KB
/
ZActor.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
using System;
namespace ZeroMQ
{
public delegate void ZAction0(ZSocket backend, System.Threading.CancellationTokenSource cancellor, object[] args);
public delegate void ZAction(ZContext context, ZSocket backend, System.Threading.CancellationTokenSource cancellor, object[] args);
public class ZActor : ZThread
{
public ZContext Context { get; protected set; }
public string Endpoint { get; protected set; }
public ZAction Action { get; protected set; }
public ZAction0 Action0 { get; protected set; }
public object[] Arguments { get; protected set; }
public ZSocket Backend { get; protected set; }
public ZSocket Frontend { get; protected set; }
public ZActor(ZContext context, ZAction action, params object[] args)
: this(context, default(string), action, args)
{
var rnd0 = new byte[8];
using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider()) rng.GetNonZeroBytes(rnd0);
this.Endpoint = string.Format("inproc://{0}", ZContext.Encoding.GetString(rnd0));
}
public ZActor(ZContext context, string endpoint, ZAction action, params object[] args)
: base()
{
this.Context = context;
this.Endpoint = endpoint;
this.Action = action;
this.Arguments = args;
}
/// <summary>
/// You are using ZContext.Current!
/// </summary>
public ZActor(ZAction0 action, params object[] args)
: this(default(string), action, args)
{
var rnd0 = new byte[8];
using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider()) rng.GetNonZeroBytes(rnd0);
this.Endpoint = string.Format("inproc://{0}", ZContext.Encoding.GetString(rnd0));
}
/// <summary>
/// You are using ZContext.Current!
/// </summary>
public ZActor(string endpoint, ZAction0 action, params object[] args)
: base()
{
this.Context = ZContext.Current;
this.Endpoint = endpoint;
this.Action0 = action;
this.Arguments = args;
}
protected override void Run()
{
using (Backend = ZSocket.Create(Context, ZSocketType.PAIR))
{
Backend.Bind(Endpoint);
if (Action0 != null)
{
Action0(Backend, Cancellor, Arguments);
}
if (Action != null)
{
Action(Context, Backend, Cancellor, Arguments);
}
}
}
public override void Start()
{
base.Start();
if (Frontend == null)
{
Frontend = ZSocket.Create(Context, ZSocketType.PAIR);
Frontend.Connect(Endpoint);
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
if (Frontend != null)
{
Frontend.Dispose();
Frontend = null;
}
if (Backend != null)
{
Backend.Dispose();
Backend = null;
}
}
}
}
}