-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathChannel.cs
191 lines (149 loc) · 5.09 KB
/
Channel.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
using System;
using System.Runtime.InteropServices;
namespace Zenoh;
public enum ChannelType
{
Ring,
Fifo
}
public abstract class Channel : IDisposable
{
internal nint Handle{get; private protected set;}
public ChannelType ChannelType { get; private protected set; }
public abstract void Dispose();
internal void CheckDisposed()
{
if (Handle == nint.Zero)
{
throw new InvalidOperationException("Channel is disposed");
}
}
}
public abstract class ChannelSample: Channel
{
/// <summary>
/// <para>
/// Returns sample from the buffer channel.
/// </para>
/// <para>
/// If there are no more pending replies will block until next sample is received,
/// or until the channel is dropped (normally when there are no more samples to receive).
/// </para>
/// </summary>
/// <param name="sample"></param>
/// <returns>
/// <para>"ZResult.Ok" in case of success.</para>
/// <para>"ZResult.ChannelDisconnected" if channel was dropped (the sample will be set to "null").</para>
/// </returns>
public abstract Result Recv(out Sample? sample);
/// <summary>
/// <para>
/// Returns sample from the buffer channel.
/// </para>
/// <para>
/// If there are no more pending replies will return immediately (with sample set to null).
/// </para>
/// </summary>
/// <param name="sample"></param>
/// <returns>
/// <para>"ZResult.Ok" in case of success.</para>
/// <para>"ZResult.ChannelDisconnected" if channel was dropped (the sample will be set to "null").</para>
/// <para>"ZResult.ChannelNodata" if the channel is still alive, but its buffer is empty (the sample will be set to "null").</para>
/// </returns>
public abstract Result TryRecv(out Sample? sample);
}
public sealed class ChannelSampleRing : ChannelSample
{
// z_owned_ring_handler_sample_t
internal ChannelSampleRing()
{
ChannelType = ChannelType.Ring;
var pChannel = Marshal.AllocHGlobal(Marshal.SizeOf<ZOwnedRingHandlerSample>());
ZenohC.z_internal_ring_handler_sample_null(pChannel);
Handle = pChannel;
}
public override void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~ChannelSampleRing()=>Dispose(false);
private void Dispose(bool disposing)
{
if (Handle == nint.Zero) return;
ZenohC.z_ring_handler_sample_drop(Handle);
Marshal.FreeHGlobal(Handle);
Handle = nint.Zero;
}
public override Result Recv(out Sample? sample)
{
CheckDisposed();
sample = Sample.CreateOwnedSample();
var pLoanedRingHandlerSample = ZenohC.z_ring_handler_sample_loan(Handle);
var r = ZenohC.z_ring_handler_sample_recv(pLoanedRingHandlerSample, sample.Handle);
if (r == Result.Ok) return Result.Ok;
sample.Dispose();
sample = null;
return r;
}
public override Result TryRecv(out Sample? sample)
{
CheckDisposed();
sample = Sample.CreateOwnedSample();
var pLoanedRingHandlerSample = ZenohC.z_ring_handler_sample_loan(Handle);
var r = ZenohC.z_ring_handler_sample_try_recv(pLoanedRingHandlerSample, sample.Handle);
if (r == Result.Ok) return Result.Ok;
sample.Dispose();
sample = null;
return r;
}
}
public sealed class ChannelSampleFifo : ChannelSample
{
// z_owned_fifo_handler_sample_t
private ChannelSampleFifo()
{
ChannelType = ChannelType.Fifo;
var pChannel = Marshal.AllocHGlobal(Marshal.SizeOf<ZOwnedFifoHandlerSample>());
ZenohC.z_internal_fifo_handler_sample_null(pChannel);
Handle = pChannel;
}
public override void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~ChannelSampleFifo()=>Dispose(false);
private void Dispose(bool disposing)
{
if (Handle == nint.Zero) return;
ZenohC.z_fifo_handler_sample_drop(Handle);
Marshal.FreeHGlobal(Handle);
Handle = nint.Zero;
}
public override Result Recv(out Sample? sample)
{
CheckDisposed();
sample = Sample.CreateOwnedSample();
var pLoanedFifoHandlerSample = ZenohC.z_fifo_handler_sample_loan(Handle);
var r = ZenohC.z_fifo_handler_sample_recv(pLoanedFifoHandlerSample, sample.Handle);
if (r == Result.Ok) return Result.Ok;
sample.Dispose();
sample = null;
return r;
}
public override Result TryRecv(out Sample? sample)
{
CheckDisposed();
sample = Sample.CreateOwnedSample();
var pLoanedFifoHandlerSample = ZenohC.z_fifo_handler_sample_loan(Handle);
var r = ZenohC.z_fifo_handler_sample_try_recv(pLoanedFifoHandlerSample, sample.Handle);
if (r == Result.Ok) return Result.Ok;
sample.Dispose();
sample = null;
return r;
}
}
public abstract class ChannelReply : Channel
{
}