From 388baa81bf13d0ef13f93c3172a716cd08fd87aa Mon Sep 17 00:00:00 2001 From: sanri Date: Sat, 17 Sep 2022 13:16:22 +0800 Subject: [PATCH 01/34] upgrade to Zenoh V0.6.0-dev.0 --- .gitignore | 3 +- Zenoh/Config.cs | 40 ++++ Zenoh/Net/Session.cs | 172 --------------- Zenoh/Net/Types.cs | 226 ------------------- Zenoh/Session.cs | 146 ++++++++++++ Zenoh/Subscriber.cs | 39 ++++ Zenoh/Types.cs | 71 ------ Zenoh/Zenoh.cs | 354 ++++++++++++++++++++++++++++-- Zenoh/Zenoh.csproj | 32 +-- examples/Zenoh.Net/README.md | 163 -------------- examples/Zenoh.Net/ZNInfo.cs | 97 -------- examples/Zenoh.Net/ZNInfo.csproj | 32 --- examples/Zenoh.Net/ZNSub.cs | 116 ---------- examples/Zenoh.Net/ZNSub.csproj | 32 --- examples/Zenoh.Net/ZNWrite.cs | 103 --------- examples/Zenoh.Net/ZNWrite.csproj | 32 --- examples/Zenoh/ZGet.cs | 41 ++++ examples/Zenoh/ZGet.csproj | 20 ++ examples/Zenoh/ZPut.cs | 41 ++++ examples/Zenoh/ZPut.csproj | 20 ++ examples/Zenoh/ZSub.cs | 67 ++++++ examples/Zenoh/ZSub.csproj | 20 ++ examples/zenoh-examples.sln | 92 ++++---- 23 files changed, 846 insertions(+), 1113 deletions(-) create mode 100644 Zenoh/Config.cs delete mode 100644 Zenoh/Net/Session.cs delete mode 100644 Zenoh/Net/Types.cs create mode 100644 Zenoh/Session.cs create mode 100644 Zenoh/Subscriber.cs delete mode 100644 Zenoh/Types.cs delete mode 100644 examples/Zenoh.Net/README.md delete mode 100644 examples/Zenoh.Net/ZNInfo.cs delete mode 100644 examples/Zenoh.Net/ZNInfo.csproj delete mode 100644 examples/Zenoh.Net/ZNSub.cs delete mode 100644 examples/Zenoh.Net/ZNSub.csproj delete mode 100644 examples/Zenoh.Net/ZNWrite.cs delete mode 100644 examples/Zenoh.Net/ZNWrite.csproj create mode 100644 examples/Zenoh/ZGet.cs create mode 100644 examples/Zenoh/ZGet.csproj create mode 100644 examples/Zenoh/ZPut.cs create mode 100644 examples/Zenoh/ZPut.csproj create mode 100644 examples/Zenoh/ZSub.cs create mode 100644 examples/Zenoh/ZSub.csproj diff --git a/.gitignore b/.gitignore index bc8c141..0a5c015 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ - +.idea obj bin +*.user diff --git a/Zenoh/Config.cs b/Zenoh/Config.cs new file mode 100644 index 0000000..8ced203 --- /dev/null +++ b/Zenoh/Config.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; + +namespace Zenoh +{ + public class Config + { + internal struct NativeType // z_owned_config_t + { + internal IntPtr p; + } + + internal NativeType native; + + public Config() + { + this.native = ZConfigDefault(); + } + + + [DllImport(Zenoh.DllName, EntryPoint = "z_config_default", CallingConvention = CallingConvention.Cdecl)] + internal static extern NativeType ZConfigDefault(); + + [DllImport(Zenoh.DllName, EntryPoint = "z_config_from_str", CallingConvention = CallingConvention.Cdecl)] + internal static extern NativeType ZConfigFromStr([MarshalAs(UnmanagedType.LPStr)] string str); + + [DllImport(Zenoh.DllName, EntryPoint = "z_config_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern bool ZConfigCheck(ref NativeType config); + + [DllImport(Zenoh.DllName, EntryPoint = "z_config_to_str", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZString ZConfigToStr(ref NativeType config); + + [DllImport(Zenoh.DllName, EntryPoint = "z_config_get", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZString ZConfigGet(ref NativeType config, ZString key); + + [DllImport(Zenoh.DllName, EntryPoint = "z_config_free", CallingConvention = CallingConvention.Cdecl)] + internal static extern void ZConfigFree(ref NativeType config); + } +} \ No newline at end of file diff --git a/Zenoh/Net/Session.cs b/Zenoh/Net/Session.cs deleted file mode 100644 index dc25109..0000000 --- a/Zenoh/Net/Session.cs +++ /dev/null @@ -1,172 +0,0 @@ -// -// Copyright (c) 2021 ADLINK Technology Inc. -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ADLINK zenoh team, -// - -using System; -using System.Runtime.InteropServices; -using System.Collections.Generic; -using System.Text; -using System.Threading; - -namespace Zenoh.Net -{ - - public class Session : IDisposable - { - private IntPtr /*zn_session_t*/ _nativePtr = IntPtr.Zero; - - // keep a reference to the SubscriberCallbackNative delegate to avoid garbage collection - private SubscriberCallbackNative _subscriberCallbackNative; - - // counter for subscriber handles generation - private Int32 _subscriberCounter = Int32.MinValue; - // map of declared subscribers - internal Dictionary Subscribers; - - private Session(IntPtr /*zn_session_t*/ nativeSession) - { - this._nativePtr = nativeSession; - this._subscriberCallbackNative = new SubscriberCallbackNative(SubscriberCallbackNativeImpl); - this.Subscribers = new Dictionary(); - } - - public void Dispose() => Dispose(true); - - protected virtual void Dispose(bool disposing) - { - if (_nativePtr != IntPtr.Zero) - { - // make sure each Subscriber is disposed before closing the Session - foreach (Subscriber s in Subscribers.Values) - { - s.Dispose(); - } - Subscribers.Clear(); - - ZnClose(_nativePtr); - _nativePtr = IntPtr.Zero; - } - } - - public static Session Open(Dictionary config) - { - // It's simpler to encode the config as a single string to pass it to Rust where is will be decoded - string configStr = ""; - foreach (KeyValuePair kvp in config) - { - configStr += kvp.Key + "=" + kvp.Value + ";"; - } - var props = Zenoh.ZnConfigFromStr(configStr); - - var nativeSession = ZnOpen(props); - // TODO: check errors... - return new Session(nativeSession); - } - - public Dictionary Info() - { - var zstr = ZnInfoAsStr(_nativePtr); - return ZTypes.ZStringToProperties(zstr); - } - - public ulong DeclareResource(ResKey reskey) - { - return ZnDeclareResource(_nativePtr, reskey._key); - } - - unsafe public void Write(ResKey reskey, byte[] payload) - { - fixed (byte* p = payload) - { - ZnWrite(_nativePtr, reskey._key, (IntPtr)p, (uint)payload.Length); - } - } - - unsafe public void Write(ResKey reskey, byte[] payload, uint encoding, uint kind, CongestionControl congestionControl) - { - fixed (byte* p = payload) - { - ZnWriteExt(_nativePtr, reskey._key, (IntPtr)p, (uint)payload.Length, encoding, kind, congestionControl); - } - } - - internal void SubscriberCallbackNativeImpl(IntPtr /* *const zn_sample_t */ samplePtr, IntPtr /* *const c_void */ callBackPtr) - { - Sample s = new Sample(samplePtr); - try - { - Int32 subscriberHandle = callBackPtr.ToInt32(); - Subscriber subscriber = Subscribers[subscriberHandle]; - subscriber.UserCallback(s); - } - catch (OverflowException) - { - Console.WriteLine("Internal error: invalid subscriberHandle received in Subscriber callback: {0}", callBackPtr); - } - catch (KeyNotFoundException) - { - // The subscriber has been unregistered - } - } - - public Subscriber DeclareSubscriber(ResKey reskey, SubInfo subInfo, SubscriberCallback callback) - { - Int32 subscriberHandle = Interlocked.Increment(ref _subscriberCounter); - var nativeSubscriber = ZnDeclareSubscriber(_nativePtr, reskey._key, subInfo._subInfo, _subscriberCallbackNative, new IntPtr(subscriberHandle)); - var subscriber = new Subscriber(this, subscriberHandle, nativeSubscriber, callback); - Subscribers[subscriberHandle] = subscriber; - return subscriber; - } - - [DllImport("zenohc", EntryPoint = "zn_open")] - internal static extern IntPtr /*zn_session_t*/ ZnOpen(IntPtr /*zn_properties_t*/ config); - - [DllImport("zenohc", EntryPoint = "zn_info_as_str")] - internal static extern ZString ZnInfoAsStr(IntPtr /*zn_session_t*/ rustSession); - - [DllImport("zenohc", EntryPoint = "zn_close")] - internal static extern void ZnClose(IntPtr /*zn_session_t*/ rustSession); - - [DllImport("zenohc", EntryPoint = "zn_declare_resource")] - internal static extern ulong ZnDeclareResource(IntPtr /*zn_session_t*/ rustSession, ResKey.NativeType zResKey); - - [DllImport("zenohc", EntryPoint = "zn_write")] - internal static extern int ZnWrite( - IntPtr /*zn_session_t*/ rustSession, - ResKey.NativeType zResKey, - IntPtr payload, - uint len); - - [DllImport("zenohc", EntryPoint = "zn_write_ext")] - internal static extern int ZnWriteExt( - IntPtr /*zn_session_t*/ rustSession, - ResKey.NativeType zResKey, - IntPtr payload, - uint len, - uint encoding, - uint kind, - CongestionControl congestion); - - [DllImport("zenohc", EntryPoint = "zn_declare_subscriber")] - internal static extern IntPtr /*zn_subscriber_t*/ ZnDeclareSubscriber( - IntPtr /*zn_session_t*/ rustSession, - ResKey.NativeType zResKey, - SubInfo.NativeType zSubInfo, - SubscriberCallbackNative callback, - IntPtr /*void* */ arg); - - [DllImport("zenohc", EntryPoint = "zn_undeclare_subscriber")] - internal static extern void ZnUndeclareSubscriber(IntPtr /*zn_subscriber_t*/ sub); - - } -} \ No newline at end of file diff --git a/Zenoh/Net/Types.cs b/Zenoh/Net/Types.cs deleted file mode 100644 index d26dd8c..0000000 --- a/Zenoh/Net/Types.cs +++ /dev/null @@ -1,226 +0,0 @@ -// -// Copyright (c) 2021 ADLINK Technology Inc. -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ADLINK zenoh team, -// -using System; -using System.Runtime.InteropServices; - - -namespace Zenoh.Net -{ - public class ResKey - { - [StructLayout(LayoutKind.Sequential)] - internal struct NativeType // zn_reskey_t - { - internal ulong id; // c_ulong - internal IntPtr suffix; // *const c_char - } - - internal NativeType _key; - - public ulong Id - { - get - { - return _key.id; - } - } - - public string Suffix - { - get - { - return Marshal.PtrToStringAnsi(_key.suffix); - } - } - - internal ResKey(NativeType resKey) - { - this._key = resKey; - } - - public static ResKey RId(ulong id) - { - NativeType resKey; - resKey.id = id; - resKey.suffix = IntPtr.Zero; - return new ResKey(resKey); - } - - public static ResKey RName(string suffix) - { - NativeType resKey; - resKey.id = 0; - resKey.suffix = Marshal.StringToHGlobalAnsi(suffix); - return new ResKey(resKey); - } - - public static ResKey RIdWithSuffix(ulong id, string suffix) - { - NativeType resKey; - resKey.id = id; - resKey.suffix = Marshal.StringToHGlobalAnsi(suffix); - return new ResKey(resKey); - } - - public bool IsNumerical() - { - return _key.suffix == IntPtr.Zero; - } - } - - public enum CongestionControl - { - BLOCK, - DROP - } - - public class Sample - { - [StructLayout(LayoutKind.Sequential)] - internal struct NativeType // zn_sample_t - { - internal ZString key; // z_string_t - internal ZBytes value; // z_bytes_t - } - - public string ResName - { get; } - public byte[] Payload - { get; } - - unsafe internal Sample(IntPtr /* *const zn_sample_t */ _sample) - { - // Note: copies are made here. Could we avoid this ? - NativeType* s = (NativeType*)_sample; - ResName = ZTypes.ZStringToString(s->key); - Payload = ZTypes.ZBytesToBytesArray(s->value); - } - - internal Sample(NativeType _sample) - { - // Note: copies are made here. Could we avoid this ? - ResName = ZTypes.ZStringToString(_sample.key); - Payload = ZTypes.ZBytesToBytesArray(_sample.value); - ZnSampleFree(_sample); - } - - [DllImport("zenohc", EntryPoint = "zn_sample_free")] - internal static extern void ZnSampleFree(NativeType sample); - } - - public enum Reliability - { - BEST_EFFORT, - RELIABLE, - } - - public enum SubMode - { - PUSH, - PULL, - } - - [StructLayout(LayoutKind.Sequential)] - public struct Period // zn_period_t - { - public uint origin; // c_uint - public uint period; // c_uint - public uint duration; // c_uint - - public Period(uint origin, uint period, uint duration) - { - this.origin = origin; - this.period = period; - this.duration = duration; - } - - public static Period None() - { - return new Period(0, 0, 0); - } - - public override string ToString() - { - return String.Format("origin={0}, period={1}, duration={2}", origin, period, duration); - } - } - - - public class SubInfo - { - [StructLayout(LayoutKind.Sequential)] - public struct NativeType // zn_subinfo_t - { - public Reliability reliability; // zn_reliability_t - public SubMode mode; // zn_submode_t - public IntPtr period; // *mut zn_period_t - } - - public NativeType _subInfo; - - public SubInfo() - { - this._subInfo = ZnSubInfoDefault(); - } - - public SubInfo(SubMode mode) : this() - { - _subInfo.mode = mode; - } - - // TODO: add period param - public SubInfo(Reliability reliability, SubMode mode) : this() - { - _subInfo.reliability = reliability; - _subInfo.mode = mode; - } - - [DllImport("zenohc", EntryPoint = "zn_subinfo_default")] - internal static extern NativeType ZnSubInfoDefault(); - } - - // SubscriberCallback to be implemented by user. - public delegate void SubscriberCallback(Sample sample); - - // Type of the callback function expected by zenoh-c - internal delegate void SubscriberCallbackNative(IntPtr /* *const zn_sample_t */ samplePtr, IntPtr /* *const c_void */ callBackPtr); - - public class Subscriber - { - private Session _session; - private Int32 _subscriberHandle; - private IntPtr /*zn_subscriber_t*/ _nativePtr = IntPtr.Zero; - internal SubscriberCallback UserCallback; - - internal Subscriber(Session session, Int32 subscriberHandle, IntPtr nativeSubscriber, SubscriberCallback userCallback) - { - this._session = session; - this._subscriberHandle = subscriberHandle; - this._nativePtr = nativeSubscriber; - this.UserCallback = userCallback; - } - - public void Dispose() => Dispose(true); - - protected virtual void Dispose(bool disposing) - { - if (_nativePtr != IntPtr.Zero) - { - Session.ZnUndeclareSubscriber(_nativePtr); - _session.Subscribers.Remove(_subscriberHandle); - _nativePtr = IntPtr.Zero; - } - } - } -} \ No newline at end of file diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs new file mode 100644 index 0000000..be829cc --- /dev/null +++ b/Zenoh/Session.cs @@ -0,0 +1,146 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; + +namespace Zenoh +{ + public class Session : IDisposable + { + [StructLayout(LayoutKind.Sequential)] + internal struct NativeType // z_owned_session_t + { + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] + public IntPtr[] p; + } + + internal NativeType native; + internal Dictionary Subscribers; + + public Session() + { + this.native = new NativeType(); + this.Subscribers = new Dictionary(); + } + + public void Dispose() => Dispose(true); + + protected virtual void Dispose(bool disposing) + { + if (native.p[0] != IntPtr.Zero) + { + foreach (Subscriber s in Subscribers.Values) + { + // free + } + + Subscribers.Clear(); + + ZClose(ref native); + for (int i = 0; i < 3; i++) + { + native.p[i] = IntPtr.Zero; + } + } + } + + public bool Open(Config config) + { + NativeType s = ZOpen(ref config.native); + if (ZSessionCheck(ref s)) + { + this.native = s; + return true; + } + else + { + return false; + } + } + + public void Close() + { + ZClose(ref native); + } + + public bool Put(KeyExpr key, string value) + { + IntPtr v = Marshal.StringToHGlobalAnsi(value); + int r = ZPut(ref native, key.native, v, (ulong)value.Length); + Marshal.FreeHGlobal(v); + if (r == 0) + { + return true; + } + else + { + return false; + } + } + + public ReplayDataArray Get(KeyExpr key) + { + QueryTarget target = new QueryTarget(); + target.Target = ZTarget.All; + QueryConsolidation consolidation = new QueryConsolidation(); + string v = "abc"; + IntPtr predicate = Marshal.StringToHGlobalAnsi(v); + ReplayDataArray.NativeType nativeData = + ZGetCollect(ref native, key.native, predicate, target.native, consolidation.native); + Marshal.FreeHGlobal(predicate); + return new ReplayDataArray(nativeData); + } + + public bool RegisterSubscriber(SubInfo subInfo, Subscriber subscriber) + { + Subscriber.NativeType nativeSubscriber = ZSubscribe(ref native, subscriber.key.native, subInfo.native, + subscriber.nativeCallback, + IntPtr.Zero); + if (ZSubscriberCheck(ref nativeSubscriber)) + { + subscriber.nativeSubscriber = nativeSubscriber; + Subscribers[subscriber.key.Suffix] = subscriber; + return true; + } + else + { + return false; + } + } + + public void UnregisterSubscriber(string key) + { + Subscriber subscriber = Subscribers[key]; + // 目前执行这句会出问题, 还在解决中 + // zSubscriberClose(ref subscriber.nativeSubscriber); + Subscribers.Remove(key); + } + + [DllImport(Zenoh.DllName, EntryPoint = "z_open", CallingConvention = CallingConvention.Cdecl)] + internal static extern NativeType ZOpen(ref Config.NativeType config); + + [DllImport(Zenoh.DllName, EntryPoint = "z_session_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern bool ZSessionCheck(ref NativeType session); + + [DllImport(Zenoh.DllName, EntryPoint = "z_close", CallingConvention = CallingConvention.Cdecl)] + internal static extern void ZClose(ref NativeType session); + + [DllImport(Zenoh.DllName, EntryPoint = "z_put", CallingConvention = CallingConvention.Cdecl)] + internal static extern int ZPut(ref NativeType session, KeyExpr.NativeType keyexpr, IntPtr payload, ulong len); + + [DllImport(Zenoh.DllName, EntryPoint = "z_subscriber_check")] + internal static extern bool ZSubscriberCheck(ref Subscriber.NativeType sub); + + [DllImport(Zenoh.DllName, EntryPoint = "z_subscribe")] + internal static extern Subscriber.NativeType ZSubscribe(ref NativeType session, KeyExpr.NativeType keyexpr, + SubInfo.NativeType sub_info, + SubscriberCallbackNative callback, IntPtr arg); + + [DllImport(Zenoh.DllName, EntryPoint = "z_subscriber_close")] + internal static extern void ZSubscriberClose(ref Subscriber.NativeType sub); + + [DllImport(Zenoh.DllName, EntryPoint = "z_get_collect", CallingConvention = CallingConvention.Cdecl)] + internal static extern ReplayDataArray.NativeType ZGetCollect(ref NativeType session, + KeyExpr.NativeType keyexpr, IntPtr predicate, QueryTarget.NativeType target, + QueryConsolidation.NativeType consolidation); + } +} \ No newline at end of file diff --git a/Zenoh/Subscriber.cs b/Zenoh/Subscriber.cs new file mode 100644 index 0000000..96f1394 --- /dev/null +++ b/Zenoh/Subscriber.cs @@ -0,0 +1,39 @@ +using System; +using System.Runtime.InteropServices; + +namespace Zenoh +{ + public delegate void SubscriberCallback(Sample sample); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void + SubscriberCallbackNative(ref Sample.NativeType samplePtr, IntPtr arg); + + public class Subscriber + { + [StructLayout(LayoutKind.Sequential)] + public struct NativeType // z_owned_subscriber_t + { + public IntPtr p; + } + + internal KeyExpr key; + internal NativeType nativeSubscriber; + internal SubscriberCallback userCallback; + internal SubscriberCallbackNative nativeCallback; + + public Subscriber(string key, SubscriberCallback userCallback) + { + this.key = KeyExpr.FromString(key); + this.nativeSubscriber.p = IntPtr.Zero; + this.userCallback = userCallback; + this.nativeCallback = new SubscriberCallbackNative(SubscriberNativeCallbackImpl); + } + + internal void SubscriberNativeCallbackImpl(ref Sample.NativeType samplePtr, IntPtr arg) + { + Sample s = new Sample(samplePtr); + this.userCallback(s); + } + } +} \ No newline at end of file diff --git a/Zenoh/Types.cs b/Zenoh/Types.cs deleted file mode 100644 index 2eebb44..0000000 --- a/Zenoh/Types.cs +++ /dev/null @@ -1,71 +0,0 @@ -// -// Copyright (c) 2021 ADLINK Technology Inc. -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ADLINK zenoh team, -// -using System; -using System.Runtime.InteropServices; -using System.Collections.Generic; -using System.Linq; - - -namespace Zenoh -{ - [StructLayout(LayoutKind.Sequential)] - internal struct ZString - { - public IntPtr val; // *const c_char - public IntPtr len; // size_t - } - - [StructLayout(LayoutKind.Sequential)] - internal struct ZBytes - { - public IntPtr val; // *const u8 - public IntPtr len; // size_t - } - - internal static class ZTypes - { - internal static string ZStringToString(ZString zs) - { - byte[] managedArray = new byte[(int)zs.len]; - System.Runtime.InteropServices.Marshal.Copy(zs.val, managedArray, 0, (int)zs.len); - string result = System.Text.Encoding.UTF8.GetString(managedArray, 0, (int)zs.len); - // TODO Free ZString ??? - return result; - } - - internal static byte[] ZBytesToBytesArray(ZBytes zb) - { - byte[] managedArray = new byte[(int)zb.len]; - System.Runtime.InteropServices.Marshal.Copy(zb.val, managedArray, 0, (int)zb.len); - // TODO Free ZBytes ??? - return managedArray; - } - - private static char[] _propSeparator = { ';' }; - private static char[] _kvSeparator = { '=' }; - - internal static Dictionary ZStringToProperties(ZString zs) - { - var str = ZTypes.ZStringToString(zs); - - // Parse the properties from the string - var properties = str.Split(_propSeparator, StringSplitOptions.RemoveEmptyEntries) - .Select(x => x.Split(_kvSeparator, 2)) - .ToDictionary(x => x.First(), x => (x.Length == 2) ? x.Last() : ""); - return properties; - } - - } - -} \ No newline at end of file diff --git a/Zenoh/Zenoh.cs b/Zenoh/Zenoh.cs index 9b6d493..a0d0dd4 100644 --- a/Zenoh/Zenoh.cs +++ b/Zenoh/Zenoh.cs @@ -11,6 +11,7 @@ // Contributors: // ADLINK zenoh team, // + using System; using System.Runtime.InteropServices; using System.Collections.Generic; @@ -18,31 +19,356 @@ namespace Zenoh { - public class Zenoh + public static class Zenoh { + public const string DllName = "zenohc"; - [DllImport("zenohc", EntryPoint = "z_init_logger")] + [DllImport(Zenoh.DllName, EntryPoint = "z_init_logger")] public static extern void InitLogger(); + } + + public enum KnownEncoding : int // e_known_encoding_t + { + Empty = 0, + AppOctetStream = 1, + AppCustom = 2, + TextPlain = 3, + AppProperties = 4, + AppJson = 5, + AppSql = 6, + AppInteger = 7, + AppFloat = 8, + AppXml = 9, + AppXhtmlXml = 10, + AppXWwwFormUrlencoded = 11, + TextJson = 12, + TextHtml = 13, + TextXml = 14, + TextCss = 15, + TextCsv = 16, + TextJavascript = 17, + ImageJpeg = 18, + ImagePng = 19, + ImageGif = 20, + } + + public enum ConsolidationMode : int // z_consolidation_mode_t + { + Full, + Lazy, + None, + } + + public enum Reliability : int // z_reliability_t + { + BestEffort, + Reliable, + } + + public enum SubMode : int // z_submode_t + { + Push, + Pull, + } + + public enum ZTarget : int // z_target_t + { + BestMatching, + All, + None, + AllComplete, + } + + public enum QueryConsolidationTag // z_query_consolidation_t_Tag + { + Auto, + Manual, + } + + [StructLayout(LayoutKind.Sequential)] + public struct ZBytes // z_bytes_t, z_owned_bytes_t + { + public IntPtr start; + public ulong len; + } + + [StructLayout(LayoutKind.Sequential)] + public struct ZString // z_owned_string_t, z_string_t + { + internal IntPtr start; + } + + [StructLayout(LayoutKind.Sequential)] + public struct ZEncoding // z_encoding_t + { + internal KnownEncoding prefix; + internal ZBytes suffix; + + public String PrefixToString() + { + return prefix.ToString(); + } + } + + [StructLayout(LayoutKind.Sequential)] + public struct Period // z_period_t + { + public uint origin; + public uint period; + public uint duration; + + public Period(uint origin, uint period, uint duration) + { + this.origin = origin; + this.period = period; + this.duration = duration; + } + } + + + public class KeyExpr : IDisposable + { + [StructLayout(LayoutKind.Sequential)] + public struct NativeType // z_keyexpr_t + { + internal ulong id; + internal ZBytes suffix; + } + + internal NativeType native; - private static char[] _propSeparator = { ';' }; - private static char[] _kvSeparator = { '=' }; + public ulong Id + { + get { return native.id; } + } - public static Dictionary ConfigFromFile(string path) + public string Suffix { - var zprops = ZnConfigFromFile(path); - var zstr = ZnConfigToStr(zprops); - return ZTypes.ZStringToProperties(zstr); + get { return Marshal.PtrToStringAnsi(native.suffix.start, (int)native.suffix.len); } } - [DllImport("zenohc", EntryPoint = "zn_config_from_str", CharSet = CharSet.Ansi)] - internal static extern IntPtr /*zn_properties_t*/ ZnConfigFromStr([MarshalAs(UnmanagedType.LPStr)] string str); + internal KeyExpr(NativeType native) + { + this.native = native; + } - [DllImport("zenohc", EntryPoint = "zn_config_to_str", CharSet = CharSet.Ansi)] - internal static extern ZString ZnConfigToStr(IntPtr /*zn_properties_t*/ znProps); + public static KeyExpr FromString(string name) + { + IntPtr p = Marshal.StringToHGlobalAnsi(name); + NativeType key = ZExprNew(p); + Marshal.FreeHGlobal(p); + return new KeyExpr(key); + } + + public void Dispose() + { + ZKeyexprFree(ref native); + } - [DllImport("zenohc", EntryPoint = "zn_config_from_file", CharSet = CharSet.Ansi)] - internal static extern IntPtr /*zn_properties_t*/ ZnConfigFromFile([MarshalAs(UnmanagedType.LPStr)] string str); + [DllImport(Zenoh.DllName, EntryPoint = "z_expr_new", CallingConvention = CallingConvention.Cdecl)] + internal static extern NativeType ZExprNew(IntPtr name); + [DllImport(Zenoh.DllName, EntryPoint = "z_keyexpr_free", CallingConvention = CallingConvention.Cdecl)] + internal static extern void ZKeyexprFree(ref NativeType key); + } + + public class QueryTarget + { + [StructLayout(LayoutKind.Sequential)] + public struct NativeType // z_query_target_t + { + internal uint kind; + internal ZTarget target; + } + + internal NativeType native; + + public ZTarget Target + { + get { return native.target; } + set { native.target = value; } + } + + public QueryTarget() + { + this.native = ZQueryTargetDefault(); + } + + [DllImport(Zenoh.DllName, EntryPoint = "z_query_target_default", CallingConvention = CallingConvention.Cdecl)] + internal static extern NativeType ZQueryTargetDefault(); + } + + + [StructLayout(LayoutKind.Sequential)] + public struct ConsolidationStrategy // z_consolidation_strategy_t + { + public ConsolidationMode firstRouters; + public ConsolidationMode lastRouters; + public ConsolidationMode reception; } + public class QueryConsolidation + { + [StructLayout(LayoutKind.Sequential)] + public struct NativeType // z_query_consolidation_t + { + public QueryConsolidationTag tag; + public ConsolidationStrategy manual; + } + + internal NativeType native; + + public QueryConsolidation() + { + this.native = ZQueryConsolidationDefault(); + } + + [DllImport(Zenoh.DllName, EntryPoint = "z_query_consolidation_default", + CallingConvention = CallingConvention.Cdecl)] + internal static extern QueryConsolidation.NativeType ZQueryConsolidationDefault(); + } + + public class Sample + { + [StructLayout(LayoutKind.Explicit)] + public struct NativeType // z_owned_sample_t + { + [FieldOffset(0)] internal KeyExpr.NativeType key; + [FieldOffset(24)] internal ZBytes value; + [FieldOffset(48)] internal ZEncoding encoding; + } + + public string Key { get; } + public byte[] Value { get; } + public ZEncoding Encoding { get; } + + internal Sample(NativeType sample) + { + Key = Marshal.PtrToStringAnsi(sample.key.suffix.start, (int)sample.key.suffix.len); + Value = ZTypes.ZBytesToBytesArray(sample.value); + Encoding = sample.encoding; + } + + public string ValueToString() + { + string result = System.Text.Encoding.UTF8.GetString(Value, 0, Value.Length); + return result; + } + + [DllImport(Zenoh.DllName, EntryPoint = "z_sample_free", CallingConvention = CallingConvention.Cdecl)] + internal static extern void ZSampleFree(ref NativeType sample); + } + + public class ReplayData + { + [StructLayout(LayoutKind.Sequential)] + public struct NativeType // z_owned_reply_data_t + { + internal Sample.NativeType sample; + internal uint sourceKind; + internal ZBytes replierId; + } + + public Sample Sample { get; } + public uint SourceKind { get; } + public byte[] ReplierID { get; } + + public ReplayData(NativeType native) + { + this.Sample = new Sample(native.sample); + this.SourceKind = native.sourceKind; + this.ReplierID = ZTypes.ZBytesToBytesArray(native.replierId); + } + } + + public class ReplayDataArray + { + [StructLayout(LayoutKind.Sequential)] + public struct NativeType // z_owned_reply_data_array_t + { + internal IntPtr val; + internal ulong len; + } + + public List List; + + public ReplayDataArray(NativeType native) + { + List = new List(); + if (!ZReplyDataArrayCheck(ref native)) + { + return; + } + + unsafe + { + for (int i = 0; i < (int)native.len; i++) + { + ReplayData.NativeType* p = (ReplayData.NativeType*)native.val + + i * sizeof(ReplayData.NativeType); + ReplayData ele = new ReplayData(*p); + List.Add(ele); + } + } + + ZReplyDataArrayFree(ref native); + } + + [DllImport(Zenoh.DllName, EntryPoint = "z_reply_data_array_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern bool ZReplyDataArrayCheck(ref NativeType replies); + + [DllImport(Zenoh.DllName, EntryPoint = "z_reply_data_array_free", CallingConvention = CallingConvention.Cdecl)] + internal static extern void ZReplyDataArrayFree(ref NativeType replies); + } + + public class SubInfo + { + [StructLayout(LayoutKind.Sequential)] + public struct NativeType // z_subinfo_t + { + public Reliability reliability; + public SubMode mode; + public Period period; + } + + internal NativeType native; + + public SubInfo() + { + this.native = ZSubInfoDefault(); + } + + public SubInfo(SubMode mode) : this() + { + this.native.mode = mode; + } + + public SubInfo(Reliability reliability, SubMode mode) : this() + { + native.reliability = reliability; + native.mode = mode; + } + + [DllImport(Zenoh.DllName, EntryPoint = "z_subinfo_default", CallingConvention = CallingConvention.Cdecl)] + internal static extern NativeType ZSubInfoDefault(); + } + + + internal static class ZTypes + { + internal static byte[] ZBytesToBytesArray(ZBytes zb) + { + byte[] managedArray = new byte[(int)zb.len]; + System.Runtime.InteropServices.Marshal.Copy(zb.start, managedArray, 0, (int)zb.len); + return managedArray; + } + + internal static string ZBytesToString(ZBytes zb) + { + byte[] managedArray = new byte[(int)zb.len]; + System.Runtime.InteropServices.Marshal.Copy(zb.start, managedArray, 0, (int)zb.len); + string result = System.Text.Encoding.UTF8.GetString(managedArray, 0, (int)zb.len); + return result; + } + } } \ No newline at end of file diff --git a/Zenoh/Zenoh.csproj b/Zenoh/Zenoh.csproj index 9ed8c9b..8df4b70 100644 --- a/Zenoh/Zenoh.csproj +++ b/Zenoh/Zenoh.csproj @@ -12,22 +12,24 @@ ADLINK zenoh team, --> - - Zenoh - Zenoh - 0.0.4 - Julien Enoch - EPL-2.0 OR Apache-2.0 - Zenoh: Zero Overhead Pub/sub, Store/Query and Compute. - false + + Zenoh + Zenoh + 0.0.4 + Julien Enoch + EPL-2.0 OR Apache-2.0 + Zenoh: Zero Overhead Pub/sub, Store/Query and Compute. + false - https://github.com/eclipse-zenoh/zenoh-csharp - https://github.com/eclipse-zenoh/zenoh-csharp - git + https://github.com/eclipse-zenoh/zenoh-csharp + https://github.com/eclipse-zenoh/zenoh-csharp + git - Library - netstandard2.0 + Library + net6.0 - true - + true + + 10 + diff --git a/examples/Zenoh.Net/README.md b/examples/Zenoh.Net/README.md deleted file mode 100644 index 0d1dbb0..0000000 --- a/examples/Zenoh.Net/README.md +++ /dev/null @@ -1,163 +0,0 @@ -# Zenoh-net C# examples - -## Start instructions - - ```bash - dotnet run -p - ``` - - Each example accepts the `-h` or `--help` option that provides a description of its arguments and their default values. - - :warning: _To pass any options to an example, specify them after the `--`argument. For instance:_ - - ```bash - dotnet run -p ZNInfo.csproj -- -h - ``` - - If you run the tests against the zenoh router running in a Docker container, you need to add the - `-e tcp/localhost:7447` option to your examples. That's because Docker doesn't support UDP multicast - transport, and therefore the zenoh scouting and discrovery mechanism cannot work with. - -## Examples description - - - -### ZNInfo - - Gets information about the zenoh-net session. - - Typical usage: - ```bash - dotnet run -p ZNInfo.csproj - ``` - - -### ZNWrite - - Writes a path/value into Zenoh. - The path/value will be received by all matching subscribers, for instance the [ZNSub](#ZNSub) - and [ZNStorage](#ZNStorage) examples. - - Typical usage: - ```bash - dotnet run -p ZNWrite.csproj - ``` - or - ```bash - dotnet run -p ZNWrite.csproj -- -p /demo/example/test -v 'Hello World' - ``` - - - -### ZNSub - - Registers a subscriber with a selector. - The subscriber will be notified of each write made on any path matching the selector, - and will print this notification. - - Typical usage: - ```bash - dotnet run -p ZNSub.csproj - ``` - or - ```bash - dotnet run -p ZNSub.csproj -- -s /demo/** - ``` - - - - - - - - - - \ No newline at end of file diff --git a/examples/Zenoh.Net/ZNInfo.cs b/examples/Zenoh.Net/ZNInfo.cs deleted file mode 100644 index f78a365..0000000 --- a/examples/Zenoh.Net/ZNInfo.cs +++ /dev/null @@ -1,97 +0,0 @@ -// -// Copyright (c) 2021 ADLINK Technology Inc. -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ADLINK zenoh team, -// -using System; -using System.Diagnostics; -using System.Collections.Generic; -using Zenoh; -using PowerArgs; - -class ZNInfo -{ - static void Main(string[] args) - { - try - { - // initiate logging - Zenoh.Zenoh.InitLogger(); - - // arguments parsing - var arguments = Args.Parse(args); - if (arguments == null) return; - Dictionary conf = arguments.GetConf(); - - Console.WriteLine("Openning session.."); - var s = Zenoh.Net.Session.Open(conf); - - var props = s.Info(); - foreach (KeyValuePair entry in props) - { - Console.WriteLine("{0} : {1}", entry.Key, entry.Value); - } - - s.Dispose(); - } - catch (ArgException) - { - Console.WriteLine(ArgUsage.GenerateUsageFromTemplate()); - } - } -} - - -[ArgExceptionBehavior(ArgExceptionPolicy.StandardExceptionHandling)] -public class ExampleArgs -{ - [HelpHook, ArgShortcut("h"), ArgDescription("Shows this help")] - public Boolean help { get; set; } - - [ArgShortcut("m"), ArgDescription("The zenoh session mode (peer by default). Possible values [peer|client].")] - public string mode { get; set; } - - [ArgShortcut("e"), ArgDescription("Peer locators used to initiate the zenoh session.")] - public string peer { get; set; } - - [ArgShortcut("l"), ArgDescription("Locators to listen on.")] - public string listener { get; set; } - - [ArgShortcut("c"), ArgDescription("A configuration file.")] - public string config { get; set; } - - public Dictionary GetConf() - { - Dictionary conf; - if (this.config != null) - { - conf = Zenoh.Zenoh.ConfigFromFile(this.config); - } - else - { - conf = new Dictionary(); - } - - if (this.mode != null) - { - conf["mode"] = this.mode; - } - if (this.peer != null) - { - conf["peer"] = this.peer; - } - if (this.listener != null) - { - conf["listener"] = this.listener; - } - return conf; - } -} diff --git a/examples/Zenoh.Net/ZNInfo.csproj b/examples/Zenoh.Net/ZNInfo.csproj deleted file mode 100644 index 3ed3881..0000000 --- a/examples/Zenoh.Net/ZNInfo.csproj +++ /dev/null @@ -1,32 +0,0 @@ - - - - ZNInfo - ZNInfo - Exe - net5.0 - false - - - - - - - - - - - - diff --git a/examples/Zenoh.Net/ZNSub.cs b/examples/Zenoh.Net/ZNSub.cs deleted file mode 100644 index da0955d..0000000 --- a/examples/Zenoh.Net/ZNSub.cs +++ /dev/null @@ -1,116 +0,0 @@ -// -// Copyright (c) 2021 ADLINK Technology Inc. -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ADLINK zenoh team, -// -using System; -using System.Diagnostics; -using System.Collections.Generic; -using System.Text; -using Zenoh; -using PowerArgs; - -class ZNSub -{ - - public static void SubscriberCallback(Zenoh.Net.Sample sample) - { - Console.WriteLine(">> [Subscription listener] Received ('{0}': '{1}')", - sample.ResName, - BitConverter.ToString(sample.Payload)); - - // Encoding.UTF8.GetString(sample.Payload)); - } - - static void Main(string[] args) - { - try - { - // initiate logging - Zenoh.Zenoh.InitLogger(); - - // arguments parsing - var arguments = Args.Parse(args); - if (arguments == null) return; - Dictionary conf = arguments.GetConf(); - - Console.WriteLine("Openning session..."); - var s = Zenoh.Net.Session.Open(conf); - - Console.WriteLine("Declaring Subscriber on '{0}'...", arguments.selector); - var rkey = Zenoh.Net.ResKey.RName(arguments.selector); - var subInfo = new Zenoh.Net.SubInfo(); - s.DeclareSubscriber(rkey, subInfo, ZNSub.SubscriberCallback); - - char c = ' '; - while (c != 'q') - { - c = Console.ReadKey().KeyChar; - } - - s.Dispose(); - } - catch (ArgException) - { - Console.WriteLine(ArgUsage.GenerateUsageFromTemplate()); - } - } -} - - -[ArgExceptionBehavior(ArgExceptionPolicy.StandardExceptionHandling)] -public class ExampleArgs -{ - [HelpHook, ArgShortcut("h"), ArgDescription("Shows this help")] - public Boolean help { get; set; } - - [ArgShortcut("m"), ArgDefaultValue("peer"), ArgDescription("The zenoh session mode. Possible values [peer|client].")] - public string mode { get; set; } - - [ArgShortcut("e"), ArgDescription("Peer locators used to initiate the zenoh session.")] - public string peer { get; set; } - - [ArgShortcut("l"), ArgDescription("Locators to listen on.")] - public string listener { get; set; } - - [ArgShortcut("c"), ArgDescription("A configuration file.")] - public string config { get; set; } - - [ArgShortcut("s"), ArgDefaultValue("/demo/example/**"), ArgDescription("The selection of resources to subscribe.")] - public string selector { get; set; } - - public Dictionary GetConf() - { - Dictionary conf; - if (this.config != null) - { - conf = Zenoh.Zenoh.ConfigFromFile(this.config); - } - else - { - conf = new Dictionary(); - } - - if (this.mode != null) - { - conf["mode"] = this.mode; - } - if (this.peer != null) - { - conf["peer"] = this.peer; - } - if (this.listener != null) - { - conf["listener"] = this.listener; - } - return conf; - } -} diff --git a/examples/Zenoh.Net/ZNSub.csproj b/examples/Zenoh.Net/ZNSub.csproj deleted file mode 100644 index 0d08792..0000000 --- a/examples/Zenoh.Net/ZNSub.csproj +++ /dev/null @@ -1,32 +0,0 @@ - - - - ZNSub - ZNSub - Exe - net5.0 - false - - - - - - - - - - - - diff --git a/examples/Zenoh.Net/ZNWrite.cs b/examples/Zenoh.Net/ZNWrite.cs deleted file mode 100644 index 603df50..0000000 --- a/examples/Zenoh.Net/ZNWrite.cs +++ /dev/null @@ -1,103 +0,0 @@ -// -// Copyright (c) 2021 ADLINK Technology Inc. -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ADLINK zenoh team, -// -using System; -using System.Diagnostics; -using System.Collections.Generic; -using System.Text; -using Zenoh; -using PowerArgs; - -class ZNWrite -{ - static void Main(string[] args) - { - try - { - // initiate logging - Zenoh.Zenoh.InitLogger(); - - // arguments parsing - var arguments = Args.Parse(args); - if (arguments == null) return; - Dictionary conf = arguments.GetConf(); - - Console.WriteLine("Openning session..."); - var s = Zenoh.Net.Session.Open(conf); - - var rkey = Zenoh.Net.ResKey.RName(arguments.path); - - Console.WriteLine("Writing Data ({0}, {1})...", rkey, arguments.value); - s.Write(rkey, Encoding.UTF8.GetBytes(arguments.value)); - - s.Dispose(); - } - catch (ArgException) - { - Console.WriteLine(ArgUsage.GenerateUsageFromTemplate()); - } - } -} - - -[ArgExceptionBehavior(ArgExceptionPolicy.StandardExceptionHandling)] -public class ExampleArgs -{ - [HelpHook, ArgShortcut("h"), ArgDescription("Shows this help")] - public Boolean help { get; set; } - - [ArgShortcut("m"), ArgDefaultValue("peer"), ArgDescription("The zenoh session mode. Possible values [peer|client].")] - public string mode { get; set; } - - [ArgShortcut("e"), ArgDescription("Peer locators used to initiate the zenoh session.")] - public string peer { get; set; } - - [ArgShortcut("l"), ArgDescription("Locators to listen on.")] - public string listener { get; set; } - - [ArgShortcut("c"), ArgDescription("A configuration file.")] - public string config { get; set; } - - [ArgShortcut("p"), ArgDefaultValue("/demo/example/zenoh-csharp-write"), ArgDescription("The name of the resource to write.")] - public string path { get; set; } - - [ArgShortcut("v"), ArgDefaultValue("Write from C#!"), ArgDescription("The value of the resource to write.")] - public string value { get; set; } - - public Dictionary GetConf() - { - Dictionary conf; - if (this.config != null) - { - conf = Zenoh.Zenoh.ConfigFromFile(this.config); - } - else - { - conf = new Dictionary(); - } - - if (this.mode != null) - { - conf["mode"] = this.mode; - } - if (this.peer != null) - { - conf["peer"] = this.peer; - } - if (this.listener != null) - { - conf["listener"] = this.listener; - } - return conf; - } -} diff --git a/examples/Zenoh.Net/ZNWrite.csproj b/examples/Zenoh.Net/ZNWrite.csproj deleted file mode 100644 index 11ea7c1..0000000 --- a/examples/Zenoh.Net/ZNWrite.csproj +++ /dev/null @@ -1,32 +0,0 @@ - - - - ZNWrite - ZNWrite - Exe - net5.0 - false - - - - - - - - - - - - diff --git a/examples/Zenoh/ZGet.cs b/examples/Zenoh/ZGet.cs new file mode 100644 index 0000000..f13a500 --- /dev/null +++ b/examples/Zenoh/ZGet.cs @@ -0,0 +1,41 @@ +using System; +using System.Threading; +using Zenoh; + +Zenoh.Zenoh.InitLogger(); +Config config = new Config(); +Session session = new Session(); + +Console.WriteLine("Opening session..."); +if (session.Open(config)) +{ + // wait + Thread.Sleep(1000); +} +else +{ + Console.WriteLine("Opening session unsuccessful"); + return; +} + +KeyExpr key1 = KeyExpr.FromString("/demo/example/zenoh-cs-put1"); +KeyExpr key2 = KeyExpr.FromString("/demo/example/zenoh-cs-put2"); + +Console.WriteLine($"Sending Query '{key1.Suffix}'"); +ReplayDataArray data1 = session.Get(key1); +foreach (ReplayData ele in data1.List) +{ + string s = $">> Received ('{ele.Sample.Key}': '{ele.Sample.ValueToString()}'"; + Console.WriteLine(s); +} + + +ReplayDataArray data2 = session.Get(key2); +Console.WriteLine($"Sending Query '{key2.Suffix}'"); +foreach (ReplayData ele in data2.List) +{ + string s = $">> Received ('{ele.Sample.Key}': '{ele.Sample.ValueToString()}'"; + Console.WriteLine(s); +} + +session.Close(); \ No newline at end of file diff --git a/examples/Zenoh/ZGet.csproj b/examples/Zenoh/ZGet.csproj new file mode 100644 index 0000000..a9d414f --- /dev/null +++ b/examples/Zenoh/ZGet.csproj @@ -0,0 +1,20 @@ + + + ZGet + ZGet + Exe + net6.0 + false + 10 + + + + + + + + + + + + diff --git a/examples/Zenoh/ZPut.cs b/examples/Zenoh/ZPut.cs new file mode 100644 index 0000000..2c2bf7a --- /dev/null +++ b/examples/Zenoh/ZPut.cs @@ -0,0 +1,41 @@ +using System; +using System.Threading; +using Zenoh; + +Zenoh.Zenoh.InitLogger(); +Config config = new Config(); +Session session = new Session(); + +Console.WriteLine("Opening session..."); +if (session.Open(config)) +{ + Thread.Sleep(200); +} +else +{ + Console.WriteLine("Opening session unsuccessful"); + return; +} + +KeyExpr key1 = KeyExpr.FromString("/demo/example/zenoh-cs-put1"); +KeyExpr key2 = KeyExpr.FromString("/demo/example/zenoh-cs-put2"); + +for (int i = 0; i < 5; i++) +{ + KeyExpr key = (i % 2) == 0 ? key1 : key2; + + string data = $"Put from csharp {i}!"; + bool r = session.Put(key, data); + if (r) + { + Console.WriteLine($"Putting Data ('{key.Suffix}': '{data}')"); + Thread.Sleep(50); + } + else + { + Console.WriteLine("Putting Data Fault!"); + break; + } +} + +session.Close(); \ No newline at end of file diff --git a/examples/Zenoh/ZPut.csproj b/examples/Zenoh/ZPut.csproj new file mode 100644 index 0000000..0d3346a --- /dev/null +++ b/examples/Zenoh/ZPut.csproj @@ -0,0 +1,20 @@ + + + ZPut + ZPut + Exe + net6.0 + false + 10 + + + + + + + + + + + + diff --git a/examples/Zenoh/ZSub.cs b/examples/Zenoh/ZSub.cs new file mode 100644 index 0000000..7542367 --- /dev/null +++ b/examples/Zenoh/ZSub.cs @@ -0,0 +1,67 @@ +using System; +using System.Text; +using System.Threading; +using Zenoh; + +Zenoh.Zenoh.InitLogger(); +Config config = new Config(); +Session session = new Session(); + +Console.WriteLine("Opening session..."); +if (session.Open(config)) +{ + // wait + Thread.Sleep(200); +} +else +{ + Console.WriteLine("Opening session unsuccessful"); + return; +} + +SubInfo info = new SubInfo(Reliability.Reliable, SubMode.Push); + +void Callback1(Sample sample) +{ + string key = sample.Key; + string value = Encoding.UTF8.GetString(sample.Value); + Console.WriteLine($">> [Subscriber1] Received PUT ('{key}': '{value}')"); +} + +void Callback2(Sample sample) +{ + string key = sample.Key; + string value = Encoding.UTF8.GetString(sample.Value); + Console.WriteLine($">> [Subscriber2] Received PUT ('{key}': '{value}')"); +} + +SubscriberCallback userCallback1 = Callback1; +SubscriberCallback userCallback2 = Callback2; + +string key1 = "/demo/example/zenoh-cs-put1"; +string key2 = "/demo/example/zenoh-cs-put2"; + +Subscriber subscriber1 = new Subscriber(key1, userCallback1); +Subscriber subscriber2 = new Subscriber(key2, userCallback2); + +if (session.RegisterSubscriber(info, subscriber1)) +{ + Console.WriteLine($"Registered Subscriber1 On '{key1}'"); +} + +if (session.RegisterSubscriber(info, subscriber2)) +{ + Console.WriteLine($"Registered Subscriber2 On '{key2}'"); +} + +Console.WriteLine("Enter 'q' to quit..."); +while (true) +{ + var input = Console.ReadKey(); + if (input.Key == ConsoleKey.Q) + { + break; + } +} + +session.Close(); \ No newline at end of file diff --git a/examples/Zenoh/ZSub.csproj b/examples/Zenoh/ZSub.csproj new file mode 100644 index 0000000..62d4ab6 --- /dev/null +++ b/examples/Zenoh/ZSub.csproj @@ -0,0 +1,20 @@ + + + ZSub + ZSub + Exe + net6.0 + false + 10 + + + + + + + + + + + + diff --git a/examples/zenoh-examples.sln b/examples/zenoh-examples.sln index 8a18860..9640e23 100644 --- a/examples/zenoh-examples.sln +++ b/examples/zenoh-examples.sln @@ -3,11 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.26124.0 MinimumVisualStudioVersion = 15.0.26124.0 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZNInfo", "Zenoh.Net\ZNInfo.csproj", "{12DDE9FF-E244-4FC5-9061-97F6B600AEBA}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Zenoh", "..\Zenoh\Zenoh.csproj", "{05075D28-DE9E-4CB4-9976-FCBDD170A7B5}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZNWrite", "Zenoh.Net\ZNWrite.csproj", "{B9C668F9-B880-479D-BEB9-7435E5B9742B}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZGet", "Zenoh\ZGet.csproj", "{7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZNSub", "Zenoh.Net\ZNSub.csproj", "{ECF3EA7C-E5A1-4261-8861-E9A20436BD83}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZPut", "Zenoh\ZPut.csproj", "{13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZSub", "Zenoh\ZSub.csproj", "{435FE932-CE46-49D0-B921-AC1B11585F13}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -22,41 +24,53 @@ Global HideSolutionNode = FALSE EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {12DDE9FF-E244-4FC5-9061-97F6B600AEBA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {12DDE9FF-E244-4FC5-9061-97F6B600AEBA}.Debug|Any CPU.Build.0 = Debug|Any CPU - {12DDE9FF-E244-4FC5-9061-97F6B600AEBA}.Debug|x64.ActiveCfg = Debug|Any CPU - {12DDE9FF-E244-4FC5-9061-97F6B600AEBA}.Debug|x64.Build.0 = Debug|Any CPU - {12DDE9FF-E244-4FC5-9061-97F6B600AEBA}.Debug|x86.ActiveCfg = Debug|Any CPU - {12DDE9FF-E244-4FC5-9061-97F6B600AEBA}.Debug|x86.Build.0 = Debug|Any CPU - {12DDE9FF-E244-4FC5-9061-97F6B600AEBA}.Release|Any CPU.ActiveCfg = Release|Any CPU - {12DDE9FF-E244-4FC5-9061-97F6B600AEBA}.Release|Any CPU.Build.0 = Release|Any CPU - {12DDE9FF-E244-4FC5-9061-97F6B600AEBA}.Release|x64.ActiveCfg = Release|Any CPU - {12DDE9FF-E244-4FC5-9061-97F6B600AEBA}.Release|x64.Build.0 = Release|Any CPU - {12DDE9FF-E244-4FC5-9061-97F6B600AEBA}.Release|x86.ActiveCfg = Release|Any CPU - {12DDE9FF-E244-4FC5-9061-97F6B600AEBA}.Release|x86.Build.0 = Release|Any CPU - {B9C668F9-B880-479D-BEB9-7435E5B9742B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B9C668F9-B880-479D-BEB9-7435E5B9742B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B9C668F9-B880-479D-BEB9-7435E5B9742B}.Debug|x64.ActiveCfg = Debug|Any CPU - {B9C668F9-B880-479D-BEB9-7435E5B9742B}.Debug|x64.Build.0 = Debug|Any CPU - {B9C668F9-B880-479D-BEB9-7435E5B9742B}.Debug|x86.ActiveCfg = Debug|Any CPU - {B9C668F9-B880-479D-BEB9-7435E5B9742B}.Debug|x86.Build.0 = Debug|Any CPU - {B9C668F9-B880-479D-BEB9-7435E5B9742B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B9C668F9-B880-479D-BEB9-7435E5B9742B}.Release|Any CPU.Build.0 = Release|Any CPU - {B9C668F9-B880-479D-BEB9-7435E5B9742B}.Release|x64.ActiveCfg = Release|Any CPU - {B9C668F9-B880-479D-BEB9-7435E5B9742B}.Release|x64.Build.0 = Release|Any CPU - {B9C668F9-B880-479D-BEB9-7435E5B9742B}.Release|x86.ActiveCfg = Release|Any CPU - {B9C668F9-B880-479D-BEB9-7435E5B9742B}.Release|x86.Build.0 = Release|Any CPU - {ECF3EA7C-E5A1-4261-8861-E9A20436BD83}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {ECF3EA7C-E5A1-4261-8861-E9A20436BD83}.Debug|Any CPU.Build.0 = Debug|Any CPU - {ECF3EA7C-E5A1-4261-8861-E9A20436BD83}.Debug|x64.ActiveCfg = Debug|Any CPU - {ECF3EA7C-E5A1-4261-8861-E9A20436BD83}.Debug|x64.Build.0 = Debug|Any CPU - {ECF3EA7C-E5A1-4261-8861-E9A20436BD83}.Debug|x86.ActiveCfg = Debug|Any CPU - {ECF3EA7C-E5A1-4261-8861-E9A20436BD83}.Debug|x86.Build.0 = Debug|Any CPU - {ECF3EA7C-E5A1-4261-8861-E9A20436BD83}.Release|Any CPU.ActiveCfg = Release|Any CPU - {ECF3EA7C-E5A1-4261-8861-E9A20436BD83}.Release|Any CPU.Build.0 = Release|Any CPU - {ECF3EA7C-E5A1-4261-8861-E9A20436BD83}.Release|x64.ActiveCfg = Release|Any CPU - {ECF3EA7C-E5A1-4261-8861-E9A20436BD83}.Release|x64.Build.0 = Release|Any CPU - {ECF3EA7C-E5A1-4261-8861-E9A20436BD83}.Release|x86.ActiveCfg = Release|Any CPU - {ECF3EA7C-E5A1-4261-8861-E9A20436BD83}.Release|x86.Build.0 = Release|Any CPU + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Debug|x64.ActiveCfg = Debug|Any CPU + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Debug|x64.Build.0 = Debug|Any CPU + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Debug|x86.ActiveCfg = Debug|Any CPU + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Debug|x86.Build.0 = Debug|Any CPU + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Release|Any CPU.Build.0 = Release|Any CPU + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Release|x64.ActiveCfg = Release|Any CPU + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Release|x64.Build.0 = Release|Any CPU + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Release|x86.ActiveCfg = Release|Any CPU + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Release|x86.Build.0 = Release|Any CPU + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Debug|x64.ActiveCfg = Debug|Any CPU + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Debug|x64.Build.0 = Debug|Any CPU + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Debug|x86.ActiveCfg = Debug|Any CPU + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Debug|x86.Build.0 = Debug|Any CPU + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Release|Any CPU.Build.0 = Release|Any CPU + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Release|x64.ActiveCfg = Release|Any CPU + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Release|x64.Build.0 = Release|Any CPU + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Release|x86.ActiveCfg = Release|Any CPU + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Release|x86.Build.0 = Release|Any CPU + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Debug|x64.ActiveCfg = Debug|Any CPU + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Debug|x64.Build.0 = Debug|Any CPU + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Debug|x86.ActiveCfg = Debug|Any CPU + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Debug|x86.Build.0 = Debug|Any CPU + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Release|Any CPU.Build.0 = Release|Any CPU + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Release|x64.ActiveCfg = Release|Any CPU + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Release|x64.Build.0 = Release|Any CPU + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Release|x86.ActiveCfg = Release|Any CPU + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Release|x86.Build.0 = Release|Any CPU + {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|Any CPU.Build.0 = Debug|Any CPU + {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|x64.ActiveCfg = Debug|Any CPU + {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|x64.Build.0 = Debug|Any CPU + {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|x86.ActiveCfg = Debug|Any CPU + {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|x86.Build.0 = Debug|Any CPU + {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|Any CPU.ActiveCfg = Release|Any CPU + {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|Any CPU.Build.0 = Release|Any CPU + {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|x64.ActiveCfg = Release|Any CPU + {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|x64.Build.0 = Release|Any CPU + {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|x86.ActiveCfg = Release|Any CPU + {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection EndGlobal From 1a73c8112b9a9b8d92ff5a67d3a788de98c0c6e3 Mon Sep 17 00:00:00 2001 From: sanri Date: Tue, 20 Sep 2022 13:58:43 +0800 Subject: [PATCH 02/34] create example ZInfo --- README.md | 6 +- Zenoh/Config.cs | 100 ++++++++++++++- Zenoh/Session.cs | 12 ++ Zenoh/Zenoh.cs | 82 +++++++++++++ Zenoh/example_config.json5 | 237 ++++++++++++++++++++++++++++++++++++ examples/Zenoh/README.md | 53 ++++++++ examples/Zenoh/ZGet.cs | 2 + examples/Zenoh/ZInfo.cs | 38 ++++++ examples/Zenoh/ZInfo.csproj | 20 +++ examples/Zenoh/ZPut.cs | 2 + examples/Zenoh/ZSub.cs | 2 + examples/zenoh-examples.sln | 14 +++ 12 files changed, 564 insertions(+), 4 deletions(-) create mode 100644 Zenoh/example_config.json5 create mode 100644 examples/Zenoh/README.md create mode 100644 examples/Zenoh/ZInfo.cs create mode 100644 examples/Zenoh/ZInfo.csproj diff --git a/README.md b/README.md index 031235f..ffb5946 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ The Eclipse zenoh C# library is available on NuGet: ### Supported .NET Standards -The library is configured to target the **.NET Standard 2.0** at minimum. +The library is configured to target the **.NET 6.0** at minimum. ------------------------------- @@ -38,8 +38,8 @@ Simply run `dotnet build Zenoh/`. ------------------------------- ## Running the Examples -The examples are configured to target **net5.0**. +The examples are configured to target **net6.0**. The simplest way to run some of the examples is to get a Docker image of the **zenoh** network router (see https://github.com/eclipse-zenoh/zenoh#how-to-test-it) and then to run the examples on your machine. -Then, run the zenoh-csharp examples following the instructions in [examples/Zenoh.Net/README.md](https://github.com/eclipse-zenoh/zenoh-csharp/blob/master/examples/Zenoh.Net/README.md) +Then, run the zenoh-csharp examples following the instructions in [examples/Zenoh/README.md](https://github.com/eclipse-zenoh/zenoh-csharp/blob/master/examples/Zenoh/README.md) diff --git a/Zenoh/Config.cs b/Zenoh/Config.cs index 8ced203..a7f0cf6 100644 --- a/Zenoh/Config.cs +++ b/Zenoh/Config.cs @@ -4,20 +4,112 @@ namespace Zenoh { - public class Config + public class Config : IDisposable { + public enum Mode + { + Router, + Peer, + Client, + } + internal struct NativeType // z_owned_config_t { internal IntPtr p; } internal NativeType native; + private bool _disposed; public Config() { this.native = ZConfigDefault(); + _disposed = false; } + public void Dispose() => Dispose(true); + + private void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + + ZConfigFree(ref native); + _disposed = true; + } + + // The Zenoh ID (as hex-string) of the instance. + // This ID cannot exceed 16 bytes (as hex-string 32 bytes) of length. + // If left unset, a random UUIDv4 will be generated. + // WARNING: this id must be unique in your zenoh network. + public bool SetId(string id) + { + string key = "id"; + string value = $"\"{id}\""; + return InsertJson(key, value); + } + + // The v such as "tcp/172.30.1.1:7447" + public bool SetConnect(string[] v) + { + string key = "connect/endpoints"; + string value = "["; + foreach (var ele in v) + { + value += $"\"{ele}\","; + } + + value += "]"; + return InsertJson(key, value); + } + + public bool SetMode(Mode mode) + { + string key = "mode"; + string value = ""; + switch (mode) + { + case Mode.Client: + value = "\"client\""; + break; + case Mode.Peer: + value = "\"peer\""; + break; + case Mode.Router: + value = "\"client\""; + break; + } + + return InsertJson(key, value); + } + + // Whether data messages should be timestamped + public bool SetTimestamp(bool b) + { + string key = "add_timestamp"; + string value = b ? "true" : "false"; + return InsertJson(key, value); + } + + public string ToStr() + { + ZString zs = ZConfigToStr(ref native); + string result = ZTypes.ZStringToString(zs); + ZTypes.ZStringFree(ref zs); + return result; + } + + private bool InsertJson(string key, string value) + { + IntPtr k = Marshal.StringToHGlobalAnsi(key); + IntPtr v = Marshal.StringToHGlobalAnsi(value); + bool r = ZConfigInsertJson(ref native, k, v); + Marshal.FreeHGlobal(k); + Marshal.FreeHGlobal(v); + return r; + } [DllImport(Zenoh.DllName, EntryPoint = "z_config_default", CallingConvention = CallingConvention.Cdecl)] internal static extern NativeType ZConfigDefault(); @@ -25,6 +117,9 @@ public Config() [DllImport(Zenoh.DllName, EntryPoint = "z_config_from_str", CallingConvention = CallingConvention.Cdecl)] internal static extern NativeType ZConfigFromStr([MarshalAs(UnmanagedType.LPStr)] string str); + [DllImport(Zenoh.DllName, EntryPoint = "z_config_from_file", CallingConvention = CallingConvention.Cdecl)] + internal static extern NativeType ZConfigFromFile([MarshalAs(UnmanagedType.LPStr)] string path); + [DllImport(Zenoh.DllName, EntryPoint = "z_config_check", CallingConvention = CallingConvention.Cdecl)] internal static extern bool ZConfigCheck(ref NativeType config); @@ -34,6 +129,9 @@ public Config() [DllImport(Zenoh.DllName, EntryPoint = "z_config_get", CallingConvention = CallingConvention.Cdecl)] internal static extern ZString ZConfigGet(ref NativeType config, ZString key); + [DllImport(Zenoh.DllName, EntryPoint = "z_config_insert_json", CallingConvention = CallingConvention.Cdecl)] + internal static extern bool ZConfigInsertJson(ref NativeType config, IntPtr key, IntPtr value); + [DllImport(Zenoh.DllName, EntryPoint = "z_config_free", CallingConvention = CallingConvention.Cdecl)] internal static extern void ZConfigFree(ref NativeType config); } diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs index be829cc..fa0c3e8 100644 --- a/Zenoh/Session.cs +++ b/Zenoh/Session.cs @@ -62,6 +62,12 @@ public void Close() ZClose(ref native); } + public Info Info() + { + global::Zenoh.Info.NativeType n = ZInfo(ref native); + return new Info(n); + } + public bool Put(KeyExpr key, string value) { IntPtr v = Marshal.StringToHGlobalAnsi(value); @@ -124,6 +130,12 @@ public void UnregisterSubscriber(string key) [DllImport(Zenoh.DllName, EntryPoint = "z_close", CallingConvention = CallingConvention.Cdecl)] internal static extern void ZClose(ref NativeType session); + [DllImport(Zenoh.DllName, EntryPoint = "z_info", CallingConvention = CallingConvention.Cdecl)] + internal static extern Info.NativeType ZInfo(ref NativeType session); + + [DllImport(Zenoh.DllName, EntryPoint = "z_info_as_str", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZString ZInfoAsStr(ref Session.NativeType session); + [DllImport(Zenoh.DllName, EntryPoint = "z_put", CallingConvention = CallingConvention.Cdecl)] internal static extern int ZPut(ref NativeType session, KeyExpr.NativeType keyexpr, IntPtr payload, ulong len); diff --git a/Zenoh/Zenoh.cs b/Zenoh/Zenoh.cs index a0d0dd4..62de581 100644 --- a/Zenoh/Zenoh.cs +++ b/Zenoh/Zenoh.cs @@ -125,6 +125,77 @@ public Period(uint origin, uint period, uint duration) } } + public class Info : IDisposable + { + [StructLayout(LayoutKind.Sequential)] + public struct NativeType // z_owned_info_t + { + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] + internal UInt64[] align; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] + internal UIntPtr[] pad; + } + + internal NativeType native; + private bool _disposed; + + internal Info(NativeType natvie) + { + native = natvie; + _disposed = false; + } + + public void Dispose() => Dispose(true); + + private void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + + ZInfoFree(ref native); + _disposed = true; + } + + public String Pid() + { + uint key = 0x00; + return InfoGet(key); + } + + public String[] PeerPid() + { + uint key = 0x01; + string r = InfoGet(key); + return r.Split(','); + } + + public String[] RouterPid() + { + uint key = 0x02; + string r = InfoGet(key); + return r.Split(','); + } + + private String InfoGet(uint key) + { + ZString s = ZInfoGet(ref native, key); + string result = ZTypes.ZStringToString(s); + ZTypes.ZStringFree(ref s); + return result; + } + + [DllImport(Zenoh.DllName, EntryPoint = "z_info_free", CallingConvention = CallingConvention.Cdecl)] + internal static extern void ZInfoFree(ref NativeType info); + + [DllImport(Zenoh.DllName, EntryPoint = "z_info_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern bool ZInfoCheck(ref NativeType info); + + [DllImport(Zenoh.DllName, EntryPoint = "z_info_get", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZString ZInfoGet(ref NativeType info, UInt64 key); + } public class KeyExpr : IDisposable { @@ -370,5 +441,16 @@ internal static string ZBytesToString(ZBytes zb) string result = System.Text.Encoding.UTF8.GetString(managedArray, 0, (int)zb.len); return result; } + + internal static string ZStringToString(ZString zs) + { + return Marshal.PtrToStringAnsi(zs.start); + } + + [DllImport(Zenoh.DllName, EntryPoint = "z_string_free", CallingConvention = CallingConvention.Cdecl)] + internal static extern void ZStringFree(ref ZString s); + + [DllImport(Zenoh.DllName, EntryPoint = "z_string_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern bool ZStringCheck(ref ZString s); } } \ No newline at end of file diff --git a/Zenoh/example_config.json5 b/Zenoh/example_config.json5 new file mode 100644 index 0000000..f865df9 --- /dev/null +++ b/Zenoh/example_config.json5 @@ -0,0 +1,237 @@ +/// This file attempts to list and document available configuration elements. +/// For a more complete view of the configuration's structure, check out `zenoh/src/config.rs`'s `Config` structure. +/// Note that the values here are correctly typed, but may not be sensible, so copying this file to change only the parts that matter to you is not good practice. +{ + /// Plugins are only loaded if present in the configuration. When starting + /// Once loaded, they may react to changes in the configuration made through the zenoh instance's adminspace. + plugins: { + /// If no `__path__` is given to a plugin, zenohd will automatically search for a shared library matching the plugin's name (here, `libzplugin_rest.so` would be searched for on linux) + rest: { + /// Setting this option to true allows zenohd to panic should it detect issues with this plugin. Setting it to false politely asks the plugin not to panic. + __required__: true, + // defaults to false + http_port: 8000, + }, + storage_manager: { + /// When a path is present, automatic search is disabled, and zenohd will instead select the first path which manages to load. + __path__: [ + "./target/release/libzplugin_storages.so", + "./target/release/libzplugin_storages.dylib", + ], + /// The "memory" volume is always available, but you may create other volumes here, with various backends to support the actual storing. + volumes: { + /// An influxdb backend is also available at https://github.com/eclipse-zenoh/zenoh-backend-influxdb + influxdb: { + url: "https://myinfluxdb.example", + /// Some plugins may need passwords in their configuration. + /// To avoid leaking them through the adminspace, they may be masked behind a privacy barrier. + /// any value held at the key "private" will not be shown in the adminspace. + private: { + username: "user1", + password: "pw1", + }, + }, + influxdb2: { + /// A second backend of the same type can be spawned using `__path__`, for examples when different DBs are needed. + backend: "influxdb", + private: { + username: "user2", + password: "pw2", + }, + url: "https://localhost:8086", + }, + }, + /// + storages: { + demo: { + /// Storages always need to know what set of keys they must work with. These sets are defined by a key expression. + key_expr: "/demo/memory/**", + /// Storages also need to know which volume will be used to actually store their key-value pairs. + /// The "memory" volume is always available, and doesn't require any per-storage options, so requesting "memory" by string is always sufficient. + volume: "memory", + }, + influx_demo: { + key_expr: "/demo/influxdb/**", + /// This prefix will be stripped of the received keys when storing. + strip_prefix: "/demo/influxdb/", + /// influxdb-backed volumes need a bit more configuration, which is passed like-so: + volume: { + id: "influxdb", + db: "example", + }, + }, + influx_demo2: { + key_expr: "/demo/influxdb2/**", + strip_prefix: "/demo/influxdb2/", + volume: { + id: "influxdb2", + db: "example", + }, + }, + }, + }, + }, + /// Directories where plugins configured by name should be looked for. PLugins configured by __path__ are not subject to lookup + plugins_search_dirs: [], + /// The identifier (as hex-string) that zenohd must use. If not set, a random UUIDv4 will be used. + /// WARNING: this id must be unique in your zenoh network. + // id: "5975702c206974277320415343494921", + /// The node's mode (router, peer or client) + mode: "router", + /// Which endpoints to connect to. E.g. tcp/localhost:7447. + /// By configuring the endpoints, it is possible to tell zenoh which router/peer to connect to at startup. + connect: { + endpoints: [ + // "/
" + ], + }, + /// Which endpoints to listen on. E.g. tcp/localhost:7447. + /// By configuring the endpoints, it is possible to tell zenoh which are the endpoints that other routers, + /// peers, or client can use to establish a zenoh session. + listen: { + endpoints: [ + // "/
" + ], + }, + startup: { + subscribe: [ + "/demo/subs/**" + ], + declare_publications: [ + "/demo/pubs/**" + ], + }, + scouting: { + /// In client mode, the period dedicated to scouting for a router before failing + timeout: 3000, + /// In peer mode, the period dedicated to scouting remote peers before attempting other operations + delay: 200, + /// How multicast should behave + multicast: { + /// Whether multicast scouting is enabled or not + enabled: true, + /// The socket which should be used for multicast scouting + address: "224.0.0.224:7447", + /// The network interface which should be used for multicast scouting + interface: "", + // empty string means auto + /// An autoconnection mask (accepted values are bit-or-like combinations of peer, router and client). + /// If the configured instance's mode intersects with this field, zenoh will automatically establish a connection with other nodes discovered through this method of scouting. + autoconnect: "peer|client", + }, + gossip: { + autoconnect: "", + }, + /// If set to `false`, peers will never automatically establish sessions between each-other. + peers_autoconnect: false, + }, + /// Whether data messages should be timestamped + add_timestamp: true, + /// Whether local writes/queries should reach local subscribers/queryables + local_routing: true, + transport: { + unicast: { + /// Timeout in milliseconds when opening a link + accept_timeout: 10000, + /// Maximum number of zenoh session in pending state while accepting + accept_pending: 100, + /// Maximum number of sessions that can be simultaneously alive + max_sessions: 1024, + /// Maximum number of incoming links that are admitted per session + max_links: 1, + }, + multicast: { + /// Link join interval duration in milliseconds + join_interval: 2500, + /// Maximum number of sessions that can be simultaneously alive + max_sessions: 1024, + }, + qos: { + enabled: false, + }, + link: { + tx: { + /// The largest value allowed for Zenoh message sequence numbers (wrappring to 0 when reached). + /// When establishing a session with another Zenoh instance, the lowest value of the two instances will be used. + /// Defaults to 2^28. + sequence_number_resolution: 268435456, + /// Link lease duration in milliseconds to announce to other zenoh nodes + lease: 10000, + /// Number of keep-alive messages in a link lease duration. If no data is sent, keep alive + /// messages will be sent at the configured time interval. + /// NOTE: In order to consider eventual packet loss and transmission latency and jitter, + /// set the actual keep_alive timeout to one fourth of the lease time. + /// This is in-line with the ITU-T G.8013/Y.1731 specification on continous connectivity + /// check which considers a link as failed when no messages are received in 3.5 times the + /// target interval. + keep_alive: 4, + /// Batch size in bytes is expressed as a 16bit unsigned integer. + /// Therefore, the maximum batch size is 2^16-1 (i.e. 65535). + /// The default batch size value is the maximum batch size: 65535. + batch_size: 65535, + queue: { + /// The size of each priority queue indicates the number of batches a given queue can contain. + /// The amount of memory being allocated for each queue is then SIZE_XXX * BATCH_SIZE. + /// In the case of the transport link MTU being smaller than the ZN_BATCH_SIZE, + /// then amount of memory being allocated for each queue is SIZE_XXX * LINK_MTU. + /// If qos is false, then only the DATA priority will be allocated. + size: { + control: 1, + real_time: 1, + interactive_high: 1, + interactive_low: 1, + data_high: 2, + data: 4, + data_low: 4, + background: 4, + }, + /// The initial exponential backoff time in nanoseconds to allow the batching to eventually progress. + /// Higher values lead to a more aggressive batching but it will introduce additional latency. + backoff: 100, + } + }, + rx: { + /// Receiving buffer size in bytes for each link + /// The default the rx_buffer_size value is the same as the default batch size: 65335. + /// For very high throughput scenarios, the rx_buffer_size can be increased to accomodate + /// more in-flight data. This is particularly relevant when dealing with large messages. + /// E.g. for 16MiB rx_buffer_size set the value to: 16777216. + buffer_size: 65535, + /// Maximum size of the defragmentation buffer at receiver end. + /// Fragmented messages that are larger than the configured size will be dropped. + /// The default value is 1GiB. This would work in most scenarios. + /// NOTE: reduce the value if you are operating on a memory constrained device. + max_message_size: 1073741824, + }, + tls: { + root_ca_certificate: null, + server_private_key: null, + server_certificate: null, + client_auth: null, + client_private_key: null, + client_certificate: null, + }, + }, + shared_memory: { + enabled: true, + }, + auth: { + /// The configuration of authentification. + /// A password implies a username is required. + usrpwd: { + user: null, + password: null, + /// The path to a file containing the user password dictionary + dictionary_file: null, + }, + pubkey: { + public_key_pem: null, + private_key_pem: null, + public_key_file: null, + private_key_file: null, + key_size: null, + known_keys_file: null, + }, + }, + }, +} diff --git a/examples/Zenoh/README.md b/examples/Zenoh/README.md new file mode 100644 index 0000000..c9b593b --- /dev/null +++ b/examples/Zenoh/README.md @@ -0,0 +1,53 @@ +# Zenoh C# examples + +## Start instructions + + ```bash + dotnet run -p + ``` + + Each example accepts the `-h` or `--help` option that provides a description of its arguments and their default values. + + :warning: _To pass any options to an example, specify them after the `--`argument. For instance:_ + + ```bash + dotnet run -p ZGet.csproj -- -h + ``` + + If you run the tests against the zenoh router running in a Docker container, you need to add the + `-e tcp/localhost:7447` option to your examples. That's because Docker doesn't support UDP multicast + transport, and therefore the zenoh scouting and discrovery mechanism cannot work with. + +## Examples description + + +### ZGet + + Gets value from zenoh session. + + Typical usage: + ```bash + dotnet run -p ZGet.csproj + ``` + + +### ZPut + + Writes a path/value into Zenoh. + The path/value will be received by all matching subscribers, for instance the [ZSub](#ZSub) examples. + + Typical usage: + ```bash + dotnet run -p ZPut.csproj + ``` + +### ZSub + + Registers a subscriber with a selector. + The subscriber will be notified of each write made on any path matching the selector, + and will print this notification. + + Typical usage: + ```bash + dotnet run -p ZSub.csproj + ``` diff --git a/examples/Zenoh/ZGet.cs b/examples/Zenoh/ZGet.cs index f13a500..3f5aa26 100644 --- a/examples/Zenoh/ZGet.cs +++ b/examples/Zenoh/ZGet.cs @@ -4,6 +4,8 @@ Zenoh.Zenoh.InitLogger(); Config config = new Config(); +//string[] connect = {"tcp/172.30.100.3:7447"}; +//config.SetConnect(connect); Session session = new Session(); Console.WriteLine("Opening session..."); diff --git a/examples/Zenoh/ZInfo.cs b/examples/Zenoh/ZInfo.cs new file mode 100644 index 0000000..bd6077d --- /dev/null +++ b/examples/Zenoh/ZInfo.cs @@ -0,0 +1,38 @@ +using System; +using System.Linq; +using System.Threading; +using Zenoh; + +Zenoh.Zenoh.InitLogger(); +Config config = new Config(); +config.SetId("01"); +//string[] c = { "tcp/172.30.100.3:7447", "tcp/172.30.100.1:7447" }; +//config.SetConnect(c); +config.SetMode(Config.Mode.Client); +config.SetTimestamp(true); +string configStr = config.ToStr(); +Console.WriteLine($"config string:\n{configStr}"); + +Session session = new Session(); +Console.WriteLine("Opening session..."); +if (session.Open(config)) +{ + // wait + Thread.Sleep(1000); +} +else +{ + Console.WriteLine("Opening session unsuccessful"); + return; +} + + +Info info = session.Info(); +string pid = info.Pid(); +string[] routerPid = info.RouterPid(); +string[] peerPid = info.PeerPid(); +info.Dispose(); + +Console.WriteLine($"PID: {pid}\nPeer PID: {String.Join(',', peerPid)}\nRouter PID: {String.Join(',', routerPid)}"); + +session.Close(); \ No newline at end of file diff --git a/examples/Zenoh/ZInfo.csproj b/examples/Zenoh/ZInfo.csproj new file mode 100644 index 0000000..b33306b --- /dev/null +++ b/examples/Zenoh/ZInfo.csproj @@ -0,0 +1,20 @@ + + + ZInfo + ZInfo + Exe + net6.0 + false + 10 + + + + + + + + + + + + diff --git a/examples/Zenoh/ZPut.cs b/examples/Zenoh/ZPut.cs index 2c2bf7a..6002c7c 100644 --- a/examples/Zenoh/ZPut.cs +++ b/examples/Zenoh/ZPut.cs @@ -4,6 +4,8 @@ Zenoh.Zenoh.InitLogger(); Config config = new Config(); +//string[] connect = {"tcp/172.30.100.3:7447"}; +//config.SetConnect(connect); Session session = new Session(); Console.WriteLine("Opening session..."); diff --git a/examples/Zenoh/ZSub.cs b/examples/Zenoh/ZSub.cs index 7542367..f58d2e8 100644 --- a/examples/Zenoh/ZSub.cs +++ b/examples/Zenoh/ZSub.cs @@ -5,6 +5,8 @@ Zenoh.Zenoh.InitLogger(); Config config = new Config(); +//string[] connect = {"tcp/172.30.100.3:7447"}; +//config.SetConnect(connect); Session session = new Session(); Console.WriteLine("Opening session..."); diff --git a/examples/zenoh-examples.sln b/examples/zenoh-examples.sln index 9640e23..e2ecd5e 100644 --- a/examples/zenoh-examples.sln +++ b/examples/zenoh-examples.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZPut", "Zenoh\ZPut.csproj", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZSub", "Zenoh\ZSub.csproj", "{435FE932-CE46-49D0-B921-AC1B11585F13}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZInfo", "Zenoh\ZInfo.csproj", "{B973A563-44AD-4B68-855C-46B5D58DCE32}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -72,5 +74,17 @@ Global {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|x64.Build.0 = Release|Any CPU {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|x86.ActiveCfg = Release|Any CPU {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|x86.Build.0 = Release|Any CPU + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Debug|x64.ActiveCfg = Debug|Any CPU + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Debug|x64.Build.0 = Debug|Any CPU + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Debug|x86.ActiveCfg = Debug|Any CPU + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Debug|x86.Build.0 = Debug|Any CPU + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Release|Any CPU.Build.0 = Release|Any CPU + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Release|x64.ActiveCfg = Release|Any CPU + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Release|x64.Build.0 = Release|Any CPU + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Release|x86.ActiveCfg = Release|Any CPU + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection EndGlobal From 29576845afd1c980bd7886f665509d407549d6ae Mon Sep 17 00:00:00 2001 From: sanri Date: Tue, 20 Sep 2022 14:50:16 +0800 Subject: [PATCH 03/34] change KeyExpr Dispose() --- Zenoh/Zenoh.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Zenoh/Zenoh.cs b/Zenoh/Zenoh.cs index 62de581..0b1cfc3 100644 --- a/Zenoh/Zenoh.cs +++ b/Zenoh/Zenoh.cs @@ -207,6 +207,7 @@ public struct NativeType // z_keyexpr_t } internal NativeType native; + private bool _disposed = false; public ulong Id { @@ -231,9 +232,17 @@ public static KeyExpr FromString(string name) return new KeyExpr(key); } - public void Dispose() + public void Dispose() => Dispose(true); + + protected virtual void Dispose(bool disposing) { + if (_disposed) + { + return; + } + ZKeyexprFree(ref native); + _disposed = true; } [DllImport(Zenoh.DllName, EntryPoint = "z_expr_new", CallingConvention = CallingConvention.Cdecl)] @@ -452,5 +461,8 @@ internal static string ZStringToString(ZString zs) [DllImport(Zenoh.DllName, EntryPoint = "z_string_check", CallingConvention = CallingConvention.Cdecl)] internal static extern bool ZStringCheck(ref ZString s); + + [DllImport(Zenoh.DllName, EntryPoint = "z_bytes_free", CallingConvention = CallingConvention.Cdecl)] + internal static extern void ZBytesFree(ref ZString s); } } \ No newline at end of file From c057bac6c0cacb96a69cb9b5edcb92486c546ed0 Mon Sep 17 00:00:00 2001 From: sanri Date: Tue, 20 Sep 2022 21:08:14 +0800 Subject: [PATCH 04/34] publish nuget --- .gitignore | 2 +- Zenoh/Zenoh.csproj | 29 +++++++---------------------- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 0a5c015..3b11171 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ obj bin *.user - +package/ diff --git a/Zenoh/Zenoh.csproj b/Zenoh/Zenoh.csproj index 8df4b70..64f962e 100644 --- a/Zenoh/Zenoh.csproj +++ b/Zenoh/Zenoh.csproj @@ -1,35 +1,20 @@ - Zenoh - Zenoh - 0.0.4 - Julien Enoch + Zenoh-CS + 0.0.1 + sanri EPL-2.0 OR Apache-2.0 Zenoh: Zero Overhead Pub/sub, Store/Query and Compute. false - - https://github.com/eclipse-zenoh/zenoh-csharp - https://github.com/eclipse-zenoh/zenoh-csharp + https://github.com/sanri/zenoh-csharp + https://github.com/sanri/zenoh-csharp git - Library net6.0 - true - 10 + 0.0.1 + zenoh From 2638e095abd8b3dcb1b15274d6450eb512b8a8df Mon Sep 17 00:00:00 2001 From: sanri Date: Fri, 4 Nov 2022 18:42:07 +0800 Subject: [PATCH 05/34] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=91=E9=80=81?= =?UTF-8?q?=E4=B8=AD=E6=96=87=E5=AD=97=E7=AC=A6=E4=B8=B2=E5=87=BA=E7=8E=B0?= =?UTF-8?q?=E4=B9=B1=E7=A0=81=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zenoh/Session.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs index fa0c3e8..2d0a2ee 100644 --- a/Zenoh/Session.cs +++ b/Zenoh/Session.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Runtime.InteropServices; +using System.Text; namespace Zenoh { @@ -70,8 +71,10 @@ public Info Info() public bool Put(KeyExpr key, string value) { - IntPtr v = Marshal.StringToHGlobalAnsi(value); - int r = ZPut(ref native, key.native, v, (ulong)value.Length); + byte[] data = Encoding.UTF8.GetBytes(value); + IntPtr v = Marshal.AllocHGlobal(data.Length); + Marshal.Copy(data, 0, v, data.Length); + int r = ZPut(ref native, key.native, v, (ulong)data.Length); Marshal.FreeHGlobal(v); if (r == 0) { From c7c69e0ac54c3e548ada3a95c174a75fb875314e Mon Sep 17 00:00:00 2001 From: sanri Date: Mon, 7 Nov 2022 09:38:25 +0800 Subject: [PATCH 06/34] update version number --- Zenoh/Zenoh.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Zenoh/Zenoh.csproj b/Zenoh/Zenoh.csproj index 64f962e..7dbdb18 100644 --- a/Zenoh/Zenoh.csproj +++ b/Zenoh/Zenoh.csproj @@ -2,7 +2,7 @@ Zenoh Zenoh-CS - 0.0.1 + 0.0.2 sanri EPL-2.0 OR Apache-2.0 Zenoh: Zero Overhead Pub/sub, Store/Query and Compute. @@ -14,7 +14,7 @@ net6.0 true 10 - 0.0.1 + 0.0.2 zenoh From 8d1fd1c56e6b6f5b3a7bc18e59ca257ca619c14b Mon Sep 17 00:00:00 2001 From: sanri Date: Thu, 10 Nov 2022 15:57:58 +0800 Subject: [PATCH 07/34] delete function z_init_logger() --- Zenoh/Zenoh.cs | 4 ++-- Zenoh/Zenoh.csproj | 4 ++-- examples/Zenoh/ZGet.cs | 2 +- examples/Zenoh/ZInfo.cs | 2 +- examples/Zenoh/ZPut.cs | 2 +- examples/Zenoh/ZSub.cs | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Zenoh/Zenoh.cs b/Zenoh/Zenoh.cs index 0b1cfc3..029db67 100644 --- a/Zenoh/Zenoh.cs +++ b/Zenoh/Zenoh.cs @@ -23,8 +23,8 @@ public static class Zenoh { public const string DllName = "zenohc"; - [DllImport(Zenoh.DllName, EntryPoint = "z_init_logger")] - public static extern void InitLogger(); + //[DllImport(Zenoh.DllName, EntryPoint = "z_init_logger")] + //public static extern void InitLogger(); } public enum KnownEncoding : int // e_known_encoding_t diff --git a/Zenoh/Zenoh.csproj b/Zenoh/Zenoh.csproj index 7dbdb18..b6f61d9 100644 --- a/Zenoh/Zenoh.csproj +++ b/Zenoh/Zenoh.csproj @@ -2,7 +2,7 @@ Zenoh Zenoh-CS - 0.0.2 + 0.0.3 sanri EPL-2.0 OR Apache-2.0 Zenoh: Zero Overhead Pub/sub, Store/Query and Compute. @@ -14,7 +14,7 @@ net6.0 true 10 - 0.0.2 + 0.0.3 zenoh diff --git a/examples/Zenoh/ZGet.cs b/examples/Zenoh/ZGet.cs index 3f5aa26..13dda29 100644 --- a/examples/Zenoh/ZGet.cs +++ b/examples/Zenoh/ZGet.cs @@ -2,7 +2,7 @@ using System.Threading; using Zenoh; -Zenoh.Zenoh.InitLogger(); +//Zenoh.Zenoh.InitLogger(); Config config = new Config(); //string[] connect = {"tcp/172.30.100.3:7447"}; //config.SetConnect(connect); diff --git a/examples/Zenoh/ZInfo.cs b/examples/Zenoh/ZInfo.cs index bd6077d..cfaa73c 100644 --- a/examples/Zenoh/ZInfo.cs +++ b/examples/Zenoh/ZInfo.cs @@ -3,7 +3,7 @@ using System.Threading; using Zenoh; -Zenoh.Zenoh.InitLogger(); +//Zenoh.Zenoh.InitLogger(); Config config = new Config(); config.SetId("01"); //string[] c = { "tcp/172.30.100.3:7447", "tcp/172.30.100.1:7447" }; diff --git a/examples/Zenoh/ZPut.cs b/examples/Zenoh/ZPut.cs index 6002c7c..cef5495 100644 --- a/examples/Zenoh/ZPut.cs +++ b/examples/Zenoh/ZPut.cs @@ -2,7 +2,7 @@ using System.Threading; using Zenoh; -Zenoh.Zenoh.InitLogger(); +//Zenoh.Zenoh.InitLogger(); Config config = new Config(); //string[] connect = {"tcp/172.30.100.3:7447"}; //config.SetConnect(connect); diff --git a/examples/Zenoh/ZSub.cs b/examples/Zenoh/ZSub.cs index f58d2e8..a7d560d 100644 --- a/examples/Zenoh/ZSub.cs +++ b/examples/Zenoh/ZSub.cs @@ -3,7 +3,7 @@ using System.Threading; using Zenoh; -Zenoh.Zenoh.InitLogger(); +//Zenoh.Zenoh.InitLogger(); Config config = new Config(); //string[] connect = {"tcp/172.30.100.3:7447"}; //config.SetConnect(connect); From 9cd602c7c81d5701536536ea94358a139b069928 Mon Sep 17 00:00:00 2001 From: sanri Date: Fri, 11 Nov 2022 17:39:36 +0800 Subject: [PATCH 08/34] update example ZInfo --- Zenoh/Config.cs | 37 +++++++-------- Zenoh/Session.cs | 88 +++++++++++++++++++++++++++++++--- Zenoh/Zenoh.cs | 101 +++++++++++++++------------------------- examples/Zenoh/ZInfo.cs | 13 +++--- 4 files changed, 143 insertions(+), 96 deletions(-) diff --git a/Zenoh/Config.cs b/Zenoh/Config.cs index a7f0cf6..721b0bd 100644 --- a/Zenoh/Config.cs +++ b/Zenoh/Config.cs @@ -36,7 +36,7 @@ private void Dispose(bool disposing) return; } - ZConfigFree(ref native); + ZConfigDrop(ref native); _disposed = true; } @@ -95,9 +95,8 @@ public bool SetTimestamp(bool b) public string ToStr() { - ZString zs = ZConfigToStr(ref native); - string result = ZTypes.ZStringToString(zs); - ZTypes.ZStringFree(ref zs); + IntPtr p = ZCConfigToString(ref native); + string result = Marshal.PtrToStringAnsi(p); return result; } @@ -105,34 +104,34 @@ private bool InsertJson(string key, string value) { IntPtr k = Marshal.StringToHGlobalAnsi(key); IntPtr v = Marshal.StringToHGlobalAnsi(value); - bool r = ZConfigInsertJson(ref native, k, v); + sbyte r = ZCConfigInsertJson(ref native, k, v); Marshal.FreeHGlobal(k); Marshal.FreeHGlobal(v); - return r; + return r == 1 ? true : false; } [DllImport(Zenoh.DllName, EntryPoint = "z_config_default", CallingConvention = CallingConvention.Cdecl)] internal static extern NativeType ZConfigDefault(); - [DllImport(Zenoh.DllName, EntryPoint = "z_config_from_str", CallingConvention = CallingConvention.Cdecl)] - internal static extern NativeType ZConfigFromStr([MarshalAs(UnmanagedType.LPStr)] string str); + [DllImport(Zenoh.DllName, EntryPoint = "z_config_drop", CallingConvention = CallingConvention.Cdecl)] + internal static extern void ZConfigDrop(ref NativeType config); - [DllImport(Zenoh.DllName, EntryPoint = "z_config_from_file", CallingConvention = CallingConvention.Cdecl)] - internal static extern NativeType ZConfigFromFile([MarshalAs(UnmanagedType.LPStr)] string path); + [DllImport(Zenoh.DllName, EntryPoint = "zc_config_from_str", CallingConvention = CallingConvention.Cdecl)] + internal static extern NativeType ZCConfigFromStr([MarshalAs(UnmanagedType.LPStr)] string str); + + [DllImport(Zenoh.DllName, EntryPoint = "zc_config_from_file", CallingConvention = CallingConvention.Cdecl)] + internal static extern NativeType ZCConfigFromFile([MarshalAs(UnmanagedType.LPStr)] string path); [DllImport(Zenoh.DllName, EntryPoint = "z_config_check", CallingConvention = CallingConvention.Cdecl)] internal static extern bool ZConfigCheck(ref NativeType config); - [DllImport(Zenoh.DllName, EntryPoint = "z_config_to_str", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZString ZConfigToStr(ref NativeType config); - - [DllImport(Zenoh.DllName, EntryPoint = "z_config_get", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZString ZConfigGet(ref NativeType config, ZString key); + [DllImport(Zenoh.DllName, EntryPoint = "zc_config_to_string", CallingConvention = CallingConvention.Cdecl)] + internal static extern IntPtr ZCConfigToString(ref NativeType config); - [DllImport(Zenoh.DllName, EntryPoint = "z_config_insert_json", CallingConvention = CallingConvention.Cdecl)] - internal static extern bool ZConfigInsertJson(ref NativeType config, IntPtr key, IntPtr value); + [DllImport(Zenoh.DllName, EntryPoint = "zc_config_get", CallingConvention = CallingConvention.Cdecl)] + internal static extern IntPtr ZCConfigGet(ref NativeType config, ZString key); - [DllImport(Zenoh.DllName, EntryPoint = "z_config_free", CallingConvention = CallingConvention.Cdecl)] - internal static extern void ZConfigFree(ref NativeType config); + [DllImport(Zenoh.DllName, EntryPoint = "zc_config_insert_json", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte ZCConfigInsertJson(ref NativeType config, IntPtr key, IntPtr value); } } \ No newline at end of file diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs index 2d0a2ee..d704c20 100644 --- a/Zenoh/Session.cs +++ b/Zenoh/Session.cs @@ -63,10 +63,81 @@ public void Close() ZClose(ref native); } - public Info Info() + public string LocalZid() { - global::Zenoh.Info.NativeType n = ZInfo(ref native); - return new Info(n); + Zid zid = ZInfoZid(ref native); + return zid.ToStr(); + } + + + private static List GetIdStrings(IntPtr buf) + { + List list = new List(); + int len = Marshal.ReadByte(buf); + for (int i = 0; i < len; i++) + { + byte[] b = new byte[Zenoh.IdLength]; + Marshal.Copy(buf + 1 + Zenoh.IdLength * i, b, 0, Zenoh.IdLength); + list.Add(Zenoh.IdBytesToStr(b)); + } + + return list; + } + + private static void InfoZidCallback(ref Zid zid, IntPtr buf) + { + int i = Marshal.ReadByte(buf); + if (i >= Zenoh.RoutersNum) + { + return; + } + + Marshal.Copy(zid.id, 0, buf + 1 + Zenoh.IdLength * i, Zenoh.IdLength); + Marshal.WriteByte(buf, (byte)(i + 1)); + } + + public string[] RoutersZid() + { + int length = 1 + Zenoh.IdLength * Zenoh.RoutersNum; + IntPtr buffer = Marshal.AllocHGlobal(length); + for (int i = 0; i < length; i++) + { + Marshal.WriteByte(buffer, i, 0); + } + + ZClosureZid closure = new ZClosureZid(InfoZidCallback, buffer); + sbyte r = ZInfoRoutersZid(ref native, ref closure); + if (r != 0) + { + return Array.Empty(); + } + + var output = GetIdStrings(buffer); + + Marshal.FreeHGlobal(buffer); + return output.ToArray(); + } + + public string[] PeersZid() + { + int length = 1 + Zenoh.IdLength * Zenoh.PeersNum; + IntPtr buffer = Marshal.AllocHGlobal(length); + for (int i = 0; i < length; i++) + { + Marshal.WriteByte(buffer, i, 0); + } + + ZClosureZid closure = new ZClosureZid(InfoZidCallback, buffer); + sbyte r = ZInfoPeersZid(ref native, ref closure); + if (r != 0) + { + return Array.Empty(); + } + + var output = GetIdStrings(buffer); + + Marshal.FreeHGlobal(buffer); + return output.ToArray(); } public bool Put(KeyExpr key, string value) @@ -133,11 +204,14 @@ public void UnregisterSubscriber(string key) [DllImport(Zenoh.DllName, EntryPoint = "z_close", CallingConvention = CallingConvention.Cdecl)] internal static extern void ZClose(ref NativeType session); - [DllImport(Zenoh.DllName, EntryPoint = "z_info", CallingConvention = CallingConvention.Cdecl)] - internal static extern Info.NativeType ZInfo(ref NativeType session); + [DllImport(Zenoh.DllName, EntryPoint = "z_info_zid", CallingConvention = CallingConvention.Cdecl)] + internal static extern Zid ZInfoZid(ref NativeType session); + + [DllImport(Zenoh.DllName, EntryPoint = "z_info_peers_zid", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte ZInfoPeersZid(ref NativeType session, ref ZClosureZid callback); - [DllImport(Zenoh.DllName, EntryPoint = "z_info_as_str", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZString ZInfoAsStr(ref Session.NativeType session); + [DllImport(Zenoh.DllName, EntryPoint = "z_info_routers_zid", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte ZInfoRoutersZid(ref NativeType session, ref ZClosureZid callback); [DllImport(Zenoh.DllName, EntryPoint = "z_put", CallingConvention = CallingConvention.Cdecl)] internal static extern int ZPut(ref NativeType session, KeyExpr.NativeType keyexpr, IntPtr payload, ulong len); diff --git a/Zenoh/Zenoh.cs b/Zenoh/Zenoh.cs index 029db67..b902a3e 100644 --- a/Zenoh/Zenoh.cs +++ b/Zenoh/Zenoh.cs @@ -15,6 +15,7 @@ using System; using System.Runtime.InteropServices; using System.Collections.Generic; +using System.Text; namespace Zenoh @@ -23,8 +24,23 @@ public static class Zenoh { public const string DllName = "zenohc"; - //[DllImport(Zenoh.DllName, EntryPoint = "z_init_logger")] - //public static extern void InitLogger(); + [DllImport(Zenoh.DllName, EntryPoint = "zc_init_logger")] + public static extern void InitLogger(); + + public static string IdBytesToStr(byte[] buf) + { + StringBuilder str = new StringBuilder(); + for (int i = buf.Length - 1; i >= 0; i--) + { + str.Append($"{buf[i]:X2}"); + } + + return str.ToString(); + } + + internal const int RoutersNum = 128; + internal const int PeersNum = 256; + internal const int IdLength = 16; } public enum KnownEncoding : int // e_known_encoding_t @@ -125,76 +141,35 @@ public Period(uint origin, uint period, uint duration) } } - public class Info : IDisposable + [StructLayout(LayoutKind.Sequential)] + public struct Zid { - [StructLayout(LayoutKind.Sequential)] - public struct NativeType // z_owned_info_t - { - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] - internal UInt64[] align; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + internal byte[] id; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] - internal UIntPtr[] pad; - } - - internal NativeType native; - private bool _disposed; - - internal Info(NativeType natvie) + public string ToStr() { - native = natvie; - _disposed = false; - } - - public void Dispose() => Dispose(true); - - private void Dispose(bool disposing) - { - if (_disposed) - { - return; - } - - ZInfoFree(ref native); - _disposed = true; + return Zenoh.IdBytesToStr(id); } + } - public String Pid() - { - uint key = 0x00; - return InfoGet(key); - } - public String[] PeerPid() - { - uint key = 0x01; - string r = InfoGet(key); - return r.Split(','); - } + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ZOwnedClosureZidCallback(ref Zid zid, IntPtr p); - public String[] RouterPid() - { - uint key = 0x02; - string r = InfoGet(key); - return r.Split(','); - } + [StructLayout(LayoutKind.Sequential)] + struct ZClosureZid // z_owned_closure_zid_t + { + internal IntPtr context; + internal ZOwnedClosureZidCallback call; + internal IntPtr drop; - private String InfoGet(uint key) + public ZClosureZid(ZOwnedClosureZidCallback call, IntPtr context) { - ZString s = ZInfoGet(ref native, key); - string result = ZTypes.ZStringToString(s); - ZTypes.ZStringFree(ref s); - return result; + this.context = context; + this.call = call; + this.drop = IntPtr.Zero; } - - [DllImport(Zenoh.DllName, EntryPoint = "z_info_free", CallingConvention = CallingConvention.Cdecl)] - internal static extern void ZInfoFree(ref NativeType info); - - [DllImport(Zenoh.DllName, EntryPoint = "z_info_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern bool ZInfoCheck(ref NativeType info); - - [DllImport(Zenoh.DllName, EntryPoint = "z_info_get", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZString ZInfoGet(ref NativeType info, UInt64 key); } public class KeyExpr : IDisposable @@ -461,7 +436,7 @@ internal static string ZStringToString(ZString zs) [DllImport(Zenoh.DllName, EntryPoint = "z_string_check", CallingConvention = CallingConvention.Cdecl)] internal static extern bool ZStringCheck(ref ZString s); - + [DllImport(Zenoh.DllName, EntryPoint = "z_bytes_free", CallingConvention = CallingConvention.Cdecl)] internal static extern void ZBytesFree(ref ZString s); } diff --git a/examples/Zenoh/ZInfo.cs b/examples/Zenoh/ZInfo.cs index cfaa73c..90e481a 100644 --- a/examples/Zenoh/ZInfo.cs +++ b/examples/Zenoh/ZInfo.cs @@ -26,13 +26,12 @@ return; } +string pid = session.LocalZid(); +string[] routerPid = session.RoutersZid(); +string[] peerPid = session.PeersZid(); -Info info = session.Info(); -string pid = info.Pid(); -string[] routerPid = info.RouterPid(); -string[] peerPid = info.PeerPid(); -info.Dispose(); - -Console.WriteLine($"PID: {pid}\nPeer PID: {String.Join(',', peerPid)}\nRouter PID: {String.Join(',', routerPid)}"); +Console.WriteLine($"Local PID: {pid}"); +Console.WriteLine($"PeersPID: {String.Join(',', peerPid)}"); +Console.WriteLine($"Routers PID: {String.Join(',', routerPid)}"); session.Close(); \ No newline at end of file From 897d052a5d628f88982a90720dd18b8144b75831 Mon Sep 17 00:00:00 2001 From: sanri Date: Fri, 11 Nov 2022 20:01:51 +0800 Subject: [PATCH 09/34] 20221111 --- Zenoh/Session.cs | 9 +-- Zenoh/Zenoh.cs | 118 ++++++++++++++++++++++++++++------------ examples/Zenoh/ZGet.cs | 4 +- examples/Zenoh/ZInfo.cs | 4 +- examples/Zenoh/ZPut.cs | 10 ++-- 5 files changed, 98 insertions(+), 47 deletions(-) diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs index d704c20..87cfc04 100644 --- a/Zenoh/Session.cs +++ b/Zenoh/Session.cs @@ -140,12 +140,12 @@ public string[] PeersZid() return output.ToArray(); } - public bool Put(KeyExpr key, string value) + public bool Put(KeyExpr key, string value, ref PutOptions options) { byte[] data = Encoding.UTF8.GetBytes(value); IntPtr v = Marshal.AllocHGlobal(data.Length); Marshal.Copy(data, 0, v, data.Length); - int r = ZPut(ref native, key.native, v, (ulong)data.Length); + int r = ZPut(ref native, key.native, v, (ulong)data.Length, ref options.native); Marshal.FreeHGlobal(v); if (r == 0) { @@ -178,7 +178,7 @@ public bool RegisterSubscriber(SubInfo subInfo, Subscriber subscriber) if (ZSubscriberCheck(ref nativeSubscriber)) { subscriber.nativeSubscriber = nativeSubscriber; - Subscribers[subscriber.key.Suffix] = subscriber; + Subscribers[subscriber.key.GetStr()] = subscriber; return true; } else @@ -214,7 +214,8 @@ public void UnregisterSubscriber(string key) internal static extern sbyte ZInfoRoutersZid(ref NativeType session, ref ZClosureZid callback); [DllImport(Zenoh.DllName, EntryPoint = "z_put", CallingConvention = CallingConvention.Cdecl)] - internal static extern int ZPut(ref NativeType session, KeyExpr.NativeType keyexpr, IntPtr payload, ulong len); + internal static extern int ZPut(ref NativeType session, KeyExpr.NativeType keyexpr, IntPtr payload, ulong len, + ref PutOptions.NativeType opts); [DllImport(Zenoh.DllName, EntryPoint = "z_subscriber_check")] internal static extern bool ZSubscriberCheck(ref Subscriber.NativeType sub); diff --git a/Zenoh/Zenoh.cs b/Zenoh/Zenoh.cs index b902a3e..cafe2de 100644 --- a/Zenoh/Zenoh.cs +++ b/Zenoh/Zenoh.cs @@ -43,7 +43,7 @@ public static string IdBytesToStr(byte[] buf) internal const int IdLength = 16; } - public enum KnownEncoding : int // e_known_encoding_t + public enum ZEncodingPrefix : int // z_encoding_prefix_t { Empty = 0, AppOctetStream = 1, @@ -68,6 +68,23 @@ public enum KnownEncoding : int // e_known_encoding_t ImageGif = 20, } + public enum ZCongestionControl : int // z_congestion_control_t + { + Block, + Drop, + } + + public enum ZPriority : int // z_priority_t + { + RealTime = 1, + InteractiveHigh = 2, + InteractiveLow = 3, + DataHigh = 4, + Data = 5, + DataLow = 6, + Background = 7, + } + public enum ConsolidationMode : int // z_consolidation_mode_t { Full, @@ -117,13 +134,21 @@ public struct ZString // z_owned_string_t, z_string_t [StructLayout(LayoutKind.Sequential)] public struct ZEncoding // z_encoding_t { - internal KnownEncoding prefix; + internal ZEncodingPrefix prefix; internal ZBytes suffix; public String PrefixToString() { return prefix.ToString(); } + + public static ZEncoding New(ZEncodingPrefix prefix) + { + return FnZEncoding(prefix, IntPtr.Zero); + } + + [DllImport(Zenoh.DllName, EntryPoint = "z_encoding", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZEncoding FnZEncoding(ZEncodingPrefix prefix, IntPtr suffix); } [StructLayout(LayoutKind.Sequential)] @@ -153,6 +178,41 @@ public string ToStr() } } + public struct PutOptions + { + [StructLayout(LayoutKind.Sequential)] + internal struct NativeType // z_put_options_t + { + internal ZEncoding encoding; + internal ZCongestionControl congestionControl; + internal ZPriority priority; + } + + internal NativeType native; + + public PutOptions() + { + native = ZPutOptionsDefault(); + } + + public void SetEncoding(ZEncoding encoding) + { + native.encoding = encoding; + } + + public void SetCongestionControl(ZCongestionControl congestionControl) + { + native.congestionControl = congestionControl; + } + + public void SetPriority(ZPriority priority) + { + native.priority = priority; + } + + [DllImport(Zenoh.DllName, EntryPoint = "z_put_options_default", CallingConvention = CallingConvention.Cdecl)] + private static extern NativeType ZPutOptionsDefault(); + } [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate void ZOwnedClosureZidCallback(ref Zid zid, IntPtr p); @@ -172,59 +232,47 @@ public ZClosureZid(ZOwnedClosureZidCallback call, IntPtr context) } } - public class KeyExpr : IDisposable + public class KeyExpr { [StructLayout(LayoutKind.Sequential)] - public struct NativeType // z_keyexpr_t + public struct NativeType // z_keyexpr_t { - internal ulong id; - internal ZBytes suffix; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] + private UInt64[] _align; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] + private UIntPtr[] _padding; } internal NativeType native; - private bool _disposed = false; + internal string key; - public ulong Id - { - get { return native.id; } - } - - public string Suffix - { - get { return Marshal.PtrToStringAnsi(native.suffix.start, (int)native.suffix.len); } - } - - internal KeyExpr(NativeType native) + internal KeyExpr(NativeType native, string key) { this.native = native; + this.key = key; } public static KeyExpr FromString(string name) { IntPtr p = Marshal.StringToHGlobalAnsi(name); - NativeType key = ZExprNew(p); + NativeType keyexpr = ZKeyexpr(p); Marshal.FreeHGlobal(p); - return new KeyExpr(key); + return new KeyExpr(keyexpr,name); } - public void Dispose() => Dispose(true); - - protected virtual void Dispose(bool disposing) + public string GetStr() { - if (_disposed) - { - return; - } - - ZKeyexprFree(ref native); - _disposed = true; + //IntPtr ptr = ZKeyexprToString(native); + //string output = Marshal.PtrToStringAnsi(ptr); + return this.key; } - [DllImport(Zenoh.DllName, EntryPoint = "z_expr_new", CallingConvention = CallingConvention.Cdecl)] - internal static extern NativeType ZExprNew(IntPtr name); - - [DllImport(Zenoh.DllName, EntryPoint = "z_keyexpr_free", CallingConvention = CallingConvention.Cdecl)] - internal static extern void ZKeyexprFree(ref NativeType key); + [DllImport(Zenoh.DllName, EntryPoint = "z_keyexpr", CallingConvention = CallingConvention.Cdecl)] + internal static extern NativeType ZKeyexpr(IntPtr name); + + [DllImport(Zenoh.DllName, EntryPoint = "z_keyexpr_to_string", CallingConvention = CallingConvention.Cdecl)] + internal static extern IntPtr ZKeyexprToString(NativeType keyexpr); } public class QueryTarget diff --git a/examples/Zenoh/ZGet.cs b/examples/Zenoh/ZGet.cs index 13dda29..09d8275 100644 --- a/examples/Zenoh/ZGet.cs +++ b/examples/Zenoh/ZGet.cs @@ -23,7 +23,7 @@ KeyExpr key1 = KeyExpr.FromString("/demo/example/zenoh-cs-put1"); KeyExpr key2 = KeyExpr.FromString("/demo/example/zenoh-cs-put2"); -Console.WriteLine($"Sending Query '{key1.Suffix}'"); +Console.WriteLine($"Sending Query '{key1.GetStr()}'"); ReplayDataArray data1 = session.Get(key1); foreach (ReplayData ele in data1.List) { @@ -33,7 +33,7 @@ ReplayDataArray data2 = session.Get(key2); -Console.WriteLine($"Sending Query '{key2.Suffix}'"); +Console.WriteLine($"Sending Query '{key2.GetStr()}'"); foreach (ReplayData ele in data2.List) { string s = $">> Received ('{ele.Sample.Key}': '{ele.Sample.ValueToString()}'"; diff --git a/examples/Zenoh/ZInfo.cs b/examples/Zenoh/ZInfo.cs index 90e481a..b5cb490 100644 --- a/examples/Zenoh/ZInfo.cs +++ b/examples/Zenoh/ZInfo.cs @@ -5,13 +5,13 @@ //Zenoh.Zenoh.InitLogger(); Config config = new Config(); -config.SetId("01"); +//config.SetId("01"); //string[] c = { "tcp/172.30.100.3:7447", "tcp/172.30.100.1:7447" }; //config.SetConnect(c); config.SetMode(Config.Mode.Client); config.SetTimestamp(true); string configStr = config.ToStr(); -Console.WriteLine($"config string:\n{configStr}"); +Console.WriteLine($"config string:\n{configStr}\n----------------------\n"); Session session = new Session(); Console.WriteLine("Opening session..."); diff --git a/examples/Zenoh/ZPut.cs b/examples/Zenoh/ZPut.cs index cef5495..d15263f 100644 --- a/examples/Zenoh/ZPut.cs +++ b/examples/Zenoh/ZPut.cs @@ -19,18 +19,20 @@ return; } -KeyExpr key1 = KeyExpr.FromString("/demo/example/zenoh-cs-put1"); -KeyExpr key2 = KeyExpr.FromString("/demo/example/zenoh-cs-put2"); +KeyExpr key1 = KeyExpr.FromString("demo/example/zenoh-cs-put1"); +KeyExpr key2 = KeyExpr.FromString("demo/example/zenoh-cs-put2"); +PutOptions options = new PutOptions(); +options.SetEncoding(ZEncoding.New(ZEncodingPrefix.TextPlain)); for (int i = 0; i < 5; i++) { KeyExpr key = (i % 2) == 0 ? key1 : key2; string data = $"Put from csharp {i}!"; - bool r = session.Put(key, data); + bool r = session.Put(key, data,ref options); if (r) { - Console.WriteLine($"Putting Data ('{key.Suffix}': '{data}')"); + Console.WriteLine($"Putting Data ('{key.GetStr()}': '{data}')"); Thread.Sleep(50); } else From 8f9a249f4421e441a53076a9db3ff36e2da1209e Mon Sep 17 00:00:00 2001 From: sanri Date: Sun, 13 Nov 2022 17:38:05 +0800 Subject: [PATCH 10/34] 20221113 --- Zenoh/Session.cs | 30 ++++++++---- Zenoh/Subscriber.cs | 40 +++++++++++++-- Zenoh/Zenoh.cs | 108 ++++++++++++++++++++--------------------- examples/Zenoh/ZGet.cs | 11 +++-- examples/Zenoh/ZPut.cs | 8 +-- examples/Zenoh/ZSub.cs | 14 +++--- 6 files changed, 128 insertions(+), 83 deletions(-) diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs index 87cfc04..1b94b7a 100644 --- a/Zenoh/Session.cs +++ b/Zenoh/Session.cs @@ -143,11 +143,12 @@ public string[] PeersZid() public bool Put(KeyExpr key, string value, ref PutOptions options) { byte[] data = Encoding.UTF8.GetBytes(value); - IntPtr v = Marshal.AllocHGlobal(data.Length); + IntPtr v = Marshal.AllocHGlobal(data.Length + 1); Marshal.Copy(data, 0, v, data.Length); + Marshal.WriteByte(v, data.Length, 0); int r = ZPut(ref native, key.native, v, (ulong)data.Length, ref options.native); Marshal.FreeHGlobal(v); - if (r == 0) + if (r == 1) { return true; } @@ -157,6 +158,7 @@ public bool Put(KeyExpr key, string value, ref PutOptions options) } } + /* public ReplayDataArray Get(KeyExpr key) { QueryTarget target = new QueryTarget(); @@ -169,12 +171,18 @@ public ReplayDataArray Get(KeyExpr key) Marshal.FreeHGlobal(predicate); return new ReplayDataArray(nativeData); } + */ - public bool RegisterSubscriber(SubInfo subInfo, Subscriber subscriber) + public bool RegisterSubscriber(Subscriber subscriber) { - Subscriber.NativeType nativeSubscriber = ZSubscribe(ref native, subscriber.key.native, subInfo.native, - subscriber.nativeCallback, - IntPtr.Zero); + return RegisterSubscriber(subscriber, Subscriber.GetOptionsDefault()); + } + + public bool RegisterSubscriber(Subscriber subscriber, Subscriber.Options options) + { + Subscriber.NativeType nativeSubscriber = ZDeclareSubscribe(ref native, subscriber.key.native, + ref subscriber.closure, + ref options); if (ZSubscriberCheck(ref nativeSubscriber)) { subscriber.nativeSubscriber = nativeSubscriber; @@ -220,17 +228,19 @@ internal static extern int ZPut(ref NativeType session, KeyExpr.NativeType keyex [DllImport(Zenoh.DllName, EntryPoint = "z_subscriber_check")] internal static extern bool ZSubscriberCheck(ref Subscriber.NativeType sub); - [DllImport(Zenoh.DllName, EntryPoint = "z_subscribe")] - internal static extern Subscriber.NativeType ZSubscribe(ref NativeType session, KeyExpr.NativeType keyexpr, - SubInfo.NativeType sub_info, - SubscriberCallbackNative callback, IntPtr arg); + [DllImport(Zenoh.DllName, EntryPoint = "z_declare_subscriber")] + internal static extern Subscriber.NativeType ZDeclareSubscribe(ref NativeType session, + KeyExpr.NativeType keyexpr, + ref ZClosureSample callback, ref Subscriber.Options opts); [DllImport(Zenoh.DllName, EntryPoint = "z_subscriber_close")] internal static extern void ZSubscriberClose(ref Subscriber.NativeType sub); + /* [DllImport(Zenoh.DllName, EntryPoint = "z_get_collect", CallingConvention = CallingConvention.Cdecl)] internal static extern ReplayDataArray.NativeType ZGetCollect(ref NativeType session, KeyExpr.NativeType keyexpr, IntPtr predicate, QueryTarget.NativeType target, QueryConsolidation.NativeType consolidation); + */ } } \ No newline at end of file diff --git a/Zenoh/Subscriber.cs b/Zenoh/Subscriber.cs index 96f1394..02eb191 100644 --- a/Zenoh/Subscriber.cs +++ b/Zenoh/Subscriber.cs @@ -6,8 +6,25 @@ namespace Zenoh public delegate void SubscriberCallback(Sample sample); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - internal delegate void - SubscriberCallbackNative(ref Sample.NativeType samplePtr, IntPtr arg); + internal delegate void ZOwnedClosureSampleCallback(ref Sample.NativeType sample, IntPtr context); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ZOwnedClosureSampleDrop(ref Sample.NativeType sample, IntPtr context); + + [StructLayout(LayoutKind.Sequential)] + internal struct ZClosureSample // z_owned_closure_sample_t + { + internal IntPtr context; + internal ZOwnedClosureSampleCallback call; + internal ZOwnedClosureSampleDrop drop; + + internal ZClosureSample(ZOwnedClosureSampleCallback call, ZOwnedClosureSampleDrop drop, IntPtr context) + { + this.call = call; + this.drop = drop; + this.context = context; + } + } public class Subscriber { @@ -17,17 +34,24 @@ public struct NativeType // z_owned_subscriber_t public IntPtr p; } + [StructLayout(LayoutKind.Sequential)] + public struct Options // z_subscriber_options_t + { + public Reliability reliability; + } + internal KeyExpr key; internal NativeType nativeSubscriber; internal SubscriberCallback userCallback; - internal SubscriberCallbackNative nativeCallback; + internal ZClosureSample closure; public Subscriber(string key, SubscriberCallback userCallback) { this.key = KeyExpr.FromString(key); this.nativeSubscriber.p = IntPtr.Zero; this.userCallback = userCallback; - this.nativeCallback = new SubscriberCallbackNative(SubscriberNativeCallbackImpl); + ZOwnedClosureSampleCallback call = SubscriberNativeCallbackImpl; + this.closure = new ZClosureSample(call, null, IntPtr.Zero); } internal void SubscriberNativeCallbackImpl(ref Sample.NativeType samplePtr, IntPtr arg) @@ -35,5 +59,13 @@ internal void SubscriberNativeCallbackImpl(ref Sample.NativeType samplePtr, IntP Sample s = new Sample(samplePtr); this.userCallback(s); } + + public static Options GetOptionsDefault() + { + return ZSubScriberOptionsDefault(); + } + + [DllImport(Zenoh.DllName, EntryPoint = "z_subscriber_options_default")] + internal static extern Options ZSubScriberOptionsDefault(); } } \ No newline at end of file diff --git a/Zenoh/Zenoh.cs b/Zenoh/Zenoh.cs index cafe2de..0ad9402 100644 --- a/Zenoh/Zenoh.cs +++ b/Zenoh/Zenoh.cs @@ -68,7 +68,13 @@ public enum ZEncodingPrefix : int // z_encoding_prefix_t ImageGif = 20, } - public enum ZCongestionControl : int // z_congestion_control_t + public enum ZSampleKind : int // z_sample_kind_t + { + Put = 0, + Delete = 1, + } + + public enum ZCongestionControl : int // z_congestion_control_t { Block, Drop, @@ -125,6 +131,13 @@ public struct ZBytes // z_bytes_t, z_owned_bytes_t public ulong len; } + [StructLayout(LayoutKind.Sequential)] + public struct ZTimestamp // z_timestamp_t + { + public UInt64 time; + public ZBytes id; + } + [StructLayout(LayoutKind.Sequential)] public struct ZString // z_owned_string_t, z_string_t { @@ -232,45 +245,62 @@ public ZClosureZid(ZOwnedClosureZidCallback call, IntPtr context) } } - public class KeyExpr + public class KeyExpr : IDisposable { [StructLayout(LayoutKind.Sequential)] public struct NativeType // z_keyexpr_t { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] private UInt64[] _align; - + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] private UIntPtr[] _padding; + + internal string GetStr() + { + IntPtr ptr = ZKeyexprToString(this); + string output = Marshal.PtrToStringAnsi(ptr); + // should free ptr + return output; + } } internal NativeType native; - internal string key; + private IntPtr _key_buf; + private bool _disposed; + + public void Dispose() => Dispose(true); + + protected virtual void Dispose(bool disposing) + { + if (_disposed) return; + Marshal.FreeHGlobal(this._key_buf); + _disposed = true; + } - internal KeyExpr(NativeType native, string key) + internal KeyExpr(NativeType native, IntPtr keyBuf) { this.native = native; - this.key = key; + this._key_buf = keyBuf; + this._disposed = false; } public static KeyExpr FromString(string name) { IntPtr p = Marshal.StringToHGlobalAnsi(name); NativeType keyexpr = ZKeyexpr(p); - Marshal.FreeHGlobal(p); - return new KeyExpr(keyexpr,name); + //Marshal.FreeHGlobal(p); + return new KeyExpr(keyexpr, p); } public string GetStr() { - //IntPtr ptr = ZKeyexprToString(native); - //string output = Marshal.PtrToStringAnsi(ptr); - return this.key; + return native.GetStr(); } [DllImport(Zenoh.DllName, EntryPoint = "z_keyexpr", CallingConvention = CallingConvention.Cdecl)] internal static extern NativeType ZKeyexpr(IntPtr name); - + [DllImport(Zenoh.DllName, EntryPoint = "z_keyexpr_to_string", CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr ZKeyexprToString(NativeType keyexpr); } @@ -333,12 +363,16 @@ public QueryConsolidation() public class Sample { - [StructLayout(LayoutKind.Explicit)] + //[StructLayout(LayoutKind.Explicit)] + //[FieldOffset(0)] + [StructLayout(LayoutKind.Sequential)] public struct NativeType // z_owned_sample_t { - [FieldOffset(0)] internal KeyExpr.NativeType key; - [FieldOffset(24)] internal ZBytes value; - [FieldOffset(48)] internal ZEncoding encoding; + internal KeyExpr.NativeType keyexpr; + internal ZBytes payload; + internal ZEncoding encoding; + internal ZSampleKind kind; + internal ZTimestamp timestamp; } public string Key { get; } @@ -347,8 +381,8 @@ public struct NativeType // z_owned_sample_t internal Sample(NativeType sample) { - Key = Marshal.PtrToStringAnsi(sample.key.suffix.start, (int)sample.key.suffix.len); - Value = ZTypes.ZBytesToBytesArray(sample.value); + Key = sample.keyexpr.GetStr(); + Value = ZTypes.ZBytesToBytesArray(sample.payload); Encoding = sample.encoding; } @@ -357,9 +391,6 @@ public string ValueToString() string result = System.Text.Encoding.UTF8.GetString(Value, 0, Value.Length); return result; } - - [DllImport(Zenoh.DllName, EntryPoint = "z_sample_free", CallingConvention = CallingConvention.Cdecl)] - internal static extern void ZSampleFree(ref NativeType sample); } public class ReplayData @@ -384,6 +415,7 @@ public ReplayData(NativeType native) } } + /* public class ReplayDataArray { [StructLayout(LayoutKind.Sequential)] @@ -423,39 +455,7 @@ public ReplayDataArray(NativeType native) [DllImport(Zenoh.DllName, EntryPoint = "z_reply_data_array_free", CallingConvention = CallingConvention.Cdecl)] internal static extern void ZReplyDataArrayFree(ref NativeType replies); } - - public class SubInfo - { - [StructLayout(LayoutKind.Sequential)] - public struct NativeType // z_subinfo_t - { - public Reliability reliability; - public SubMode mode; - public Period period; - } - - internal NativeType native; - - public SubInfo() - { - this.native = ZSubInfoDefault(); - } - - public SubInfo(SubMode mode) : this() - { - this.native.mode = mode; - } - - public SubInfo(Reliability reliability, SubMode mode) : this() - { - native.reliability = reliability; - native.mode = mode; - } - - [DllImport(Zenoh.DllName, EntryPoint = "z_subinfo_default", CallingConvention = CallingConvention.Cdecl)] - internal static extern NativeType ZSubInfoDefault(); - } - + */ internal static class ZTypes { diff --git a/examples/Zenoh/ZGet.cs b/examples/Zenoh/ZGet.cs index 09d8275..8403f1c 100644 --- a/examples/Zenoh/ZGet.cs +++ b/examples/Zenoh/ZGet.cs @@ -2,6 +2,7 @@ using System.Threading; using Zenoh; +/* //Zenoh.Zenoh.InitLogger(); Config config = new Config(); //string[] connect = {"tcp/172.30.100.3:7447"}; @@ -20,8 +21,8 @@ return; } -KeyExpr key1 = KeyExpr.FromString("/demo/example/zenoh-cs-put1"); -KeyExpr key2 = KeyExpr.FromString("/demo/example/zenoh-cs-put2"); +KeyExpr key1 = KeyExpr.FromString("demo/example/zenoh-cs-put1"); +KeyExpr key2 = KeyExpr.FromString("demo/example/zenoh-cs-put2"); Console.WriteLine($"Sending Query '{key1.GetStr()}'"); ReplayDataArray data1 = session.Get(key1); @@ -40,4 +41,8 @@ Console.WriteLine(s); } -session.Close(); \ No newline at end of file +session.Close(); + +*/ + +Console.WriteLine("hello"); \ No newline at end of file diff --git a/examples/Zenoh/ZPut.cs b/examples/Zenoh/ZPut.cs index d15263f..eab85c9 100644 --- a/examples/Zenoh/ZPut.cs +++ b/examples/Zenoh/ZPut.cs @@ -2,10 +2,10 @@ using System.Threading; using Zenoh; -//Zenoh.Zenoh.InitLogger(); +Zenoh.Zenoh.InitLogger(); Config config = new Config(); -//string[] connect = {"tcp/172.30.100.3:7447"}; -//config.SetConnect(connect); +string[] connect = { "tcp/172.19.94.129:7447" }; +config.SetConnect(connect); Session session = new Session(); Console.WriteLine("Opening session..."); @@ -29,7 +29,7 @@ KeyExpr key = (i % 2) == 0 ? key1 : key2; string data = $"Put from csharp {i}!"; - bool r = session.Put(key, data,ref options); + bool r = session.Put(key, data, ref options); if (r) { Console.WriteLine($"Putting Data ('{key.GetStr()}': '{data}')"); diff --git a/examples/Zenoh/ZSub.cs b/examples/Zenoh/ZSub.cs index a7d560d..22b0e4b 100644 --- a/examples/Zenoh/ZSub.cs +++ b/examples/Zenoh/ZSub.cs @@ -21,37 +21,35 @@ return; } -SubInfo info = new SubInfo(Reliability.Reliable, SubMode.Push); - void Callback1(Sample sample) { string key = sample.Key; - string value = Encoding.UTF8.GetString(sample.Value); + string value = sample.ValueToString(); Console.WriteLine($">> [Subscriber1] Received PUT ('{key}': '{value}')"); } void Callback2(Sample sample) { string key = sample.Key; - string value = Encoding.UTF8.GetString(sample.Value); + string value = sample.ValueToString(); Console.WriteLine($">> [Subscriber2] Received PUT ('{key}': '{value}')"); } SubscriberCallback userCallback1 = Callback1; SubscriberCallback userCallback2 = Callback2; -string key1 = "/demo/example/zenoh-cs-put1"; -string key2 = "/demo/example/zenoh-cs-put2"; +string key1 = "demo/example/zenoh-cs-put1"; +string key2 = "demo/example/zenoh-cs-put2"; Subscriber subscriber1 = new Subscriber(key1, userCallback1); Subscriber subscriber2 = new Subscriber(key2, userCallback2); -if (session.RegisterSubscriber(info, subscriber1)) +if (session.RegisterSubscriber(subscriber1)) { Console.WriteLine($"Registered Subscriber1 On '{key1}'"); } -if (session.RegisterSubscriber(info, subscriber2)) +if (session.RegisterSubscriber(subscriber2)) { Console.WriteLine($"Registered Subscriber2 On '{key2}'"); } From 6b3ab72aa2ec33ff8033b5e164de0f86cfd73994 Mon Sep 17 00:00:00 2001 From: sanri Date: Sun, 13 Nov 2022 23:36:59 +0800 Subject: [PATCH 11/34] 20221113 --- Zenoh/Session.cs | 31 +++++++++++++++++++++++++------ examples/Zenoh/ZPut.cs | 4 +--- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs index 1b94b7a..cf063da 100644 --- a/Zenoh/Session.cs +++ b/Zenoh/Session.cs @@ -140,13 +140,32 @@ public string[] PeersZid() return output.ToArray(); } - public bool Put(KeyExpr key, string value, ref PutOptions options) + public bool Put(KeyExpr key, string s) { - byte[] data = Encoding.UTF8.GetBytes(value); - IntPtr v = Marshal.AllocHGlobal(data.Length + 1); - Marshal.Copy(data, 0, v, data.Length); - Marshal.WriteByte(v, data.Length, 0); - int r = ZPut(ref native, key.native, v, (ulong)data.Length, ref options.native); + byte[] data = Encoding.UTF8.GetBytes(s); + PutOptions options = new PutOptions(); + options.SetEncoding(ZEncoding.New(ZEncodingPrefix.TextPlain)); + return _Put(key, data, ref options); + } + + public bool PutJson(KeyExpr key, string s) + { + byte[] data = Encoding.UTF8.GetBytes(s); + PutOptions options = new PutOptions(); + options.SetEncoding(ZEncoding.New(ZEncodingPrefix.TextJson)); + return _Put(key, data, ref options); + } + +// public bool Put(KeyExpr key, Int64 value) { } + + // public bool Put(KeyExpr key, double value){ } + + + protected bool _Put(KeyExpr key, byte[] value, ref PutOptions options) + { + IntPtr v = Marshal.AllocHGlobal(value.Length); + Marshal.Copy(value, 0, v, value.Length); + int r = ZPut(ref native, key.native, v, (ulong)value.Length, ref options.native); Marshal.FreeHGlobal(v); if (r == 1) { diff --git a/examples/Zenoh/ZPut.cs b/examples/Zenoh/ZPut.cs index eab85c9..8531af9 100644 --- a/examples/Zenoh/ZPut.cs +++ b/examples/Zenoh/ZPut.cs @@ -21,15 +21,13 @@ KeyExpr key1 = KeyExpr.FromString("demo/example/zenoh-cs-put1"); KeyExpr key2 = KeyExpr.FromString("demo/example/zenoh-cs-put2"); -PutOptions options = new PutOptions(); -options.SetEncoding(ZEncoding.New(ZEncodingPrefix.TextPlain)); for (int i = 0; i < 5; i++) { KeyExpr key = (i % 2) == 0 ? key1 : key2; string data = $"Put from csharp {i}!"; - bool r = session.Put(key, data, ref options); + bool r = session.Put(key, data); if (r) { Console.WriteLine($"Putting Data ('{key.GetStr()}': '{data}')"); From d678ef4ec07d2d2d1f307c67f684ab4de70c8fe9 Mon Sep 17 00:00:00 2001 From: sanri Date: Mon, 14 Nov 2022 13:53:59 +0800 Subject: [PATCH 12/34] update to zenohc v0.6 --- Zenoh/Session.cs | 34 ++++++++++++++++++++-------------- Zenoh/Zenoh.cs | 3 ++- Zenoh/Zenoh.csproj | 4 ++-- examples/Zenoh/ZPut.cs | 39 +++++++++++++++++++++------------------ 4 files changed, 45 insertions(+), 35 deletions(-) diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs index cf063da..e7d99b1 100644 --- a/Zenoh/Session.cs +++ b/Zenoh/Session.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Runtime.InteropServices; using System.Text; @@ -140,7 +141,7 @@ public string[] PeersZid() return output.ToArray(); } - public bool Put(KeyExpr key, string s) + public bool PutStr(KeyExpr key, string s) { byte[] data = Encoding.UTF8.GetBytes(s); PutOptions options = new PutOptions(); @@ -152,14 +153,26 @@ public bool PutJson(KeyExpr key, string s) { byte[] data = Encoding.UTF8.GetBytes(s); PutOptions options = new PutOptions(); - options.SetEncoding(ZEncoding.New(ZEncodingPrefix.TextJson)); + options.SetEncoding(ZEncoding.New(ZEncodingPrefix.AppJson)); return _Put(key, data, ref options); } -// public bool Put(KeyExpr key, Int64 value) { } - - // public bool Put(KeyExpr key, double value){ } - + public bool PutInt(KeyExpr key, Int64 value) + { + byte[] data = Encoding.UTF8.GetBytes(value.ToString()); + PutOptions options = new PutOptions(); + options.SetEncoding(ZEncoding.New(ZEncodingPrefix.AppInteger)); + return _Put(key, data, ref options); + } + + public bool PutFloat(KeyExpr key, double value) + { + string s = value.ToString(); + byte[] data = Encoding.UTF8.GetBytes(s); + PutOptions options = new PutOptions(); + options.SetEncoding(ZEncoding.New(ZEncodingPrefix.AppFloat)); + return _Put(key, data, ref options); + } protected bool _Put(KeyExpr key, byte[] value, ref PutOptions options) { @@ -167,14 +180,7 @@ protected bool _Put(KeyExpr key, byte[] value, ref PutOptions options) Marshal.Copy(value, 0, v, value.Length); int r = ZPut(ref native, key.native, v, (ulong)value.Length, ref options.native); Marshal.FreeHGlobal(v); - if (r == 1) - { - return true; - } - else - { - return false; - } + return r == 1; } /* diff --git a/Zenoh/Zenoh.cs b/Zenoh/Zenoh.cs index 0ad9402..34ee5de 100644 --- a/Zenoh/Zenoh.cs +++ b/Zenoh/Zenoh.cs @@ -260,7 +260,8 @@ internal string GetStr() { IntPtr ptr = ZKeyexprToString(this); string output = Marshal.PtrToStringAnsi(ptr); - // should free ptr + // should be free using libc free() + Marshal.FreeHGlobal(ptr); return output; } } diff --git a/Zenoh/Zenoh.csproj b/Zenoh/Zenoh.csproj index b6f61d9..7b90078 100644 --- a/Zenoh/Zenoh.csproj +++ b/Zenoh/Zenoh.csproj @@ -2,7 +2,7 @@ Zenoh Zenoh-CS - 0.0.3 + 0.0.4 sanri EPL-2.0 OR Apache-2.0 Zenoh: Zero Overhead Pub/sub, Store/Query and Compute. @@ -14,7 +14,7 @@ net6.0 true 10 - 0.0.3 + 0.0.4 zenoh diff --git a/examples/Zenoh/ZPut.cs b/examples/Zenoh/ZPut.cs index 8531af9..aaed443 100644 --- a/examples/Zenoh/ZPut.cs +++ b/examples/Zenoh/ZPut.cs @@ -19,25 +19,28 @@ return; } -KeyExpr key1 = KeyExpr.FromString("demo/example/zenoh-cs-put1"); -KeyExpr key2 = KeyExpr.FromString("demo/example/zenoh-cs-put2"); +KeyExpr keyStr = KeyExpr.FromString("demo/example/zenoh-cs-put/string"); +string dataStr = "Put from csharp !"; +Console.WriteLine(session.PutStr(keyStr, dataStr) + ? $"Putting data string ('{keyStr.GetStr()}': '{dataStr}')" + : "Putting data string fault!"); -for (int i = 0; i < 5; i++) -{ - KeyExpr key = (i % 2) == 0 ? key1 : key2; +KeyExpr keyJson = KeyExpr.FromString("demo/example/zenoh-cs-put/json"); +string dataJson = "{\"value\": \"Put from csharp\"}"; +Console.WriteLine(session.PutJson(keyJson, dataJson) + ? $"Putting data json ('{keyJson.GetStr()}': {dataJson})" + : "Putting data json fault!"); - string data = $"Put from csharp {i}!"; - bool r = session.Put(key, data); - if (r) - { - Console.WriteLine($"Putting Data ('{key.GetStr()}': '{data}')"); - Thread.Sleep(50); - } - else - { - Console.WriteLine("Putting Data Fault!"); - break; - } -} +KeyExpr keyInt = KeyExpr.FromString("demo/example/zenoh-cs-put/int"); +Int64 dataInt = 965; +Console.WriteLine(session.PutInt(keyInt, dataInt) + ? $"Putting data int ('{keyInt.GetStr()}': {dataInt})" + : "Putting data int fault!"); + +KeyExpr keyFloat = KeyExpr.FromString("demo/example/zenoh-cs-put/float"); +double dataFloat = 99.6; +Console.WriteLine(session.PutFloat(keyFloat, dataFloat) + ? $"Putting data float ('{keyFloat.GetStr()}': {dataFloat})" + : "Putting data float fault!"); session.Close(); \ No newline at end of file From 82e12ea426daa96ff6c6e9390e7507b0ba226865 Mon Sep 17 00:00:00 2001 From: sanri Date: Tue, 15 Nov 2022 11:01:55 +0800 Subject: [PATCH 13/34] repair "A callback was made on a garbage collected delegate" --- Zenoh/Config.cs | 14 ++++++++++++++ Zenoh/Subscriber.cs | 7 +++++-- Zenoh/Zenoh.csproj | 4 ++-- examples/Zenoh/ZSub.cs | 9 ++++++--- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Zenoh/Config.cs b/Zenoh/Config.cs index 721b0bd..a88b3d7 100644 --- a/Zenoh/Config.cs +++ b/Zenoh/Config.cs @@ -64,6 +64,20 @@ public bool SetConnect(string[] v) value += "]"; return InsertJson(key, value); } + + // The v such as "tcp/127.0.0.1:7888" + public bool SetListen(string[] v) + { + string key = "listen/endpoints"; + string value = "["; + foreach (var ele in v) + { + value += $"\"{ele}\","; + } + + value += "]"; + return InsertJson(key, value); + } public bool SetMode(Mode mode) { diff --git a/Zenoh/Subscriber.cs b/Zenoh/Subscriber.cs index 02eb191..1abf638 100644 --- a/Zenoh/Subscriber.cs +++ b/Zenoh/Subscriber.cs @@ -42,7 +42,9 @@ public struct Options // z_subscriber_options_t internal KeyExpr key; internal NativeType nativeSubscriber; - internal SubscriberCallback userCallback; + internal SubscriberCallback userCallback = null; + internal ZOwnedClosureSampleCallback sampleCallback = null; + internal ZOwnedClosureSampleDrop sampleDrop = null; internal ZClosureSample closure; public Subscriber(string key, SubscriberCallback userCallback) @@ -50,7 +52,8 @@ public Subscriber(string key, SubscriberCallback userCallback) this.key = KeyExpr.FromString(key); this.nativeSubscriber.p = IntPtr.Zero; this.userCallback = userCallback; - ZOwnedClosureSampleCallback call = SubscriberNativeCallbackImpl; + ZOwnedClosureSampleCallback call = new ZOwnedClosureSampleCallback(SubscriberNativeCallbackImpl); + sampleCallback = call; this.closure = new ZClosureSample(call, null, IntPtr.Zero); } diff --git a/Zenoh/Zenoh.csproj b/Zenoh/Zenoh.csproj index 7b90078..43f754d 100644 --- a/Zenoh/Zenoh.csproj +++ b/Zenoh/Zenoh.csproj @@ -2,7 +2,7 @@ Zenoh Zenoh-CS - 0.0.4 + 0.0.5 sanri EPL-2.0 OR Apache-2.0 Zenoh: Zero Overhead Pub/sub, Store/Query and Compute. @@ -14,7 +14,7 @@ net6.0 true 10 - 0.0.4 + 0.0.5 zenoh diff --git a/examples/Zenoh/ZSub.cs b/examples/Zenoh/ZSub.cs index 22b0e4b..be8e746 100644 --- a/examples/Zenoh/ZSub.cs +++ b/examples/Zenoh/ZSub.cs @@ -7,6 +7,8 @@ Config config = new Config(); //string[] connect = {"tcp/172.30.100.3:7447"}; //config.SetConnect(connect); +string[] listen = {"tcp/127.0.0.1:7888"}; +config.SetListen(listen); Session session = new Session(); Console.WriteLine("Opening session..."); @@ -23,9 +25,10 @@ void Callback1(Sample sample) { - string key = sample.Key; - string value = sample.ValueToString(); - Console.WriteLine($">> [Subscriber1] Received PUT ('{key}': '{value}')"); + //string key = sample.Key; + //string value = sample.ValueToString(); + //Console.WriteLine($">> [Subscriber1] Received PUT ('{key}': '{value}')"); + Console.Write("."); } void Callback2(Sample sample) From 38a5232325cffe6bd76eb6bbb613901ad98db7a5 Mon Sep 17 00:00:00 2001 From: sanri Date: Mon, 19 Dec 2022 15:59:39 +0800 Subject: [PATCH 14/34] update to zenohc v0.7 --- Zenoh/Session.cs | 3 +-- Zenoh/Zenoh.csproj | 4 ++-- examples/Zenoh/ZPut.cs | 2 +- examples/Zenoh/ZSub.cs | 16 ++++++++-------- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs index e7d99b1..5785321 100644 --- a/Zenoh/Session.cs +++ b/Zenoh/Session.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.Runtime.InteropServices; using System.Text; @@ -180,7 +179,7 @@ protected bool _Put(KeyExpr key, byte[] value, ref PutOptions options) Marshal.Copy(value, 0, v, value.Length); int r = ZPut(ref native, key.native, v, (ulong)value.Length, ref options.native); Marshal.FreeHGlobal(v); - return r == 1; + return r == 0; } /* diff --git a/Zenoh/Zenoh.csproj b/Zenoh/Zenoh.csproj index 43f754d..a3d5584 100644 --- a/Zenoh/Zenoh.csproj +++ b/Zenoh/Zenoh.csproj @@ -2,7 +2,7 @@ Zenoh Zenoh-CS - 0.0.5 + 0.0.6 sanri EPL-2.0 OR Apache-2.0 Zenoh: Zero Overhead Pub/sub, Store/Query and Compute. @@ -14,7 +14,7 @@ net6.0 true 10 - 0.0.5 + 0.0.6 zenoh diff --git a/examples/Zenoh/ZPut.cs b/examples/Zenoh/ZPut.cs index aaed443..72af839 100644 --- a/examples/Zenoh/ZPut.cs +++ b/examples/Zenoh/ZPut.cs @@ -4,7 +4,7 @@ Zenoh.Zenoh.InitLogger(); Config config = new Config(); -string[] connect = { "tcp/172.19.94.129:7447" }; +string[] connect = { "tcp/127.0.0.1:7447" }; config.SetConnect(connect); Session session = new Session(); diff --git a/examples/Zenoh/ZSub.cs b/examples/Zenoh/ZSub.cs index be8e746..b11faf1 100644 --- a/examples/Zenoh/ZSub.cs +++ b/examples/Zenoh/ZSub.cs @@ -5,10 +5,10 @@ //Zenoh.Zenoh.InitLogger(); Config config = new Config(); -//string[] connect = {"tcp/172.30.100.3:7447"}; -//config.SetConnect(connect); -string[] listen = {"tcp/127.0.0.1:7888"}; -config.SetListen(listen); +string[] connect = {"tcp/127.0.0.1:7447"}; +config.SetConnect(connect); +//string[] listen = {"tcp/127.0.0.1:7888"}; +//config.SetListen(listen); Session session = new Session(); Console.WriteLine("Opening session..."); @@ -25,10 +25,10 @@ void Callback1(Sample sample) { - //string key = sample.Key; - //string value = sample.ValueToString(); - //Console.WriteLine($">> [Subscriber1] Received PUT ('{key}': '{value}')"); - Console.Write("."); + string key = sample.Key; + string value = sample.ValueToString(); + Console.WriteLine($">> [Subscriber1] Received PUT ('{key}': '{value}')"); + //Console.Write("."); } void Callback2(Sample sample) From 01917afd2e7ef3de356e4ae7bff30251b4a6c030 Mon Sep 17 00:00:00 2001 From: sanri Date: Sun, 13 Aug 2023 11:49:21 +0800 Subject: [PATCH 15/34] change project directory --- Zenoh/Config.cs | 16 +-- Zenoh/Session.cs | 34 +++--- Zenoh/Subscriber.cs | 2 +- Zenoh/Zenoh.csproj | 2 +- Zenoh/{Zenoh.cs => ZenohC.cs} | 24 ++--- examples/{Zenoh => }/README.md | 0 examples/{Zenoh => ZGet}/ZGet.cs | 94 ++++++++--------- examples/{Zenoh => ZGet}/ZGet.csproj | 40 +++---- examples/{Zenoh => ZInfo}/ZInfo.cs | 72 ++++++------- examples/{Zenoh => ZInfo}/ZInfo.csproj | 40 +++---- examples/{Zenoh => ZPut}/ZPut.cs | 90 ++++++++-------- examples/{Zenoh => ZPut}/ZPut.csproj | 40 +++---- examples/{Zenoh => ZSub}/ZSub.cs | 138 ++++++++++++------------- examples/{Zenoh => ZSub}/ZSub.csproj | 40 +++---- examples/zenoh-examples.sln | 8 +- 15 files changed, 320 insertions(+), 320 deletions(-) rename Zenoh/{Zenoh.cs => ZenohC.cs} (93%) rename examples/{Zenoh => }/README.md (100%) rename examples/{Zenoh => ZGet}/ZGet.cs (95%) rename examples/{Zenoh => ZGet}/ZGet.csproj (88%) rename examples/{Zenoh => ZInfo}/ZInfo.cs (96%) rename examples/{Zenoh => ZInfo}/ZInfo.csproj (88%) rename examples/{Zenoh => ZPut}/ZPut.cs (95%) rename examples/{Zenoh => ZPut}/ZPut.csproj (88%) rename examples/{Zenoh => ZSub}/ZSub.cs (95%) rename examples/{Zenoh => ZSub}/ZSub.csproj (88%) diff --git a/Zenoh/Config.cs b/Zenoh/Config.cs index a88b3d7..3d53a0e 100644 --- a/Zenoh/Config.cs +++ b/Zenoh/Config.cs @@ -124,28 +124,28 @@ private bool InsertJson(string key, string value) return r == 1 ? true : false; } - [DllImport(Zenoh.DllName, EntryPoint = "z_config_default", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_config_default", CallingConvention = CallingConvention.Cdecl)] internal static extern NativeType ZConfigDefault(); - [DllImport(Zenoh.DllName, EntryPoint = "z_config_drop", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_config_drop", CallingConvention = CallingConvention.Cdecl)] internal static extern void ZConfigDrop(ref NativeType config); - [DllImport(Zenoh.DllName, EntryPoint = "zc_config_from_str", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "zc_config_from_str", CallingConvention = CallingConvention.Cdecl)] internal static extern NativeType ZCConfigFromStr([MarshalAs(UnmanagedType.LPStr)] string str); - [DllImport(Zenoh.DllName, EntryPoint = "zc_config_from_file", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "zc_config_from_file", CallingConvention = CallingConvention.Cdecl)] internal static extern NativeType ZCConfigFromFile([MarshalAs(UnmanagedType.LPStr)] string path); - [DllImport(Zenoh.DllName, EntryPoint = "z_config_check", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_config_check", CallingConvention = CallingConvention.Cdecl)] internal static extern bool ZConfigCheck(ref NativeType config); - [DllImport(Zenoh.DllName, EntryPoint = "zc_config_to_string", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "zc_config_to_string", CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr ZCConfigToString(ref NativeType config); - [DllImport(Zenoh.DllName, EntryPoint = "zc_config_get", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "zc_config_get", CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr ZCConfigGet(ref NativeType config, ZString key); - [DllImport(Zenoh.DllName, EntryPoint = "zc_config_insert_json", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "zc_config_insert_json", CallingConvention = CallingConvention.Cdecl)] internal static extern sbyte ZCConfigInsertJson(ref NativeType config, IntPtr key, IntPtr value); } } \ No newline at end of file diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs index 5785321..e28ae76 100644 --- a/Zenoh/Session.cs +++ b/Zenoh/Session.cs @@ -76,9 +76,9 @@ private static List GetIdStrings(IntPtr buf) int len = Marshal.ReadByte(buf); for (int i = 0; i < len; i++) { - byte[] b = new byte[Zenoh.IdLength]; - Marshal.Copy(buf + 1 + Zenoh.IdLength * i, b, 0, Zenoh.IdLength); - list.Add(Zenoh.IdBytesToStr(b)); + byte[] b = new byte[ZenohC.IdLength]; + Marshal.Copy(buf + 1 + ZenohC.IdLength * i, b, 0, ZenohC.IdLength); + list.Add(ZenohC.IdBytesToStr(b)); } return list; @@ -87,18 +87,18 @@ private static List GetIdStrings(IntPtr buf) private static void InfoZidCallback(ref Zid zid, IntPtr buf) { int i = Marshal.ReadByte(buf); - if (i >= Zenoh.RoutersNum) + if (i >= ZenohC.RoutersNum) { return; } - Marshal.Copy(zid.id, 0, buf + 1 + Zenoh.IdLength * i, Zenoh.IdLength); + Marshal.Copy(zid.id, 0, buf + 1 + ZenohC.IdLength * i, ZenohC.IdLength); Marshal.WriteByte(buf, (byte)(i + 1)); } public string[] RoutersZid() { - int length = 1 + Zenoh.IdLength * Zenoh.RoutersNum; + int length = 1 + ZenohC.IdLength * ZenohC.RoutersNum; IntPtr buffer = Marshal.AllocHGlobal(length); for (int i = 0; i < length; i++) { @@ -120,7 +120,7 @@ public string[] RoutersZid() public string[] PeersZid() { - int length = 1 + Zenoh.IdLength * Zenoh.PeersNum; + int length = 1 + ZenohC.IdLength * ZenohC.PeersNum; IntPtr buffer = Marshal.AllocHGlobal(length); for (int i = 0; i < length; i++) { @@ -227,37 +227,37 @@ public void UnregisterSubscriber(string key) Subscribers.Remove(key); } - [DllImport(Zenoh.DllName, EntryPoint = "z_open", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_open", CallingConvention = CallingConvention.Cdecl)] internal static extern NativeType ZOpen(ref Config.NativeType config); - [DllImport(Zenoh.DllName, EntryPoint = "z_session_check", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_session_check", CallingConvention = CallingConvention.Cdecl)] internal static extern bool ZSessionCheck(ref NativeType session); - [DllImport(Zenoh.DllName, EntryPoint = "z_close", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_close", CallingConvention = CallingConvention.Cdecl)] internal static extern void ZClose(ref NativeType session); - [DllImport(Zenoh.DllName, EntryPoint = "z_info_zid", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_info_zid", CallingConvention = CallingConvention.Cdecl)] internal static extern Zid ZInfoZid(ref NativeType session); - [DllImport(Zenoh.DllName, EntryPoint = "z_info_peers_zid", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_info_peers_zid", CallingConvention = CallingConvention.Cdecl)] internal static extern sbyte ZInfoPeersZid(ref NativeType session, ref ZClosureZid callback); - [DllImport(Zenoh.DllName, EntryPoint = "z_info_routers_zid", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_info_routers_zid", CallingConvention = CallingConvention.Cdecl)] internal static extern sbyte ZInfoRoutersZid(ref NativeType session, ref ZClosureZid callback); - [DllImport(Zenoh.DllName, EntryPoint = "z_put", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_put", CallingConvention = CallingConvention.Cdecl)] internal static extern int ZPut(ref NativeType session, KeyExpr.NativeType keyexpr, IntPtr payload, ulong len, ref PutOptions.NativeType opts); - [DllImport(Zenoh.DllName, EntryPoint = "z_subscriber_check")] + [DllImport(ZenohC.DllName, EntryPoint = "z_subscriber_check")] internal static extern bool ZSubscriberCheck(ref Subscriber.NativeType sub); - [DllImport(Zenoh.DllName, EntryPoint = "z_declare_subscriber")] + [DllImport(ZenohC.DllName, EntryPoint = "z_declare_subscriber")] internal static extern Subscriber.NativeType ZDeclareSubscribe(ref NativeType session, KeyExpr.NativeType keyexpr, ref ZClosureSample callback, ref Subscriber.Options opts); - [DllImport(Zenoh.DllName, EntryPoint = "z_subscriber_close")] + [DllImport(ZenohC.DllName, EntryPoint = "z_subscriber_close")] internal static extern void ZSubscriberClose(ref Subscriber.NativeType sub); /* diff --git a/Zenoh/Subscriber.cs b/Zenoh/Subscriber.cs index 1abf638..724ce89 100644 --- a/Zenoh/Subscriber.cs +++ b/Zenoh/Subscriber.cs @@ -68,7 +68,7 @@ public static Options GetOptionsDefault() return ZSubScriberOptionsDefault(); } - [DllImport(Zenoh.DllName, EntryPoint = "z_subscriber_options_default")] + [DllImport(ZenohC.DllName, EntryPoint = "z_subscriber_options_default")] internal static extern Options ZSubScriberOptionsDefault(); } } \ No newline at end of file diff --git a/Zenoh/Zenoh.csproj b/Zenoh/Zenoh.csproj index a3d5584..fabdb32 100644 --- a/Zenoh/Zenoh.csproj +++ b/Zenoh/Zenoh.csproj @@ -11,10 +11,10 @@ https://github.com/sanri/zenoh-csharp git Library - net6.0 true 10 0.0.6 zenoh + net7.0;net6.0 diff --git a/Zenoh/Zenoh.cs b/Zenoh/ZenohC.cs similarity index 93% rename from Zenoh/Zenoh.cs rename to Zenoh/ZenohC.cs index 34ee5de..567137d 100644 --- a/Zenoh/Zenoh.cs +++ b/Zenoh/ZenohC.cs @@ -20,11 +20,11 @@ namespace Zenoh { - public static class Zenoh + public static class ZenohC { public const string DllName = "zenohc"; - [DllImport(Zenoh.DllName, EntryPoint = "zc_init_logger")] + [DllImport(ZenohC.DllName, EntryPoint = "zc_init_logger")] public static extern void InitLogger(); public static string IdBytesToStr(byte[] buf) @@ -160,7 +160,7 @@ public static ZEncoding New(ZEncodingPrefix prefix) return FnZEncoding(prefix, IntPtr.Zero); } - [DllImport(Zenoh.DllName, EntryPoint = "z_encoding", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_encoding", CallingConvention = CallingConvention.Cdecl)] internal static extern ZEncoding FnZEncoding(ZEncodingPrefix prefix, IntPtr suffix); } @@ -187,7 +187,7 @@ public struct Zid public string ToStr() { - return Zenoh.IdBytesToStr(id); + return ZenohC.IdBytesToStr(id); } } @@ -223,7 +223,7 @@ public void SetPriority(ZPriority priority) native.priority = priority; } - [DllImport(Zenoh.DllName, EntryPoint = "z_put_options_default", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_put_options_default", CallingConvention = CallingConvention.Cdecl)] private static extern NativeType ZPutOptionsDefault(); } @@ -299,10 +299,10 @@ public string GetStr() return native.GetStr(); } - [DllImport(Zenoh.DllName, EntryPoint = "z_keyexpr", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_keyexpr", CallingConvention = CallingConvention.Cdecl)] internal static extern NativeType ZKeyexpr(IntPtr name); - [DllImport(Zenoh.DllName, EntryPoint = "z_keyexpr_to_string", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_keyexpr_to_string", CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr ZKeyexprToString(NativeType keyexpr); } @@ -328,7 +328,7 @@ public QueryTarget() this.native = ZQueryTargetDefault(); } - [DllImport(Zenoh.DllName, EntryPoint = "z_query_target_default", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_query_target_default", CallingConvention = CallingConvention.Cdecl)] internal static extern NativeType ZQueryTargetDefault(); } @@ -357,7 +357,7 @@ public QueryConsolidation() this.native = ZQueryConsolidationDefault(); } - [DllImport(Zenoh.DllName, EntryPoint = "z_query_consolidation_default", + [DllImport(ZenohC.DllName, EntryPoint = "z_query_consolidation_default", CallingConvention = CallingConvention.Cdecl)] internal static extern QueryConsolidation.NativeType ZQueryConsolidationDefault(); } @@ -480,13 +480,13 @@ internal static string ZStringToString(ZString zs) return Marshal.PtrToStringAnsi(zs.start); } - [DllImport(Zenoh.DllName, EntryPoint = "z_string_free", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_string_free", CallingConvention = CallingConvention.Cdecl)] internal static extern void ZStringFree(ref ZString s); - [DllImport(Zenoh.DllName, EntryPoint = "z_string_check", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_string_check", CallingConvention = CallingConvention.Cdecl)] internal static extern bool ZStringCheck(ref ZString s); - [DllImport(Zenoh.DllName, EntryPoint = "z_bytes_free", CallingConvention = CallingConvention.Cdecl)] + [DllImport(ZenohC.DllName, EntryPoint = "z_bytes_free", CallingConvention = CallingConvention.Cdecl)] internal static extern void ZBytesFree(ref ZString s); } } \ No newline at end of file diff --git a/examples/Zenoh/README.md b/examples/README.md similarity index 100% rename from examples/Zenoh/README.md rename to examples/README.md diff --git a/examples/Zenoh/ZGet.cs b/examples/ZGet/ZGet.cs similarity index 95% rename from examples/Zenoh/ZGet.cs rename to examples/ZGet/ZGet.cs index 8403f1c..4f1a84f 100644 --- a/examples/Zenoh/ZGet.cs +++ b/examples/ZGet/ZGet.cs @@ -1,48 +1,48 @@ -using System; -using System.Threading; -using Zenoh; - -/* -//Zenoh.Zenoh.InitLogger(); -Config config = new Config(); -//string[] connect = {"tcp/172.30.100.3:7447"}; -//config.SetConnect(connect); -Session session = new Session(); - -Console.WriteLine("Opening session..."); -if (session.Open(config)) -{ - // wait - Thread.Sleep(1000); -} -else -{ - Console.WriteLine("Opening session unsuccessful"); - return; -} - -KeyExpr key1 = KeyExpr.FromString("demo/example/zenoh-cs-put1"); -KeyExpr key2 = KeyExpr.FromString("demo/example/zenoh-cs-put2"); - -Console.WriteLine($"Sending Query '{key1.GetStr()}'"); -ReplayDataArray data1 = session.Get(key1); -foreach (ReplayData ele in data1.List) -{ - string s = $">> Received ('{ele.Sample.Key}': '{ele.Sample.ValueToString()}'"; - Console.WriteLine(s); -} - - -ReplayDataArray data2 = session.Get(key2); -Console.WriteLine($"Sending Query '{key2.GetStr()}'"); -foreach (ReplayData ele in data2.List) -{ - string s = $">> Received ('{ele.Sample.Key}': '{ele.Sample.ValueToString()}'"; - Console.WriteLine(s); -} - -session.Close(); - -*/ - +using System; +using System.Threading; +using Zenoh; + +/* +//Zenoh.Zenoh.InitLogger(); +Config config = new Config(); +//string[] connect = {"tcp/172.30.100.3:7447"}; +//config.SetConnect(connect); +Session session = new Session(); + +Console.WriteLine("Opening session..."); +if (session.Open(config)) +{ + // wait + Thread.Sleep(1000); +} +else +{ + Console.WriteLine("Opening session unsuccessful"); + return; +} + +KeyExpr key1 = KeyExpr.FromString("demo/example/zenoh-cs-put1"); +KeyExpr key2 = KeyExpr.FromString("demo/example/zenoh-cs-put2"); + +Console.WriteLine($"Sending Query '{key1.GetStr()}'"); +ReplayDataArray data1 = session.Get(key1); +foreach (ReplayData ele in data1.List) +{ + string s = $">> Received ('{ele.Sample.Key}': '{ele.Sample.ValueToString()}'"; + Console.WriteLine(s); +} + + +ReplayDataArray data2 = session.Get(key2); +Console.WriteLine($"Sending Query '{key2.GetStr()}'"); +foreach (ReplayData ele in data2.List) +{ + string s = $">> Received ('{ele.Sample.Key}': '{ele.Sample.ValueToString()}'"; + Console.WriteLine(s); +} + +session.Close(); + +*/ + Console.WriteLine("hello"); \ No newline at end of file diff --git a/examples/Zenoh/ZGet.csproj b/examples/ZGet/ZGet.csproj similarity index 88% rename from examples/Zenoh/ZGet.csproj rename to examples/ZGet/ZGet.csproj index a9d414f..782f972 100644 --- a/examples/Zenoh/ZGet.csproj +++ b/examples/ZGet/ZGet.csproj @@ -1,20 +1,20 @@ - - - ZGet - ZGet - Exe - net6.0 - false - 10 - - - - - - - - - - - - + + + ZGet + ZGet + Exe + false + 10 + net7.0;net6.0 + + + + + + + + + + + + diff --git a/examples/Zenoh/ZInfo.cs b/examples/ZInfo/ZInfo.cs similarity index 96% rename from examples/Zenoh/ZInfo.cs rename to examples/ZInfo/ZInfo.cs index b5cb490..d90c481 100644 --- a/examples/Zenoh/ZInfo.cs +++ b/examples/ZInfo/ZInfo.cs @@ -1,37 +1,37 @@ -using System; -using System.Linq; -using System.Threading; -using Zenoh; - -//Zenoh.Zenoh.InitLogger(); -Config config = new Config(); -//config.SetId("01"); -//string[] c = { "tcp/172.30.100.3:7447", "tcp/172.30.100.1:7447" }; -//config.SetConnect(c); -config.SetMode(Config.Mode.Client); -config.SetTimestamp(true); -string configStr = config.ToStr(); -Console.WriteLine($"config string:\n{configStr}\n----------------------\n"); - -Session session = new Session(); -Console.WriteLine("Opening session..."); -if (session.Open(config)) -{ - // wait - Thread.Sleep(1000); -} -else -{ - Console.WriteLine("Opening session unsuccessful"); - return; -} - -string pid = session.LocalZid(); -string[] routerPid = session.RoutersZid(); -string[] peerPid = session.PeersZid(); - -Console.WriteLine($"Local PID: {pid}"); -Console.WriteLine($"PeersPID: {String.Join(',', peerPid)}"); -Console.WriteLine($"Routers PID: {String.Join(',', routerPid)}"); - +using System; +using System.Linq; +using System.Threading; +using Zenoh; + +//Zenoh.Zenoh.InitLogger(); +Config config = new Config(); +//config.SetId("01"); +//string[] c = { "tcp/172.30.100.3:7447", "tcp/172.30.100.1:7447" }; +//config.SetConnect(c); +config.SetMode(Config.Mode.Client); +config.SetTimestamp(true); +string configStr = config.ToStr(); +Console.WriteLine($"config string:\n{configStr}\n----------------------\n"); + +Session session = new Session(); +Console.WriteLine("Opening session..."); +if (session.Open(config)) +{ + // wait + Thread.Sleep(1000); +} +else +{ + Console.WriteLine("Opening session unsuccessful"); + return; +} + +string pid = session.LocalZid(); +string[] routerPid = session.RoutersZid(); +string[] peerPid = session.PeersZid(); + +Console.WriteLine($"Local PID: {pid}"); +Console.WriteLine($"PeersPID: {String.Join(',', peerPid)}"); +Console.WriteLine($"Routers PID: {String.Join(',', routerPid)}"); + session.Close(); \ No newline at end of file diff --git a/examples/Zenoh/ZInfo.csproj b/examples/ZInfo/ZInfo.csproj similarity index 88% rename from examples/Zenoh/ZInfo.csproj rename to examples/ZInfo/ZInfo.csproj index b33306b..8f9fd95 100644 --- a/examples/Zenoh/ZInfo.csproj +++ b/examples/ZInfo/ZInfo.csproj @@ -1,20 +1,20 @@ - - - ZInfo - ZInfo - Exe - net6.0 - false - 10 - - - - - - - - - - - - + + + ZInfo + ZInfo + Exe + false + 10 + net7.0;net6.0 + + + + + + + + + + + + diff --git a/examples/Zenoh/ZPut.cs b/examples/ZPut/ZPut.cs similarity index 95% rename from examples/Zenoh/ZPut.cs rename to examples/ZPut/ZPut.cs index 72af839..8653d89 100644 --- a/examples/Zenoh/ZPut.cs +++ b/examples/ZPut/ZPut.cs @@ -1,46 +1,46 @@ -using System; -using System.Threading; -using Zenoh; - -Zenoh.Zenoh.InitLogger(); -Config config = new Config(); -string[] connect = { "tcp/127.0.0.1:7447" }; -config.SetConnect(connect); -Session session = new Session(); - -Console.WriteLine("Opening session..."); -if (session.Open(config)) -{ - Thread.Sleep(200); -} -else -{ - Console.WriteLine("Opening session unsuccessful"); - return; -} - -KeyExpr keyStr = KeyExpr.FromString("demo/example/zenoh-cs-put/string"); -string dataStr = "Put from csharp !"; -Console.WriteLine(session.PutStr(keyStr, dataStr) - ? $"Putting data string ('{keyStr.GetStr()}': '{dataStr}')" - : "Putting data string fault!"); - -KeyExpr keyJson = KeyExpr.FromString("demo/example/zenoh-cs-put/json"); -string dataJson = "{\"value\": \"Put from csharp\"}"; -Console.WriteLine(session.PutJson(keyJson, dataJson) - ? $"Putting data json ('{keyJson.GetStr()}': {dataJson})" - : "Putting data json fault!"); - -KeyExpr keyInt = KeyExpr.FromString("demo/example/zenoh-cs-put/int"); -Int64 dataInt = 965; -Console.WriteLine(session.PutInt(keyInt, dataInt) - ? $"Putting data int ('{keyInt.GetStr()}': {dataInt})" - : "Putting data int fault!"); - -KeyExpr keyFloat = KeyExpr.FromString("demo/example/zenoh-cs-put/float"); -double dataFloat = 99.6; -Console.WriteLine(session.PutFloat(keyFloat, dataFloat) - ? $"Putting data float ('{keyFloat.GetStr()}': {dataFloat})" - : "Putting data float fault!"); - +using System; +using System.Threading; +using Zenoh; + +Zenoh.ZenohC.InitLogger(); +Config config = new Config(); +string[] connect = { "tcp/127.0.0.1:7447" }; +config.SetConnect(connect); +Session session = new Session(); + +Console.WriteLine("Opening session..."); +if (session.Open(config)) +{ + Thread.Sleep(200); +} +else +{ + Console.WriteLine("Opening session unsuccessful"); + return; +} + +KeyExpr keyStr = KeyExpr.FromString("demo/example/zenoh-cs-put/string"); +string dataStr = "Put from csharp !"; +Console.WriteLine(session.PutStr(keyStr, dataStr) + ? $"Putting data string ('{keyStr.GetStr()}': '{dataStr}')" + : "Putting data string fault!"); + +KeyExpr keyJson = KeyExpr.FromString("demo/example/zenoh-cs-put/json"); +string dataJson = "{\"value\": \"Put from csharp\"}"; +Console.WriteLine(session.PutJson(keyJson, dataJson) + ? $"Putting data json ('{keyJson.GetStr()}': {dataJson})" + : "Putting data json fault!"); + +KeyExpr keyInt = KeyExpr.FromString("demo/example/zenoh-cs-put/int"); +Int64 dataInt = 965; +Console.WriteLine(session.PutInt(keyInt, dataInt) + ? $"Putting data int ('{keyInt.GetStr()}': {dataInt})" + : "Putting data int fault!"); + +KeyExpr keyFloat = KeyExpr.FromString("demo/example/zenoh-cs-put/float"); +double dataFloat = 99.6; +Console.WriteLine(session.PutFloat(keyFloat, dataFloat) + ? $"Putting data float ('{keyFloat.GetStr()}': {dataFloat})" + : "Putting data float fault!"); + session.Close(); \ No newline at end of file diff --git a/examples/Zenoh/ZPut.csproj b/examples/ZPut/ZPut.csproj similarity index 88% rename from examples/Zenoh/ZPut.csproj rename to examples/ZPut/ZPut.csproj index 0d3346a..434b043 100644 --- a/examples/Zenoh/ZPut.csproj +++ b/examples/ZPut/ZPut.csproj @@ -1,20 +1,20 @@ - - - ZPut - ZPut - Exe - net6.0 - false - 10 - - - - - - - - - - - - + + + ZPut + ZPut + Exe + false + 10 + net7.0;net6.0 + + + + + + + + + + + + diff --git a/examples/Zenoh/ZSub.cs b/examples/ZSub/ZSub.cs similarity index 95% rename from examples/Zenoh/ZSub.cs rename to examples/ZSub/ZSub.cs index b11faf1..c48746f 100644 --- a/examples/Zenoh/ZSub.cs +++ b/examples/ZSub/ZSub.cs @@ -1,70 +1,70 @@ -using System; -using System.Text; -using System.Threading; -using Zenoh; - -//Zenoh.Zenoh.InitLogger(); -Config config = new Config(); -string[] connect = {"tcp/127.0.0.1:7447"}; -config.SetConnect(connect); -//string[] listen = {"tcp/127.0.0.1:7888"}; -//config.SetListen(listen); -Session session = new Session(); - -Console.WriteLine("Opening session..."); -if (session.Open(config)) -{ - // wait - Thread.Sleep(200); -} -else -{ - Console.WriteLine("Opening session unsuccessful"); - return; -} - -void Callback1(Sample sample) -{ - string key = sample.Key; - string value = sample.ValueToString(); - Console.WriteLine($">> [Subscriber1] Received PUT ('{key}': '{value}')"); - //Console.Write("."); -} - -void Callback2(Sample sample) -{ - string key = sample.Key; - string value = sample.ValueToString(); - Console.WriteLine($">> [Subscriber2] Received PUT ('{key}': '{value}')"); -} - -SubscriberCallback userCallback1 = Callback1; -SubscriberCallback userCallback2 = Callback2; - -string key1 = "demo/example/zenoh-cs-put1"; -string key2 = "demo/example/zenoh-cs-put2"; - -Subscriber subscriber1 = new Subscriber(key1, userCallback1); -Subscriber subscriber2 = new Subscriber(key2, userCallback2); - -if (session.RegisterSubscriber(subscriber1)) -{ - Console.WriteLine($"Registered Subscriber1 On '{key1}'"); -} - -if (session.RegisterSubscriber(subscriber2)) -{ - Console.WriteLine($"Registered Subscriber2 On '{key2}'"); -} - -Console.WriteLine("Enter 'q' to quit..."); -while (true) -{ - var input = Console.ReadKey(); - if (input.Key == ConsoleKey.Q) - { - break; - } -} - +using System; +using System.Text; +using System.Threading; +using Zenoh; + +//Zenoh.Zenoh.InitLogger(); +Config config = new Config(); +string[] connect = {"tcp/127.0.0.1:7447"}; +config.SetConnect(connect); +//string[] listen = {"tcp/127.0.0.1:7888"}; +//config.SetListen(listen); +Session session = new Session(); + +Console.WriteLine("Opening session..."); +if (session.Open(config)) +{ + // wait + Thread.Sleep(200); +} +else +{ + Console.WriteLine("Opening session unsuccessful"); + return; +} + +void Callback1(Sample sample) +{ + string key = sample.Key; + string value = sample.ValueToString(); + Console.WriteLine($">> [Subscriber1] Received PUT ('{key}': '{value}')"); + //Console.Write("."); +} + +void Callback2(Sample sample) +{ + string key = sample.Key; + string value = sample.ValueToString(); + Console.WriteLine($">> [Subscriber2] Received PUT ('{key}': '{value}')"); +} + +SubscriberCallback userCallback1 = Callback1; +SubscriberCallback userCallback2 = Callback2; + +string key1 = "demo/example/zenoh-cs-put1"; +string key2 = "demo/example/zenoh-cs-put2"; + +Subscriber subscriber1 = new Subscriber(key1, userCallback1); +Subscriber subscriber2 = new Subscriber(key2, userCallback2); + +if (session.RegisterSubscriber(subscriber1)) +{ + Console.WriteLine($"Registered Subscriber1 On '{key1}'"); +} + +if (session.RegisterSubscriber(subscriber2)) +{ + Console.WriteLine($"Registered Subscriber2 On '{key2}'"); +} + +Console.WriteLine("Enter 'q' to quit..."); +while (true) +{ + var input = Console.ReadKey(); + if (input.Key == ConsoleKey.Q) + { + break; + } +} + session.Close(); \ No newline at end of file diff --git a/examples/Zenoh/ZSub.csproj b/examples/ZSub/ZSub.csproj similarity index 88% rename from examples/Zenoh/ZSub.csproj rename to examples/ZSub/ZSub.csproj index 62d4ab6..815181c 100644 --- a/examples/Zenoh/ZSub.csproj +++ b/examples/ZSub/ZSub.csproj @@ -1,20 +1,20 @@ - - - ZSub - ZSub - Exe - net6.0 - false - 10 - - - - - - - - - - - - + + + ZSub + ZSub + Exe + false + 10 + net7.0;net6.0 + + + + + + + + + + + + diff --git a/examples/zenoh-examples.sln b/examples/zenoh-examples.sln index e2ecd5e..68c84dd 100644 --- a/examples/zenoh-examples.sln +++ b/examples/zenoh-examples.sln @@ -5,13 +5,13 @@ VisualStudioVersion = 15.0.26124.0 MinimumVisualStudioVersion = 15.0.26124.0 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Zenoh", "..\Zenoh\Zenoh.csproj", "{05075D28-DE9E-4CB4-9976-FCBDD170A7B5}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZGet", "Zenoh\ZGet.csproj", "{7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZGet", "ZGet\ZGet.csproj", "{7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZPut", "Zenoh\ZPut.csproj", "{13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZPut", "ZPut\ZPut.csproj", "{13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZSub", "Zenoh\ZSub.csproj", "{435FE932-CE46-49D0-B921-AC1B11585F13}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZSub", "ZSub\ZSub.csproj", "{435FE932-CE46-49D0-B921-AC1B11585F13}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZInfo", "Zenoh\ZInfo.csproj", "{B973A563-44AD-4B68-855C-46B5D58DCE32}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZInfo", "ZInfo\ZInfo.csproj", "{B973A563-44AD-4B68-855C-46B5D58DCE32}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution From 7b3164f7e7c01b060209f7fc7d1beb176dea4b80 Mon Sep 17 00:00:00 2001 From: sanri Date: Fri, 18 Aug 2023 00:27:54 +0800 Subject: [PATCH 16/34] reconstruction --- Zenoh/Config.cs | 229 +++--- Zenoh/Sample.cs | 97 +++ Zenoh/Session.cs | 480 +++++++----- Zenoh/Subscriber.cs | 104 +-- Zenoh/Zenoh.csproj | 11 + Zenoh/ZenohC.cs | 1425 +++++++++++++++++++++++++---------- examples/ZGet/ZGet.csproj | 11 + examples/ZInfo/ZInfo.cs | 43 +- examples/ZInfo/ZInfo.csproj | 12 + examples/ZPut/ZPut.cs | 60 +- examples/ZPut/ZPut.csproj | 12 + examples/ZSub/ZSub.cs | 52 +- examples/ZSub/ZSub.csproj | 11 + examples/zenoh-examples.sln | 106 ++- 14 files changed, 1775 insertions(+), 878 deletions(-) create mode 100644 Zenoh/Sample.cs diff --git a/Zenoh/Config.cs b/Zenoh/Config.cs index 3d53a0e..f59488b 100644 --- a/Zenoh/Config.cs +++ b/Zenoh/Config.cs @@ -1,151 +1,166 @@ +#nullable enable + using System; -using System.Collections.Generic; using System.Runtime.InteropServices; +using System.Text; + +namespace Zenoh; -namespace Zenoh +public class Config : IDisposable { - public class Config : IDisposable + public enum Mode { - public enum Mode - { - Router, - Peer, - Client, - } + Peer, + Client, + } - internal struct NativeType // z_owned_config_t - { - internal IntPtr p; - } + internal unsafe ZOwnedConfig* ownedConfig; + private bool _disposed; - internal NativeType native; - private bool _disposed; - public Config() + public Config() + { + unsafe { - this.native = ZConfigDefault(); - _disposed = false; + ZOwnedConfig config = ZenohC.z_config_default(); + nint p = Marshal.AllocHGlobal(Marshal.SizeOf(config)); + Marshal.StructureToPtr(config, p, false); + ownedConfig = (ZOwnedConfig*)p; } + + _disposed = false; + } - public void Dispose() => Dispose(true); - - private void Dispose(bool disposing) + public static Config? LoadFromFile(string path) + { + unsafe { - if (_disposed) + ZOwnedConfig config = ZenohC.zc_config_from_file(path); + int b = ZenohC.z_config_check(&config); + // if (!ZenohC.z_config_check(&config)) + if (b==0) { - return; + ZenohC.z_config_drop(&config); + return null; } - ZConfigDrop(ref native); - _disposed = true; + nint p = Marshal.AllocHGlobal(Marshal.SizeOf(config)); + Marshal.StructureToPtr(config, p, false); + return new Config + { + ownedConfig = (ZOwnedConfig*)p, + _disposed = false, + }; } + } + + public void Dispose() => Dispose(true); + + private void Dispose(bool disposing) + { + if (_disposed) return; - // The Zenoh ID (as hex-string) of the instance. - // This ID cannot exceed 16 bytes (as hex-string 32 bytes) of length. - // If left unset, a random UUIDv4 will be generated. - // WARNING: this id must be unique in your zenoh network. - public bool SetId(string id) + unsafe { - string key = "id"; - string value = $"\"{id}\""; - return InsertJson(key, value); + ZenohC.z_config_drop(ownedConfig); + Marshal.FreeHGlobal((nint)ownedConfig); } - // The v such as "tcp/172.30.1.1:7447" - public bool SetConnect(string[] v) - { - string key = "connect/endpoints"; - string value = "["; - foreach (var ele in v) - { - value += $"\"{ele}\","; - } + _disposed = true; + } - value += "]"; - return InsertJson(key, value); - } - - // The v such as "tcp/127.0.0.1:7888" - public bool SetListen(string[] v) - { - string key = "listen/endpoints"; - string value = "["; - foreach (var ele in v) - { - value += $"\"{ele}\","; - } + public string ToStr() + { + if (_disposed) return ""; - value += "]"; - return InsertJson(key, value); + unsafe + { + ZConfig config = ZenohC.z_config_loan(ownedConfig); + ZOwnedStr str = ZenohC.zc_config_to_string(config); + string o = ZenohC.ZOwnedStrToString(&str); + ZenohC.z_str_drop(&str); + return o; } + } - public bool SetMode(Mode mode) - { - string key = "mode"; - string value = ""; - switch (mode) - { - case Mode.Client: - value = "\"client\""; - break; - case Mode.Peer: - value = "\"peer\""; - break; - case Mode.Router: - value = "\"client\""; - break; - } - return InsertJson(key, value); - } + public bool SetMode(Mode mode) + { + if (_disposed) return false; - // Whether data messages should be timestamped - public bool SetTimestamp(bool b) + string value = ""; + switch (mode) { - string key = "add_timestamp"; - string value = b ? "true" : "false"; - return InsertJson(key, value); + case Mode.Client: + value = "\"client\""; + break; + case Mode.Peer: + value = "\"peer\""; + break; } - public string ToStr() + unsafe { - IntPtr p = ZCConfigToString(ref native); - string result = Marshal.PtrToStringAnsi(p); - return result; + ZConfig config = ZenohC.z_config_loan(ownedConfig); + sbyte r = ZenohC.zc_config_insert_json(config, ZenohC.zConfigModeKey, value); + return r == 0; } + } - private bool InsertJson(string key, string value) + // The v such as "tcp/172.30.1.1:7447" + public bool SetConnect(string[] v) + { + if (_disposed) return false; + + StringBuilder value = new StringBuilder("["); + foreach (var ele in v) { - IntPtr k = Marshal.StringToHGlobalAnsi(key); - IntPtr v = Marshal.StringToHGlobalAnsi(value); - sbyte r = ZCConfigInsertJson(ref native, k, v); - Marshal.FreeHGlobal(k); - Marshal.FreeHGlobal(v); - return r == 1 ? true : false; + value.Append($"\"{ele}\","); } - [DllImport(ZenohC.DllName, EntryPoint = "z_config_default", CallingConvention = CallingConvention.Cdecl)] - internal static extern NativeType ZConfigDefault(); + value.Append("]"); + + unsafe + { + ZConfig config = ZenohC.z_config_loan(ownedConfig); + sbyte r = ZenohC.zc_config_insert_json(config, ZenohC.zConfigConnectKey, value.ToString()); + return r == 0; + } + } - [DllImport(ZenohC.DllName, EntryPoint = "z_config_drop", CallingConvention = CallingConvention.Cdecl)] - internal static extern void ZConfigDrop(ref NativeType config); + // The v such as "tcp/127.0.0.1:7888" + public bool SetListen(string[] v) + { + if (_disposed) return false; - [DllImport(ZenohC.DllName, EntryPoint = "zc_config_from_str", CallingConvention = CallingConvention.Cdecl)] - internal static extern NativeType ZCConfigFromStr([MarshalAs(UnmanagedType.LPStr)] string str); + StringBuilder value = new StringBuilder("["); + foreach (var ele in v) + { + value.Append($"\"{ele}\","); + } - [DllImport(ZenohC.DllName, EntryPoint = "zc_config_from_file", CallingConvention = CallingConvention.Cdecl)] - internal static extern NativeType ZCConfigFromFile([MarshalAs(UnmanagedType.LPStr)] string path); + value.Append("]"); - [DllImport(ZenohC.DllName, EntryPoint = "z_config_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern bool ZConfigCheck(ref NativeType config); + unsafe + { + ZConfig config = ZenohC.z_config_loan(ownedConfig); + sbyte r = ZenohC.zc_config_insert_json(config, ZenohC.zConfigListenKey, value.ToString()); + return r == 0; + } + } - [DllImport(ZenohC.DllName, EntryPoint = "zc_config_to_string", CallingConvention = CallingConvention.Cdecl)] - internal static extern IntPtr ZCConfigToString(ref NativeType config); + // Whether data messages should be timestamped + public bool SetTimestamp(bool b) + { + if (_disposed) return false; - [DllImport(ZenohC.DllName, EntryPoint = "zc_config_get", CallingConvention = CallingConvention.Cdecl)] - internal static extern IntPtr ZCConfigGet(ref NativeType config, ZString key); + string value = b ? "true" : "false"; - [DllImport(ZenohC.DllName, EntryPoint = "zc_config_insert_json", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte ZCConfigInsertJson(ref NativeType config, IntPtr key, IntPtr value); + unsafe + { + ZConfig config = ZenohC.z_config_loan(ownedConfig); + sbyte r = ZenohC.zc_config_insert_json(config, ZenohC.zConfigAddTimestampKey, value); + return r == 0; + } } } \ No newline at end of file diff --git a/Zenoh/Sample.cs b/Zenoh/Sample.cs new file mode 100644 index 0000000..cbfb98c --- /dev/null +++ b/Zenoh/Sample.cs @@ -0,0 +1,97 @@ +#nullable enable + +using System; +using System.Runtime.InteropServices; + +namespace Zenoh; + +public class Sample +{ + private readonly unsafe ZSample* _native; + + internal unsafe Sample(ZSample* sample) + { + _native = sample; + } + + public string GetKeyexpr() + { + unsafe + { + return ZenohC.ZKeyexprToString(_native->keyexpr); + } + } + + public EncodingPrefix GetEncodingPrefix() + { + unsafe + { + return _native->encoding.prefix; + } + } + + public byte[] GetEncodingSuffix() + { + unsafe + { + nuint len = _native->encoding.suffix.len; + nint start = (nint)_native->encoding.suffix.start; + byte[] data = new byte[len]; + Marshal.Copy(start, data, 0, (int)len); + return data; + } + } + + public SampleKind GetSampleKind() + { + unsafe + { + return _native->kind; + } + } + + public byte[] GetPayload() + { + unsafe + { + if (ZenohC.z_bytes_check(&_native->payload) != 1) + return Array.Empty(); + var len = _native->payload.len; + byte[] data = new byte[len]; + Marshal.Copy((nint)_native->payload.start, data, 0, (int)len); + return data; + } + } + + public string? GetString() + { + unsafe + { + if (ZenohC.z_bytes_check(&_native->payload) != 1) + return null; + return Marshal.PtrToStringUTF8((nint)_native->payload.start, (int)_native->payload.len); + } + } + + public long? GetInteger() + { + var str = GetString(); + if (!Int64.TryParse(str, out long n)) + { + return null; + } + + return n; + } + + public double? GetDouble() + { + var str = GetString(); + if (!Double.TryParse(str, out double n)) + { + return null; + } + + return n; + } +} \ No newline at end of file diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs index e28ae76..1d5e792 100644 --- a/Zenoh/Session.cs +++ b/Zenoh/Session.cs @@ -1,270 +1,380 @@ +#pragma warning disable CS8500 +#nullable enable + using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; +using System.Threading; + +namespace Zenoh; -namespace Zenoh +public class Session : IDisposable { - public class Session : IDisposable + internal SortedDictionary subscribers; + private int _indexSubscriber = 1; + private bool _disposed; + private readonly unsafe ZOwnedSession* _session; + + private unsafe Session(ZOwnedSession* session) { - [StructLayout(LayoutKind.Sequential)] - internal struct NativeType // z_owned_session_t + subscribers = new SortedDictionary(); + _disposed = false; + _session = session; + } + + public static Session? Open(Config config) + { + unsafe { - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] - public IntPtr[] p; - } + ZOwnedSession session = ZenohC.z_open(config.ownedConfig); + if (ZenohC.z_session_check(&session) != 1) + { + return null; + } - internal NativeType native; - internal Dictionary Subscribers; + nint p = Marshal.AllocHGlobal(Marshal.SizeOf(session)); + Marshal.StructureToPtr(session, p, false); - public Session() - { - this.native = new NativeType(); - this.Subscribers = new Dictionary(); + return new Session((ZOwnedSession*)p); } + } - public void Dispose() => Dispose(true); - protected virtual void Dispose(bool disposing) + public void Close() + { + if (_disposed) return; + + unsafe { - if (native.p[0] != IntPtr.Zero) + foreach ((_, Subscriber subscriber) in subscribers) { - foreach (Subscriber s in Subscribers.Values) - { - // free - } + ZenohC.z_undeclare_subscriber(subscriber.ownedSubscriber); + Marshal.FreeHGlobal((nint)subscriber.ownedSubscriber); + subscriber.ownedSubscriber = null; + } + + subscribers.Clear(); + + ZenohC.z_close(_session); + Marshal.FreeHGlobal((nint)_session); + } + + _disposed = true; + } - Subscribers.Clear(); + public void Dispose() => Dispose(true); - ZClose(ref native); - for (int i = 0; i < 3; i++) + private void Dispose(bool disposing) + { + Close(); + } + + + // private static List GetIdStrings(IntPtr buf) + // { + // List list = new List(); + // int len = Marshal.ReadByte(buf); + // for (int i = 0; i < len; i++) + // { + // // byte[] b = new byte[ZenohC.IdLength]; + // // Marshal.Copy(buf + 1 + ZenohC.IdLength * i, b, 0, ZenohC.IdLength); + // // list.Add(ZenohC.IdBytesToStr(b)); + // } + // + // return list; + // } + + // private static void InfoZidCallback(ref Zid zid, IntPtr buf) + // { + // int i = Marshal.ReadByte(buf); + // if (i >= ZenohC.RoutersNum) + // { + // return; + // } + // + // Marshal.Copy(zid.id, 0, buf + 1 + ZenohC.IdLength * i, ZenohC.IdLength); + // Marshal.WriteByte(buf, (byte)(i + 1)); + // } + + public struct Id + { + internal byte[] data; + + internal Id(ZId zid) + { + data = new byte[16]; + for (int i = 0; i < 16; i++) + { + unsafe { - native.p[i] = IntPtr.Zero; + data[i] = zid.id[i]; } } } - public bool Open(Config config) + public string ToStr() { - NativeType s = ZOpen(ref config.native); - if (ZSessionCheck(ref s)) - { - this.native = s; - return true; - } - else + StringBuilder sb = new StringBuilder(); + foreach (byte b in data) { - return false; + sb.Append(b.ToString("x")); } - } - public void Close() - { - ZClose(ref native); + return sb.ToString(); } + } - public string LocalZid() + public Id LocalId() + { + unsafe { - Zid zid = ZInfoZid(ref native); - return zid.ToStr(); + ZSession session = ZenohC.z_session_loan(_session); + ZId zid = ZenohC.z_info_zid(session); + return new Id(zid); } + } + [StructLayout(LayoutKind.Sequential, Pack = 8)] + private unsafe struct ZIdBuffer + { + internal nuint count; + internal fixed byte data[256 * 16]; - private static List GetIdStrings(IntPtr buf) + public ZIdBuffer() { - List list = new List(); - int len = Marshal.ReadByte(buf); - for (int i = 0; i < len; i++) - { - byte[] b = new byte[ZenohC.IdLength]; - Marshal.Copy(buf + 1 + ZenohC.IdLength * i, b, 0, ZenohC.IdLength); - list.Add(ZenohC.IdBytesToStr(b)); - } - - return list; + count = 0; } - private static void InfoZidCallback(ref Zid zid, IntPtr buf) + internal void Add(ZId* zId) { - int i = Marshal.ReadByte(buf); - if (i >= ZenohC.RoutersNum) + if (count >= 256) return; + for (nuint i = 0; i < 16; i++) { - return; + data[count * 16 + i] = zId->id[i]; } - Marshal.Copy(zid.id, 0, buf + 1 + ZenohC.IdLength * i, ZenohC.IdLength); - Marshal.WriteByte(buf, (byte)(i + 1)); + count += 1; } - public string[] RoutersZid() + internal Id[] ToIds() { - int length = 1 + ZenohC.IdLength * ZenohC.RoutersNum; - IntPtr buffer = Marshal.AllocHGlobal(length); - for (int i = 0; i < length; i++) + Id[] ids = new Id[count]; + for (nuint i = 0; i < count; i++) { - Marshal.WriteByte(buffer, i, 0); - } + byte[] o = new byte[16]; + for (nuint j = 0; j < 16; j++) + { + o[j] = data[i * 16 + j]; + } - ZClosureZid closure = new ZClosureZid(InfoZidCallback, buffer); - sbyte r = ZInfoRoutersZid(ref native, ref closure); - if (r != 0) - { - return Array.Empty(); + Id id = new Id + { + data = o, + }; + ids[i] = id; } - var output = GetIdStrings(buffer); - - Marshal.FreeHGlobal(buffer); - return output.ToArray(); + return ids; } + } - public string[] PeersZid() - { - int length = 1 + ZenohC.IdLength * ZenohC.PeersNum; - IntPtr buffer = Marshal.AllocHGlobal(length); - for (int i = 0; i < length; i++) - { - Marshal.WriteByte(buffer, i, 0); - } + internal static unsafe void z_id_call(ZId* zId, void* context) + { + ZIdBuffer* pIdBuffer = (ZIdBuffer*)context; + pIdBuffer->Add(zId); + } - ZClosureZid closure = new ZClosureZid(InfoZidCallback, buffer); - sbyte r = ZInfoPeersZid(ref native, ref closure); - if (r != 0) + public Id[] RoutersId() + { + unsafe + { + ZSession session = ZenohC.z_session_loan(_session); + nint pIdBuffer = Marshal.AllocHGlobal(Marshal.SizeOf()); + ZOwnedClosureZId ownedClosureZId = new ZOwnedClosureZId { - return Array.Empty(); - } + context = (void*)pIdBuffer, + call = z_id_call, + drop = null, + }; + nint pOwnedClosureZId = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.StructureToPtr(ownedClosureZId, pOwnedClosureZId, false); + + ZenohC.z_info_routers_zid(session, (ZOwnedClosureZId*)pOwnedClosureZId); + + ZIdBuffer zIdBuffer; + zIdBuffer = (ZIdBuffer)Marshal.PtrToStructure(pIdBuffer, typeof(ZIdBuffer)); + Id[] ids = zIdBuffer.ToIds(); + + Marshal.FreeHGlobal(pOwnedClosureZId); + Marshal.FreeHGlobal(pIdBuffer); + return ids; + } + } - var output = GetIdStrings(buffer); + // public Id[] PeersId() + // { + // } - Marshal.FreeHGlobal(buffer); - return output.ToArray(); - } + public bool PutStr(string key, string value) + { + return PutStr(key, value, CongestionControl.Block, Priority.RealTime); + } - public bool PutStr(KeyExpr key, string s) + public bool PutStr(string key, string s, CongestionControl congestionControl, Priority priority) + { + unsafe { byte[] data = Encoding.UTF8.GetBytes(s); - PutOptions options = new PutOptions(); - options.SetEncoding(ZEncoding.New(ZEncodingPrefix.TextPlain)); - return _Put(key, data, ref options); + ZPutOptions options = new ZPutOptions + { + encoding = ZenohC.z_encoding(EncodingPrefix.TextPlain, null), + congestionControl = congestionControl, + priority = priority, + }; + return _put(key, data, options); } - - public bool PutJson(KeyExpr key, string s) + } + + public bool PutJson(string key, string value) + { + return PutJson(key, value, CongestionControl.Block, Priority.RealTime); + } + + public bool PutJson(string key, string s, CongestionControl congestionControl, Priority priority) + { + unsafe { byte[] data = Encoding.UTF8.GetBytes(s); - PutOptions options = new PutOptions(); - options.SetEncoding(ZEncoding.New(ZEncodingPrefix.AppJson)); - return _Put(key, data, ref options); + ZPutOptions options = new ZPutOptions + { + encoding = ZenohC.z_encoding(EncodingPrefix.AppJson, null), + congestionControl = congestionControl, + priority = priority, + }; + return _put(key, data, options); } + } - public bool PutInt(KeyExpr key, Int64 value) - { - byte[] data = Encoding.UTF8.GetBytes(value.ToString()); - PutOptions options = new PutOptions(); - options.SetEncoding(ZEncoding.New(ZEncodingPrefix.AppInteger)); - return _Put(key, data, ref options); - } + public bool PutInt(string key, long value) + { + return PutInt(key, value, CongestionControl.Block, Priority.RealTime); + } - public bool PutFloat(KeyExpr key, double value) + public bool PutInt(string key, long value, CongestionControl congestionControl, Priority priority) + { + unsafe { - string s = value.ToString(); + string s = value.ToString("G"); byte[] data = Encoding.UTF8.GetBytes(s); - PutOptions options = new PutOptions(); - options.SetEncoding(ZEncoding.New(ZEncodingPrefix.AppFloat)); - return _Put(key, data, ref options); + ZPutOptions options = new ZPutOptions + { + encoding = ZenohC.z_encoding(EncodingPrefix.AppInteger, null), + congestionControl = congestionControl, + priority = priority, + }; + return _put(key, data, options); } + } - protected bool _Put(KeyExpr key, byte[] value, ref PutOptions options) - { - IntPtr v = Marshal.AllocHGlobal(value.Length); - Marshal.Copy(value, 0, v, value.Length); - int r = ZPut(ref native, key.native, v, (ulong)value.Length, ref options.native); - Marshal.FreeHGlobal(v); - return r == 0; - } + public bool PutFloat(string key, double value) + { + return PutFloat(key, value, CongestionControl.Block, Priority.RealTime); + } - /* - public ReplayDataArray Get(KeyExpr key) + public bool PutFloat(string key, double value, CongestionControl congestionControl, Priority priority) + { + unsafe { - QueryTarget target = new QueryTarget(); - target.Target = ZTarget.All; - QueryConsolidation consolidation = new QueryConsolidation(); - string v = "abc"; - IntPtr predicate = Marshal.StringToHGlobalAnsi(v); - ReplayDataArray.NativeType nativeData = - ZGetCollect(ref native, key.native, predicate, target.native, consolidation.native); - Marshal.FreeHGlobal(predicate); - return new ReplayDataArray(nativeData); + string s = value.ToString("G"); + byte[] data = Encoding.UTF8.GetBytes(s); + ZPutOptions options = new ZPutOptions + { + encoding = ZenohC.z_encoding(EncodingPrefix.AppFloat, null), + congestionControl = congestionControl, + priority = priority, + }; + return _put(key, data, options); } - */ + } - public bool RegisterSubscriber(Subscriber subscriber) + private bool _put(string key, byte[] value, ZPutOptions options) + { + if (_disposed) return false; + unsafe { - return RegisterSubscriber(subscriber, Subscriber.GetOptionsDefault()); + nint pv = Marshal.AllocHGlobal(value.Length); + nuint len = (nuint)value.Length; + Marshal.Copy(value, 0, pv, value.Length); + nint pKey = Marshal.StringToHGlobalAnsi(key); + ZSession session = ZenohC.z_session_loan(_session); + ZKeyexpr keyexpr = ZenohC.z_keyexpr((byte*)pKey); + int r = ZenohC.z_put(session, keyexpr, (byte*)pv, len, &options); + Marshal.FreeHGlobal(pv); + Marshal.FreeHGlobal(pKey); + return r == 0; } + } - public bool RegisterSubscriber(Subscriber subscriber, Subscriber.Options options) + public SubscriberHandle? RegisterSubscriber(Subscriber subscriber) + { + unsafe { - Subscriber.NativeType nativeSubscriber = ZDeclareSubscribe(ref native, subscriber.key.native, - ref subscriber.closure, - ref options); - if (ZSubscriberCheck(ref nativeSubscriber)) + if (subscriber.ownedSubscriber != null) { - subscriber.nativeSubscriber = nativeSubscriber; - Subscribers[subscriber.key.GetStr()] = subscriber; - return true; + return null; } - else + + ZSession session = ZenohC.z_session_loan(_session); + nint pKey = Marshal.StringToHGlobalAnsi(subscriber.keyexpr); + ZKeyexpr keyexpr = ZenohC.z_keyexpr((byte*)pKey); + ZSubscriberOptions options = new ZSubscriberOptions { - return false; + reliability = subscriber.reliability, + }; + nint pOptions = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.StructureToPtr(options, pOptions, false); + ZOwnedSubscriber sub = + ZenohC.z_declare_subscriber(session, keyexpr, subscriber.closureSample, (ZSubscriberOptions*)pOptions); + Marshal.FreeHGlobal(pOptions); + Marshal.FreeHGlobal(pKey); + + if (ZenohC.z_subscriber_check(&sub) != 1) + { + return null; } - } - - public void UnregisterSubscriber(string key) - { - Subscriber subscriber = Subscribers[key]; - // 目前执行这句会出问题, 还在解决中 - // zSubscriberClose(ref subscriber.nativeSubscriber); - Subscribers.Remove(key); - } - - [DllImport(ZenohC.DllName, EntryPoint = "z_open", CallingConvention = CallingConvention.Cdecl)] - internal static extern NativeType ZOpen(ref Config.NativeType config); - - [DllImport(ZenohC.DllName, EntryPoint = "z_session_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern bool ZSessionCheck(ref NativeType session); - [DllImport(ZenohC.DllName, EntryPoint = "z_close", CallingConvention = CallingConvention.Cdecl)] - internal static extern void ZClose(ref NativeType session); + nint pOwnedSubscriber = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.StructureToPtr(sub, pOwnedSubscriber, false); + subscriber.ownedSubscriber = (ZOwnedSubscriber*)pOwnedSubscriber; - [DllImport(ZenohC.DllName, EntryPoint = "z_info_zid", CallingConvention = CallingConvention.Cdecl)] - internal static extern Zid ZInfoZid(ref NativeType session); + _indexSubscriber += 1; + subscribers.Add(_indexSubscriber, subscriber); - [DllImport(ZenohC.DllName, EntryPoint = "z_info_peers_zid", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte ZInfoPeersZid(ref NativeType session, ref ZClosureZid callback); - - [DllImport(ZenohC.DllName, EntryPoint = "z_info_routers_zid", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte ZInfoRoutersZid(ref NativeType session, ref ZClosureZid callback); - - [DllImport(ZenohC.DllName, EntryPoint = "z_put", CallingConvention = CallingConvention.Cdecl)] - internal static extern int ZPut(ref NativeType session, KeyExpr.NativeType keyexpr, IntPtr payload, ulong len, - ref PutOptions.NativeType opts); - - [DllImport(ZenohC.DllName, EntryPoint = "z_subscriber_check")] - internal static extern bool ZSubscriberCheck(ref Subscriber.NativeType sub); + return new SubscriberHandle + { + handle = _indexSubscriber, + }; + } + } - [DllImport(ZenohC.DllName, EntryPoint = "z_declare_subscriber")] - internal static extern Subscriber.NativeType ZDeclareSubscribe(ref NativeType session, - KeyExpr.NativeType keyexpr, - ref ZClosureSample callback, ref Subscriber.Options opts); + public void UnregisterSubscriber(SubscriberHandle handle) + { + UnregisterSubscriber(handle.handle); + } - [DllImport(ZenohC.DllName, EntryPoint = "z_subscriber_close")] - internal static extern void ZSubscriberClose(ref Subscriber.NativeType sub); + private void UnregisterSubscriber(int handle) + { + if (subscribers.TryGetValue(handle, out Subscriber? subscriber)) + { + unsafe + { + ZenohC.z_undeclare_subscriber(subscriber.ownedSubscriber); + Marshal.FreeHGlobal((nint)subscriber.ownedSubscriber); + subscriber.ownedSubscriber = null; + } - /* - [DllImport(Zenoh.DllName, EntryPoint = "z_get_collect", CallingConvention = CallingConvention.Cdecl)] - internal static extern ReplayDataArray.NativeType ZGetCollect(ref NativeType session, - KeyExpr.NativeType keyexpr, IntPtr predicate, QueryTarget.NativeType target, - QueryConsolidation.NativeType consolidation); - */ + subscribers.Remove(handle); + } } } \ No newline at end of file diff --git a/Zenoh/Subscriber.cs b/Zenoh/Subscriber.cs index 724ce89..412ab18 100644 --- a/Zenoh/Subscriber.cs +++ b/Zenoh/Subscriber.cs @@ -1,74 +1,78 @@ +#pragma warning disable CS8500 + using System; using System.Runtime.InteropServices; -namespace Zenoh +namespace Zenoh; + +public struct SubscriberHandle { - public delegate void SubscriberCallback(Sample sample); + internal int handle; +} - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - internal delegate void ZOwnedClosureSampleCallback(ref Sample.NativeType sample, IntPtr context); +public delegate void SubscriberCallback(Sample sample); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - internal delegate void ZOwnedClosureSampleDrop(ref Sample.NativeType sample, IntPtr context); +public class Subscriber : IDisposable +{ + internal readonly unsafe ZOwnedClosureSample* closureSample; + internal unsafe ZOwnedSubscriber* ownedSubscriber; + internal readonly string keyexpr; + internal readonly Reliability reliability; + private GCHandle _userCallbackGcHandle; + private bool _disposed; - [StructLayout(LayoutKind.Sequential)] - internal struct ZClosureSample // z_owned_closure_sample_t + public Subscriber(string key, SubscriberCallback userCallback) + : this(key, userCallback, Reliability.Reliable) { - internal IntPtr context; - internal ZOwnedClosureSampleCallback call; - internal ZOwnedClosureSampleDrop drop; - - internal ZClosureSample(ZOwnedClosureSampleCallback call, ZOwnedClosureSampleDrop drop, IntPtr context) - { - this.call = call; - this.drop = drop; - this.context = context; - } } - public class Subscriber + public Subscriber(string key, SubscriberCallback userCallback, Reliability reliability) { - [StructLayout(LayoutKind.Sequential)] - public struct NativeType // z_owned_subscriber_t + unsafe { - public IntPtr p; - } + keyexpr = key; + _disposed = false; + this.reliability = reliability; + _userCallbackGcHandle = GCHandle.Alloc(userCallback); + ZOwnedClosureSample ownedClosureSample = new ZOwnedClosureSample + { + context = (void*)GCHandle.ToIntPtr(_userCallbackGcHandle), + call = Call, + drop = null, + }; - [StructLayout(LayoutKind.Sequential)] - public struct Options // z_subscriber_options_t - { - public Reliability reliability; + nint p = Marshal.AllocHGlobal(Marshal.SizeOf(ownedClosureSample)); + Marshal.StructureToPtr(ownedClosureSample, p, false); + closureSample = (ZOwnedClosureSample*)p; + ownedSubscriber = null; } + } - internal KeyExpr key; - internal NativeType nativeSubscriber; - internal SubscriberCallback userCallback = null; - internal ZOwnedClosureSampleCallback sampleCallback = null; - internal ZOwnedClosureSampleDrop sampleDrop = null; - internal ZClosureSample closure; + public void Dispose() => Dispose(true); - public Subscriber(string key, SubscriberCallback userCallback) - { - this.key = KeyExpr.FromString(key); - this.nativeSubscriber.p = IntPtr.Zero; - this.userCallback = userCallback; - ZOwnedClosureSampleCallback call = new ZOwnedClosureSampleCallback(SubscriberNativeCallbackImpl); - sampleCallback = call; - this.closure = new ZClosureSample(call, null, IntPtr.Zero); - } + private void Dispose(bool disposing) + { + if (_disposed) return; - internal void SubscriberNativeCallbackImpl(ref Sample.NativeType samplePtr, IntPtr arg) + unsafe { - Sample s = new Sample(samplePtr); - this.userCallback(s); + ZenohC.z_closure_sample_drop(closureSample); + Marshal.FreeHGlobal((nint)closureSample); + Marshal.FreeHGlobal((nint)ownedSubscriber); } - public static Options GetOptionsDefault() + _userCallbackGcHandle.Free(); + _disposed = true; + } + + private static unsafe void Call(ZSample* zSample, void* context) + { + GCHandle gch = GCHandle.FromIntPtr((nint)context); + SubscriberCallback callback = (SubscriberCallback)gch.Target; + Sample sample = new Sample(zSample); + if (callback != null) { - return ZSubScriberOptionsDefault(); + callback(sample); } - - [DllImport(ZenohC.DllName, EntryPoint = "z_subscriber_options_default")] - internal static extern Options ZSubScriberOptionsDefault(); } } \ No newline at end of file diff --git a/Zenoh/Zenoh.csproj b/Zenoh/Zenoh.csproj index fabdb32..4f453ed 100644 --- a/Zenoh/Zenoh.csproj +++ b/Zenoh/Zenoh.csproj @@ -16,5 +16,16 @@ 0.0.6 zenoh net7.0;net6.0 + Release;Debug + x64;ARM64 + + + TRACE;PLATFORM_ARM + + + TRACE;PLATFORM_ARM64 + + + TRACE;PLATFORM_x64 diff --git a/Zenoh/ZenohC.cs b/Zenoh/ZenohC.cs index 567137d..4eedef6 100644 --- a/Zenoh/ZenohC.cs +++ b/Zenoh/ZenohC.cs @@ -1,492 +1,1097 @@ -// -// Copyright (c) 2021 ADLINK Technology Inc. -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ADLINK zenoh team, -// +#pragma warning disable CS8500 -using System; using System.Runtime.InteropServices; -using System.Collections.Generic; -using System.Text; +namespace Zenoh; -namespace Zenoh +// z_congestion_control_t +public enum CongestionControl { - public static class ZenohC - { - public const string DllName = "zenohc"; + Block, + Drop +} - [DllImport(ZenohC.DllName, EntryPoint = "zc_init_logger")] - public static extern void InitLogger(); +// z_encoding_prefix_t +public enum EncodingPrefix +{ + Empty = 0, + AppOctetStream = 1, + AppCustom = 2, + TextPlain = 3, + AppProperties = 4, + AppJson = 5, + AppSql = 6, + AppInteger = 7, + AppFloat = 8, + AppXml = 9, + AppXhtmlXml = 10, + AppXWwwFormUrlencoded = 11, + TextJson = 12, + TextHtml = 13, + TextXml = 14, + TextCss = 15, + TextCsv = 16, + TextJavascript = 17, + ImageJpeg = 18, + ImagePng = 19, + ImageGif = 20 +} + +// z_sample_kind_t +public enum SampleKind +{ + Put = 0, + Delete = 1 +} - public static string IdBytesToStr(byte[] buf) - { - StringBuilder str = new StringBuilder(); - for (int i = buf.Length - 1; i >= 0; i--) - { - str.Append($"{buf[i]:X2}"); - } +// z_priority_t +public enum Priority +{ + RealTime = 1, + InteractiveHigh = 2, + InteractiveLow = 3, + DataHigh = 4, + Data = 5, + DataLow = 6, + Background = 7 +} + +// z_consolidation_mode_t +public enum ConsolidationMode +{ + Auto = -1, + None = 0, + Monotonic = 1, + Latest = 2, +} + +// z_query_consolidation_t +public enum QueryConsolidation +{ + Auto = -1, + None = 0, + Monotonic = 1, + Latest = 2, +} + +// z_reliability_t +public enum Reliability +{ + BestEffort, + Reliable +} - return str.ToString(); - } +// z_query_target_t +public enum QueryTarget +{ + BestMatching, + All, + AllComplete, +}; + +// z_bytes_t +// -------------------------------- +// typedef struct z_bytes_t { +// size_t len; +// const uint8_t *start; +// } z_bytes_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal unsafe struct ZBytes +{ + internal nuint len; + internal byte* start; +} + +// z_id_t +// -------------------------------- +// typedef struct z_id_t { +// uint8_t id[16]; +// } z_id_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal unsafe struct ZId +{ + // [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + internal fixed byte id[16]; +} + +// z_owned_str_t +// -------------------------------- +// typedef struct z_owned_str_t { +// char *_cstr; +// } z_owned_str_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZOwnedStr +{ + internal nint cstr; +} + +// z_owned_str_array_t +// -------------------------------- +// typedef struct z_owned_str_array_t { +// char **val; +// size_t len; +// } z_owned_str_array_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZOwnedStrArray +{ + internal nint val; + internal nuint len; +} + +// z_str_array_t +// -------------------------------- +// typedef struct z_str_array_t { +// size_t len; +// const char *const *val; +// } z_str_array_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZStrArray +{ + internal nuint len; + internal nint val; +} + +// z_owned_encoding_t +// -------------------------------- +// typedef struct z_owned_encoding_t { +// enum z_encoding_prefix_t prefix; +// struct z_bytes_t suffix; +// bool _dropped; +// } z_owned_encoding_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZOwnedEncoding +{ + internal EncodingPrefix prefix; + internal ZBytes suffix; + internal sbyte _dropped; +} + +// z_encoding_t +// -------------------------------- +// typedef struct z_encoding_t { +// enum z_encoding_prefix_t prefix; +// struct z_bytes_t suffix; +// } z_encoding_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZEncoding +{ + internal EncodingPrefix prefix; + internal ZBytes suffix; +} + +// public string PrefixToString() +// { +// return prefix.ToString(); +// } +// +// public static ZEncoding New(EncodingPrefix prefix) +// { +// return FnZEncoding(prefix, IntPtr.Zero); +// } +// +// [DllImport(ZenohC.DllName, EntryPoint = "z_encoding", CallingConvention = CallingConvention.Cdecl)] +// internal static extern ZEncoding FnZEncoding(EncodingPrefix prefix, IntPtr suffix); + +// z_timestamp_t +// -------------------------------- +// typedef struct z_timestamp_t { +// uint64_t time; +// struct z_id_t id; +// } z_timestamp_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZTimestamp +{ + internal ulong time; + internal ZId id; +} + +// z_keyexpr_t +// -------------------------------- +// typedef struct z_keyexpr_t { +// uint64_t _0[4]; +// } z_keyexpr_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZKeyexpr +{ + // [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] + private fixed ulong _[4]; +} + +// -------------------------------- +// typedef struct z_owned_keyexpr_t { +// uint64_t _0[4]; +// } z_keyexpr_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedKeyexpr +{ + // [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] + private fixed ulong _[4]; +} + +// z_sample_t +// -------------------------------- +// typedef struct z_sample_t { +// struct z_keyexpr_t keyexpr; +// struct z_bytes_t payload; +// struct z_encoding_t encoding; +// const void *_zc_buf; +// enum z_sample_kind_t kind; +// struct z_timestamp_t timestamp; +// } z_sample_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZSample +{ + internal ZKeyexpr keyexpr; + internal ZBytes payload; + internal ZEncoding encoding; + private nint _zc_buf; + internal SampleKind kind; + internal ZTimestamp timestamp; +} + +// z_owned_config_t +// -------------------------------- +// typedef struct z_owned_config_t { +// void *_0; +// } z_owned_config_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZOwnedConfig +{ + private nint _; +} + +// z_config_t +// -------------------------------- +// typedef struct z_config_t { +// const struct z_owned_config_t *_0; +// } z_config_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZConfig +{ + private nint _; +} + +// z_owned_publisher_t +// -------------------------------- +// typedef struct ALIGN(8) z_owned_publisher_t { +// uint64_t _0[7]; +// } z_owned_publisher_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedPublisher +{ + // [MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)] + private fixed ulong _[7]; +} + +// z_owned_queryable_t +// -------------------------------- +// typedef struct ALIGN(8) z_owned_queryable_t { +// uint64_t _0[4]; +// } z_owned_queryable_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedQueryable +{ + private fixed ulong _[4]; +} + +// z_publisher_t +// -------------------------------- +// typedef struct z_publisher_t { +// const struct z_owned_publisher_t *_0; +// } z_publisher_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZPublisher +{ + private nint _; +} + +// z_publisher_options_t +// -------------------------------- +// typedef struct z_publisher_options_t { +// enum z_congestion_control_t congestion_control; +// enum z_priority_t priority; +// } z_publisher_options_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZPublisherOptions +{ + internal CongestionControl congestion_control; + internal Priority priority; +} + +// z_publisher_delete_options_t +// -------------------------------- +// typedef struct z_publisher_delete_options_t { +// uint8_t __dummy; +// } z_publisher_delete_options_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZPublisherDeleteOptions +{ + internal byte dummy; +} + +// z_publisher_put_options_t +// -------------------------------- +// typedef struct z_publisher_put_options_t { +// struct z_encoding_t encoding; +// } z_publisher_put_options_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZPublisherPutOptions +{ + internal ZEncoding encoding; +} + +// z_pull_subscriber_options_t +// -------------------------------- +// typedef struct z_pull_subscriber_options_t { +// enum z_reliability_t reliability; +// } z_pull_subscriber_options_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZPullSubscriberOptions +{ + internal Reliability reliability; +} + +// z_owned_pull_subscriber_t +// -------------------------------- +// typedef struct ALIGN(8) z_owned_pull_subscriber_t { +// uint64_t _0[1]; +// } z_owned_pull_subscriber_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal struct ZOwnedPullSubscriber +{ + private ulong _; +} + +// z_pull_subscriber_t +// -------------------------------- +// typedef struct z_pull_subscriber_t { +// const struct z_owned_pull_subscriber_t *_0; +// } z_pull_subscriber_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZPullSubscriber +{ + internal nint _; +} + +// z_queryable_options_t +// -------------------------------- +// typedef struct z_queryable_options_t { +// bool complete; +// } z_queryable_options_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZQueryableOptions +{ + internal sbyte complete; +} + +// z_subscriber_options_t +// -------------------------------- +// typedef struct z_subscriber_options_t { +// enum z_reliability_t reliability; +// } z_subscriber_options_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZSubscriberOptions +{ + internal Reliability reliability; +} + +// z_owned_subscriber_t +// -------------------------------- +// typedef struct ALIGN(8) z_owned_subscriber_t { +// uint64_t _0[1]; +// } z_owned_subscriber_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal struct ZOwnedSubscriber +{ + private ulong _; +} + +#if PLATFORM_ARM64 +// -------------------------------- +// typedef struct ALIGN(16) z_owned_reply_t { +// uint64_t _0[24]; +// } z_owned_reply_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential, Pack = 16)] +internal unsafe struct ZOwnedReply{ + private fixed ulong _[24]; +} +#elif PLATFORM_x64 +// -------------------------------- +// typedef struct ALIGN(8) z_owned_reply_t { +// uint64_t _0[22]; +// } z_owned_reply_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedReply +{ + private fixed ulong _[22]; +} +#else +#error PLATFORM_ARM64 or PLATFORM_x64 +#endif + +// z_delete_options_t +// -------------------------------- +// typedef struct z_delete_options_t { +// enum z_congestion_control_t congestion_control; +// enum z_priority_t priority; +// } z_delete_options_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZDeleteOptions +{ + internal CongestionControl congestion_control; + internal Priority priority; +} + +// z_query_consolidation_t +// -------------------------------- +// typedef struct z_query_consolidation_t { +// enum z_consolidation_mode_t mode; +// } z_query_consolidation_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZQueryConsolidation +{ + internal ConsolidationMode mode; +} + +// z_get_options_t +// -------------------------------- +// typedef struct z_get_options_t { +// enum z_query_target_t target; +// struct z_query_consolidation_t consolidation; +// struct z_value_t value; +// } z_get_options_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZGetOptions +{ + internal QueryTarget target; + internal ZQueryConsolidation consolidation; + internal ZValue value; +} + +// z_value_t +// -------------------------------- +// typedef struct z_value_t { +// struct z_bytes_t payload; +// struct z_encoding_t encoding; +// } z_value_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZValue +{ + internal ZBytes payload; + internal ZEncoding encoding; +} + +// zc_owned_payload_t +// -------------------------------- +// typedef struct zc_owned_payload_t { +// struct z_bytes_t payload; +// uintptr_t _owner[4]; +// } zc_owned_payload_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal unsafe struct ZcOwnedPayload +{ + private fixed ulong _[4]; +} + +// zc_owned_shmbuf_t +// -------------------------------- +// typedef struct zc_owned_shmbuf_t { +// uintptr_t _0[9]; +// } zc_owned_shmbuf_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal unsafe struct ZcOwnedShmbuf +{ + private fixed ulong _[9]; +} + +// zc_owned_shm_manager_t +// -------------------------------- +// typedef struct zc_owned_shm_manager_t { +// uintptr_t _0; +// } zc_owned_shm_manager_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZcOwnedShmManager +{ + private nint _; +} + +// z_put_options_t +// -------------------------------- +// typedef struct z_put_options_t { +// struct z_encoding_t encoding; +// enum z_congestion_control_t congestion_control; +// enum z_priority_t priority; +// } z_put_options_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZPutOptions +{ + internal ZEncoding encoding; + internal CongestionControl congestionControl; + internal Priority priority; +} + +// z_query_reply_options_t +// -------------------------------- +// typedef struct z_query_reply_options_t { +// struct z_encoding_t encoding; +// } z_query_reply_options_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZQueryReplyOptions +{ + internal ZEncoding encoding; +} + +// z_query_t +// -------------------------------- +// typedef struct z_query_t { +// void *_0; +// } z_query_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZQuery +{ + private nint _; +} + +// z_owned_session_t +// -------------------------------- +// typedef struct z_owned_session_t { +// uintptr_t _0; +// } z_owned_session_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZOwnedSession +{ + private nint _; +} + +// z_session_t +// -------------------------------- +// typedef struct z_session_t { +// uintptr_t _0; +// } z_session_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZSession +{ + private nint _; +} + +// z_owned_closure_sample_t +// -------------------------------- +// typedef struct z_owned_closure_sample_t { +// void *context; +// void (*call)(const struct z_sample_t*, void *context); +// void (*drop)(void*); +// } z_owned_closure_sample_t; +// -------------------------------- +internal unsafe delegate void ZOwnedClosureSampleCall(ZSample* sample, void* context); + +internal unsafe delegate void ZOwnedClosureSampleDrop(void* context); + +[StructLayout(LayoutKind.Sequential)] +internal unsafe struct ZOwnedClosureSample +{ + internal void* context; + internal ZOwnedClosureSampleCall call; + internal ZOwnedClosureSampleDrop drop; +} + +// -------------------------------- +// typedef struct z_owned_closure_zid_t { +// void *context; +// void (*call)(const struct z_id_t*, void*); +// void (*drop)(void*); +// } z_owned_closure_zid_t; +// -------------------------------- +internal unsafe delegate void ZOwnedClosureZIdCall(ZId* zId, void* context); + +internal unsafe delegate void ZOwnedClosureZIdDrop(void* context); + +[StructLayout(LayoutKind.Sequential)] +internal unsafe struct ZOwnedClosureZId +{ + internal void* context; + internal ZOwnedClosureZIdCall call; + internal ZOwnedClosureZIdDrop drop; +} + +// z_owned_closure_query_t +// -------------------------------- +// typedef struct z_owned_closure_query_t { +// void *context; +// void (*call)(const struct z_query_t*, void *context); +// void (*drop)(void*); +// } z_owned_closure_query_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal unsafe struct ZOwnedClosureQuery +{ + internal void* context; + internal delegate* unmanaged[Cdecl] call; + internal delegate* unmanaged[Cdecl] drop; +} + +// z_owned_closure_reply_t +// -------------------------------- +// typedef struct z_owned_closure_reply_t { +// void *context; +// void (*call)(struct z_owned_reply_t*, void*); +// void (*drop)(void*); +// } z_owned_closure_reply_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal unsafe struct ZOwnedClosureReply +{ + internal void* context; + internal delegate* unmanaged[Cdecl] call; + internal delegate* unmanaged[Cdecl] drop; +} - internal const int RoutersNum = 128; - internal const int PeersNum = 256; - internal const int IdLength = 16; - } +[StructLayout(LayoutKind.Sequential)] +public struct ConsolidationStrategy // z_consolidation_strategy_t +{ + public ConsolidationMode firstRouters; + public ConsolidationMode lastRouters; + public ConsolidationMode reception; +} - public enum ZEncodingPrefix : int // z_encoding_prefix_t +internal static unsafe class ZenohC +{ + internal const string DllName = "zenohc"; + internal static uint zRouter = 1; + internal static uint zPeer = 2; + internal static uint zClient = 4; + internal static string zConfigModeKey = "mode"; + internal static string zConfigConnectKey = "connect/endpoints"; + internal static string zConfigListenKey = "listen/endpoints"; + internal static string zConfigUserKey = "transport/auth/usrpwd/user"; + internal static string zConfigPasswordKey = "transport/auth/usrpwd/password"; + internal static string zConfigMulticastScoutingKey = "scouting/multicast/enabled"; + internal static string zConfigMulticastInterfaceKey = "scouting/multicast/interface"; + internal static string zConfigMulticastIpv4AddressKey = "scouting/multicast/address"; + internal static string zConfigScoutingTimeoutKey = "scouting/timeout"; + internal static string zConfigScoutingDelayKey = "scouting/delay"; + internal static string zConfigAddTimestampKey = "add_timestamp"; + + internal static string ZOwnedStrToString(ZOwnedStr* zs) { - Empty = 0, - AppOctetStream = 1, - AppCustom = 2, - TextPlain = 3, - AppProperties = 4, - AppJson = 5, - AppSql = 6, - AppInteger = 7, - AppFloat = 8, - AppXml = 9, - AppXhtmlXml = 10, - AppXWwwFormUrlencoded = 11, - TextJson = 12, - TextHtml = 13, - TextXml = 14, - TextCss = 15, - TextCsv = 16, - TextJavascript = 17, - ImageJpeg = 18, - ImagePng = 19, - ImageGif = 20, - } + if (z_str_check(zs) != 1) + { + return ""; + } - public enum ZSampleKind : int // z_sample_kind_t - { - Put = 0, - Delete = 1, + return Marshal.PtrToStringUTF8(zs->cstr); } - public enum ZCongestionControl : int // z_congestion_control_t + internal static string ZKeyexprToString(ZKeyexpr keyexpr) { - Block, - Drop, + ZOwnedStr str = z_keyexpr_to_string(keyexpr); + string o = ZOwnedStrToString(&str); + z_str_drop(&str); + return o; } - public enum ZPriority : int // z_priority_t - { - RealTime = 1, - InteractiveHigh = 2, - InteractiveLow = 3, - DataHigh = 4, - Data = 5, - DataLow = 6, - Background = 7, - } + [DllImport(DllName, EntryPoint = "z_bytes_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_bytes_check(ZBytes* b); - public enum ConsolidationMode : int // z_consolidation_mode_t - { - Full, - Lazy, - None, - } + [DllImport(DllName, EntryPoint = "z_str_array_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_str_array_check(ZOwnedStrArray* strs); - public enum Reliability : int // z_reliability_t - { - BestEffort, - Reliable, - } + [DllImport(DllName, EntryPoint = "z_str_array_drop", CallingConvention = CallingConvention.Cdecl)] + internal static extern void z_str_array_drop(ZOwnedStrArray* strs); - public enum SubMode : int // z_submode_t - { - Push, - Pull, - } + [DllImport(DllName, EntryPoint = "z_str_array_loan", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZStrArray z_str_array_loan(ZOwnedStrArray* strs); - public enum ZTarget : int // z_target_t - { - BestMatching, - All, - None, - AllComplete, - } + [DllImport(DllName, EntryPoint = "z_str_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_str_check(ZOwnedStr* s); - public enum QueryConsolidationTag // z_query_consolidation_t_Tag - { - Auto, - Manual, - } + [DllImport(DllName, EntryPoint = "z_str_drop", CallingConvention = CallingConvention.Cdecl)] + internal static extern void z_str_drop(ZOwnedStr* s); - [StructLayout(LayoutKind.Sequential)] - public struct ZBytes // z_bytes_t, z_owned_bytes_t - { - public IntPtr start; - public ulong len; - } + [DllImport(DllName, EntryPoint = "z_str_loan", CallingConvention = CallingConvention.Cdecl)] + internal static extern byte* z_str_loan(ZOwnedStr* s); - [StructLayout(LayoutKind.Sequential)] - public struct ZTimestamp // z_timestamp_t - { - public UInt64 time; - public ZBytes id; - } + [DllImport(DllName, EntryPoint = "z_str_null", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedStr z_str_null(); - [StructLayout(LayoutKind.Sequential)] - public struct ZString // z_owned_string_t, z_string_t - { - internal IntPtr start; - } + [DllImport(DllName, EntryPoint = "z_config_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_config_check(ZOwnedConfig* config); - [StructLayout(LayoutKind.Sequential)] - public struct ZEncoding // z_encoding_t - { - internal ZEncodingPrefix prefix; - internal ZBytes suffix; + [DllImport(DllName, EntryPoint = "z_config_drop", CallingConvention = CallingConvention.Cdecl)] + internal static extern void z_config_drop(ZOwnedConfig* config); - public String PrefixToString() - { - return prefix.ToString(); - } + [DllImport(DllName, EntryPoint = "z_config_loan", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZConfig z_config_loan(ZOwnedConfig* config); - public static ZEncoding New(ZEncodingPrefix prefix) - { - return FnZEncoding(prefix, IntPtr.Zero); - } + [DllImport(DllName, EntryPoint = "z_config_default", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedConfig z_config_default(); - [DllImport(ZenohC.DllName, EntryPoint = "z_encoding", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZEncoding FnZEncoding(ZEncodingPrefix prefix, IntPtr suffix); - } + [DllImport(DllName, EntryPoint = "z_config_new", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedConfig z_config_new(); - [StructLayout(LayoutKind.Sequential)] - public struct Period // z_period_t - { - public uint origin; - public uint period; - public uint duration; + [DllImport(DllName, EntryPoint = "z_config_null", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedConfig z_config_null(); - public Period(uint origin, uint period, uint duration) - { - this.origin = origin; - this.period = period; - this.duration = duration; - } - } + [DllImport(DllName, EntryPoint = "z_config_client", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedConfig z_config_client(string[] peers, nuint nPeers); - [StructLayout(LayoutKind.Sequential)] - public struct Zid - { - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] - internal byte[] id; + [DllImport(DllName, EntryPoint = "z_config_peer", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedConfig z_config_peer(); - public string ToStr() - { - return ZenohC.IdBytesToStr(id); - } - } + [DllImport(DllName, EntryPoint = "zc_config_from_file", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedConfig zc_config_from_file([MarshalAs(UnmanagedType.LPStr)] string path); - public struct PutOptions - { - [StructLayout(LayoutKind.Sequential)] - internal struct NativeType // z_put_options_t - { - internal ZEncoding encoding; - internal ZCongestionControl congestionControl; - internal ZPriority priority; - } + [DllImport(DllName, EntryPoint = "zc_config_from_str", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedConfig zc_config_from_str([MarshalAs(UnmanagedType.LPStr)] string s); - internal NativeType native; + [DllImport(DllName, EntryPoint = "zc_config_get", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedStr zc_config_get(ZConfig config, [MarshalAs(UnmanagedType.LPStr)] string key); - public PutOptions() - { - native = ZPutOptionsDefault(); - } + [DllImport(DllName, EntryPoint = "zc_config_insert_json", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte zc_config_insert_json( + ZConfig config, [MarshalAs(UnmanagedType.LPStr)] string key, [MarshalAs(UnmanagedType.LPStr)] string value); - public void SetEncoding(ZEncoding encoding) - { - native.encoding = encoding; - } + [DllImport(DllName, EntryPoint = "zc_config_to_string", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedStr zc_config_to_string(ZConfig config); - public void SetCongestionControl(ZCongestionControl congestionControl) - { - native.congestionControl = congestionControl; - } + [DllImport(DllName, EntryPoint = "z_encoding", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZEncoding z_encoding(EncodingPrefix prefix, byte* suffix); - public void SetPriority(ZPriority priority) - { - native.priority = priority; - } + [DllImport(DllName, EntryPoint = "z_encoding_default", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZEncoding z_encoding_default(); - [DllImport(ZenohC.DllName, EntryPoint = "z_put_options_default", CallingConvention = CallingConvention.Cdecl)] - private static extern NativeType ZPutOptionsDefault(); - } + [DllImport(DllName, EntryPoint = "z_encoding_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_encoding_check(ZOwnedEncoding* encoding); - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - internal delegate void ZOwnedClosureZidCallback(ref Zid zid, IntPtr p); + [DllImport(DllName, EntryPoint = "z_encoding_drop", CallingConvention = CallingConvention.Cdecl)] + internal static extern void z_encoding_drop(ZOwnedEncoding* encoding); - [StructLayout(LayoutKind.Sequential)] - struct ZClosureZid // z_owned_closure_zid_t - { - internal IntPtr context; - internal ZOwnedClosureZidCallback call; - internal IntPtr drop; + [DllImport(DllName, EntryPoint = "z_encoding_loan", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZEncoding z_encoding_loan(ZOwnedEncoding* encoding); - public ZClosureZid(ZOwnedClosureZidCallback call, IntPtr context) - { - this.context = context; - this.call = call; - this.drop = IntPtr.Zero; - } - } + [DllImport(DllName, EntryPoint = "z_encoding_null", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedEncoding z_encoding_null(); - public class KeyExpr : IDisposable - { - [StructLayout(LayoutKind.Sequential)] - public struct NativeType // z_keyexpr_t - { - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] - private UInt64[] _align; - - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] - private UIntPtr[] _padding; - - internal string GetStr() - { - IntPtr ptr = ZKeyexprToString(this); - string output = Marshal.PtrToStringAnsi(ptr); - // should be free using libc free() - Marshal.FreeHGlobal(ptr); - return output; - } - } + [DllImport(DllName, EntryPoint = "z_keyexpr_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_keyexpr_check(ZOwnedKeyexpr* keyexpr); - internal NativeType native; - private IntPtr _key_buf; - private bool _disposed; + [DllImport(DllName, EntryPoint = "z_keyexpr", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZKeyexpr z_keyexpr(byte* name); + // internal static extern ZKeyexpr z_keyexpr([MarshalAs(UnmanagedType.LPStr)] string name); - public void Dispose() => Dispose(true); + [DllImport(DllName, EntryPoint = "z_keyexpr_as_bytes", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZBytes z_keyexpr_as_bytes(ZKeyexpr keyexpr); - protected virtual void Dispose(bool disposing) - { - if (_disposed) return; - Marshal.FreeHGlobal(this._key_buf); - _disposed = true; - } + [DllImport(DllName, EntryPoint = "z_keyexpr_canonize", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_keyexpr_canonize(byte* start, nuint* len); - internal KeyExpr(NativeType native, IntPtr keyBuf) - { - this.native = native; - this._key_buf = keyBuf; - this._disposed = false; - } + [DllImport(DllName, EntryPoint = "z_keyexpr_canonize_null_terminated", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_keyexpr_canonize_null_terminated(byte* start); - public static KeyExpr FromString(string name) - { - IntPtr p = Marshal.StringToHGlobalAnsi(name); - NativeType keyexpr = ZKeyexpr(p); - //Marshal.FreeHGlobal(p); - return new KeyExpr(keyexpr, p); - } + [DllImport(DllName, EntryPoint = "z_keyexpr_drop", CallingConvention = CallingConvention.Cdecl)] + internal static extern void z_keyexpr_drop(ZOwnedKeyexpr* keyexpr); - public string GetStr() - { - return native.GetStr(); - } + [DllImport(DllName, EntryPoint = "z_keyexpr_concat", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedKeyexpr z_keyexpr_concat(ZKeyexpr left, byte* rightStart, nuint rightLen); - [DllImport(ZenohC.DllName, EntryPoint = "z_keyexpr", CallingConvention = CallingConvention.Cdecl)] - internal static extern NativeType ZKeyexpr(IntPtr name); + [DllImport(DllName, EntryPoint = "z_keyexpr_equals", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_keyexpr_equals(ZKeyexpr left, ZKeyexpr right); - [DllImport(ZenohC.DllName, EntryPoint = "z_keyexpr_to_string", CallingConvention = CallingConvention.Cdecl)] - internal static extern IntPtr ZKeyexprToString(NativeType keyexpr); - } + [DllImport(DllName, EntryPoint = "z_keyexpr_includes", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_keyexpr_includes(ZKeyexpr left, ZKeyexpr right); - public class QueryTarget - { - [StructLayout(LayoutKind.Sequential)] - public struct NativeType // z_query_target_t - { - internal uint kind; - internal ZTarget target; - } + [DllImport(DllName, EntryPoint = "z_keyexpr_intersects", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_keyexpr_intersects(ZKeyexpr left, ZKeyexpr right); - internal NativeType native; + [DllImport(DllName, EntryPoint = "z_keyexpr_is_canon", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_keyexpr_is_canon(byte* start, nuint len); - public ZTarget Target - { - get { return native.target; } - set { native.target = value; } - } + [DllImport(DllName, EntryPoint = "z_keyexpr_is_initialized", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_keyexpr_is_initialized(ZKeyexpr* keyexpr); - public QueryTarget() - { - this.native = ZQueryTargetDefault(); - } + [DllImport(DllName, EntryPoint = "z_keyexpr_join", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedKeyexpr z_keyexpr_join(ZKeyexpr left, ZKeyexpr right); - [DllImport(ZenohC.DllName, EntryPoint = "z_query_target_default", CallingConvention = CallingConvention.Cdecl)] - internal static extern NativeType ZQueryTargetDefault(); - } + [DllImport(DllName, EntryPoint = "z_keyexpr_loan", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZKeyexpr z_keyexpr_loan(ZOwnedKeyexpr* keyexpr); + [DllImport(DllName, EntryPoint = "z_keyexpr_new", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedKeyexpr z_keyexpr_new(byte* name); - [StructLayout(LayoutKind.Sequential)] - public struct ConsolidationStrategy // z_consolidation_strategy_t - { - public ConsolidationMode firstRouters; - public ConsolidationMode lastRouters; - public ConsolidationMode reception; - } + [DllImport(DllName, EntryPoint = "z_keyexpr_unchecked", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZKeyexpr z_keyexpr_unchecked(byte* name); - public class QueryConsolidation - { - [StructLayout(LayoutKind.Sequential)] - public struct NativeType // z_query_consolidation_t - { - public QueryConsolidationTag tag; - public ConsolidationStrategy manual; - } + [DllImport(DllName, EntryPoint = "z_keyexpr_null", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedKeyexpr z_keyexpr_null(); - internal NativeType native; + [DllImport(DllName, EntryPoint = "z_keyexpr_to_string", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedStr z_keyexpr_to_string(ZKeyexpr keyexpr); - public QueryConsolidation() - { - this.native = ZQueryConsolidationDefault(); - } + [DllImport(DllName, EntryPoint = "z_declare_keyexpr", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedKeyexpr z_declare_keyexpr(ZSession session, ZKeyexpr keyexpr); - [DllImport(ZenohC.DllName, EntryPoint = "z_query_consolidation_default", - CallingConvention = CallingConvention.Cdecl)] - internal static extern QueryConsolidation.NativeType ZQueryConsolidationDefault(); - } + [DllImport(DllName, EntryPoint = "z_undeclare_keyexpr", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_undeclare_keyexpr(ZSession session, ZOwnedKeyexpr* keyexpr); - public class Sample - { - //[StructLayout(LayoutKind.Explicit)] - //[FieldOffset(0)] - [StructLayout(LayoutKind.Sequential)] - public struct NativeType // z_owned_sample_t - { - internal KeyExpr.NativeType keyexpr; - internal ZBytes payload; - internal ZEncoding encoding; - internal ZSampleKind kind; - internal ZTimestamp timestamp; - } + [DllImport(DllName, EntryPoint = "z_timestamp_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_timestamp_check(ZTimestamp ts); - public string Key { get; } - public byte[] Value { get; } - public ZEncoding Encoding { get; } + [DllImport(DllName, EntryPoint = "z_subscriber_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_subscriber_check(ZOwnedSubscriber* sub); - internal Sample(NativeType sample) - { - Key = sample.keyexpr.GetStr(); - Value = ZTypes.ZBytesToBytesArray(sample.payload); - Encoding = sample.encoding; - } + [DllImport(DllName, EntryPoint = "z_subscriber_null", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedSubscriber z_subscriber_null(); - public string ValueToString() - { - string result = System.Text.Encoding.UTF8.GetString(Value, 0, Value.Length); - return result; - } - } + [DllImport(DllName, EntryPoint = "z_subscriber_options_default", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZSubscriberOptions z_subscriber_options_default(); - public class ReplayData - { - [StructLayout(LayoutKind.Sequential)] - public struct NativeType // z_owned_reply_data_t - { - internal Sample.NativeType sample; - internal uint sourceKind; - internal ZBytes replierId; - } + [DllImport(DllName, EntryPoint = "z_subscriber_pull", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_subscriber_pull(ZPullSubscriber* sub); - public Sample Sample { get; } - public uint SourceKind { get; } - public byte[] ReplierID { get; } + [DllImport(DllName, EntryPoint = "z_declare_subscriber", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedSubscriber z_declare_subscriber( + ZSession session, ZKeyexpr keyexpr, ZOwnedClosureSample* callback, ZSubscriberOptions* options); - public ReplayData(NativeType native) - { - this.Sample = new Sample(native.sample); - this.SourceKind = native.sourceKind; - this.ReplierID = ZTypes.ZBytesToBytesArray(native.replierId); - } - } + [DllImport(DllName, EntryPoint = "z_undeclare_subscriber", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_undeclare_subscriber(ZOwnedSubscriber* sub); - /* - public class ReplayDataArray - { - [StructLayout(LayoutKind.Sequential)] - public struct NativeType // z_owned_reply_data_array_t - { - internal IntPtr val; - internal ulong len; - } + [DllImport(DllName, EntryPoint = "z_pull_subscriber_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_pull_subscriber_check(ZOwnedPullSubscriber* sub); - public List List; + [DllImport(DllName, EntryPoint = "z_pull_subscriber_loan", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZPullSubscriber z_pull_subscriber_loan(ZOwnedPullSubscriber* sub); - public ReplayDataArray(NativeType native) - { - List = new List(); - if (!ZReplyDataArrayCheck(ref native)) - { - return; - } - - unsafe - { - for (int i = 0; i < (int)native.len; i++) - { - ReplayData.NativeType* p = (ReplayData.NativeType*)native.val + - i * sizeof(ReplayData.NativeType); - ReplayData ele = new ReplayData(*p); - List.Add(ele); - } - } - - ZReplyDataArrayFree(ref native); - } + [DllImport(DllName, EntryPoint = "z_pull_subscriber_null", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedPullSubscriber z_pull_subscriber_null(); - [DllImport(Zenoh.DllName, EntryPoint = "z_reply_data_array_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern bool ZReplyDataArrayCheck(ref NativeType replies); + [DllImport(DllName, EntryPoint = "z_pull_subscriber_options_default", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZPullSubscriberOptions z_pull_subscriber_options_default(); - [DllImport(Zenoh.DllName, EntryPoint = "z_reply_data_array_free", CallingConvention = CallingConvention.Cdecl)] - internal static extern void ZReplyDataArrayFree(ref NativeType replies); - } - */ + [DllImport(DllName, EntryPoint = "z_declare_pull_subscriber", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedPullSubscriber z_declare_pull_subscriber( + ZSession session, ZKeyexpr keyexpr, ZOwnedClosureSample* callback, ZPullSubscriberOptions* options); - internal static class ZTypes - { - internal static byte[] ZBytesToBytesArray(ZBytes zb) - { - byte[] managedArray = new byte[(int)zb.len]; - System.Runtime.InteropServices.Marshal.Copy(zb.start, managedArray, 0, (int)zb.len); - return managedArray; - } + [DllImport(DllName, EntryPoint = "z_undeclare_pull_subscriber", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_undeclare_pull_subscriber(ZOwnedPullSubscriber* sub); - internal static string ZBytesToString(ZBytes zb) - { - byte[] managedArray = new byte[(int)zb.len]; - System.Runtime.InteropServices.Marshal.Copy(zb.start, managedArray, 0, (int)zb.len); - string result = System.Text.Encoding.UTF8.GetString(managedArray, 0, (int)zb.len); - return result; - } + [DllImport(DllName, EntryPoint = "z_declare_queryable", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedQueryable z_declare_queryable( + ZSession session, ZKeyexpr keyexpr, ZOwnedClosureQuery* callback, ZQueryableOptions* options); - internal static string ZStringToString(ZString zs) - { - return Marshal.PtrToStringAnsi(zs.start); - } + [DllImport(DllName, EntryPoint = "z_undeclare_queryable", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_undeclare_queryable(ZOwnedQueryable* queryable); - [DllImport(ZenohC.DllName, EntryPoint = "z_string_free", CallingConvention = CallingConvention.Cdecl)] - internal static extern void ZStringFree(ref ZString s); + [DllImport(DllName, EntryPoint = "z_delete", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_delete(ZSession session, ZKeyexpr keyexpr, ZDeleteOptions* options); - [DllImport(ZenohC.DllName, EntryPoint = "z_string_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern bool ZStringCheck(ref ZString s); + [DllImport(DllName, EntryPoint = "z_delete_options_default", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZDeleteOptions z_delete_options_default(); - [DllImport(ZenohC.DllName, EntryPoint = "z_bytes_free", CallingConvention = CallingConvention.Cdecl)] - internal static extern void ZBytesFree(ref ZString s); - } + [DllImport(DllName, EntryPoint = "z_get", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_get( + ZSession session, ZKeyexpr keyexpr, byte* parameters, ZOwnedClosureReply* callback, ZGetOptions* options); + + [DllImport(DllName, EntryPoint = "z_get_options_default", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZGetOptions z_get_options_default(); + + [DllImport(DllName, EntryPoint = "z_info_peers_zid", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_info_peers_zid(ZSession session, ZOwnedClosureZId* callback); + + [DllImport(DllName, EntryPoint = "z_info_routers_zid", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_info_routers_zid(ZSession session, ZOwnedClosureZId* callback); + + [DllImport(DllName, EntryPoint = "z_info_zid", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZId z_info_zid(ZSession session); + + [DllImport(DllName, EntryPoint = "z_declare_publisher", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedPublisher z_declare_publisher( + ZSession session, ZKeyexpr keyexpr, ZPublisherOptions* options); + + [DllImport(DllName, EntryPoint = "z_undeclare_publisher", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_undeclare_publisher(ZOwnedPublisher* publisher); + + [DllImport(DllName, EntryPoint = "z_publisher_options_default", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZPublisherOptions z_publisher_options_default(); + + [DllImport(DllName, EntryPoint = "z_publisher_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_publisher_check(ZOwnedPublisher* pbl); + + [DllImport(DllName, EntryPoint = "z_publisher_delete", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_publisher_delete(ZPublisher publisher, ZPublisherDeleteOptions* options); + + [DllImport(DllName, EntryPoint = "z_publisher_delete_options_default", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZPublisherDeleteOptions z_publisher_delete_options_default(); + + [DllImport(DllName, EntryPoint = "z_publisher_loan", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZPublisher z_publisher_loan(ZOwnedPublisher* pbl); + + [DllImport(DllName, EntryPoint = "z_publisher_null", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedPublisher z_publisher_null(); + + [DllImport(DllName, EntryPoint = "z_publisher_put", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_publisher_put( + ZPublisher publisher, byte* payload, nuint len, ZPublisherPutOptions* options); + + [DllImport(DllName, EntryPoint = "zc_publisher_put_owned", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte zc_publisher_put_owned( + ZPublisher publisher, ZcOwnedPayload* payload, ZPublisherPutOptions* options); + + [DllImport(DllName, EntryPoint = "z_publisher_put_options_default", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZPublisherOptions z_publisher_put_options_default(); + + [DllImport(DllName, EntryPoint = "z_put", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_put(ZSession session, ZKeyexpr keyexpr, byte* payload, nuint len, ZPutOptions* opts); + + [DllImport(DllName, EntryPoint = "zc_put_owned", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte zc_put_owned( + ZSession session, ZKeyexpr keyexpr, ZcOwnedPayload* payload, ZPutOptions* opts); + + [DllImport(DllName, EntryPoint = "z_put_options_default", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZPutOptions z_put_options_default(); + + [DllImport(DllName, EntryPoint = "z_query_consolidation_auto", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZQueryConsolidation z_query_consolidation_auto(); + + [DllImport(DllName, EntryPoint = "z_query_consolidation_default", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZQueryConsolidation z_query_consolidation_default(); + + [DllImport(DllName, EntryPoint = "z_query_consolidation_latest", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZQueryConsolidation z_query_consolidation_latest(); + + [DllImport(DllName, EntryPoint = "z_query_consolidation_monotonic", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZQueryConsolidation z_query_consolidation_monotonic(); + + [DllImport(DllName, EntryPoint = "z_query_consolidation_none", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZQueryConsolidation z_query_consolidation_none(); + + [DllImport(DllName, EntryPoint = "z_query_keyexpr", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZKeyexpr z_query_keyexpr(ZQuery* query); + + [DllImport(DllName, EntryPoint = "z_query_parameters", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZBytes z_query_parameters(ZQuery* query); + + [DllImport(DllName, EntryPoint = "z_query_reply", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_query_reply( + ZQuery* query, ZKeyexpr key, byte* payload, nuint len, ZQueryReplyOptions* options); + + [DllImport(DllName, EntryPoint = "z_query_parameters", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZQueryReplyOptions z_query_reply_options_default(); + + [DllImport(DllName, EntryPoint = "z_query_target_default", CallingConvention = CallingConvention.Cdecl)] + internal static extern QueryTarget z_query_target_default(); + + [DllImport(DllName, EntryPoint = "z_query_value", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZValue z_query_value(ZQuery* query); + + [DllImport(DllName, EntryPoint = "z_queryable_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_queryable_check(ZOwnedQueryable* queryable); + + [DllImport(DllName, EntryPoint = "z_queryable_null", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedQueryable z_queryable_null(); + + [DllImport(DllName, EntryPoint = "z_queryable_options_default", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZQueryableOptions z_queryable_options_default(); + + [DllImport(DllName, EntryPoint = "z_reply_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_reply_check(ZOwnedReply* replyData); + + [DllImport(DllName, EntryPoint = "z_reply_is_ok", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_reply_is_ok(ZOwnedReply* reply); + + [DllImport(DllName, EntryPoint = "z_reply_drop", CallingConvention = CallingConvention.Cdecl)] + internal static extern void z_reply_drop(ZOwnedReply* replyData); + + [DllImport(DllName, EntryPoint = "z_reply_err", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZValue z_reply_err(ZOwnedReply* replyData); + + [DllImport(DllName, EntryPoint = "z_reply_null", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedReply z_reply_null(); + + [DllImport(DllName, EntryPoint = "z_reply_ok", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZSample z_reply_ok(ZOwnedReply* reply); + + [DllImport(DllName, EntryPoint = "z_open", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedSession z_open(ZOwnedConfig* config); + + [DllImport(DllName, EntryPoint = "z_close", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_close(ZOwnedSession* session); + + [DllImport(DllName, EntryPoint = "z_session_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_session_check(ZOwnedSession* session); + + [DllImport(DllName, EntryPoint = "z_session_loan", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZSession z_session_loan(ZOwnedSession* session); + + [DllImport(DllName, EntryPoint = "z_session_null", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedSession z_session_null(); + + [DllImport(DllName, EntryPoint = "zc_session_rcinc", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedSession zc_session_rcinc(ZSession session); + + [DllImport(DllName, EntryPoint = "zc_init_logger", CallingConvention = CallingConvention.Cdecl)] + internal static extern void zc_init_logger(); + + [DllImport(DllName, EntryPoint = "zc_payload_check", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte zc_payload_check(ZcOwnedPayload* payload); + + [DllImport(DllName, EntryPoint = "zc_payload_drop", CallingConvention = CallingConvention.Cdecl)] + internal static extern void zc_payload_drop(ZcOwnedPayload* payload); + + [DllImport(DllName, EntryPoint = "zc_payload_null", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZcOwnedPayload zc_payload_null(); + + [DllImport(DllName, EntryPoint = "zc_payload_rcinc", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZcOwnedPayload zc_payload_rcinc(ZcOwnedPayload* payload); + + [DllImport(DllName, EntryPoint = "zc_sample_payload_rcinc", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZcOwnedPayload zc_sample_payload_rcinc(ZSample* sample); + + [DllImport(DllName, EntryPoint = "z_closure_sample_drop", CallingConvention = CallingConvention.Cdecl)] + internal static extern void z_closure_sample_drop(ZOwnedClosureSample* closure); + + // public static string IdBytesToStr(byte[] buf) + // { + // var str = new StringBuilder(); + // for (var i = buf.Length - 1; i >= 0; i--) str.Append($"{buf[i]:X2}"); + + // return str.ToString(); + // } } \ No newline at end of file diff --git a/examples/ZGet/ZGet.csproj b/examples/ZGet/ZGet.csproj index 782f972..df36679 100644 --- a/examples/ZGet/ZGet.csproj +++ b/examples/ZGet/ZGet.csproj @@ -6,6 +6,17 @@ false 10 net7.0;net6.0 + Release;Debug + x64;ARM64 + + + TRACE;PLATFORM_ARM + + + TRACE;PLATFORM_ARM64 + + + TRACE;PLATFORM_x64 diff --git a/examples/ZInfo/ZInfo.cs b/examples/ZInfo/ZInfo.cs index d90c481..a42ffa4 100644 --- a/examples/ZInfo/ZInfo.cs +++ b/examples/ZInfo/ZInfo.cs @@ -1,37 +1,38 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading; using Zenoh; -//Zenoh.Zenoh.InitLogger(); Config config = new Config(); -//config.SetId("01"); -//string[] c = { "tcp/172.30.100.3:7447", "tcp/172.30.100.1:7447" }; -//config.SetConnect(c); config.SetMode(Config.Mode.Client); -config.SetTimestamp(true); -string configStr = config.ToStr(); -Console.WriteLine($"config string:\n{configStr}\n----------------------\n"); +string[] connect = { "tcp/127.0.0.1:7447" }; +config.SetConnect(connect); +//string[] listen = {"tcp/127.0.0.1:7888"}; +//config.SetListen(listen); -Session session = new Session(); Console.WriteLine("Opening session..."); -if (session.Open(config)) +var session = Session.Open(config); +if (session is null) { - // wait - Thread.Sleep(1000); -} -else -{ - Console.WriteLine("Opening session unsuccessful"); + Console.WriteLine("Opening session fault!"); return; } -string pid = session.LocalZid(); -string[] routerPid = session.RoutersZid(); -string[] peerPid = session.PeersZid(); +Thread.Sleep(200); +Console.WriteLine("Opening session successful!"); + +string localId = session.LocalId().ToStr(); +Session.Id[] routersId = session.RoutersId(); +List routersIdStr = new List(); +foreach (var id in routersId) +{ + routersIdStr.Add(id.ToStr()); +} +// string[] peerPid = session.PeersZid(); -Console.WriteLine($"Local PID: {pid}"); -Console.WriteLine($"PeersPID: {String.Join(',', peerPid)}"); -Console.WriteLine($"Routers PID: {String.Join(',', routerPid)}"); +Console.WriteLine($"Local ID: {localId}"); +// Console.WriteLine($"PeersPID: {String.Join(',', peerPid)}"); +Console.WriteLine($"Routers ID: {String.Join(',', routersIdStr)}"); session.Close(); \ No newline at end of file diff --git a/examples/ZInfo/ZInfo.csproj b/examples/ZInfo/ZInfo.csproj index 8f9fd95..267f2e0 100644 --- a/examples/ZInfo/ZInfo.csproj +++ b/examples/ZInfo/ZInfo.csproj @@ -6,7 +6,19 @@ false 10 net7.0;net6.0 + Release;Debug + x64;ARM64 + + TRACE;PLATFORM_ARM + + + TRACE;PLATFORM_ARM64 + + + TRACE;PLATFORM_x64 + + diff --git a/examples/ZPut/ZPut.cs b/examples/ZPut/ZPut.cs index 8653d89..8f0e95e 100644 --- a/examples/ZPut/ZPut.cs +++ b/examples/ZPut/ZPut.cs @@ -1,46 +1,66 @@ -using System; +#nullable enable + +using System; using System.Threading; using Zenoh; -Zenoh.ZenohC.InitLogger(); -Config config = new Config(); -string[] connect = { "tcp/127.0.0.1:7447" }; -config.SetConnect(connect); -Session session = new Session(); +// Config config = new Config(); +// if (!config.SetMode(Config.Mode.Client)) +// { +// Console.WriteLine("Config set mode fail"); +// return; +// } +// string[] connect = { "tcp/127.0.0.1:7447" }; +// if (!config.SetConnect(connect)) +// { +// Console.WriteLine("Config set connect fail"); +// return; +// } +//string[] listen = {"tcp/127.0.0.1:7888"}; +//config.SetListen(listen); -Console.WriteLine("Opening session..."); -if (session.Open(config)) +Config? c = Config.LoadFromFile("../../../../zenoh.json5"); +if (c == null) { - Thread.Sleep(200); + Console.WriteLine("Load config error!"); + return; } -else + +Config config = c; + +Console.WriteLine("Opening session..."); +var session = Session.Open(config); +if (session is null) { - Console.WriteLine("Opening session unsuccessful"); + Console.WriteLine("Opening session unsuccessful!"); return; } -KeyExpr keyStr = KeyExpr.FromString("demo/example/zenoh-cs-put/string"); +Thread.Sleep(200); +Console.WriteLine("Opening session successful!"); + +string keyStr = "demo/example/zenoh-cs-put/string"; string dataStr = "Put from csharp !"; Console.WriteLine(session.PutStr(keyStr, dataStr) - ? $"Putting data string ('{keyStr.GetStr()}': '{dataStr}')" + ? $"Putting data string ('{keyStr}': '{dataStr}')" : "Putting data string fault!"); -KeyExpr keyJson = KeyExpr.FromString("demo/example/zenoh-cs-put/json"); +string keyJson = "demo/example/zenoh-cs-put/json"; string dataJson = "{\"value\": \"Put from csharp\"}"; Console.WriteLine(session.PutJson(keyJson, dataJson) - ? $"Putting data json ('{keyJson.GetStr()}': {dataJson})" + ? $"Putting data json ('{keyJson}': {dataJson})" : "Putting data json fault!"); -KeyExpr keyInt = KeyExpr.FromString("demo/example/zenoh-cs-put/int"); -Int64 dataInt = 965; +string keyInt = "demo/example/zenoh-cs-put/int"; +long dataInt = 965; Console.WriteLine(session.PutInt(keyInt, dataInt) - ? $"Putting data int ('{keyInt.GetStr()}': {dataInt})" + ? $"Putting data int ('{keyInt}': {dataInt})" : "Putting data int fault!"); -KeyExpr keyFloat = KeyExpr.FromString("demo/example/zenoh-cs-put/float"); +string keyFloat = "demo/example/zenoh-cs-put/float"; double dataFloat = 99.6; Console.WriteLine(session.PutFloat(keyFloat, dataFloat) - ? $"Putting data float ('{keyFloat.GetStr()}': {dataFloat})" + ? $"Putting data float ('{keyFloat}': {dataFloat})" : "Putting data float fault!"); session.Close(); \ No newline at end of file diff --git a/examples/ZPut/ZPut.csproj b/examples/ZPut/ZPut.csproj index 434b043..0aa112e 100644 --- a/examples/ZPut/ZPut.csproj +++ b/examples/ZPut/ZPut.csproj @@ -6,7 +6,19 @@ false 10 net7.0;net6.0 + Release;Debug + x64;ARM64 + + TRACE;PLATFORM_ARM + + + TRACE;PLATFORM_ARM64 + + + TRACE;PLATFORM_x64 + + diff --git a/examples/ZSub/ZSub.cs b/examples/ZSub/ZSub.cs index c48746f..7c23c3b 100644 --- a/examples/ZSub/ZSub.cs +++ b/examples/ZSub/ZSub.cs @@ -1,40 +1,39 @@ -using System; -using System.Text; +#nullable enable + +using System; using System.Threading; using Zenoh; -//Zenoh.Zenoh.InitLogger(); Config config = new Config(); -string[] connect = {"tcp/127.0.0.1:7447"}; +config.SetMode(Config.Mode.Client); +string[] connect = { "tcp/127.0.0.1:7447" }; config.SetConnect(connect); //string[] listen = {"tcp/127.0.0.1:7888"}; //config.SetListen(listen); -Session session = new Session(); Console.WriteLine("Opening session..."); -if (session.Open(config)) -{ - // wait - Thread.Sleep(200); -} -else +var session = Session.Open(config); +if (session is null) { - Console.WriteLine("Opening session unsuccessful"); + Console.WriteLine("Opening session fault!"); return; } +Thread.Sleep(200); +Console.WriteLine("Opening session successful!"); + + void Callback1(Sample sample) { - string key = sample.Key; - string value = sample.ValueToString(); + string key = sample.GetKeyexpr(); + string value = sample.GetString() ?? ""; Console.WriteLine($">> [Subscriber1] Received PUT ('{key}': '{value}')"); - //Console.Write("."); } void Callback2(Sample sample) { - string key = sample.Key; - string value = sample.ValueToString(); + string key = sample.GetKeyexpr(); + string value = sample.GetString() ?? ""; Console.WriteLine($">> [Subscriber2] Received PUT ('{key}': '{value}')"); } @@ -47,16 +46,24 @@ void Callback2(Sample sample) Subscriber subscriber1 = new Subscriber(key1, userCallback1); Subscriber subscriber2 = new Subscriber(key2, userCallback2); -if (session.RegisterSubscriber(subscriber1)) +var handle1 = session.RegisterSubscriber(subscriber1); +if (handle1 is null) { - Console.WriteLine($"Registered Subscriber1 On '{key1}'"); + Console.WriteLine($"Register Subscriber1 fault On '{key1}'"); + return; } -if (session.RegisterSubscriber(subscriber2)) +Console.WriteLine($"Registered Subscriber1 On '{key1}'"); + +var handle2 = session.RegisterSubscriber(subscriber2); +if (handle2 is null) { - Console.WriteLine($"Registered Subscriber2 On '{key2}'"); + Console.WriteLine($"Register Subscriber2 fault On '{key2}'"); + return; } +Console.WriteLine($"Registered Subscriber2 On '{key2}'"); + Console.WriteLine("Enter 'q' to quit..."); while (true) { @@ -67,4 +74,7 @@ void Callback2(Sample sample) } } +session.UnregisterSubscriber((SubscriberHandle)handle1); +session.UnregisterSubscriber((SubscriberHandle)handle2); + session.Close(); \ No newline at end of file diff --git a/examples/ZSub/ZSub.csproj b/examples/ZSub/ZSub.csproj index 815181c..dd9af01 100644 --- a/examples/ZSub/ZSub.csproj +++ b/examples/ZSub/ZSub.csproj @@ -6,6 +6,17 @@ false 10 net7.0;net6.0 + Debug;Release + ARM64;x64 + + + TRACE;PLATFORM_ARM + + + TRACE;PLATFORM_ARM64 + + + TRACE;PLATFORM_x64 diff --git a/examples/zenoh-examples.sln b/examples/zenoh-examples.sln index 68c84dd..947df82 100644 --- a/examples/zenoh-examples.sln +++ b/examples/zenoh-examples.sln @@ -15,76 +15,54 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZInfo", "ZInfo\ZInfo.csproj EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|Any CPU = Release|Any CPU + Debug|ARM64 = Debug|ARM64 Release|x64 = Release|x64 - Release|x86 = Release|x86 + Release|ARM64 = Release|ARM64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Debug|Any CPU.Build.0 = Debug|Any CPU - {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Debug|x64.ActiveCfg = Debug|Any CPU - {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Debug|x64.Build.0 = Debug|Any CPU - {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Debug|x86.ActiveCfg = Debug|Any CPU - {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Debug|x86.Build.0 = Debug|Any CPU - {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Release|Any CPU.ActiveCfg = Release|Any CPU - {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Release|Any CPU.Build.0 = Release|Any CPU - {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Release|x64.ActiveCfg = Release|Any CPU - {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Release|x64.Build.0 = Release|Any CPU - {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Release|x86.ActiveCfg = Release|Any CPU - {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Release|x86.Build.0 = Release|Any CPU - {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Debug|x64.ActiveCfg = Debug|Any CPU - {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Debug|x64.Build.0 = Debug|Any CPU - {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Debug|x86.ActiveCfg = Debug|Any CPU - {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Debug|x86.Build.0 = Debug|Any CPU - {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Release|Any CPU.Build.0 = Release|Any CPU - {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Release|x64.ActiveCfg = Release|Any CPU - {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Release|x64.Build.0 = Release|Any CPU - {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Release|x86.ActiveCfg = Release|Any CPU - {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Release|x86.Build.0 = Release|Any CPU - {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Debug|x64.ActiveCfg = Debug|Any CPU - {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Debug|x64.Build.0 = Debug|Any CPU - {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Debug|x86.ActiveCfg = Debug|Any CPU - {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Debug|x86.Build.0 = Debug|Any CPU - {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Release|Any CPU.Build.0 = Release|Any CPU - {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Release|x64.ActiveCfg = Release|Any CPU - {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Release|x64.Build.0 = Release|Any CPU - {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Release|x86.ActiveCfg = Release|Any CPU - {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Release|x86.Build.0 = Release|Any CPU - {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|Any CPU.Build.0 = Debug|Any CPU - {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|x64.ActiveCfg = Debug|Any CPU - {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|x64.Build.0 = Debug|Any CPU - {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|x86.ActiveCfg = Debug|Any CPU - {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|x86.Build.0 = Debug|Any CPU - {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|Any CPU.ActiveCfg = Release|Any CPU - {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|Any CPU.Build.0 = Release|Any CPU - {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|x64.ActiveCfg = Release|Any CPU - {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|x64.Build.0 = Release|Any CPU - {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|x86.ActiveCfg = Release|Any CPU - {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|x86.Build.0 = Release|Any CPU - {B973A563-44AD-4B68-855C-46B5D58DCE32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B973A563-44AD-4B68-855C-46B5D58DCE32}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B973A563-44AD-4B68-855C-46B5D58DCE32}.Debug|x64.ActiveCfg = Debug|Any CPU - {B973A563-44AD-4B68-855C-46B5D58DCE32}.Debug|x64.Build.0 = Debug|Any CPU - {B973A563-44AD-4B68-855C-46B5D58DCE32}.Debug|x86.ActiveCfg = Debug|Any CPU - {B973A563-44AD-4B68-855C-46B5D58DCE32}.Debug|x86.Build.0 = Debug|Any CPU - {B973A563-44AD-4B68-855C-46B5D58DCE32}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B973A563-44AD-4B68-855C-46B5D58DCE32}.Release|Any CPU.Build.0 = Release|Any CPU - {B973A563-44AD-4B68-855C-46B5D58DCE32}.Release|x64.ActiveCfg = Release|Any CPU - {B973A563-44AD-4B68-855C-46B5D58DCE32}.Release|x64.Build.0 = Release|Any CPU - {B973A563-44AD-4B68-855C-46B5D58DCE32}.Release|x86.ActiveCfg = Release|Any CPU - {B973A563-44AD-4B68-855C-46B5D58DCE32}.Release|x86.Build.0 = Release|Any CPU + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Debug|x64.ActiveCfg = Debug|x64 + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Debug|x64.Build.0 = Debug|x64 + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Release|x64.ActiveCfg = Release|x64 + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Release|x64.Build.0 = Release|x64 + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Debug|ARM64.Build.0 = Debug|ARM64 + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Release|ARM64.ActiveCfg = Release|ARM64 + {05075D28-DE9E-4CB4-9976-FCBDD170A7B5}.Release|ARM64.Build.0 = Release|ARM64 + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Debug|x64.ActiveCfg = Debug|x64 + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Debug|x64.Build.0 = Debug|x64 + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Release|x64.ActiveCfg = Release|x64 + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Release|x64.Build.0 = Release|x64 + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Debug|ARM64.Build.0 = Debug|ARM64 + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Release|ARM64.ActiveCfg = Release|ARM64 + {7CBBC11C-8F9D-40D4-B1A6-AD472FDF99C0}.Release|ARM64.Build.0 = Release|ARM64 + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Debug|x64.ActiveCfg = Debug|x64 + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Debug|x64.Build.0 = Debug|x64 + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Release|x64.ActiveCfg = Release|x64 + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Release|x64.Build.0 = Release|x64 + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Debug|ARM64.Build.0 = Debug|ARM64 + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Release|ARM64.ActiveCfg = Release|ARM64 + {B973A563-44AD-4B68-855C-46B5D58DCE32}.Release|ARM64.Build.0 = Release|ARM64 + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Debug|x64.ActiveCfg = Debug|x64 + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Debug|x64.Build.0 = Debug|x64 + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Release|x64.ActiveCfg = Release|x64 + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Release|x64.Build.0 = Release|x64 + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Debug|ARM64.Build.0 = Debug|ARM64 + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Release|ARM64.ActiveCfg = Release|ARM64 + {13E0FD6C-35D7-4422-A37F-4724D7DCDA2C}.Release|ARM64.Build.0 = Release|ARM64 + {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|x64.ActiveCfg = Debug|x64 + {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|x64.Build.0 = Debug|x64 + {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|x64.ActiveCfg = Release|x64 + {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|x64.Build.0 = Release|x64 + {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|ARM64.Build.0 = Debug|ARM64 + {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|ARM64.ActiveCfg = Release|ARM64 + {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|ARM64.Build.0 = Release|ARM64 EndGlobalSection EndGlobal From 2bb0b60b9cd044c2e4e936e1b2fc1457f1234564 Mon Sep 17 00:00:00 2001 From: sanri Date: Fri, 18 Aug 2023 10:45:31 +0800 Subject: [PATCH 17/34] ZInfo --- Zenoh/Id.cs | 82 ++++++++++++++++++++++++ Zenoh/Session.cs | 136 ++++++++-------------------------------- examples/ZInfo/ZInfo.cs | 16 +++-- examples/ZPut/ZPut.cs | 40 ++++++------ 4 files changed, 138 insertions(+), 136 deletions(-) create mode 100644 Zenoh/Id.cs diff --git a/Zenoh/Id.cs b/Zenoh/Id.cs new file mode 100644 index 0000000..c573637 --- /dev/null +++ b/Zenoh/Id.cs @@ -0,0 +1,82 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace Zenoh; + +public struct Id +{ + internal byte[] data; + + internal Id(ZId zid) + { + data = new byte[16]; + for (int i = 0; i < 16; i++) + { + unsafe + { + data[i] = zid.id[i]; + } + } + } + + public string ToStr() + { + StringBuilder sb = new StringBuilder(); + foreach (byte b in data) + { + sb.Append(b.ToString("x")); + } + + return sb.ToString(); + } +} + +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZIdBuffer +{ + internal nuint count = 0; + internal fixed byte data[256 * 16]; + + public ZIdBuffer() + { + } + + internal void Add(ZId* zId) + { + if (count >= 256) return; + for (nuint i = 0; i < 16; i++) + { + data[count * 16 + i] = zId->id[i]; + } + + count += 1; + } + + internal Id[] ToIds() + { + Id[] ids = new Id[count]; + for (nuint i = 0; i < count; i++) + { + byte[] o = new byte[16]; + for (nuint j = 0; j < 16; j++) + { + o[j] = data[i * 16 + j]; + } + + Id id = new Id + { + data = o, + }; + ids[i] = id; + } + + return ids; + } + + internal static void z_id_call(ZId* zId, void* context) + { + ZIdBuffer* pIdBuffer = (ZIdBuffer*)context; + pIdBuffer->Add(zId); + } +} \ No newline at end of file diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs index 1d5e792..565991d 100644 --- a/Zenoh/Session.cs +++ b/Zenoh/Session.cs @@ -5,7 +5,7 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; -using System.Threading; + namespace Zenoh; @@ -70,61 +70,6 @@ private void Dispose(bool disposing) Close(); } - - // private static List GetIdStrings(IntPtr buf) - // { - // List list = new List(); - // int len = Marshal.ReadByte(buf); - // for (int i = 0; i < len; i++) - // { - // // byte[] b = new byte[ZenohC.IdLength]; - // // Marshal.Copy(buf + 1 + ZenohC.IdLength * i, b, 0, ZenohC.IdLength); - // // list.Add(ZenohC.IdBytesToStr(b)); - // } - // - // return list; - // } - - // private static void InfoZidCallback(ref Zid zid, IntPtr buf) - // { - // int i = Marshal.ReadByte(buf); - // if (i >= ZenohC.RoutersNum) - // { - // return; - // } - // - // Marshal.Copy(zid.id, 0, buf + 1 + ZenohC.IdLength * i, ZenohC.IdLength); - // Marshal.WriteByte(buf, (byte)(i + 1)); - // } - - public struct Id - { - internal byte[] data; - - internal Id(ZId zid) - { - data = new byte[16]; - for (int i = 0; i < 16; i++) - { - unsafe - { - data[i] = zid.id[i]; - } - } - } - - public string ToStr() - { - StringBuilder sb = new StringBuilder(); - foreach (byte b in data) - { - sb.Append(b.ToString("x")); - } - - return sb.ToString(); - } - } - public Id LocalId() { unsafe @@ -135,87 +80,56 @@ public Id LocalId() } } - [StructLayout(LayoutKind.Sequential, Pack = 8)] - private unsafe struct ZIdBuffer + public Id[] RoutersId() { - internal nuint count; - internal fixed byte data[256 * 16]; - - public ZIdBuffer() - { - count = 0; - } - - internal void Add(ZId* zId) + unsafe { - if (count >= 256) return; - for (nuint i = 0; i < 16; i++) + ZSession session = ZenohC.z_session_loan(_session); + nint pIdBuffer = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.WriteInt64(pIdBuffer, 0); + ZOwnedClosureZId ownedClosureZId = new ZOwnedClosureZId { - data[count * 16 + i] = zId->id[i]; - } - - count += 1; - } + context = (void*)pIdBuffer, + call = ZIdBuffer.z_id_call, + drop = null, + }; + nint pOwnedClosureZId = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.StructureToPtr(ownedClosureZId, pOwnedClosureZId, false); + ZenohC.z_info_routers_zid(session, (ZOwnedClosureZId*)pOwnedClosureZId); + ZIdBuffer? zIdBuffer = (ZIdBuffer?)Marshal.PtrToStructure(pIdBuffer, typeof(ZIdBuffer)); - internal Id[] ToIds() - { - Id[] ids = new Id[count]; - for (nuint i = 0; i < count; i++) - { - byte[] o = new byte[16]; - for (nuint j = 0; j < 16; j++) - { - o[j] = data[i * 16 + j]; - } - - Id id = new Id - { - data = o, - }; - ids[i] = id; - } + Marshal.FreeHGlobal(pOwnedClosureZId); + Marshal.FreeHGlobal(pIdBuffer); - return ids; + return zIdBuffer is null ? Array.Empty() : zIdBuffer.Value.ToIds(); } } - internal static unsafe void z_id_call(ZId* zId, void* context) - { - ZIdBuffer* pIdBuffer = (ZIdBuffer*)context; - pIdBuffer->Add(zId); - } - - public Id[] RoutersId() + public Id[] PeersId() { unsafe { ZSession session = ZenohC.z_session_loan(_session); nint pIdBuffer = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.WriteInt64(pIdBuffer, 0); ZOwnedClosureZId ownedClosureZId = new ZOwnedClosureZId { context = (void*)pIdBuffer, - call = z_id_call, + call = ZIdBuffer.z_id_call, drop = null, }; nint pOwnedClosureZId = Marshal.AllocHGlobal(Marshal.SizeOf()); Marshal.StructureToPtr(ownedClosureZId, pOwnedClosureZId, false); - - ZenohC.z_info_routers_zid(session, (ZOwnedClosureZId*)pOwnedClosureZId); - - ZIdBuffer zIdBuffer; - zIdBuffer = (ZIdBuffer)Marshal.PtrToStructure(pIdBuffer, typeof(ZIdBuffer)); - Id[] ids = zIdBuffer.ToIds(); + ZenohC.z_info_peers_zid(session, (ZOwnedClosureZId*)pOwnedClosureZId); + ZIdBuffer? zIdBuffer = (ZIdBuffer?)Marshal.PtrToStructure(pIdBuffer, typeof(ZIdBuffer)); Marshal.FreeHGlobal(pOwnedClosureZId); Marshal.FreeHGlobal(pIdBuffer); - return ids; + + return zIdBuffer is null ? Array.Empty() : zIdBuffer.Value.ToIds(); } } - // public Id[] PeersId() - // { - // } - public bool PutStr(string key, string value) { return PutStr(key, value, CongestionControl.Block, Priority.RealTime); diff --git a/examples/ZInfo/ZInfo.cs b/examples/ZInfo/ZInfo.cs index a42ffa4..7c0266e 100644 --- a/examples/ZInfo/ZInfo.cs +++ b/examples/ZInfo/ZInfo.cs @@ -1,11 +1,10 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Threading; using Zenoh; Config config = new Config(); -config.SetMode(Config.Mode.Client); +config.SetMode(Config.Mode.Peer); string[] connect = { "tcp/127.0.0.1:7447" }; config.SetConnect(connect); //string[] listen = {"tcp/127.0.0.1:7888"}; @@ -23,16 +22,23 @@ Console.WriteLine("Opening session successful!"); string localId = session.LocalId().ToStr(); -Session.Id[] routersId = session.RoutersId(); +Id[] routersId = session.RoutersId(); List routersIdStr = new List(); foreach (var id in routersId) { routersIdStr.Add(id.ToStr()); } -// string[] peerPid = session.PeersZid(); + + +Id[] peerId = session.PeersId(); +List peerIdStr = new List(); +foreach (var id in peerId) +{ + peerIdStr.Add(id.ToStr()); +} Console.WriteLine($"Local ID: {localId}"); -// Console.WriteLine($"PeersPID: {String.Join(',', peerPid)}"); +Console.WriteLine($"PeersPID: {String.Join(',', peerIdStr)}"); Console.WriteLine($"Routers ID: {String.Join(',', routersIdStr)}"); session.Close(); \ No newline at end of file diff --git a/examples/ZPut/ZPut.cs b/examples/ZPut/ZPut.cs index 8f0e95e..4fb2b9f 100644 --- a/examples/ZPut/ZPut.cs +++ b/examples/ZPut/ZPut.cs @@ -4,29 +4,29 @@ using System.Threading; using Zenoh; -// Config config = new Config(); -// if (!config.SetMode(Config.Mode.Client)) -// { -// Console.WriteLine("Config set mode fail"); -// return; -// } -// string[] connect = { "tcp/127.0.0.1:7447" }; -// if (!config.SetConnect(connect)) + Config config = new Config(); + if (!config.SetMode(Config.Mode.Client)) + { + Console.WriteLine("Config set mode fail"); + return; + } + string[] connect = { "tcp/127.0.0.1:7447" }; + if (!config.SetConnect(connect)) + { + Console.WriteLine("Config set connect fail"); + return; + } + +// string[] listen = {"tcp/127.0.0.1:7888"}; +// config.SetListen(listen); + +// Config? c = Config.LoadFromFile("../../../../zenoh.json5"); +// if (c == null) // { -// Console.WriteLine("Config set connect fail"); +// Console.WriteLine("Load config error!"); // return; // } -//string[] listen = {"tcp/127.0.0.1:7888"}; -//config.SetListen(listen); - -Config? c = Config.LoadFromFile("../../../../zenoh.json5"); -if (c == null) -{ - Console.WriteLine("Load config error!"); - return; -} - -Config config = c; +// Config config = c; Console.WriteLine("Opening session..."); var session = Session.Open(config); From 5df2b1e1f7c0e1bdf904263f189670dc18e870dc Mon Sep 17 00:00:00 2001 From: sanri Date: Fri, 18 Aug 2023 13:47:18 +0800 Subject: [PATCH 18/34] ZPub --- Zenoh/Publisher.cs | 46 ++++++++++++++ Zenoh/Session.cs | 117 +++++++++++++++++++++++++++++++++--- Zenoh/Subscriber.cs | 4 +- examples/ZPub/ZPub.cs | 65 ++++++++++++++++++++ examples/ZPub/ZPub.csproj | 32 ++++++++++ examples/ZSub/ZSub.cs | 10 ++- examples/zenoh-examples.sln | 10 +++ 7 files changed, 267 insertions(+), 17 deletions(-) create mode 100644 Zenoh/Publisher.cs create mode 100644 examples/ZPub/ZPub.cs create mode 100644 examples/ZPub/ZPub.csproj diff --git a/Zenoh/Publisher.cs b/Zenoh/Publisher.cs new file mode 100644 index 0000000..e84afa6 --- /dev/null +++ b/Zenoh/Publisher.cs @@ -0,0 +1,46 @@ +using System; +using System.Runtime.InteropServices; + +namespace Zenoh; + +public struct PublisherHandle +{ + internal int handle; +} + +public class Publisher : IDisposable +{ + internal unsafe ZOwnedPublisher* ownedPublisher; + internal readonly ZPublisherOptions options; + internal readonly string keyexpr; + private bool _disposed; + + public Publisher(string key) : this(key, CongestionControl.Block, Priority.RealTime) + { + } + + public Publisher(string key, CongestionControl control, Priority priority) + { + unsafe + { + keyexpr = key; + _disposed = false; + ownedPublisher = null; + options.congestion_control = control; + options.priority = priority; + } + } + + public void Dispose() => Dispose(true); + + private void Dispose(bool disposing) + { + if (_disposed) return; + unsafe + { + Marshal.FreeHGlobal((nint)ownedPublisher); + } + + _disposed = true; + } +} \ No newline at end of file diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs index 565991d..c374100 100644 --- a/Zenoh/Session.cs +++ b/Zenoh/Session.cs @@ -13,12 +13,15 @@ public class Session : IDisposable { internal SortedDictionary subscribers; private int _indexSubscriber = 1; + internal SortedDictionary publishers; + private int _indexPublisher = 1; private bool _disposed; private readonly unsafe ZOwnedSession* _session; private unsafe Session(ZOwnedSession* session) { subscribers = new SortedDictionary(); + publishers = new SortedDictionary(); _disposed = false; _session = session; } @@ -230,33 +233,74 @@ private bool _put(string key, byte[] value, ZPutOptions options) } } + public bool PubStr(PublisherHandle handle, string value) + { + byte[] data = Encoding.UTF8.GetBytes(value); + return _publisher_put(handle, data, EncodingPrefix.TextPlain); + } + + public bool PubJson(PublisherHandle handle, string value) + { + byte[] data = Encoding.UTF8.GetBytes(value); + return _publisher_put(handle, data, EncodingPrefix.AppJson); + } + + public bool PubInt(PublisherHandle handle, long value) + { + string s = value.ToString("G"); + byte[] data = Encoding.UTF8.GetBytes(s); + return _publisher_put(handle, data, EncodingPrefix.AppInteger); + } + + public bool PubFloat(PublisherHandle handle, double value) + { + string s = value.ToString("G"); + byte[] data = Encoding.UTF8.GetBytes(s); + return _publisher_put(handle, data, EncodingPrefix.AppFloat); + } + + + private bool _publisher_put(PublisherHandle handle, byte[] value, EncodingPrefix encodingPrefix) + { + if (_disposed) return false; + unsafe + { + if (!publishers.TryGetValue(handle.handle, out Publisher? publisher)) + return false; + + ZPublisher pub = ZenohC.z_publisher_loan(publisher.ownedPublisher); + ZPublisherPutOptions options = new ZPublisherPutOptions + { + encoding = ZenohC.z_encoding(encodingPrefix, null), + }; + nint pv = Marshal.AllocHGlobal(value.Length); + nuint len = (nuint)value.Length; + Marshal.Copy(value, 0, pv, value.Length); + int r = ZenohC.z_publisher_put(pub, (byte*)pv, len, &options); + Marshal.FreeHGlobal(pv); + return r == 0; + } + } + public SubscriberHandle? RegisterSubscriber(Subscriber subscriber) { unsafe { if (subscriber.ownedSubscriber != null) - { return null; - } ZSession session = ZenohC.z_session_loan(_session); nint pKey = Marshal.StringToHGlobalAnsi(subscriber.keyexpr); ZKeyexpr keyexpr = ZenohC.z_keyexpr((byte*)pKey); - ZSubscriberOptions options = new ZSubscriberOptions - { - reliability = subscriber.reliability, - }; nint pOptions = Marshal.AllocHGlobal(Marshal.SizeOf()); - Marshal.StructureToPtr(options, pOptions, false); + Marshal.StructureToPtr(subscriber.options, pOptions, false); ZOwnedSubscriber sub = ZenohC.z_declare_subscriber(session, keyexpr, subscriber.closureSample, (ZSubscriberOptions*)pOptions); Marshal.FreeHGlobal(pOptions); Marshal.FreeHGlobal(pKey); if (ZenohC.z_subscriber_check(&sub) != 1) - { return null; - } nint pOwnedSubscriber = Marshal.AllocHGlobal(Marshal.SizeOf()); Marshal.StructureToPtr(sub, pOwnedSubscriber, false); @@ -291,4 +335,59 @@ private void UnregisterSubscriber(int handle) subscribers.Remove(handle); } } + + public PublisherHandle? RegisterPublisher(Publisher publisher) + { + unsafe + { + if (publisher.ownedPublisher != null) + return null; + + ZSession session = ZenohC.z_session_loan(_session); + nint pKey = Marshal.StringToHGlobalAnsi(publisher.keyexpr); + ZKeyexpr keyexpr = ZenohC.z_keyexpr((byte*)pKey); + nint pOptions = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.StructureToPtr(publisher.options, pOptions, false); + + ZOwnedPublisher pub = ZenohC.z_declare_publisher(session, keyexpr, (ZPublisherOptions*)pOptions); + + Marshal.FreeHGlobal(pOptions); + Marshal.FreeHGlobal(pKey); + + if (ZenohC.z_publisher_check(&pub) != 1) + return null; + + nint pOwnedPublisher = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.StructureToPtr(pub, pOwnedPublisher, false); + publisher.ownedPublisher = (ZOwnedPublisher*)pOwnedPublisher; + + _indexPublisher += 1; + publishers.Add(_indexPublisher, publisher); + + return new PublisherHandle + { + handle = _indexPublisher + }; + } + } + + public void UnregisterPublisher(PublisherHandle handle) + { + UnregisterPublisher(handle.handle); + } + + private void UnregisterPublisher(int handle) + { + if (publishers.TryGetValue(handle, out Publisher? publisher)) + { + unsafe + { + ZenohC.z_undeclare_publisher(publisher.ownedPublisher); + Marshal.FreeHGlobal((nint)publisher.ownedPublisher); + publisher.ownedPublisher = null; + } + + publishers.Remove(handle); + } + } } \ No newline at end of file diff --git a/Zenoh/Subscriber.cs b/Zenoh/Subscriber.cs index 412ab18..f91d284 100644 --- a/Zenoh/Subscriber.cs +++ b/Zenoh/Subscriber.cs @@ -17,7 +17,7 @@ public class Subscriber : IDisposable internal readonly unsafe ZOwnedClosureSample* closureSample; internal unsafe ZOwnedSubscriber* ownedSubscriber; internal readonly string keyexpr; - internal readonly Reliability reliability; + internal readonly ZSubscriberOptions options; private GCHandle _userCallbackGcHandle; private bool _disposed; @@ -32,7 +32,7 @@ public Subscriber(string key, SubscriberCallback userCallback, Reliability relia { keyexpr = key; _disposed = false; - this.reliability = reliability; + options.reliability = reliability; _userCallbackGcHandle = GCHandle.Alloc(userCallback); ZOwnedClosureSample ownedClosureSample = new ZOwnedClosureSample { diff --git a/examples/ZPub/ZPub.cs b/examples/ZPub/ZPub.cs new file mode 100644 index 0000000..894b6ff --- /dev/null +++ b/examples/ZPub/ZPub.cs @@ -0,0 +1,65 @@ +#nullable enable + +using System; +using System.Threading; +using Zenoh; + +Config config = new Config(); +config.SetMode(Config.Mode.Client); +string[] connect = { "tcp/127.0.0.1:7447" }; +config.SetConnect(connect); +//string[] listen = {"tcp/127.0.0.1:7888"}; +//config.SetListen(listen); + +Console.WriteLine("Opening session..."); +var session = Session.Open(config); +if (session is null) +{ + Console.WriteLine("Opening session fault!"); + return; +} + +Thread.Sleep(200); +Console.WriteLine("Opening session successful!"); + +string key1 = "demo/example/zenoh-cs-pub1"; +string key2 = "demo/example/zenoh-cs-pub2"; + +Publisher publisher1 = new Publisher(key1); +Publisher publisher2 = new Publisher(key2); + +var handle1 = session.RegisterPublisher(publisher1); +if (handle1 is null) +{ + Console.WriteLine($"Register Publisher1 fault On '{key1}'"); + return; +} + +Console.WriteLine($"Registered Publisher1 On '{key1}'"); + +var handle2 = session.RegisterPublisher(publisher2); +if (handle2 is null) +{ + Console.WriteLine($"Register Publisher2 fault On '{key2}'"); + return; +} + +Console.WriteLine($"Registered Publisher2 On '{key2}'"); + +for (int i = 0; i < 100; i++) +{ + string value1 = $"[{i * 2}] Pub from C#!"; + session.PubStr(handle1.Value, value1); + Console.WriteLine($"Publishing Data ('{key1}': '{value1}').."); + + string value2 = $"[{i * 2 + 1}] Pub from C#!"; + session.PubStr(handle2.Value, value2); + Console.WriteLine($"Publishing Data ('{key2}': '{value2}').."); + + Thread.Sleep(200); +} + +session.UnregisterPublisher(handle1.Value); +session.UnregisterPublisher(handle2.Value); + +session.Close(); \ No newline at end of file diff --git a/examples/ZPub/ZPub.csproj b/examples/ZPub/ZPub.csproj new file mode 100644 index 0000000..53e51b5 --- /dev/null +++ b/examples/ZPub/ZPub.csproj @@ -0,0 +1,32 @@ + + + ZPub + ZPub + Exe + false + 10 + net7.0;net6.0 + Debug;Release + ARM64;x64 + + + + TRACE;PLATFORM_ARM + + + TRACE;PLATFORM_ARM64 + + + TRACE;PLATFORM_x64 + + + + + + + + + + + + diff --git a/examples/ZSub/ZSub.cs b/examples/ZSub/ZSub.cs index 7c23c3b..b660359 100644 --- a/examples/ZSub/ZSub.cs +++ b/examples/ZSub/ZSub.cs @@ -40,8 +40,8 @@ void Callback2(Sample sample) SubscriberCallback userCallback1 = Callback1; SubscriberCallback userCallback2 = Callback2; -string key1 = "demo/example/zenoh-cs-put1"; -string key2 = "demo/example/zenoh-cs-put2"; +string key1 = "demo/example/zenoh-cs-put$*"; +string key2 = "demo/example/zenoh-cs-pub$*"; Subscriber subscriber1 = new Subscriber(key1, userCallback1); Subscriber subscriber2 = new Subscriber(key2, userCallback2); @@ -52,7 +52,6 @@ void Callback2(Sample sample) Console.WriteLine($"Register Subscriber1 fault On '{key1}'"); return; } - Console.WriteLine($"Registered Subscriber1 On '{key1}'"); var handle2 = session.RegisterSubscriber(subscriber2); @@ -61,7 +60,6 @@ void Callback2(Sample sample) Console.WriteLine($"Register Subscriber2 fault On '{key2}'"); return; } - Console.WriteLine($"Registered Subscriber2 On '{key2}'"); Console.WriteLine("Enter 'q' to quit..."); @@ -74,7 +72,7 @@ void Callback2(Sample sample) } } -session.UnregisterSubscriber((SubscriberHandle)handle1); -session.UnregisterSubscriber((SubscriberHandle)handle2); +session.UnregisterSubscriber(handle1.Value); +session.UnregisterSubscriber(handle2.Value); session.Close(); \ No newline at end of file diff --git a/examples/zenoh-examples.sln b/examples/zenoh-examples.sln index 947df82..9b222ab 100644 --- a/examples/zenoh-examples.sln +++ b/examples/zenoh-examples.sln @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZSub", "ZSub\ZSub.csproj", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZInfo", "ZInfo\ZInfo.csproj", "{B973A563-44AD-4B68-855C-46B5D58DCE32}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZPub", "ZPub\ZPub.csproj", "{D642DF1F-7096-4D47-A5EA-FCF624536993}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -64,5 +66,13 @@ Global {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|ARM64.Build.0 = Debug|ARM64 {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|ARM64.ActiveCfg = Release|ARM64 {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|ARM64.Build.0 = Release|ARM64 + {D642DF1F-7096-4D47-A5EA-FCF624536993}.Debug|x64.ActiveCfg = Debug|Any CPU + {D642DF1F-7096-4D47-A5EA-FCF624536993}.Debug|x64.Build.0 = Debug|Any CPU + {D642DF1F-7096-4D47-A5EA-FCF624536993}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {D642DF1F-7096-4D47-A5EA-FCF624536993}.Debug|ARM64.Build.0 = Debug|Any CPU + {D642DF1F-7096-4D47-A5EA-FCF624536993}.Release|x64.ActiveCfg = Release|Any CPU + {D642DF1F-7096-4D47-A5EA-FCF624536993}.Release|x64.Build.0 = Release|Any CPU + {D642DF1F-7096-4D47-A5EA-FCF624536993}.Release|ARM64.ActiveCfg = Release|Any CPU + {D642DF1F-7096-4D47-A5EA-FCF624536993}.Release|ARM64.Build.0 = Release|Any CPU EndGlobalSection EndGlobal From fc114faab985b80f1237825a9f731787ab90524d Mon Sep 17 00:00:00 2001 From: sanri Date: Fri, 18 Aug 2023 18:42:09 +0800 Subject: [PATCH 19/34] v0.1.0 --- README.md | 47 +++++++++++------- README.zh.md | 55 +++++++++++++++++++++ Zenoh/Zenoh.csproj | 8 ++- examples/README.md | 98 +++++++++++++++++++++++++------------ examples/README.zh.md | 85 ++++++++++++++++++++++++++++++++ examples/zenoh-examples.sln | 16 +++--- 6 files changed, 249 insertions(+), 60 deletions(-) create mode 100644 README.zh.md create mode 100644 examples/README.zh.md diff --git a/README.md b/README.md index ffb5946..a954dce 100644 --- a/README.md +++ b/README.md @@ -1,45 +1,54 @@ ![zenoh banner](./zenoh-dragon.png) -[![NuGet](https://img.shields.io/nuget/v/Zenoh.svg)](https://www.nuget.org/packages/Zenoh) -[![Gitter](https://badges.gitter.im/atolab/zenoh.svg)](https://gitter.im/atolab/zenoh?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) +[![NuGet](https://img.shields.io/nuget/v/Zenoh.svg)](https://www.nuget.org/packages/Zenoh-CS/) [![License](https://img.shields.io/badge/License-EPL%202.0-blue)](https://choosealicense.com/licenses/epl-2.0/) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) -# Eclipse zenoh C# API +# Zenoh C# API -[Eclipse zenoh](http://zenoh.io) is an extremely efficient and fault-tolerant [Named Data Networking](http://named-data.net) (NDN) protocol -that is able to scale down to extremely constrainded devices and networks. +[Zenoh](http://zenoh.io) is an extremely efficient and fault-tolerant [Named Data Networking](http://named-data.net) (NDN) protocol that is able to scale down to extremely constrainded devices and networks. -The C# API is for pure clients, in other terms does not support peer-to-peer communication, can be easily tested against a zenoh router running in a Docker container (see https://github.com/eclipse-zenoh/zenoh#how-to-test-it). +The C# API is for pure clients, in other terms does not support peer-to-peer communication, +can be easily tested against a zenoh router running in a Docker container (see https://github.com/eclipse-zenoh/zenoh#how-to-test-it). ------------------------------- ## How to install it Requirements: - - The [zenoh-c](https://github.com/eclipse-zenoh/zenoh-c) library must be installed on your host - -The Eclipse zenoh C# library is available on NuGet: +- The [zenoh-c](https://github.com/eclipse-zenoh/zenoh-c) library must be installed on your host +- The zenoh C# library [Zenoh-CS](https://www.nuget.org/packages/Zenoh-CS/) is available on NuGet: ### Supported .NET Standards +- .NET 6.0 +- .NET 7.0 + +### Supported CPU arch +- x64 +- arm64 -The library is configured to target the **.NET 6.0** at minimum. +### Mapping between Zenoh-CS and Zenoh-C versions +| Zenoh-C | Zenoh-CS | +|:---------:|:--------:| +| v0.7.2-rc | v0.1.* | ------------------------------- ## How to build it -Requirements: +Requirements: * The [zenoh-c](https://github.com/eclipse-zenoh/zenoh-c) library must be installed on your host - * A .NET environment - -Simply run `dotnet build Zenoh/`. + * A .NET environment. .NET6 or .NET7 [SDK](https://dotnet.microsoft.com/zh-cn/download/dotnet) +Build: +Because some of Zenoh-C data structures are not the same length in `x64` and `arm64`, you need to add the option `-p:Platform=x64` or `-p:Platform=ARM64` when building. +```shell +# x64 CPU +dotnet build Zenoh.csproj -c Release -p:Platform=x64 +# arm64 CPU +dotnet build Zenoh.csproj -c Release -p:Platform=ARM64 +``` ------------------------------- ## Running the Examples -The examples are configured to target **net6.0**. - -The simplest way to run some of the examples is to get a Docker image of the **zenoh** network router (see https://github.com/eclipse-zenoh/zenoh#how-to-test-it) and then to run the examples on your machine. - -Then, run the zenoh-csharp examples following the instructions in [examples/Zenoh/README.md](https://github.com/eclipse-zenoh/zenoh-csharp/blob/master/examples/Zenoh/README.md) +Build and run the zenoh-csharp examples following the instructions in [examples/README.md](https://github.com/sanri/zenoh-csharp/blob/master/examples/README.md) diff --git a/README.zh.md b/README.zh.md new file mode 100644 index 0000000..fe8df7f --- /dev/null +++ b/README.zh.md @@ -0,0 +1,55 @@ +![zenoh banner](./zenoh-dragon.png) + +[![NuGet](https://img.shields.io/nuget/v/Zenoh.svg)](https://www.nuget.org/packages/Zenoh-CS/) +[![License](https://img.shields.io/badge/License-EPL%202.0-blue)](https://choosealicense.com/licenses/epl-2.0/) +[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) + +# Zenoh C# API + +[Zenoh](http://zenoh.io)是一种非常高效和容错的命名数据网络([NDN](http://named-data.net))协议, 能够在非常受限制的设备和网络中运行. + +C# API是用于纯客户端的, 可以很容易地针对运行在Docker容器中的zenoh路由器进行测试 (参考[快速测试](https://zenoh.io/docs/getting-started/quick-test/)). + +------------------------------- +## 如何安装 + +需求: +- 库 [zenoh-c](https://github.com/eclipse-zenoh/zenoh-c) 必需被安装在你的主机上. +- Zenoh C# 库 [Zenoh-CS](https://www.nuget.org/packages/Zenoh-CS/) 库在NuGet上可用 + +### 支持的 .NET 标准 +- .NET 6.0 +- .NET 7.0 + +### 支持的CPU架构 +- x64 +- arm64 + +### Zenoh-CS 版本与 Zenoh-C 版本对应关系 +| Zenoh-C | Zenoh-CS | +|:---------:|:--------:| +| v0.7.2-rc | v0.1.* | + + +------------------------------- +## 如何构建 + +需求: +- 库 [zenoh-c](https://github.com/eclipse-zenoh/zenoh-c) 必需被安装在你的主机上. +- 主机安装有 .NET6 或 .NET7 的 [SDK](https://dotnet.microsoft.com/zh-cn/download/dotnet) + +构建命令: +由于Zenoh-C的部分数据结构在 `x64` 与 `arm64` 下长度不一样, 所以构建时需要增加选项 `-p:Platform=x64` 或 `-p:Platform=ARM64` +```shell +# x64 CPU +dotnet build Zenoh.csproj -c Release -p:Platform=x64 +# arm64 CPU +dotnet build Zenoh.csproj -c Release -p:Platform=ARM64 +``` + + +------------------------------- +## 运行示例 + +构建和运行示程序, 参考 [examples/README.zh.md](https://github.com/sanri/zenoh-csharp/blob/master/examples/README.zh.md) + diff --git a/Zenoh/Zenoh.csproj b/Zenoh/Zenoh.csproj index 4f453ed..43a1803 100644 --- a/Zenoh/Zenoh.csproj +++ b/Zenoh/Zenoh.csproj @@ -2,18 +2,19 @@ Zenoh Zenoh-CS - 0.0.6 + 0.1.0 sanri EPL-2.0 OR Apache-2.0 Zenoh: Zero Overhead Pub/sub, Store/Query and Compute. false https://github.com/sanri/zenoh-csharp https://github.com/sanri/zenoh-csharp + README.md git Library true 10 - 0.0.6 + 0.1.0 zenoh net7.0;net6.0 Release;Debug @@ -28,4 +29,7 @@ TRACE;PLATFORM_x64 + + + diff --git a/examples/README.md b/examples/README.md index c9b593b..bbdfdb0 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,53 +1,89 @@ # Zenoh C# examples -## Start instructions +## Introduce - ```bash - dotnet run -p - ``` +Each example accepts the `-h` or `--help` option that provides a description of its arguments and their default values. - Each example accepts the `-h` or `--help` option that provides a description of its arguments and their default values. +If you run the tests against the zenoh router running in a Docker container, +you need to add the `-e tcp/localhost:7447` option to your examples. +That's because Docker doesn't support UDP multicast transport, +and therefore the zenoh scouting and discrovery mechanism cannot work with. - :warning: _To pass any options to an example, specify them after the `--`argument. For instance:_ +When building, option `--property:Platform=<>` are required. - ```bash - dotnet run -p ZGet.csproj -- -h - ``` +## Examples description - If you run the tests against the zenoh router running in a Docker container, you need to add the - `-e tcp/localhost:7447` option to your examples. That's because Docker doesn't support UDP multicast - transport, and therefore the zenoh scouting and discrovery mechanism cannot work with. +### ZInfo -## Examples description +Get the ID of each node in the Zenoh network. + +Build +```bash +dotnet build ZInfo/ZInfo.csproj --configuration Release --property:Platform=x64 +``` +Run +```bash +./ZInfo +``` ### ZGet - Gets value from zenoh session. +Gets value from zenoh session. - Typical usage: - ```bash - dotnet run -p ZGet.csproj - ``` +Build +```bash +dotnet build ZGet/ZGet.csproj --configuration Release --property:Platform=x64 +``` +Run +```bash +./ZGet +``` ### ZPut - Writes a path/value into Zenoh. - The path/value will be received by all matching subscribers, for instance the [ZSub](#ZSub) examples. +Writes a path/value into Zenoh. +The path/value will be received by all matching subscribers, for instance the [ZSub](#ZSub) examples. + +Build +```bash +dotnet build ZPut/ZPut.csproj --configuration Release --property:Platform=x64 +``` + +Run +```bash +./ZPut +``` + +### ZPub +Writes a path/value into Zenoh. +The path/value will be received by all matching subscribers, for instance the [ZSub](#ZSub) examples. +Compared to ZPut, this example is optimized for keys that publish data frequently, reducing some of the overhead. + +Build +```bash +dotnet build ZPub/ZPub.csproj --configuration Release --property:Platform=x64 +``` + +Run +```bash +./ZPub +``` - Typical usage: - ```bash - dotnet run -p ZPut.csproj - ``` ### ZSub - Registers a subscriber with a selector. - The subscriber will be notified of each write made on any path matching the selector, - and will print this notification. +Registers a subscriber with a selector. +The subscriber will be notified of each write made on any path matching the selector, +and will print this notification. + +Build +```bash +dotnet build ZSub/ZSub.csproj --configuration Release --property:Platform=x64 +``` - Typical usage: - ```bash - dotnet run -p ZSub.csproj - ``` +Run +```bash +./ZSub +``` \ No newline at end of file diff --git a/examples/README.zh.md b/examples/README.zh.md new file mode 100644 index 0000000..a75f7b9 --- /dev/null +++ b/examples/README.zh.md @@ -0,0 +1,85 @@ +# Zenoh C# examples + +## 介绍 +每个示例都接受 `-h` 或 `——help` 选项,这些选项提供了对其参数及其默认值的描述. + +如果对运行在Docker容器中的zenoh路由器运行测试, 则需要在示例中添加 `-e tcp/localhost:7447` 选项. +这是因为Docker不支持UDP多播传输, 因此zenoh侦察和发现机制无法工作. + + +构建时, `--property:Platform=<>` 选项是必需的. 可选 `x64` 和 `ARM64`. + +## 示例说明 + +### ZInfo + +获取当前接入的Zenoh网络中, 各个节点的ID. + +构建命令 +```bash +dotnet build ZInfo/ZInfo.csproj --configuration Release --property:Platform=x64 +``` + +启动命令, 在生成产物目录下运行 +```bash +./ZInfo +``` + +### ZGet + +从Zenoh网络中获取值. + +构建命令 +```bash +dotnet build ZGet/ZGet.csproj --configuration Release --property:Platform=x64 +``` + +启动命令, 在生成产物目录下运行 +```bash +./ZGet +``` + + +### ZPut +将 key/value 写入Zenoh网络. +key/value 将被所有匹配的订阅者接收, 例如ZSub示例. + +构建命令 +```bash +dotnet build ZPut/ZPut.csproj --configuration Release --property:Platform=x64 +``` + +启动命令, 在生成产物目录下运行 +```bash +./ZPut +``` + +### ZPub +将 key/value 写入Zenoh网络. +key/value 将被所有匹配的订阅者接收, 例如ZSub示例. +相比于ZPut, 此示例针对经常发布数据的key进行了优化, 减少了部分开销. + +构建命令 +```bash +dotnet build ZPub/ZPub.csproj --configuration Release --property:Platform=x64 +``` + +启动命令, 在生成产物目录下运行 +```bash +./ZPub +``` + +### ZSub + +使用选择器注册订阅服务器. +订阅者将收到与选择器匹配的任何路径上的每次写入的通知, 并将打印此通知. + +构建命令 +```bash +dotnet build ZSub/ZSub.csproj --configuration Release --property:Platform=x64 +``` + +启动命令, 在生成产物目录下运行 +```bash +./ZSub +``` diff --git a/examples/zenoh-examples.sln b/examples/zenoh-examples.sln index 9b222ab..b67309a 100644 --- a/examples/zenoh-examples.sln +++ b/examples/zenoh-examples.sln @@ -66,13 +66,13 @@ Global {435FE932-CE46-49D0-B921-AC1B11585F13}.Debug|ARM64.Build.0 = Debug|ARM64 {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|ARM64.ActiveCfg = Release|ARM64 {435FE932-CE46-49D0-B921-AC1B11585F13}.Release|ARM64.Build.0 = Release|ARM64 - {D642DF1F-7096-4D47-A5EA-FCF624536993}.Debug|x64.ActiveCfg = Debug|Any CPU - {D642DF1F-7096-4D47-A5EA-FCF624536993}.Debug|x64.Build.0 = Debug|Any CPU - {D642DF1F-7096-4D47-A5EA-FCF624536993}.Debug|ARM64.ActiveCfg = Debug|Any CPU - {D642DF1F-7096-4D47-A5EA-FCF624536993}.Debug|ARM64.Build.0 = Debug|Any CPU - {D642DF1F-7096-4D47-A5EA-FCF624536993}.Release|x64.ActiveCfg = Release|Any CPU - {D642DF1F-7096-4D47-A5EA-FCF624536993}.Release|x64.Build.0 = Release|Any CPU - {D642DF1F-7096-4D47-A5EA-FCF624536993}.Release|ARM64.ActiveCfg = Release|Any CPU - {D642DF1F-7096-4D47-A5EA-FCF624536993}.Release|ARM64.Build.0 = Release|Any CPU + {D642DF1F-7096-4D47-A5EA-FCF624536993}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {D642DF1F-7096-4D47-A5EA-FCF624536993}.Debug|ARM64.Build.0 = Debug|ARM64 + {D642DF1F-7096-4D47-A5EA-FCF624536993}.Debug|x64.ActiveCfg = Debug|x64 + {D642DF1F-7096-4D47-A5EA-FCF624536993}.Debug|x64.Build.0 = Debug|x64 + {D642DF1F-7096-4D47-A5EA-FCF624536993}.Release|ARM64.ActiveCfg = Debug|ARM64 + {D642DF1F-7096-4D47-A5EA-FCF624536993}.Release|ARM64.Build.0 = Debug|ARM64 + {D642DF1F-7096-4D47-A5EA-FCF624536993}.Release|x64.ActiveCfg = Release|x64 + {D642DF1F-7096-4D47-A5EA-FCF624536993}.Release|x64.Build.0 = Release|x64 EndGlobalSection EndGlobal From 4796709f132777e361e83aa713241cbb2e59679e Mon Sep 17 00:00:00 2001 From: sanri Date: Mon, 21 Aug 2023 18:21:43 +0800 Subject: [PATCH 20/34] Optimize memory usage and reduce one copy. --- README.md | 5 ++++- README.zh.md | 2 +- Zenoh/Session.cs | 33 +++++++++++++++++++-------------- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index a954dce..e83a547 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,14 @@ # Zenoh C# API +[中文/chinese readme](https://github.com/sanri/zenoh-csharp/blob/master/README.zh.md) + [Zenoh](http://zenoh.io) is an extremely efficient and fault-tolerant [Named Data Networking](http://named-data.net) (NDN) protocol that is able to scale down to extremely constrainded devices and networks. The C# API is for pure clients, in other terms does not support peer-to-peer communication, can be easily tested against a zenoh router running in a Docker container (see https://github.com/eclipse-zenoh/zenoh#how-to-test-it). + ------------------------------- ## How to install it @@ -24,7 +27,7 @@ Requirements: ### Supported CPU arch - x64 -- arm64 +- arm64 (untested) ### Mapping between Zenoh-CS and Zenoh-C versions | Zenoh-C | Zenoh-CS | diff --git a/README.zh.md b/README.zh.md index fe8df7f..30c5828 100644 --- a/README.zh.md +++ b/README.zh.md @@ -23,7 +23,7 @@ C# API是用于纯客户端的, 可以很容易地针对运行在Docker容器中 ### 支持的CPU架构 - x64 -- arm64 +- arm64 (未测试) ### Zenoh-CS 版本与 Zenoh-C 版本对应关系 | Zenoh-C | Zenoh-CS | diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs index c374100..8da620e 100644 --- a/Zenoh/Session.cs +++ b/Zenoh/Session.cs @@ -220,15 +220,18 @@ private bool _put(string key, byte[] value, ZPutOptions options) if (_disposed) return false; unsafe { - nint pv = Marshal.AllocHGlobal(value.Length); - nuint len = (nuint)value.Length; - Marshal.Copy(value, 0, pv, value.Length); - nint pKey = Marshal.StringToHGlobalAnsi(key); - ZSession session = ZenohC.z_session_loan(_session); - ZKeyexpr keyexpr = ZenohC.z_keyexpr((byte*)pKey); - int r = ZenohC.z_put(session, keyexpr, (byte*)pv, len, &options); - Marshal.FreeHGlobal(pv); - Marshal.FreeHGlobal(pKey); + int r; + + fixed (byte* pv = value) + { + nuint len = (nuint)value.Length; + nint pKey = Marshal.StringToHGlobalAnsi(key); + ZSession session = ZenohC.z_session_loan(_session); + ZKeyexpr keyexpr = ZenohC.z_keyexpr((byte*)pKey); + r = ZenohC.z_put(session, keyexpr, pv, len, &options); + Marshal.FreeHGlobal(pKey); + } + return r == 0; } } @@ -273,11 +276,13 @@ private bool _publisher_put(PublisherHandle handle, byte[] value, EncodingPrefix { encoding = ZenohC.z_encoding(encodingPrefix, null), }; - nint pv = Marshal.AllocHGlobal(value.Length); - nuint len = (nuint)value.Length; - Marshal.Copy(value, 0, pv, value.Length); - int r = ZenohC.z_publisher_put(pub, (byte*)pv, len, &options); - Marshal.FreeHGlobal(pv); + int r; + fixed (byte* pv = value) + { + nuint len = (nuint)value.Length; + r = ZenohC.z_publisher_put(pub, pv, len, &options); + } + return r == 0; } } From 4ce2b273bd550a96395ec8abc40d8ee9064517b1 Mon Sep 17 00:00:00 2001 From: sanri Date: Tue, 22 Aug 2023 15:24:26 +0800 Subject: [PATCH 21/34] ZGet --- Zenoh/Querier.cs | 114 ++++++++++++++++++++++++++++++++++++++++++ Zenoh/Sample.cs | 1 + Zenoh/Session.cs | 31 ++++++++++++ Zenoh/Zenoh.csproj | 8 +-- Zenoh/ZenohC.cs | 52 ++++++++++++++++--- examples/ZGet/ZGet.cs | 74 +++++++++++++++------------ 6 files changed, 238 insertions(+), 42 deletions(-) create mode 100644 Zenoh/Querier.cs diff --git a/Zenoh/Querier.cs b/Zenoh/Querier.cs new file mode 100644 index 0000000..8195c1f --- /dev/null +++ b/Zenoh/Querier.cs @@ -0,0 +1,114 @@ +#pragma warning disable CS8500 +#nullable enable + +using System; +using System.Runtime.InteropServices; + +namespace Zenoh; + +public class QueryOptions +{ + internal string keyexpr; + internal QueryTarget target; + internal ConsolidationMode mode; + internal EncodingPrefix encodingPrefix; + internal byte[] payload; + + public QueryOptions(string keyexpr) : + this(keyexpr, QueryTarget.BestMatching, ConsolidationMode.Auto, EncodingPrefix.Empty, Array.Empty()) + { + } + + public QueryOptions(string keyexpr, QueryTarget target, ConsolidationMode mode) : + this(keyexpr, target, mode, EncodingPrefix.Empty, Array.Empty()) + { + } + + public QueryOptions(string keyexpr, EncodingPrefix encodingPrefix, byte[] payload) : + this(keyexpr, QueryTarget.BestMatching, ConsolidationMode.Auto, encodingPrefix, payload) + { + } + + public QueryOptions(string keyexpr, QueryTarget target, ConsolidationMode mode, + EncodingPrefix encodingPrefix, byte[] payload) + { + this.keyexpr = keyexpr; + this.target = target; + this.mode = mode; + this.encodingPrefix = encodingPrefix; + this.payload = payload; + } +} + +public delegate void QuerierCallback(Sample sample); + +public class Querier : IDisposable +{ + internal unsafe ZOwnedReplyChannel* _channel; + internal unsafe ZOwnedReply* _reply; + private bool _disposed; + + internal Querier() + { + unsafe + { + _disposed = false; + + ZOwnedReplyChannel channel = ZenohC.zc_reply_fifo_new(0); + nint pChannel = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.StructureToPtr(channel, pChannel, false); + _channel = (ZOwnedReplyChannel*)pChannel; + + ZOwnedReply reply = ZenohC.z_reply_null(); + nint pReply = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.StructureToPtr(reply, pReply, false); + _reply = (ZOwnedReply*)pReply; + } + } + + public void Dispose() => Dispose(true); + + private void Dispose(bool disposing) + { + if (_disposed) return; + + unsafe + { + ZenohC.z_reply_drop(_reply); + ZenohC.z_reply_channel_drop(_channel); + + Marshal.FreeHGlobal((nint)_reply); + Marshal.FreeHGlobal((nint)_channel); + } + + _disposed = true; + } + + public bool GetSamples(QuerierCallback callback) + { + if (_disposed) + return false; + + while (true) + { + unsafe + { + sbyte b = ZenohC.z_reply_channel_closure_call(&_channel->recv, _reply); + if (b != 1) + return false; + + b = ZenohC.z_reply_check(_reply); + if (b != 1) + return true; + + b = ZenohC.z_reply_is_ok(_reply); + if (b != 1) + return false; + + ZSample zSample = ZenohC.z_reply_ok(_reply); + Sample sample = new Sample(&zSample); + callback(sample); + } + } + } +} \ No newline at end of file diff --git a/Zenoh/Sample.cs b/Zenoh/Sample.cs index cbfb98c..24da07c 100644 --- a/Zenoh/Sample.cs +++ b/Zenoh/Sample.cs @@ -5,6 +5,7 @@ namespace Zenoh; +// 这是一个引用类型, 不释放引用的内存. public class Sample { private readonly unsafe ZSample* _native; diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs index 8da620e..fe85322 100644 --- a/Zenoh/Session.cs +++ b/Zenoh/Session.cs @@ -395,4 +395,35 @@ private void UnregisterPublisher(int handle) publishers.Remove(handle); } } + + public Querier? Query(QueryOptions options) + { + if (_disposed) + return null; + + unsafe + { + ZSession session = ZenohC.z_session_loan(_session); + nint pKey = Marshal.StringToHGlobalAnsi(options.keyexpr); + ZKeyexpr keyexpr = ZenohC.z_keyexpr((byte*)pKey); + Querier querier = new Querier(); + ZGetOptions getOptions = new ZGetOptions(); + getOptions.target = options.target; + getOptions.consolidation.mode = options.mode; + getOptions.value.encoding = ZenohC.z_encoding(options.encodingPrefix, null); + getOptions.value.payload.len = (nuint)options.payload.Length; + sbyte r; + fixed (byte* data = options.payload) + { + getOptions.value.payload.start = data; + nint pOptions = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.StructureToPtr(getOptions, pOptions, false); + r = ZenohC.z_get(session, keyexpr, null, &querier._channel->send, (ZGetOptions*)pOptions); + Marshal.FreeHGlobal(pOptions); + Marshal.FreeHGlobal(pKey); + } + + return r >= 0 ? querier : null; + } + } } \ No newline at end of file diff --git a/Zenoh/Zenoh.csproj b/Zenoh/Zenoh.csproj index 43a1803..e974f6d 100644 --- a/Zenoh/Zenoh.csproj +++ b/Zenoh/Zenoh.csproj @@ -2,7 +2,7 @@ Zenoh Zenoh-CS - 0.1.0 + 0.1.1 sanri EPL-2.0 OR Apache-2.0 Zenoh: Zero Overhead Pub/sub, Store/Query and Compute. @@ -14,7 +14,7 @@ Library true 10 - 0.1.0 + 0.1.1 zenoh net7.0;net6.0 Release;Debug @@ -24,10 +24,10 @@ TRACE;PLATFORM_ARM - TRACE;PLATFORM_ARM64 + TRACE;PLATFORM_ARM64 - TRACE;PLATFORM_x64 + TRACE;PLATFORM_x64 diff --git a/Zenoh/ZenohC.cs b/Zenoh/ZenohC.cs index 4eedef6..75104f2 100644 --- a/Zenoh/ZenohC.cs +++ b/Zenoh/ZenohC.cs @@ -684,6 +684,40 @@ internal unsafe struct ZOwnedClosureReply internal delegate* unmanaged[Cdecl] drop; } +// z_owned_reply_channel_closure_t +// -------------------------------- +// typedef struct z_owned_reply_channel_closure_t { +// void *context; +// bool (*call)(struct z_owned_reply_t*, void*); +// void (*drop)(void*); +// } z_owned_reply_channel_closure_t; +// -------------------------------- +internal unsafe delegate void ZOwnedReplayChannelClosureCall(ZOwnedReply* zOwnedReply, void* context); + +internal unsafe delegate void ZOwnedReplyChannelClosureDrop(void* context); + +[StructLayout(LayoutKind.Sequential)] +internal unsafe struct ZOwnedReplyChannelClosure +{ + internal void* context; + internal ZOwnedReplayChannelClosureCall call; + internal ZOwnedReplyChannelClosureDrop drop; +} + +// z_owned_reply_channel_t +// -------------------------------- +// typedef struct z_owned_reply_channel_t { +// struct z_owned_closure_reply_t send; +// struct z_owned_reply_channel_closure_t recv; +// } z_owned_reply_channel_t; +// -------------------------------- +[StructLayout(LayoutKind.Sequential)] +internal struct ZOwnedReplyChannel +{ + internal ZOwnedClosureReply send; + internal ZOwnedReplyChannelClosure recv; +} + [StructLayout(LayoutKind.Sequential)] public struct ConsolidationStrategy // z_consolidation_strategy_t { @@ -1087,11 +1121,15 @@ internal static extern sbyte z_query_reply( [DllImport(DllName, EntryPoint = "z_closure_sample_drop", CallingConvention = CallingConvention.Cdecl)] internal static extern void z_closure_sample_drop(ZOwnedClosureSample* closure); - // public static string IdBytesToStr(byte[] buf) - // { - // var str = new StringBuilder(); - // for (var i = buf.Length - 1; i >= 0; i--) str.Append($"{buf[i]:X2}"); - - // return str.ToString(); - // } + [DllImport(DllName, EntryPoint = "z_closure_reply_call", CallingConvention = CallingConvention.Cdecl)] + internal static extern void z_closure_reply_call(ZOwnedClosureReply* closure, ZOwnedReply* sample); + + [DllImport(DllName, EntryPoint = "zc_reply_fifo_new", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZOwnedReplyChannel zc_reply_fifo_new(nuint bound); + + [DllImport(DllName, EntryPoint = "z_reply_channel_drop", CallingConvention = CallingConvention.Cdecl)] + internal static extern void z_reply_channel_drop(ZOwnedReplyChannel* channel); + + [DllImport(DllName, EntryPoint = "z_reply_channel_closure_call", CallingConvention = CallingConvention.Cdecl)] + internal static extern sbyte z_reply_channel_closure_call(ZOwnedReplyChannelClosure* closure, ZOwnedReply* reply); } \ No newline at end of file diff --git a/examples/ZGet/ZGet.cs b/examples/ZGet/ZGet.cs index 4f1a84f..5b9c652 100644 --- a/examples/ZGet/ZGet.cs +++ b/examples/ZGet/ZGet.cs @@ -1,48 +1,60 @@ -using System; +#nullable enable + +using System; +using System.Text; using System.Threading; using Zenoh; -/* -//Zenoh.Zenoh.InitLogger(); Config config = new Config(); -//string[] connect = {"tcp/172.30.100.3:7447"}; -//config.SetConnect(connect); -Session session = new Session(); +config.SetMode(Config.Mode.Client); +string[] connect = { "tcp/127.0.0.1:7447" }; +config.SetConnect(connect); +//string[] listen = {"tcp/127.0.0.1:7888"}; +//config.SetListen(listen); Console.WriteLine("Opening session..."); -if (session.Open(config)) -{ - // wait - Thread.Sleep(1000); -} -else +var session = Session.Open(config); +if (session is null) { - Console.WriteLine("Opening session unsuccessful"); + Console.WriteLine("Opening session fault!"); return; } -KeyExpr key1 = KeyExpr.FromString("demo/example/zenoh-cs-put1"); -KeyExpr key2 = KeyExpr.FromString("demo/example/zenoh-cs-put2"); +Thread.Sleep(200); +Console.WriteLine("Opening session successful!"); + +string keyexpr = "demo/**"; +byte[] data = Encoding.UTF8.GetBytes("hello"); +QueryOptions queryOptions = new QueryOptions(keyexpr, EncodingPrefix.TextPlain, data); -Console.WriteLine($"Sending Query '{key1.GetStr()}'"); -ReplayDataArray data1 = session.Get(key1); -foreach (ReplayData ele in data1.List) +Console.WriteLine($"Sending Query '{keyexpr}'"); +Querier? querier = session.Query(queryOptions); +if (querier is null) { - string s = $">> Received ('{ele.Sample.Key}': '{ele.Sample.ValueToString()}'"; - Console.WriteLine(s); + Console.WriteLine("Session query fault!"); + goto EXIT; } - -ReplayDataArray data2 = session.Get(key2); -Console.WriteLine($"Sending Query '{key2.GetStr()}'"); -foreach (ReplayData ele in data2.List) +QuerierCallback callback = sample => +{ + string key = sample.GetKeyexpr(); + EncodingPrefix encodingPrefix = sample.GetEncodingPrefix(); + string? s = sample.GetString(); + if (s is null) + { + byte[] d = sample.GetPayload(); + Console.WriteLine($">> Received ('{key}' '{encodingPrefix}': '{d}')"); + } + else + { + Console.WriteLine($">> Received ('{key}' '{encodingPrefix}': '{s}')"); + } +}; + +if (!querier.GetSamples(callback)) { - string s = $">> Received ('{ele.Sample.Key}': '{ele.Sample.ValueToString()}'"; - Console.WriteLine(s); + Console.WriteLine("querier get sample error!"); } -session.Close(); - -*/ - -Console.WriteLine("hello"); \ No newline at end of file +EXIT: +session.Close(); \ No newline at end of file From e224e4ce6d90e03629297eb7f7df39494fcc66fe Mon Sep 17 00:00:00 2001 From: sanri Date: Tue, 22 Aug 2023 16:05:32 +0800 Subject: [PATCH 22/34] update readme.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e83a547..cd3cb2a 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ [Zenoh](http://zenoh.io) is an extremely efficient and fault-tolerant [Named Data Networking](http://named-data.net) (NDN) protocol that is able to scale down to extremely constrainded devices and networks. The C# API is for pure clients, in other terms does not support peer-to-peer communication, -can be easily tested against a zenoh router running in a Docker container (see https://github.com/eclipse-zenoh/zenoh#how-to-test-it). +can be easily tested against a zenoh router running in a Docker container (see https://zenoh.io/docs/getting-started/quick-test/). ------------------------------- From e16ea6572049bd471eea3f1fc4b440342d9317ca Mon Sep 17 00:00:00 2001 From: sanri Date: Wed, 23 Aug 2023 18:18:35 +0800 Subject: [PATCH 23/34] Add command line args parse --- examples/README.md | 2 +- examples/README.zh.md | 2 +- examples/ZGet/ZGet.cs | 164 ++++++++++++++++++++++++++--------- examples/ZGet/ZGet.csproj | 2 +- examples/ZInfo/ZInfo.cs | 128 ++++++++++++++++++++------- examples/ZInfo/ZInfo.csproj | 2 +- examples/ZPub/ZPub.cs | 157 +++++++++++++++++++++++---------- examples/ZPub/ZPub.csproj | 2 +- examples/ZPut/ZPut.cs | 163 ++++++++++++++++++++++------------ examples/ZPut/ZPut.csproj | 2 +- examples/ZSub/ZSub.cs | 168 ++++++++++++++++++++++++------------ examples/ZSub/ZSub.csproj | 2 +- 12 files changed, 560 insertions(+), 234 deletions(-) diff --git a/examples/README.md b/examples/README.md index bbdfdb0..a9cbe49 100644 --- a/examples/README.md +++ b/examples/README.md @@ -2,7 +2,7 @@ ## Introduce -Each example accepts the `-h` or `--help` option that provides a description of its arguments and their default values. +Each example accepts the `--help` option that provides a description of its arguments and their default values. If you run the tests against the zenoh router running in a Docker container, you need to add the `-e tcp/localhost:7447` option to your examples. diff --git a/examples/README.zh.md b/examples/README.zh.md index a75f7b9..e6dd6c5 100644 --- a/examples/README.zh.md +++ b/examples/README.zh.md @@ -1,7 +1,7 @@ # Zenoh C# examples ## 介绍 -每个示例都接受 `-h` 或 `——help` 选项,这些选项提供了对其参数及其默认值的描述. +每个示例都接受 `——help` 选项,这些选项提供了对其参数及其默认值的描述. 如果对运行在Docker容器中的zenoh路由器运行测试, 则需要在示例中添加 `-e tcp/localhost:7447` 选项. 这是因为Docker不支持UDP多播传输, 因此zenoh侦察和发现机制无法工作. diff --git a/examples/ZGet/ZGet.cs b/examples/ZGet/ZGet.cs index 5b9c652..6b1d074 100644 --- a/examples/ZGet/ZGet.cs +++ b/examples/ZGet/ZGet.cs @@ -1,60 +1,144 @@ #nullable enable using System; +using System.Collections.Generic; using System.Text; using System.Threading; +using CommandLine; using Zenoh; -Config config = new Config(); -config.SetMode(Config.Mode.Client); -string[] connect = { "tcp/127.0.0.1:7447" }; -config.SetConnect(connect); -//string[] listen = {"tcp/127.0.0.1:7888"}; -//config.SetListen(listen); +namespace ZGet; -Console.WriteLine("Opening session..."); -var session = Session.Open(config); -if (session is null) +class Program { - Console.WriteLine("Opening session fault!"); - return; -} + static void Main(string[] args) + { + var r = Parser.Default.ParseArguments(args); + bool ok = true; + r.WithNotParsed(e => { ok = false; }); + if (!ok) return; + + ClArgs clArgs = r.Value; + Config? config = clArgs.ToConfig(); + if (config is null) + return; -Thread.Sleep(200); -Console.WriteLine("Opening session successful!"); + Console.WriteLine("Opening session..."); + var session = Session.Open(config); + if (session is null) + { + Console.WriteLine("Opening session fault!"); + return; + } -string keyexpr = "demo/**"; -byte[] data = Encoding.UTF8.GetBytes("hello"); -QueryOptions queryOptions = new QueryOptions(keyexpr, EncodingPrefix.TextPlain, data); + Thread.Sleep(200); + Console.WriteLine("Opening session successful!"); -Console.WriteLine($"Sending Query '{keyexpr}'"); -Querier? querier = session.Query(queryOptions); -if (querier is null) -{ - Console.WriteLine("Session query fault!"); - goto EXIT; + string keyexpr = clArgs.GetSelector(); + byte[] data = Encoding.UTF8.GetBytes(clArgs.GetValue()); + QueryOptions queryOptions = new QueryOptions(keyexpr, EncodingPrefix.TextPlain, data); + + Console.WriteLine($"Sending Query '{keyexpr}'"); + Querier? querier = session.Query(queryOptions); + if (querier is null) + { + Console.WriteLine("Session query fault!"); + goto EXIT; + } + + QuerierCallback callback = sample => + { + string key = sample.GetKeyexpr(); + EncodingPrefix encodingPrefix = sample.GetEncodingPrefix(); + string? s = sample.GetString(); + if (s is null) + { + byte[] d = sample.GetPayload(); + Console.WriteLine($">> Received ('{key}' '{encodingPrefix}': '{d}')"); + } + else + { + Console.WriteLine($">> Received ('{key}' '{encodingPrefix}': '{s}')"); + } + }; + + if (!querier.GetSamples(callback)) + { + Console.WriteLine("querier get sample error!"); + } + + EXIT: + session.Close(); + } } -QuerierCallback callback = sample => +class ClArgs { - string key = sample.GetKeyexpr(); - EncodingPrefix encodingPrefix = sample.GetEncodingPrefix(); - string? s = sample.GetString(); - if (s is null) + [Option('c', "config", Required = false, HelpText = "A configuration file.")] + public string? ConfigFilePath { get; set; } = null; + + [Option('e', "connect", Required = false, HelpText = "Endpoints to connect to. example: tcp/127.0.0.1:7447")] + public IEnumerable Connects { get; set; } = new List(); + + [Option('l', "listen", Required = false, HelpText = "Endpoints to listen on. example: tcp/127.0.0.1:8447")] + public IEnumerable Listens { get; set; } = new List(); + + [Option('m', "mode", Required = false, + HelpText = "The zenoh session mode (peer by default) [possible values: peer, client]")] + public string Mode { get; set; } = "peer"; + + [Option('s', "selector", Required = false, + HelpText = "The selection of resources to query [default: demo/example/**]")] + public string? Keyexpr { get; set; } = null; + + [Option('v', "value", Required = false, + HelpText = "An optional value to put in the query.")] + public string? Value { get; set; } = null; + + internal Config? ToConfig() { - byte[] d = sample.GetPayload(); - Console.WriteLine($">> Received ('{key}' '{encodingPrefix}': '{d}')"); + if (ConfigFilePath != null) + { + Config? c = Config.LoadFromFile(ConfigFilePath); + if (c is null) + { + Console.WriteLine("load config file error!"); + return null; + } + + return c; + } + + Config config = new Config(); + + config.SetMode(Mode == "client" ? Config.Mode.Client : Config.Mode.Peer); + + List connects = new List(); + foreach (string s in Connects) + { + connects.Add(s); + } + + config.SetConnect(connects.ToArray()); + + List listens = new List(); + foreach (string s in Listens) + { + listens.Add(s); + } + + config.SetConnect(listens.ToArray()); + + return config; } - else + + public string GetSelector() { - Console.WriteLine($">> Received ('{key}' '{encodingPrefix}': '{s}')"); + return Keyexpr ?? "demo/example/**"; } -}; -if (!querier.GetSamples(callback)) -{ - Console.WriteLine("querier get sample error!"); -} - -EXIT: -session.Close(); \ No newline at end of file + public string GetValue() + { + return Value ?? "hello"; + } +} \ No newline at end of file diff --git a/examples/ZGet/ZGet.csproj b/examples/ZGet/ZGet.csproj index df36679..5059234 100644 --- a/examples/ZGet/ZGet.csproj +++ b/examples/ZGet/ZGet.csproj @@ -25,7 +25,7 @@ - + diff --git a/examples/ZInfo/ZInfo.cs b/examples/ZInfo/ZInfo.cs index 7c0266e..a3abb18 100644 --- a/examples/ZInfo/ZInfo.cs +++ b/examples/ZInfo/ZInfo.cs @@ -1,44 +1,110 @@ -using System; +#nullable enable + +using System; using System.Collections.Generic; using System.Threading; +using CommandLine; using Zenoh; -Config config = new Config(); -config.SetMode(Config.Mode.Peer); -string[] connect = { "tcp/127.0.0.1:7447" }; -config.SetConnect(connect); -//string[] listen = {"tcp/127.0.0.1:7888"}; -//config.SetListen(listen); +namespace ZInfo; -Console.WriteLine("Opening session..."); -var session = Session.Open(config); -if (session is null) +class Program { - Console.WriteLine("Opening session fault!"); - return; -} + static void Main(string[] args) + { + var r = Parser.Default.ParseArguments(args); + bool ok = true; + r.WithNotParsed(e => { ok = false; }); + if (!ok) return; + + ClArgs clArgs = r.Value; + Config? config = clArgs.ToConfig(); + if (config is null) + return; -Thread.Sleep(200); -Console.WriteLine("Opening session successful!"); + Console.WriteLine("Opening session..."); + var session = Session.Open(config); + if (session is null) + { + Console.WriteLine("Opening session fault!"); + return; + } -string localId = session.LocalId().ToStr(); -Id[] routersId = session.RoutersId(); -List routersIdStr = new List(); -foreach (var id in routersId) -{ - routersIdStr.Add(id.ToStr()); -} + Thread.Sleep(200); + Console.WriteLine("Opening session successful!"); + string localId = session.LocalId().ToStr(); + Id[] routersId = session.RoutersId(); + List routersIdStr = new List(); + foreach (var id in routersId) + { + routersIdStr.Add(id.ToStr()); + } -Id[] peerId = session.PeersId(); -List peerIdStr = new List(); -foreach (var id in peerId) -{ - peerIdStr.Add(id.ToStr()); + Id[] peerId = session.PeersId(); + List peerIdStr = new List(); + foreach (var id in peerId) + { + peerIdStr.Add(id.ToStr()); + } + + Console.WriteLine($"Local ID: {localId}"); + Console.WriteLine($"PeersPID: {String.Join(',', peerIdStr)}"); + Console.WriteLine($"Routers ID: {String.Join(',', routersIdStr)}"); + + session.Close(); + } } -Console.WriteLine($"Local ID: {localId}"); -Console.WriteLine($"PeersPID: {String.Join(',', peerIdStr)}"); -Console.WriteLine($"Routers ID: {String.Join(',', routersIdStr)}"); +class ClArgs +{ + [Option('c', "config", Required = false, HelpText = "A configuration file.")] + public string? ConfigFilePath { get; set; } = null; + + [Option('e', "connect", Required = false, HelpText = "Endpoints to connect to. example: tcp/127.0.0.1:7447")] + public IEnumerable Connects { get; set; } = new List(); + + [Option('l', "listen", Required = false, HelpText = "Endpoints to listen on. example: tcp/127.0.0.1:8447")] + public IEnumerable Listens { get; set; } = new List(); + + [Option('m', "mode", Required = false, + HelpText = "The zenoh session mode (peer by default) [possible values: peer, client]")] + public string Mode { get; set; } = "peer"; + + internal Config? ToConfig() + { + if (ConfigFilePath != null) + { + Config? c = Config.LoadFromFile(ConfigFilePath); + if (c is null) + { + Console.WriteLine("load config file error!"); + return null; + } + + return c; + } + + Config config = new Config(); + + config.SetMode(Mode == "client" ? Config.Mode.Client : Config.Mode.Peer); + + List connects = new List(); + foreach (string s in Connects) + { + connects.Add(s); + } + + config.SetConnect(connects.ToArray()); + + List listens = new List(); + foreach (string s in Listens) + { + listens.Add(s); + } + + config.SetConnect(listens.ToArray()); -session.Close(); \ No newline at end of file + return config; + } +} \ No newline at end of file diff --git a/examples/ZInfo/ZInfo.csproj b/examples/ZInfo/ZInfo.csproj index 267f2e0..9372c12 100644 --- a/examples/ZInfo/ZInfo.csproj +++ b/examples/ZInfo/ZInfo.csproj @@ -26,7 +26,7 @@ - + diff --git a/examples/ZPub/ZPub.cs b/examples/ZPub/ZPub.cs index 894b6ff..211cde0 100644 --- a/examples/ZPub/ZPub.cs +++ b/examples/ZPub/ZPub.cs @@ -1,65 +1,134 @@ #nullable enable using System; +using System.Collections.Generic; using System.Threading; +using CommandLine; using Zenoh; -Config config = new Config(); -config.SetMode(Config.Mode.Client); -string[] connect = { "tcp/127.0.0.1:7447" }; -config.SetConnect(connect); -//string[] listen = {"tcp/127.0.0.1:7888"}; -//config.SetListen(listen); +namespace ZPub; -Console.WriteLine("Opening session..."); -var session = Session.Open(config); -if (session is null) +class Program { - Console.WriteLine("Opening session fault!"); - return; -} + static void Main(string[] args) + { + var r = Parser.Default.ParseArguments(args); + bool ok = true; + r.WithNotParsed(e => { ok = false; }); + if (!ok) return; -Thread.Sleep(200); -Console.WriteLine("Opening session successful!"); + ClArgs clArgs = r.Value; + Config? config = clArgs.ToConfig(); + if (config is null) + return; -string key1 = "demo/example/zenoh-cs-pub1"; -string key2 = "demo/example/zenoh-cs-pub2"; + Console.WriteLine("Opening session..."); + var session = Session.Open(config); + if (session is null) + { + Console.WriteLine("Opening session fault!"); + return; + } -Publisher publisher1 = new Publisher(key1); -Publisher publisher2 = new Publisher(key2); + Thread.Sleep(200); + Console.WriteLine("Opening session successful!"); -var handle1 = session.RegisterPublisher(publisher1); -if (handle1 is null) -{ - Console.WriteLine($"Register Publisher1 fault On '{key1}'"); - return; -} + string key = clArgs.GetKey(); + string value = clArgs.GetValue(); -Console.WriteLine($"Registered Publisher1 On '{key1}'"); + Publisher publisher = new Publisher(key); -var handle2 = session.RegisterPublisher(publisher2); -if (handle2 is null) -{ - Console.WriteLine($"Register Publisher2 fault On '{key2}'"); - return; -} + var handle = session.RegisterPublisher(publisher); + if (handle is null) + { + Console.WriteLine($"Register Publisher1 fault On '{key}'"); + return; + } -Console.WriteLine($"Registered Publisher2 On '{key2}'"); + Console.WriteLine($"Registered Publisher1 On '{key}'"); -for (int i = 0; i < 100; i++) -{ - string value1 = $"[{i * 2}] Pub from C#!"; - session.PubStr(handle1.Value, value1); - Console.WriteLine($"Publishing Data ('{key1}': '{value1}').."); + for (int i = 0; i < 100; i++) + { + string pubValue = $"[{i}] {value}"; + session.PubStr(handle.Value, pubValue); + Console.WriteLine($"Publishing Data ('{key}': '{pubValue}').."); - string value2 = $"[{i * 2 + 1}] Pub from C#!"; - session.PubStr(handle2.Value, value2); - Console.WriteLine($"Publishing Data ('{key2}': '{value2}').."); + Thread.Sleep(1000); + } - Thread.Sleep(200); + session.UnregisterPublisher(handle.Value); + + session.Close(); + } } -session.UnregisterPublisher(handle1.Value); -session.UnregisterPublisher(handle2.Value); +class ClArgs +{ + [Option('c', "config", Required = false, HelpText = "A configuration file.")] + public string? ConfigFilePath { get; set; } = null; + + [Option('e', "connect", Required = false, HelpText = "Endpoints to connect to. example: tcp/127.0.0.1:7447")] + public IEnumerable Connects { get; set; } = new List(); + + [Option('l', "listen", Required = false, HelpText = "Endpoints to listen on. example: tcp/127.0.0.1:8447")] + public IEnumerable Listens { get; set; } = new List(); + + [Option('m', "mode", Required = false, + HelpText = "The zenoh session mode (peer by default) [possible values: peer, client]")] + public string Mode { get; set; } = "peer"; + + [Option('k', "key", Required = false, + HelpText = "The key expression to publish onto. [default: demo/example/zenoh-cs-pub]")] + public string? Keyexpr { get; set; } = null; + + [Option('v', "value", Required = false, + HelpText = "The value to publish. [default: \"Pub from C#!\"]")] + public string? Value { get; set; } = null; + + internal Config? ToConfig() + { + if (ConfigFilePath != null) + { + Config? c = Config.LoadFromFile(ConfigFilePath); + if (c is null) + { + Console.WriteLine("load config file error!"); + return null; + } + + return c; + } + + Config config = new Config(); + + config.SetMode(Mode == "client" ? Config.Mode.Client : Config.Mode.Peer); + + List connects = new List(); + foreach (string s in Connects) + { + connects.Add(s); + } + + config.SetConnect(connects.ToArray()); + + List listens = new List(); + foreach (string s in Listens) + { + listens.Add(s); + } + + config.SetConnect(listens.ToArray()); + + return config; + } + + public string GetKey() + { + return Keyexpr ?? "demo/example/zenoh-cs-pub"; + } -session.Close(); \ No newline at end of file + public string GetValue() + { + return Value ?? "Pub from C#!"; + } +} \ No newline at end of file diff --git a/examples/ZPub/ZPub.csproj b/examples/ZPub/ZPub.csproj index 53e51b5..d9f5fc7 100644 --- a/examples/ZPub/ZPub.csproj +++ b/examples/ZPub/ZPub.csproj @@ -26,7 +26,7 @@ - + diff --git a/examples/ZPut/ZPut.cs b/examples/ZPut/ZPut.cs index 4fb2b9f..c98fbca 100644 --- a/examples/ZPut/ZPut.cs +++ b/examples/ZPut/ZPut.cs @@ -1,66 +1,115 @@ #nullable enable using System; +using System.Collections.Generic; using System.Threading; +using CommandLine; using Zenoh; - Config config = new Config(); - if (!config.SetMode(Config.Mode.Client)) - { - Console.WriteLine("Config set mode fail"); - return; - } - string[] connect = { "tcp/127.0.0.1:7447" }; - if (!config.SetConnect(connect)) - { - Console.WriteLine("Config set connect fail"); - return; - } - -// string[] listen = {"tcp/127.0.0.1:7888"}; -// config.SetListen(listen); - -// Config? c = Config.LoadFromFile("../../../../zenoh.json5"); -// if (c == null) -// { -// Console.WriteLine("Load config error!"); -// return; -// } -// Config config = c; - -Console.WriteLine("Opening session..."); -var session = Session.Open(config); -if (session is null) +namespace ZPut; + +class Program { - Console.WriteLine("Opening session unsuccessful!"); - return; + static void Main(string[] args) + { + var r = Parser.Default.ParseArguments(args); + bool ok = true; + r.WithNotParsed(e => { ok = false; }); + if (!ok) return; + + ClArgs clArgs = r.Value; + Config? config = clArgs.ToConfig(); + if (config is null) + return; + + Console.WriteLine("Opening session..."); + var session = Session.Open(config); + if (session is null) + { + Console.WriteLine("Opening session unsuccessful!"); + return; + } + + Thread.Sleep(200); + Console.WriteLine("Opening session successful!"); + + string keyStr = "demo/example/zenoh-cs-put/string"; + string dataStr = "Put from csharp !"; + Console.WriteLine(session.PutStr(keyStr, dataStr) + ? $"Putting data string ('{keyStr}': '{dataStr}')" + : "Putting data string fault!"); + + string keyJson = "demo/example/zenoh-cs-put/json"; + string dataJson = "{\"value\": \"Put from csharp\"}"; + Console.WriteLine(session.PutJson(keyJson, dataJson) + ? $"Putting data json ('{keyJson}': {dataJson})" + : "Putting data json fault!"); + + string keyInt = "demo/example/zenoh-cs-put/int"; + long dataInt = 965; + Console.WriteLine(session.PutInt(keyInt, dataInt) + ? $"Putting data int ('{keyInt}': {dataInt})" + : "Putting data int fault!"); + + string keyFloat = "demo/example/zenoh-cs-put/float"; + double dataFloat = 99.6; + Console.WriteLine(session.PutFloat(keyFloat, dataFloat) + ? $"Putting data float ('{keyFloat}': {dataFloat})" + : "Putting data float fault!"); + + session.Close(); + } } -Thread.Sleep(200); -Console.WriteLine("Opening session successful!"); - -string keyStr = "demo/example/zenoh-cs-put/string"; -string dataStr = "Put from csharp !"; -Console.WriteLine(session.PutStr(keyStr, dataStr) - ? $"Putting data string ('{keyStr}': '{dataStr}')" - : "Putting data string fault!"); - -string keyJson = "demo/example/zenoh-cs-put/json"; -string dataJson = "{\"value\": \"Put from csharp\"}"; -Console.WriteLine(session.PutJson(keyJson, dataJson) - ? $"Putting data json ('{keyJson}': {dataJson})" - : "Putting data json fault!"); - -string keyInt = "demo/example/zenoh-cs-put/int"; -long dataInt = 965; -Console.WriteLine(session.PutInt(keyInt, dataInt) - ? $"Putting data int ('{keyInt}': {dataInt})" - : "Putting data int fault!"); - -string keyFloat = "demo/example/zenoh-cs-put/float"; -double dataFloat = 99.6; -Console.WriteLine(session.PutFloat(keyFloat, dataFloat) - ? $"Putting data float ('{keyFloat}': {dataFloat})" - : "Putting data float fault!"); - -session.Close(); \ No newline at end of file +class ClArgs +{ + [Option('c', "config", Required = false, HelpText = "A configuration file.")] + public string? ConfigFilePath { get; set; } = null; + + [Option('e', "connect", Required = false, HelpText = "Endpoints to connect to. example: tcp/127.0.0.1:7447")] + public IEnumerable Connects { get; set; } = new List(); + + [Option('l', "listen", Required = false, HelpText = "Endpoints to listen on. example: tcp/127.0.0.1:8447")] + public IEnumerable Listens { get; set; } = new List(); + + [Option('m', "mode", Required = false, + HelpText = "The zenoh session mode (peer by default) [possible values: peer, client]")] + public string Mode { get; set; } = "peer"; + + internal Config? ToConfig() + { + if (ConfigFilePath != null) + { + Config? c = Config.LoadFromFile(ConfigFilePath); + if (c is null) + { + Console.WriteLine("load config file error!"); + return null; + } + + return c; + } + + Config config = new Config(); + + config.SetMode(Mode == "client" ? Config.Mode.Client : Config.Mode.Peer); + + List connects = new List(); + foreach (string s in Connects) + { + connects.Add(s); + } + + config.SetConnect(connects.ToArray()); + + List listens = new List(); + foreach (string s in Listens) + { + listens.Add(s); + } + + config.SetConnect(listens.ToArray()); + + return config; + } +} \ No newline at end of file diff --git a/examples/ZPut/ZPut.csproj b/examples/ZPut/ZPut.csproj index 0aa112e..34c3acd 100644 --- a/examples/ZPut/ZPut.csproj +++ b/examples/ZPut/ZPut.csproj @@ -26,7 +26,7 @@ - + diff --git a/examples/ZSub/ZSub.cs b/examples/ZSub/ZSub.cs index b660359..ca78534 100644 --- a/examples/ZSub/ZSub.cs +++ b/examples/ZSub/ZSub.cs @@ -1,78 +1,136 @@ #nullable enable using System; +using System.Collections.Generic; using System.Threading; +using CommandLine; using Zenoh; -Config config = new Config(); -config.SetMode(Config.Mode.Client); -string[] connect = { "tcp/127.0.0.1:7447" }; -config.SetConnect(connect); -//string[] listen = {"tcp/127.0.0.1:7888"}; -//config.SetListen(listen); +namespace ZSub; -Console.WriteLine("Opening session..."); -var session = Session.Open(config); -if (session is null) +class Program { - Console.WriteLine("Opening session fault!"); - return; -} + static void Main(string[] args) + { + var r = Parser.Default.ParseArguments(args); + bool ok = true; + r.WithNotParsed(e => { ok = false; }); + if (!ok) return; -Thread.Sleep(200); -Console.WriteLine("Opening session successful!"); + ClArgs clArgs = r.Value; + Config? config = clArgs.ToConfig(); + if (config is null) + return; + Console.WriteLine("Opening session..."); + var session = Session.Open(config); + if (session is null) + { + Console.WriteLine("Opening session fault!"); + return; + } -void Callback1(Sample sample) -{ - string key = sample.GetKeyexpr(); - string value = sample.GetString() ?? ""; - Console.WriteLine($">> [Subscriber1] Received PUT ('{key}': '{value}')"); -} + Thread.Sleep(200); + Console.WriteLine("Opening session successful!"); -void Callback2(Sample sample) -{ - string key = sample.GetKeyexpr(); - string value = sample.GetString() ?? ""; - Console.WriteLine($">> [Subscriber2] Received PUT ('{key}': '{value}')"); -} -SubscriberCallback userCallback1 = Callback1; -SubscriberCallback userCallback2 = Callback2; + void Callback(Sample sample) + { + string key = sample.GetKeyexpr(); + string value = sample.GetString() ?? ""; + Console.WriteLine($">> [Subscriber] Received PUT ('{key}': '{value}')"); + } -string key1 = "demo/example/zenoh-cs-put$*"; -string key2 = "demo/example/zenoh-cs-pub$*"; + SubscriberCallback userCallback = Callback; -Subscriber subscriber1 = new Subscriber(key1, userCallback1); -Subscriber subscriber2 = new Subscriber(key2, userCallback2); + string key = clArgs.GetKey(); -var handle1 = session.RegisterSubscriber(subscriber1); -if (handle1 is null) -{ - Console.WriteLine($"Register Subscriber1 fault On '{key1}'"); - return; -} -Console.WriteLine($"Registered Subscriber1 On '{key1}'"); + Subscriber subscriber = new Subscriber(key, userCallback); -var handle2 = session.RegisterSubscriber(subscriber2); -if (handle2 is null) -{ - Console.WriteLine($"Register Subscriber2 fault On '{key2}'"); - return; + var handle = session.RegisterSubscriber(subscriber); + if (handle is null) + { + Console.WriteLine($"Register Subscriber fault On '{key}'"); + return; + } + + Console.WriteLine($"Registered Subscriber On '{key}'"); + + + Console.WriteLine("Enter 'q' to quit..."); + while (true) + { + var input = Console.ReadKey(); + if (input.Key == ConsoleKey.Q) + { + break; + } + } + + session.UnregisterSubscriber(handle.Value); + + session.Close(); + } } -Console.WriteLine($"Registered Subscriber2 On '{key2}'"); -Console.WriteLine("Enter 'q' to quit..."); -while (true) +class ClArgs { - var input = Console.ReadKey(); - if (input.Key == ConsoleKey.Q) + [Option('c', "config", Required = false, HelpText = "A configuration file.")] + public string? ConfigFilePath { get; set; } = null; + + [Option('e', "connect", Required = false, HelpText = "Endpoints to connect to. example: tcp/127.0.0.1:7447")] + public IEnumerable Connects { get; set; } = new List(); + + [Option('l', "listen", Required = false, HelpText = "Endpoints to listen on. example: tcp/127.0.0.1:8447")] + public IEnumerable Listens { get; set; } = new List(); + + [Option('m', "mode", Required = false, + HelpText = "The zenoh session mode (peer by default) [possible values: peer, client]")] + public string Mode { get; set; } = "peer"; + + [Option('k', "key", Required = false, + HelpText = "The key expression to subscribe to. [default: demo/example/**]")] + public string? Keyexpr { get; set; } = null; + + internal Config? ToConfig() { - break; - } -} + if (ConfigFilePath != null) + { + Config? c = Config.LoadFromFile(ConfigFilePath); + if (c is null) + { + Console.WriteLine("load config file error!"); + return null; + } + + return c; + } + + Config config = new Config(); -session.UnregisterSubscriber(handle1.Value); -session.UnregisterSubscriber(handle2.Value); + config.SetMode(Mode == "client" ? Config.Mode.Client : Config.Mode.Peer); -session.Close(); \ No newline at end of file + List connects = new List(); + foreach (string s in Connects) + { + connects.Add(s); + } + + config.SetConnect(connects.ToArray()); + + List listens = new List(); + foreach (string s in Listens) + { + listens.Add(s); + } + + config.SetConnect(listens.ToArray()); + + return config; + } + + public string GetKey() + { + return Keyexpr ?? "demo/example/**"; + } +} \ No newline at end of file diff --git a/examples/ZSub/ZSub.csproj b/examples/ZSub/ZSub.csproj index dd9af01..72b301b 100644 --- a/examples/ZSub/ZSub.csproj +++ b/examples/ZSub/ZSub.csproj @@ -25,7 +25,7 @@ - + From db59fc2da5365083da2993bcee25f39696442b70 Mon Sep 17 00:00:00 2001 From: sanri Date: Thu, 24 Aug 2023 16:37:08 +0800 Subject: [PATCH 24/34] Add queryable --- Zenoh/Querier.cs | 1 - Zenoh/Queryable.cs | 171 ++++++++++++++++++++++++++ Zenoh/Sample.cs | 2 - Zenoh/Session.cs | 83 ++++++++++--- Zenoh/Subscriber.cs | 1 - Zenoh/Value.cs | 85 +++++++++++++ Zenoh/Zenoh.csproj | 5 +- Zenoh/ZenohC.cs | 14 ++- examples/ZQueryable/ZQueryable.cs | 140 +++++++++++++++++++++ examples/ZQueryable/ZQueryable.csproj | 29 +++++ examples/zenoh-examples.sln | 10 ++ 11 files changed, 515 insertions(+), 26 deletions(-) create mode 100644 Zenoh/Queryable.cs create mode 100644 Zenoh/Value.cs create mode 100644 examples/ZQueryable/ZQueryable.cs create mode 100644 examples/ZQueryable/ZQueryable.csproj diff --git a/Zenoh/Querier.cs b/Zenoh/Querier.cs index 8195c1f..31678cb 100644 --- a/Zenoh/Querier.cs +++ b/Zenoh/Querier.cs @@ -1,5 +1,4 @@ #pragma warning disable CS8500 -#nullable enable using System; using System.Runtime.InteropServices; diff --git a/Zenoh/Queryable.cs b/Zenoh/Queryable.cs new file mode 100644 index 0000000..2efa4ab --- /dev/null +++ b/Zenoh/Queryable.cs @@ -0,0 +1,171 @@ +#pragma warning disable CS8500 + +using System; +using System.Runtime.InteropServices; +using System.Text; + + +namespace Zenoh; + +public struct QueryableHandle +{ + internal int handle; +} + +public delegate void QueryableCallback(Query query); + +public class Queryable : IDisposable +{ + internal string keyexpr; + internal unsafe ZOwnedQueryable* zOwnedQueryable; + internal unsafe ZOwnedClosureQuery* closureQuery; + internal unsafe ZQueryableOptions* options; + private GCHandle _userCallbackGcHandle; + private bool _disposed; + + public Queryable(string keyexpr, QueryableCallback userCallback, bool complete = false) + { + unsafe + { + this.keyexpr = keyexpr; + _disposed = false; + zOwnedQueryable = null; + _userCallbackGcHandle = GCHandle.Alloc(userCallback); + + ZOwnedClosureQuery ownedClosureQuery = new ZOwnedClosureQuery + { + context = (void*)GCHandle.ToIntPtr(_userCallbackGcHandle), + call = Call, + drop = null, + }; + nint p = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.StructureToPtr(ownedClosureQuery, p, false); + closureQuery = (ZOwnedClosureQuery*)p; + + nint pOptions = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.WriteByte(pOptions, complete ? (byte)1 : (byte)0); + options = (ZQueryableOptions*)pOptions; + } + } + + public void Dispose() => Dispose(true); + + private void Dispose(bool disposing) + { + if (_disposed) return; + + unsafe + { + Marshal.FreeHGlobal((nint)closureQuery); + Marshal.FreeHGlobal((nint)options); + Marshal.FreeHGlobal((nint)zOwnedQueryable); + } + + _userCallbackGcHandle.Free(); + _disposed = true; + } + + private static unsafe void Call(ZQuery* zQuery, void* context) + { + GCHandle gch = GCHandle.FromIntPtr((nint)context); + QueryableCallback callback = (QueryableCallback)gch.Target; + Query query = new Query(zQuery); + if (callback != null) + { + callback(query); + } + } +} + +public class Query +{ + private unsafe ZQuery* _query; + + internal unsafe Query(ZQuery* query) + { + _query = query; + } + + public string GetKeyexpr() + { + unsafe + { + ZKeyexpr keyexpr = ZenohC.z_query_keyexpr(_query); + string keyStr = ZenohC.ZKeyexprToString(keyexpr); + return keyStr; + } + } + + public Value GetValue() + { + unsafe + { + ZValue zValue = ZenohC.z_query_value(_query); + return new Value(zValue); + } + } + + public bool Reply(string key, byte[] payload, EncodingPrefix encodingPrefix, byte[]? encodingSuffix) + { + unsafe + { + sbyte r; + fixed (byte* pv = payload) + { + nint pKey = Marshal.StringToHGlobalAnsi(key); + ZKeyexpr keyexpr = ZenohC.z_keyexpr((byte*)pKey); + if (encodingSuffix is null) + { + ZQueryReplyOptions options = new ZQueryReplyOptions + { + encoding = ZenohC.z_encoding(encodingPrefix, null), + }; + nuint len = (nuint)payload.Length; + r = ZenohC.z_query_reply(_query, keyexpr, pv, len, &options); + } + else + { + fixed (byte* suffix = encodingSuffix) + { + ZQueryReplyOptions options = new ZQueryReplyOptions + { + encoding = ZenohC.z_encoding(encodingPrefix, suffix), + }; + nuint len = (nuint)payload.Length; + r = ZenohC.z_query_reply(_query, keyexpr, pv, len, &options); + } + } + + Marshal.FreeHGlobal(pKey); + } + + return r == 0; + } + } + + public bool ReplyStr(string key, string value) + { + byte[] payload = Encoding.UTF8.GetBytes(value); + return Reply(key, payload, EncodingPrefix.TextPlain, null); + } + + public bool ReplyJson(string key, string value) + { + byte[] payload = Encoding.UTF8.GetBytes(value); + return Reply(key, payload, EncodingPrefix.AppJson, null); + } + + public bool ReplyInt(string key, long value) + { + string s = value.ToString("G"); + byte[] payload = Encoding.UTF8.GetBytes(s); + return Reply(key, payload, EncodingPrefix.AppInteger, null); + } + + public bool ReplyFloat(string key, double value) + { + string s = value.ToString("G"); + byte[] payload = Encoding.UTF8.GetBytes(s); + return Reply(key, payload, EncodingPrefix.AppFloat, null); + } +} \ No newline at end of file diff --git a/Zenoh/Sample.cs b/Zenoh/Sample.cs index 24da07c..526a716 100644 --- a/Zenoh/Sample.cs +++ b/Zenoh/Sample.cs @@ -1,5 +1,3 @@ -#nullable enable - using System; using System.Runtime.InteropServices; diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs index fe85322..119e75d 100644 --- a/Zenoh/Session.cs +++ b/Zenoh/Session.cs @@ -1,5 +1,4 @@ #pragma warning disable CS8500 -#nullable enable using System; using System.Collections.Generic; @@ -11,17 +10,20 @@ namespace Zenoh; public class Session : IDisposable { - internal SortedDictionary subscribers; + internal SortedDictionary subscribersDictionary; private int _indexSubscriber = 1; - internal SortedDictionary publishers; + internal SortedDictionary publishersDictionary; private int _indexPublisher = 1; + internal SortedDictionary queryableDictionary; + private int _indexQueryable = 1; private bool _disposed; private readonly unsafe ZOwnedSession* _session; private unsafe Session(ZOwnedSession* session) { - subscribers = new SortedDictionary(); - publishers = new SortedDictionary(); + subscribersDictionary = new SortedDictionary(); + publishersDictionary = new SortedDictionary(); + queryableDictionary = new SortedDictionary(); _disposed = false; _session = session; } @@ -50,14 +52,14 @@ public void Close() unsafe { - foreach ((_, Subscriber subscriber) in subscribers) + foreach ((_, Subscriber subscriber) in subscribersDictionary) { ZenohC.z_undeclare_subscriber(subscriber.ownedSubscriber); Marshal.FreeHGlobal((nint)subscriber.ownedSubscriber); subscriber.ownedSubscriber = null; } - subscribers.Clear(); + subscribersDictionary.Clear(); ZenohC.z_close(_session); Marshal.FreeHGlobal((nint)_session); @@ -262,13 +264,12 @@ public bool PubFloat(PublisherHandle handle, double value) return _publisher_put(handle, data, EncodingPrefix.AppFloat); } - private bool _publisher_put(PublisherHandle handle, byte[] value, EncodingPrefix encodingPrefix) { if (_disposed) return false; unsafe { - if (!publishers.TryGetValue(handle.handle, out Publisher? publisher)) + if (!publishersDictionary.TryGetValue(handle.handle, out Publisher? publisher)) return false; ZPublisher pub = ZenohC.z_publisher_loan(publisher.ownedPublisher); @@ -312,7 +313,7 @@ private bool _publisher_put(PublisherHandle handle, byte[] value, EncodingPrefix subscriber.ownedSubscriber = (ZOwnedSubscriber*)pOwnedSubscriber; _indexSubscriber += 1; - subscribers.Add(_indexSubscriber, subscriber); + subscribersDictionary.Add(_indexSubscriber, subscriber); return new SubscriberHandle { @@ -328,7 +329,7 @@ public void UnregisterSubscriber(SubscriberHandle handle) private void UnregisterSubscriber(int handle) { - if (subscribers.TryGetValue(handle, out Subscriber? subscriber)) + if (subscribersDictionary.TryGetValue(handle, out Subscriber? subscriber)) { unsafe { @@ -337,7 +338,7 @@ private void UnregisterSubscriber(int handle) subscriber.ownedSubscriber = null; } - subscribers.Remove(handle); + subscribersDictionary.Remove(handle); } } @@ -367,7 +368,7 @@ private void UnregisterSubscriber(int handle) publisher.ownedPublisher = (ZOwnedPublisher*)pOwnedPublisher; _indexPublisher += 1; - publishers.Add(_indexPublisher, publisher); + publishersDictionary.Add(_indexPublisher, publisher); return new PublisherHandle { @@ -383,7 +384,7 @@ public void UnregisterPublisher(PublisherHandle handle) private void UnregisterPublisher(int handle) { - if (publishers.TryGetValue(handle, out Publisher? publisher)) + if (publishersDictionary.TryGetValue(handle, out Publisher? publisher)) { unsafe { @@ -392,7 +393,7 @@ private void UnregisterPublisher(int handle) publisher.ownedPublisher = null; } - publishers.Remove(handle); + publishersDictionary.Remove(handle); } } @@ -426,4 +427,56 @@ private void UnregisterPublisher(int handle) return r >= 0 ? querier : null; } } + + public QueryableHandle? RegisterQueryable(Queryable queryable) + { + unsafe + { + if (queryable.zOwnedQueryable != null) + return null; + + ZSession session = ZenohC.z_session_loan(_session); + nint pKey = Marshal.StringToHGlobalAnsi(queryable.keyexpr); + ZKeyexpr keyexpr = ZenohC.z_keyexpr((byte*)pKey); + + ZOwnedQueryable zOwnedQueryable = + ZenohC.z_declare_queryable(session, keyexpr, queryable.closureQuery, queryable.options); + Marshal.FreeHGlobal(pKey); + + if (ZenohC.z_queryable_check(&zOwnedQueryable) != 1) + return null; + + nint pOwnedQueryable = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.StructureToPtr(zOwnedQueryable, pOwnedQueryable, false); + queryable.zOwnedQueryable = (ZOwnedQueryable*)pOwnedQueryable; + + _indexQueryable += 1; + queryableDictionary.Add(_indexQueryable, queryable); + + return new QueryableHandle + { + handle = _indexQueryable, + }; + } + } + + public void UnregisterQueryable(QueryableHandle handle) + { + UnregisterQueryable(handle.handle); + } + + private void UnregisterQueryable(int handle) + { + if (queryableDictionary.TryGetValue(handle, out Queryable? queryable)) + { + unsafe + { + ZenohC.z_undeclare_queryable(queryable.zOwnedQueryable); + Marshal.FreeHGlobal((nint)queryable.zOwnedQueryable); + queryable.zOwnedQueryable = null; + } + + queryableDictionary.Remove(handle); + } + } } \ No newline at end of file diff --git a/Zenoh/Subscriber.cs b/Zenoh/Subscriber.cs index f91d284..5719dc3 100644 --- a/Zenoh/Subscriber.cs +++ b/Zenoh/Subscriber.cs @@ -56,7 +56,6 @@ private void Dispose(bool disposing) unsafe { - ZenohC.z_closure_sample_drop(closureSample); Marshal.FreeHGlobal((nint)closureSample); Marshal.FreeHGlobal((nint)ownedSubscriber); } diff --git a/Zenoh/Value.cs b/Zenoh/Value.cs new file mode 100644 index 0000000..5c0a71f --- /dev/null +++ b/Zenoh/Value.cs @@ -0,0 +1,85 @@ +using System; +using System.Runtime.InteropServices; + +namespace Zenoh; + +// 这是一个引用类型, 不释放引用的内存. +public class Value +{ + internal ZValue zValue; + + internal Value(ZValue value) + { + zValue = value; + } + + public EncodingPrefix GetEncodingPrefix() + { + return zValue.encoding.prefix; + } + + public byte[] GetEncodingSuffix() + { + unsafe + { + nuint len = zValue.encoding.suffix.len; + nint start = (nint)zValue.encoding.suffix.start; + byte[] data = new byte[len]; + Marshal.Copy(start, data, 0, (int)len); + return data; + } + } + + public byte[] GetPayload() + { + unsafe + { + fixed (ZBytes* pBytes = &zValue.payload) + { + if (ZenohC.z_bytes_check(pBytes) != 1) + return Array.Empty(); + } + + var len = zValue.payload.len; + byte[] data = new byte[len]; + Marshal.Copy((nint)zValue.payload.start, data, 0, (int)len); + return data; + } + } + + public string? GetString() + { + unsafe + { + fixed (ZBytes* pBytes = &zValue.payload) + { + if (ZenohC.z_bytes_check(pBytes) != 1) + return null; + } + + return Marshal.PtrToStringUTF8((nint)zValue.payload.start, (int)zValue.payload.len); + } + } + + public long? GetInteger() + { + var str = GetString(); + if (!Int64.TryParse(str, out long n)) + { + return null; + } + + return n; + } + + public double? GetDouble() + { + var str = GetString(); + if (!Double.TryParse(str, out double n)) + { + return null; + } + + return n; + } +} \ No newline at end of file diff --git a/Zenoh/Zenoh.csproj b/Zenoh/Zenoh.csproj index e974f6d..34cb2f8 100644 --- a/Zenoh/Zenoh.csproj +++ b/Zenoh/Zenoh.csproj @@ -2,7 +2,7 @@ Zenoh Zenoh-CS - 0.1.1 + 0.1.2 sanri EPL-2.0 OR Apache-2.0 Zenoh: Zero Overhead Pub/sub, Store/Query and Compute. @@ -14,11 +14,12 @@ Library true 10 - 0.1.1 + 0.1.2 zenoh net7.0;net6.0 Release;Debug x64;ARM64 + enable TRACE;PLATFORM_ARM diff --git a/Zenoh/ZenohC.cs b/Zenoh/ZenohC.cs index 75104f2..6b58650 100644 --- a/Zenoh/ZenohC.cs +++ b/Zenoh/ZenohC.cs @@ -652,6 +652,10 @@ internal unsafe struct ZOwnedClosureZId internal ZOwnedClosureZIdDrop drop; } +internal unsafe delegate void ZOwnedClosureQueryCall(ZQuery* zQuery, void* context); + +internal unsafe delegate void ZOwnedClosureQueryDrop(void* context); + // z_owned_closure_query_t // -------------------------------- // typedef struct z_owned_closure_query_t { @@ -664,8 +668,8 @@ internal unsafe struct ZOwnedClosureZId internal unsafe struct ZOwnedClosureQuery { internal void* context; - internal delegate* unmanaged[Cdecl] call; - internal delegate* unmanaged[Cdecl] drop; + internal ZOwnedClosureQueryCall call; + internal ZOwnedClosureQueryDrop drop; } // z_owned_closure_reply_t @@ -1123,13 +1127,13 @@ internal static extern sbyte z_query_reply( [DllImport(DllName, EntryPoint = "z_closure_reply_call", CallingConvention = CallingConvention.Cdecl)] internal static extern void z_closure_reply_call(ZOwnedClosureReply* closure, ZOwnedReply* sample); - + [DllImport(DllName, EntryPoint = "zc_reply_fifo_new", CallingConvention = CallingConvention.Cdecl)] internal static extern ZOwnedReplyChannel zc_reply_fifo_new(nuint bound); - + [DllImport(DllName, EntryPoint = "z_reply_channel_drop", CallingConvention = CallingConvention.Cdecl)] internal static extern void z_reply_channel_drop(ZOwnedReplyChannel* channel); - + [DllImport(DllName, EntryPoint = "z_reply_channel_closure_call", CallingConvention = CallingConvention.Cdecl)] internal static extern sbyte z_reply_channel_closure_call(ZOwnedReplyChannelClosure* closure, ZOwnedReply* reply); } \ No newline at end of file diff --git a/examples/ZQueryable/ZQueryable.cs b/examples/ZQueryable/ZQueryable.cs new file mode 100644 index 0000000..de15d35 --- /dev/null +++ b/examples/ZQueryable/ZQueryable.cs @@ -0,0 +1,140 @@ +using CommandLine; +using Zenoh; + +namespace ZQueryable; + +class Program +{ + static void Main(string[] args) + { + var r = Parser.Default.ParseArguments(args); + bool ok = true; + r.WithNotParsed(e => { ok = false; }); + if (!ok) return; + + ClArgs clArgs = r.Value; + Config? config = clArgs.ToConfig(); + if (config is null) + return; + + Console.WriteLine("Opening session..."); + var session = Session.Open(config); + if (session is null) + { + Console.WriteLine("Opening session fault!"); + return; + } + + Thread.Sleep(200); + Console.WriteLine("Opening session successful!"); + + QueryableCallback callback = query => + { + string keyexpr = query.GetKeyexpr(); + string payload = query.GetValue().GetString() ?? ""; + Console.WriteLine(payload.Length > 0 + ? $">> [Queryable] Receiver Query '{keyexpr}' with value '{payload}'" + : $">> [Queryable] Receiver Query '{keyexpr}'"); + + query.ReplyStr(clArgs.GetKey(), clArgs.GetValue()); + }; + + Zenoh.Queryable queryable = new Zenoh.Queryable(clArgs.GetKey(), callback); + var handle = session.RegisterQueryable(queryable); + + + if (handle is null) + { + Console.WriteLine($"Register Queryable fault On '{clArgs.GetKey()}'"); + return; + } + + Console.WriteLine($"Registered Queryable On '{clArgs.GetKey()}'"); + + + Console.WriteLine("Enter 'q' to quit..."); + while (true) + { + var input = Console.ReadKey(); + if (input.Key == ConsoleKey.Q) + { + break; + } + } + + session.UnregisterQueryable(handle.Value); + + session.Close(); + } +} + +class ClArgs +{ + [Option('c', "config", Required = false, HelpText = "A configuration file.")] + public string? ConfigFilePath { get; set; } = null; + + [Option('e', "connect", Required = false, HelpText = "Endpoints to connect to. example: tcp/127.0.0.1:7447")] + public IEnumerable Connects { get; set; } = new List(); + + [Option('l', "listen", Required = false, HelpText = "Endpoints to listen on. example: tcp/127.0.0.1:8447")] + public IEnumerable Listens { get; set; } = new List(); + + [Option('m', "mode", Required = false, + HelpText = "The zenoh session mode (peer by default) [possible values: peer, client]")] + public string Mode { get; set; } = "peer"; + + [Option('k', "key", Required = false, + HelpText = "The key expression matching queries to reply to. [default: demo/example/zenoh-cs-queryable]")] + public string? Keyexpr { get; set; } = null; + + [Option('v', "value", Required = false, + HelpText = "The value to reply to queries. [default: \"Queryable from C#!\"]")] + public string? Value { get; set; } = null; + + internal Config? ToConfig() + { + if (ConfigFilePath != null) + { + Config? c = Config.LoadFromFile(ConfigFilePath); + if (c is null) + { + Console.WriteLine("load config file error!"); + return null; + } + + return c; + } + + Config config = new Config(); + + config.SetMode(Mode == "client" ? Config.Mode.Client : Config.Mode.Peer); + + List connects = new List(); + foreach (string s in Connects) + { + connects.Add(s); + } + + config.SetConnect(connects.ToArray()); + + List listens = new List(); + foreach (string s in Listens) + { + listens.Add(s); + } + + config.SetConnect(listens.ToArray()); + + return config; + } + + public string GetKey() + { + return Keyexpr ?? "demo/example/zenoh-cs-queryable"; + } + + public string GetValue() + { + return Value ?? "Queryable from Rust!"; + } +} \ No newline at end of file diff --git a/examples/ZQueryable/ZQueryable.csproj b/examples/ZQueryable/ZQueryable.csproj new file mode 100644 index 0000000..c4f929f --- /dev/null +++ b/examples/ZQueryable/ZQueryable.csproj @@ -0,0 +1,29 @@ + + + ZQueryable + ZQueryable + Exe + false + net7.0;net6.0 + enable + Debug;Release + ARM64;x64 + enable + + + + TRACE;PLATFORM_ARM64 + + + TRACE;PLATFORM_x64 + + + + + + + + + + + diff --git a/examples/zenoh-examples.sln b/examples/zenoh-examples.sln index b67309a..a96f83b 100644 --- a/examples/zenoh-examples.sln +++ b/examples/zenoh-examples.sln @@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZInfo", "ZInfo\ZInfo.csproj EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZPub", "ZPub\ZPub.csproj", "{D642DF1F-7096-4D47-A5EA-FCF624536993}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZQueryable", "ZQueryable\ZQueryable.csproj", "{C8BACCA5-5B34-4617-B29B-E70A749102C9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -74,5 +76,13 @@ Global {D642DF1F-7096-4D47-A5EA-FCF624536993}.Release|ARM64.Build.0 = Debug|ARM64 {D642DF1F-7096-4D47-A5EA-FCF624536993}.Release|x64.ActiveCfg = Release|x64 {D642DF1F-7096-4D47-A5EA-FCF624536993}.Release|x64.Build.0 = Release|x64 + {C8BACCA5-5B34-4617-B29B-E70A749102C9}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {C8BACCA5-5B34-4617-B29B-E70A749102C9}.Debug|ARM64.Build.0 = Debug|ARM64 + {C8BACCA5-5B34-4617-B29B-E70A749102C9}.Debug|x64.ActiveCfg = Debug|x64 + {C8BACCA5-5B34-4617-B29B-E70A749102C9}.Debug|x64.Build.0 = Debug|x64 + {C8BACCA5-5B34-4617-B29B-E70A749102C9}.Release|ARM64.ActiveCfg = Release|ARM64 + {C8BACCA5-5B34-4617-B29B-E70A749102C9}.Release|ARM64.Build.0 = Release|ARM64 + {C8BACCA5-5B34-4617-B29B-E70A749102C9}.Release|x64.ActiveCfg = Release|x64 + {C8BACCA5-5B34-4617-B29B-E70A749102C9}.Release|x64.Build.0 = Release|x64 EndGlobalSection EndGlobal From 746535790ed8875a135d93b70600c01176c0db3a Mon Sep 17 00:00:00 2001 From: sanri Date: Thu, 24 Aug 2023 16:56:27 +0800 Subject: [PATCH 25/34] update readme --- examples/README.md | 19 ++++++++++++++++++- examples/README.zh.md | 18 +++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/examples/README.md b/examples/README.md index a9cbe49..f1c199f 100644 --- a/examples/README.md +++ b/examples/README.md @@ -29,7 +29,8 @@ Run ### ZGet -Gets value from zenoh session. +Sends a query message for a selector. +The queryables with a matching path or selector (for instance ZQueryable ) will receive this query and reply with paths/values that will be received by the receiver stream. Build ```bash @@ -41,6 +42,22 @@ Run ./ZGet ``` +### ZQueryable + +Declares a queryable function with a path. +This queryable function will be triggered by each call to get with a selector that matches the path, and will return a value to the querier. + +Build +```bash +dotnet build ZQueryable/ZQueryable.csproj --configuration Release --property:Platform=x64 +``` + +Run +```bash +./ZQueryable +``` + + ### ZPut Writes a path/value into Zenoh. diff --git a/examples/README.zh.md b/examples/README.zh.md index e6dd6c5..8dea898 100644 --- a/examples/README.zh.md +++ b/examples/README.zh.md @@ -27,7 +27,8 @@ dotnet build ZInfo/ZInfo.csproj --configuration Release --property:Platform=x64 ### ZGet -从Zenoh网络中获取值. +为选择器发送查询消息. +具有匹配路径或选择器的可查询项(例如 ZQueryable)将接收此查询,并使用接收方接收到的路径进行回复. 构建命令 ```bash @@ -39,6 +40,21 @@ dotnet build ZGet/ZGet.csproj --configuration Release --property:Platform=x64 ./ZGet ``` +### ZQueryable + +声明一个带有路径的可查询函数. +这个可查询的函数将在每次调用get时触发, 调用与路径匹配的选择器, 并将向查询器返回一个值. + +构建命令 +```bash +dotnet build ZQueryable/ZQueryable.csproj --configuration Release --property:Platform=x64 +``` + +启动命令, 在生成产物目录下运行 +```bash +./ZQueryable +``` + ### ZPut 将 key/value 写入Zenoh网络. From d4f0acf8277e6a7379cd6f95241daa1369cf9632 Mon Sep 17 00:00:00 2001 From: sanri Date: Thu, 24 Aug 2023 17:12:57 +0800 Subject: [PATCH 26/34] update nuget package badges --- README.md | 2 +- README.zh.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cd3cb2a..a3516da 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ![zenoh banner](./zenoh-dragon.png) -[![NuGet](https://img.shields.io/nuget/v/Zenoh.svg)](https://www.nuget.org/packages/Zenoh-CS/) +[![NuGet](https://img.shields.io/nuget/v/Zenoh-CS)](https://www.nuget.org/packages/Zenoh-CS/) [![License](https://img.shields.io/badge/License-EPL%202.0-blue)](https://choosealicense.com/licenses/epl-2.0/) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) diff --git a/README.zh.md b/README.zh.md index 30c5828..113c22d 100644 --- a/README.zh.md +++ b/README.zh.md @@ -1,6 +1,6 @@ ![zenoh banner](./zenoh-dragon.png) -[![NuGet](https://img.shields.io/nuget/v/Zenoh.svg)](https://www.nuget.org/packages/Zenoh-CS/) +[![NuGet](https://img.shields.io/nuget/v/Zenoh-CS)](https://www.nuget.org/packages/Zenoh-CS/) [![License](https://img.shields.io/badge/License-EPL%202.0-blue)](https://choosealicense.com/licenses/epl-2.0/) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) From eb917d41f5d223294a445a512572658f8fce54e7 Mon Sep 17 00:00:00 2001 From: sanri Date: Thu, 24 Aug 2023 17:24:40 +0800 Subject: [PATCH 27/34] change nuget badge color --- README.md | 2 +- README.zh.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a3516da..070d322 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ![zenoh banner](./zenoh-dragon.png) -[![NuGet](https://img.shields.io/nuget/v/Zenoh-CS)](https://www.nuget.org/packages/Zenoh-CS/) +[![NuGet](https://img.shields.io/nuget/v/Zenoh-CS?color=blue)](https://www.nuget.org/packages/Zenoh-CS/) [![License](https://img.shields.io/badge/License-EPL%202.0-blue)](https://choosealicense.com/licenses/epl-2.0/) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) diff --git a/README.zh.md b/README.zh.md index 113c22d..833ac22 100644 --- a/README.zh.md +++ b/README.zh.md @@ -1,6 +1,6 @@ ![zenoh banner](./zenoh-dragon.png) -[![NuGet](https://img.shields.io/nuget/v/Zenoh-CS)](https://www.nuget.org/packages/Zenoh-CS/) +[![NuGet](https://img.shields.io/nuget/v/Zenoh-CS?color=blue)](https://www.nuget.org/packages/Zenoh-CS/) [![License](https://img.shields.io/badge/License-EPL%202.0-blue)](https://choosealicense.com/licenses/epl-2.0/) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) From 9d857f09be0ac0a3814e9a500ca1b06599ab2e6e Mon Sep 17 00:00:00 2001 From: sanri Date: Thu, 24 Aug 2023 18:20:01 +0800 Subject: [PATCH 28/34] add function PutData --- Zenoh/Queryable.cs | 6 +- Zenoh/Session.cs | 136 ++++++++++++++++++++++++++------------------ Zenoh/Subscriber.cs | 6 +- Zenoh/Zenoh.csproj | 4 +- Zenoh/ZenohC.cs | 8 +-- 5 files changed, 93 insertions(+), 67 deletions(-) diff --git a/Zenoh/Queryable.cs b/Zenoh/Queryable.cs index 2efa4ab..8a091f2 100644 --- a/Zenoh/Queryable.cs +++ b/Zenoh/Queryable.cs @@ -67,9 +67,9 @@ private void Dispose(bool disposing) private static unsafe void Call(ZQuery* zQuery, void* context) { - GCHandle gch = GCHandle.FromIntPtr((nint)context); - QueryableCallback callback = (QueryableCallback)gch.Target; - Query query = new Query(zQuery); + var gch = GCHandle.FromIntPtr((nint)context); + var callback = (QueryableCallback?)gch.Target; + var query = new Query(zQuery); if (callback != null) { callback(query); diff --git a/Zenoh/Session.cs b/Zenoh/Session.cs index 119e75d..0ac9438 100644 --- a/Zenoh/Session.cs +++ b/Zenoh/Session.cs @@ -142,17 +142,8 @@ public bool PutStr(string key, string value) public bool PutStr(string key, string s, CongestionControl congestionControl, Priority priority) { - unsafe - { - byte[] data = Encoding.UTF8.GetBytes(s); - ZPutOptions options = new ZPutOptions - { - encoding = ZenohC.z_encoding(EncodingPrefix.TextPlain, null), - congestionControl = congestionControl, - priority = priority, - }; - return _put(key, data, options); - } + byte[] data = Encoding.UTF8.GetBytes(s); + return _put(key, data, congestionControl, priority, EncodingPrefix.TextPlain); } public bool PutJson(string key, string value) @@ -162,17 +153,8 @@ public bool PutJson(string key, string value) public bool PutJson(string key, string s, CongestionControl congestionControl, Priority priority) { - unsafe - { - byte[] data = Encoding.UTF8.GetBytes(s); - ZPutOptions options = new ZPutOptions - { - encoding = ZenohC.z_encoding(EncodingPrefix.AppJson, null), - congestionControl = congestionControl, - priority = priority, - }; - return _put(key, data, options); - } + byte[] data = Encoding.UTF8.GetBytes(s); + return _put(key, data, congestionControl, priority, EncodingPrefix.AppJson); } public bool PutInt(string key, long value) @@ -182,18 +164,9 @@ public bool PutInt(string key, long value) public bool PutInt(string key, long value, CongestionControl congestionControl, Priority priority) { - unsafe - { - string s = value.ToString("G"); - byte[] data = Encoding.UTF8.GetBytes(s); - ZPutOptions options = new ZPutOptions - { - encoding = ZenohC.z_encoding(EncodingPrefix.AppInteger, null), - congestionControl = congestionControl, - priority = priority, - }; - return _put(key, data, options); - } + string s = value.ToString("G"); + byte[] data = Encoding.UTF8.GetBytes(s); + return _put(key, data, congestionControl, priority, EncodingPrefix.AppInteger); } public bool PutFloat(string key, double value) @@ -203,21 +176,29 @@ public bool PutFloat(string key, double value) public bool PutFloat(string key, double value, CongestionControl congestionControl, Priority priority) { - unsafe - { - string s = value.ToString("G"); - byte[] data = Encoding.UTF8.GetBytes(s); - ZPutOptions options = new ZPutOptions - { - encoding = ZenohC.z_encoding(EncodingPrefix.AppFloat, null), - congestionControl = congestionControl, - priority = priority, - }; - return _put(key, data, options); - } + string s = value.ToString("G"); + byte[] data = Encoding.UTF8.GetBytes(s); + return _put(key, data, congestionControl, priority, EncodingPrefix.AppFloat); } - private bool _put(string key, byte[] value, ZPutOptions options) + public bool PutData(string key, byte[] value, EncodingPrefix encodingPrefix, byte[]? encodingSuffix = null) + { + return PutData(key, value, CongestionControl.Block, Priority.RealTime, encodingPrefix, encodingSuffix); + } + + public bool PutData(string key, byte[] value, + CongestionControl congestionControl, Priority priority, + EncodingPrefix encodingPrefix, byte[]? encodingSuffix = null + ) + { + return _put(key, value, congestionControl, priority, encodingPrefix, encodingSuffix); + } + + private bool _put( + string key, byte[] value, + CongestionControl congestionControl, Priority priority, + EncodingPrefix encodingPrefix, byte[]? encodingSuffix = null + ) { if (_disposed) return false; unsafe @@ -230,7 +211,30 @@ private bool _put(string key, byte[] value, ZPutOptions options) nint pKey = Marshal.StringToHGlobalAnsi(key); ZSession session = ZenohC.z_session_loan(_session); ZKeyexpr keyexpr = ZenohC.z_keyexpr((byte*)pKey); - r = ZenohC.z_put(session, keyexpr, pv, len, &options); + if (encodingSuffix is null) + { + ZPutOptions options = new ZPutOptions + { + encoding = ZenohC.z_encoding(encodingPrefix, null), + congestionControl = congestionControl, + priority = priority, + }; + r = ZenohC.z_put(session, keyexpr, pv, len, &options); + } + else + { + fixed (byte* pEncodingSuffix = encodingSuffix) + { + ZPutOptions options = new ZPutOptions + { + encoding = ZenohC.z_encoding(encodingPrefix, pEncodingSuffix), + congestionControl = congestionControl, + priority = priority, + }; + r = ZenohC.z_put(session, keyexpr, pv, len, &options); + } + } + Marshal.FreeHGlobal(pKey); } @@ -264,7 +268,14 @@ public bool PubFloat(PublisherHandle handle, double value) return _publisher_put(handle, data, EncodingPrefix.AppFloat); } - private bool _publisher_put(PublisherHandle handle, byte[] value, EncodingPrefix encodingPrefix) + public bool PubData(PublisherHandle handle, byte[] data, EncodingPrefix encodingPrefix, + byte[]? encodingSuffix = null) + { + return _publisher_put(handle, data, encodingPrefix, encodingSuffix); + } + + private bool _publisher_put(PublisherHandle handle, byte[] value, EncodingPrefix encodingPrefix, + byte[]? encodingSuffix = null) { if (_disposed) return false; unsafe @@ -273,15 +284,30 @@ private bool _publisher_put(PublisherHandle handle, byte[] value, EncodingPrefix return false; ZPublisher pub = ZenohC.z_publisher_loan(publisher.ownedPublisher); - ZPublisherPutOptions options = new ZPublisherPutOptions - { - encoding = ZenohC.z_encoding(encodingPrefix, null), - }; int r; fixed (byte* pv = value) { - nuint len = (nuint)value.Length; - r = ZenohC.z_publisher_put(pub, pv, len, &options); + if (encodingSuffix is null) + { + ZPublisherPutOptions options = new ZPublisherPutOptions + { + encoding = ZenohC.z_encoding(encodingPrefix, null), + }; + nuint len = (nuint)value.Length; + r = ZenohC.z_publisher_put(pub, pv, len, &options); + } + else + { + fixed (byte* pSuffix = encodingSuffix) + { + ZPublisherPutOptions options = new ZPublisherPutOptions + { + encoding = ZenohC.z_encoding(encodingPrefix, pSuffix), + }; + nuint len = (nuint)value.Length; + r = ZenohC.z_publisher_put(pub, pv, len, &options); + } + } } return r == 0; diff --git a/Zenoh/Subscriber.cs b/Zenoh/Subscriber.cs index 5719dc3..7028461 100644 --- a/Zenoh/Subscriber.cs +++ b/Zenoh/Subscriber.cs @@ -66,9 +66,9 @@ private void Dispose(bool disposing) private static unsafe void Call(ZSample* zSample, void* context) { - GCHandle gch = GCHandle.FromIntPtr((nint)context); - SubscriberCallback callback = (SubscriberCallback)gch.Target; - Sample sample = new Sample(zSample); + var gch = GCHandle.FromIntPtr((nint)context); + var callback = (SubscriberCallback?)gch.Target; + var sample = new Sample(zSample); if (callback != null) { callback(sample); diff --git a/Zenoh/Zenoh.csproj b/Zenoh/Zenoh.csproj index 34cb2f8..a3cc48f 100644 --- a/Zenoh/Zenoh.csproj +++ b/Zenoh/Zenoh.csproj @@ -2,7 +2,7 @@ Zenoh Zenoh-CS - 0.1.2 + 0.1.3 sanri EPL-2.0 OR Apache-2.0 Zenoh: Zero Overhead Pub/sub, Store/Query and Compute. @@ -14,7 +14,7 @@ Library true 10 - 0.1.2 + 0.1.3 zenoh net7.0;net6.0 Release;Debug diff --git a/Zenoh/ZenohC.cs b/Zenoh/ZenohC.cs index 6b58650..3a92eee 100644 --- a/Zenoh/ZenohC.cs +++ b/Zenoh/ZenohC.cs @@ -630,7 +630,7 @@ internal unsafe struct ZOwnedClosureSample { internal void* context; internal ZOwnedClosureSampleCall call; - internal ZOwnedClosureSampleDrop drop; + internal ZOwnedClosureSampleDrop? drop; } // -------------------------------- @@ -649,7 +649,7 @@ internal unsafe struct ZOwnedClosureZId { internal void* context; internal ZOwnedClosureZIdCall call; - internal ZOwnedClosureZIdDrop drop; + internal ZOwnedClosureZIdDrop? drop; } internal unsafe delegate void ZOwnedClosureQueryCall(ZQuery* zQuery, void* context); @@ -669,7 +669,7 @@ internal unsafe struct ZOwnedClosureQuery { internal void* context; internal ZOwnedClosureQueryCall call; - internal ZOwnedClosureQueryDrop drop; + internal ZOwnedClosureQueryDrop? drop; } // z_owned_closure_reply_t @@ -755,7 +755,7 @@ internal static string ZOwnedStrToString(ZOwnedStr* zs) return ""; } - return Marshal.PtrToStringUTF8(zs->cstr); + return Marshal.PtrToStringUTF8(zs->cstr) ?? ""; } internal static string ZKeyexprToString(ZKeyexpr keyexpr) From 5b39152fc23adf6c0447ea561f61b0b8c62230c0 Mon Sep 17 00:00:00 2001 From: sanri Date: Tue, 21 Nov 2023 18:31:22 +0800 Subject: [PATCH 29/34] fixed an issue where callback functions were recycled early --- README.md | 2 +- README.zh.md | 2 +- Zenoh/Queryable.cs | 5 +++-- Zenoh/Subscriber.cs | 9 +++++---- Zenoh/Zenoh.csproj | 4 ++-- examples/ZGet/ZGet.cs | 4 ++-- examples/ZInfo/ZInfo.cs | 4 ++-- examples/ZPub/ZPub.cs | 2 +- examples/ZPut/ZPut.cs | 9 ++++++++- examples/ZPut/ZPut.csproj | 2 +- examples/ZQueryable/ZQueryable.cs | 2 +- examples/ZSub/ZSub.cs | 2 +- 12 files changed, 28 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 070d322..ff17d0c 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ can be easily tested against a zenoh router running in a Docker container (see h Requirements: - The [zenoh-c](https://github.com/eclipse-zenoh/zenoh-c) library must be installed on your host -- The zenoh C# library [Zenoh-CS](https://www.nuget.org/packages/Zenoh-CS/) is available on NuGet: +- The zenoh C# library [Zenoh-CS](https://www.nuget.org/packages/Zenoh-CS/) is available on NuGet(Only x64). ### Supported .NET Standards - .NET 6.0 diff --git a/README.zh.md b/README.zh.md index 833ac22..998e29e 100644 --- a/README.zh.md +++ b/README.zh.md @@ -15,7 +15,7 @@ C# API是用于纯客户端的, 可以很容易地针对运行在Docker容器中 需求: - 库 [zenoh-c](https://github.com/eclipse-zenoh/zenoh-c) 必需被安装在你的主机上. -- Zenoh C# 库 [Zenoh-CS](https://www.nuget.org/packages/Zenoh-CS/) 库在NuGet上可用 +- Zenoh C# 库 [Zenoh-CS](https://www.nuget.org/packages/Zenoh-CS/) 库在NuGet上可用(只支持x64) ### 支持的 .NET 标准 - .NET 6.0 diff --git a/Zenoh/Queryable.cs b/Zenoh/Queryable.cs index 8a091f2..a883524 100644 --- a/Zenoh/Queryable.cs +++ b/Zenoh/Queryable.cs @@ -20,6 +20,7 @@ public class Queryable : IDisposable internal unsafe ZOwnedQueryable* zOwnedQueryable; internal unsafe ZOwnedClosureQuery* closureQuery; internal unsafe ZQueryableOptions* options; + private readonly ZOwnedClosureQuery _ownedClosureQuery; private GCHandle _userCallbackGcHandle; private bool _disposed; @@ -32,14 +33,14 @@ public Queryable(string keyexpr, QueryableCallback userCallback, bool complete = zOwnedQueryable = null; _userCallbackGcHandle = GCHandle.Alloc(userCallback); - ZOwnedClosureQuery ownedClosureQuery = new ZOwnedClosureQuery + _ownedClosureQuery = new ZOwnedClosureQuery { context = (void*)GCHandle.ToIntPtr(_userCallbackGcHandle), call = Call, drop = null, }; nint p = Marshal.AllocHGlobal(Marshal.SizeOf()); - Marshal.StructureToPtr(ownedClosureQuery, p, false); + Marshal.StructureToPtr(_ownedClosureQuery, p, false); closureQuery = (ZOwnedClosureQuery*)p; nint pOptions = Marshal.AllocHGlobal(Marshal.SizeOf()); diff --git a/Zenoh/Subscriber.cs b/Zenoh/Subscriber.cs index 7028461..283bc3c 100644 --- a/Zenoh/Subscriber.cs +++ b/Zenoh/Subscriber.cs @@ -18,6 +18,7 @@ public class Subscriber : IDisposable internal unsafe ZOwnedSubscriber* ownedSubscriber; internal readonly string keyexpr; internal readonly ZSubscriberOptions options; + private readonly ZOwnedClosureSample _ownedClosureSample; private GCHandle _userCallbackGcHandle; private bool _disposed; @@ -33,16 +34,16 @@ public Subscriber(string key, SubscriberCallback userCallback, Reliability relia keyexpr = key; _disposed = false; options.reliability = reliability; - _userCallbackGcHandle = GCHandle.Alloc(userCallback); - ZOwnedClosureSample ownedClosureSample = new ZOwnedClosureSample + _userCallbackGcHandle = GCHandle.Alloc(userCallback, GCHandleType.Normal); + _ownedClosureSample = new ZOwnedClosureSample { context = (void*)GCHandle.ToIntPtr(_userCallbackGcHandle), call = Call, drop = null, }; - nint p = Marshal.AllocHGlobal(Marshal.SizeOf(ownedClosureSample)); - Marshal.StructureToPtr(ownedClosureSample, p, false); + nint p = Marshal.AllocHGlobal(Marshal.SizeOf(_ownedClosureSample)); + Marshal.StructureToPtr(_ownedClosureSample, p, false); closureSample = (ZOwnedClosureSample*)p; ownedSubscriber = null; } diff --git a/Zenoh/Zenoh.csproj b/Zenoh/Zenoh.csproj index a3cc48f..f8dbadd 100644 --- a/Zenoh/Zenoh.csproj +++ b/Zenoh/Zenoh.csproj @@ -2,7 +2,7 @@ Zenoh Zenoh-CS - 0.1.3 + 0.1.4 sanri EPL-2.0 OR Apache-2.0 Zenoh: Zero Overhead Pub/sub, Store/Query and Compute. @@ -14,7 +14,7 @@ Library true 10 - 0.1.3 + 0.1.4 zenoh net7.0;net6.0 Release;Debug diff --git a/examples/ZGet/ZGet.cs b/examples/ZGet/ZGet.cs index 6b1d074..41a8690 100644 --- a/examples/ZGet/ZGet.cs +++ b/examples/ZGet/ZGet.cs @@ -17,7 +17,7 @@ static void Main(string[] args) bool ok = true; r.WithNotParsed(e => { ok = false; }); if (!ok) return; - + ClArgs clArgs = r.Value; Config? config = clArgs.ToConfig(); if (config is null) @@ -127,7 +127,7 @@ class ClArgs listens.Add(s); } - config.SetConnect(listens.ToArray()); + config.SetListen(listens.ToArray()); return config; } diff --git a/examples/ZInfo/ZInfo.cs b/examples/ZInfo/ZInfo.cs index a3abb18..cb8ef5f 100644 --- a/examples/ZInfo/ZInfo.cs +++ b/examples/ZInfo/ZInfo.cs @@ -16,7 +16,7 @@ static void Main(string[] args) bool ok = true; r.WithNotParsed(e => { ok = false; }); if (!ok) return; - + ClArgs clArgs = r.Value; Config? config = clArgs.ToConfig(); if (config is null) @@ -103,7 +103,7 @@ class ClArgs listens.Add(s); } - config.SetConnect(listens.ToArray()); + config.SetListen(listens.ToArray()); return config; } diff --git a/examples/ZPub/ZPub.cs b/examples/ZPub/ZPub.cs index 211cde0..84593b9 100644 --- a/examples/ZPub/ZPub.cs +++ b/examples/ZPub/ZPub.cs @@ -117,7 +117,7 @@ class ClArgs listens.Add(s); } - config.SetConnect(listens.ToArray()); + config.SetListen(listens.ToArray()); return config; } diff --git a/examples/ZPut/ZPut.cs b/examples/ZPut/ZPut.cs index c98fbca..b9ac181 100644 --- a/examples/ZPut/ZPut.cs +++ b/examples/ZPut/ZPut.cs @@ -57,6 +57,13 @@ static void Main(string[] args) ? $"Putting data float ('{keyFloat}': {dataFloat})" : "Putting data float fault!"); + string keyBin = "demo/example/zenoh-cs-put/bin"; + byte[] dataBin = { 0x1, 0x2, 0x3, 0x4 }; + Console.WriteLine( + session.PutData(keyBin, dataBin, EncodingPrefix.AppCustom) + ? $"Putting data bin ('{keyBin}': {dataBin.Length} Byte" + : "Putting data bin fault!"); + session.Close(); } } @@ -108,7 +115,7 @@ class ClArgs listens.Add(s); } - config.SetConnect(listens.ToArray()); + config.SetListen(listens.ToArray()); return config; } diff --git a/examples/ZPut/ZPut.csproj b/examples/ZPut/ZPut.csproj index 34c3acd..a41407c 100644 --- a/examples/ZPut/ZPut.csproj +++ b/examples/ZPut/ZPut.csproj @@ -26,7 +26,7 @@ - + diff --git a/examples/ZQueryable/ZQueryable.cs b/examples/ZQueryable/ZQueryable.cs index de15d35..f62f082 100644 --- a/examples/ZQueryable/ZQueryable.cs +++ b/examples/ZQueryable/ZQueryable.cs @@ -123,7 +123,7 @@ class ClArgs listens.Add(s); } - config.SetConnect(listens.ToArray()); + config.SetListen(listens.ToArray()); return config; } diff --git a/examples/ZSub/ZSub.cs b/examples/ZSub/ZSub.cs index ca78534..c4a15c7 100644 --- a/examples/ZSub/ZSub.cs +++ b/examples/ZSub/ZSub.cs @@ -124,7 +124,7 @@ class ClArgs listens.Add(s); } - config.SetConnect(listens.ToArray()); + config.SetListen(listens.ToArray()); return config; } From 0a08c68af44cb605882790c8921d305a0aaeb0d3 Mon Sep 17 00:00:00 2001 From: sanri Date: Mon, 13 Jan 2025 16:35:49 +0800 Subject: [PATCH 30/34] 20250113 --- Zenoh/Zenoh.csproj | 9 +- Zenoh/ZenohC.cs | 707 +++++++++++++++++++++----- examples/ZGet/ZGet.csproj | 4 +- examples/ZInfo/ZInfo.csproj | 4 +- examples/ZPub/ZPub.csproj | 4 +- examples/ZPut/ZPut.csproj | 4 +- examples/ZQueryable/ZQueryable.csproj | 3 +- examples/ZSub/ZSub.csproj | 4 +- 8 files changed, 596 insertions(+), 143 deletions(-) diff --git a/Zenoh/Zenoh.csproj b/Zenoh/Zenoh.csproj index f8dbadd..452bdf8 100644 --- a/Zenoh/Zenoh.csproj +++ b/Zenoh/Zenoh.csproj @@ -2,7 +2,7 @@ Zenoh Zenoh-CS - 0.1.4 + 0.2.0 sanri EPL-2.0 OR Apache-2.0 Zenoh: Zero Overhead Pub/sub, Store/Query and Compute. @@ -13,10 +13,11 @@ git Library true - 10 - 0.1.4 + 12 + 0.2.0 zenoh - net7.0;net6.0 + net8.0 + Release;Debug x64;ARM64 enable diff --git a/Zenoh/ZenohC.cs b/Zenoh/ZenohC.cs index 3a92eee..d13ca42 100644 --- a/Zenoh/ZenohC.cs +++ b/Zenoh/ZenohC.cs @@ -4,13 +4,17 @@ namespace Zenoh; +using ZResult = System.SByte; + +// zenoh_commons.h // z_congestion_control_t public enum CongestionControl { - Block, - Drop + Block = 0, + Drop = 1 } + // z_encoding_prefix_t public enum EncodingPrefix { @@ -37,6 +41,7 @@ public enum EncodingPrefix ImageGif = 20 } +// zenoh_commons.h // z_sample_kind_t public enum SampleKind { @@ -44,6 +49,7 @@ public enum SampleKind Delete = 1 } +// zenoh_commons.h // z_priority_t public enum Priority { @@ -56,13 +62,14 @@ public enum Priority Background = 7 } +// zenoh_commons.h // z_consolidation_mode_t public enum ConsolidationMode { Auto = -1, None = 0, Monotonic = 1, - Latest = 2, + Latest = 2 } // z_query_consolidation_t @@ -71,7 +78,7 @@ public enum QueryConsolidation Auto = -1, None = 0, Monotonic = 1, - Latest = 2, + Latest = 2 } // z_reliability_t @@ -81,13 +88,569 @@ public enum Reliability Reliable } +// zenoh_commons.h // z_query_target_t public enum QueryTarget { - BestMatching, - All, - AllComplete, -}; + BestMatching = 0, + All = 1, + AllComplete = 2 +} + +// zenoh_commons.h +// z_what_t +public enum What +{ + Router = 1, + Peer = 2, + Client = 4, + RouterPeer = 1 | 2, + RouterClient = 1 | 4, + PeerClient = 2 | 4, + RouterPeerClient = 1 | 2 | 4, +} + +// zenoh_commons.h +// z_whatami_t +public enum Whatami +{ + Router = 1, + Peer = 2, + Client = 4 +} + +// zenoh_commons.h +// zc_locality_t +public enum ZcLocality +{ + Any = 0, + Local = 1, + Remote = 2, +} + +// zenoh_commons.h +// zc_log_severity_t +public enum ZcLogSeverity +{ + Trace = 0, + Debug = 1, + Info = 2, + Warn = 3, + Error = 4, +} + +// zenoh_opaque.h +// z_owned_bytes_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedBytes +{ + private fixed byte data[32]; +} + +// zenoh_opaque.h +// z_loaned_bytes_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedBytes +{ + private fixed byte data[32]; +} + +// zenoh_opaque.h +// z_loaned_slice_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedSlice +{ + private fixed byte data[32]; +} + +// zenoh_opaque.h +// z_owned_slice_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedSlice +{ + private fixed byte data[32]; +} + +// zenoh_opaque.h +// z_view_slice_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZViewSlice +{ + private fixed byte data[32]; +} + +// zenoh_opaque.h +// z_loaned_string_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedString +{ + private fixed byte data[32]; +} + +// zenoh_opaque.h +// z_owned_string_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedString +{ + private fixed byte data[32]; +} + +// zenoh_opaque.h +// z_view_string_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZViewString +{ + private fixed byte data[32]; +} + +// zenoh_opaque.h +// z_bytes_reader_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZBytesReader +{ + private fixed byte data[24]; +} + +// zenoh_opaque.h +// z_loaned_bytes_writer_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedBytesWriter +{ + private fixed byte data[56]; +} + +// zenoh_opaque.h +// z_owned_bytes_writer_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedBytesWriter +{ + private fixed byte data[56]; +} + +// zenoh_opaque.h +// z_loaned_session_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedSession +{ + private fixed byte data[8]; +} + +// zenoh_opaque.h +// z_loaned_hello_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedHello +{ + private fixed byte data[48]; +} + +// zenoh_opaque.h +// z_loaned_closure_hello_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedClosureHello +{ + private fixed byte data[3]; +} + +// zenoh_opaque.h +// z_loaned_query_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedQuery +{ + private fixed byte data[136]; +} + +// zenoh_opaque.h +// z_loaned_closure_query_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedClosureQuery +{ + private fixed ulong data[3]; +} + +// zenoh_opaque.h +// z_loaned_reply_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedReply +{ + private fixed byte data[184]; +} + +// zenoh_opaque.h +// z_loaned_closure_reply_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedClosureReply +{ + private fixed ulong data[3]; +} + +// zenoh_opaque.h +// z_loaned_sample_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedSample +{ + private fixed byte data[184]; +} + +// zenoh_opaque.h +// z_loaned_closure_sample_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedClosureSample +{ + private fixed ulong data[3]; +} + +// zenoh_opaque.h +// z_id_t +[StructLayout(LayoutKind.Sequential, Pack = 1)] +internal unsafe struct ZId +{ + private fixed byte id[16]; +} + +// zenoh_opaque.h +// z_loaned_closure_zid_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedClosureZid +{ + private fixed ulong data[3]; +} + +// zenoh_opaque.h +// z_owned_condvar_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedCondvar +{ + private fixed byte data[24]; +} + +// zenoh_opaque.h +// z_loaned_condvar_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedCondvar +{ + private fixed byte data[16]; +} + +// zenoh_opaque.h +// z_loaned_mutex_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedMutex +{ + private fixed byte data[32]; +} + +// zenoh_opaque.h +// z_timestamp_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZTimestamp +{ + private fixed byte data[24]; +} + +// zenoh_opaque.h +// z_owned_config_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedConfig +{ + private fixed byte data[1736]; +} + +// zenoh_opaque.h +// z_loaned_config_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedConfig +{ + private fixed byte data[1736]; +} + +// zenoh_opaque.h +// z_owned_keyexpr_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedKeyexpr +{ + private fixed byte data[32]; +} + +// zenoh_opaque.h +// z_loaned_keyexpr_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedKeyexpr +{ + private fixed byte data[32]; +} + +// zenoh_opaque.h +// z_view_keyexpr_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZViewKeyexpr +{ + private fixed byte data[32]; +} + +// zenoh_opaque.h +// z_owned_encoding_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedEncoding +{ + private fixed byte data[40]; +} + +// zenoh_opaque.h +// z_loaned_encoding_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedEncoding +{ + private fixed byte data[40]; +} + +// zenoh_opaque.h +// z_owned_publisher_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedPublisher +{ + private fixed byte data[96]; +} + +// zenoh_opaque.h +// z_loaned_publisher_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedPublisher +{ + private fixed byte data[96]; +} + +// zenoh_opaque.h +// z_owned_queryable_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedQueryable +{ + private fixed byte data[16]; +} + +// zenoh_opaque.h +// z_loaned_queryable_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedQueryable +{ + private fixed byte data[16]; +} + +// zenoh_opaque.h +// z_owned_subscriber_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedSubscriber +{ + private fixed byte data[48]; +} + +// zenoh_opaque.h +// z_loaned_subscriber_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedSubscriber +{ + private fixed byte data[48]; +} + +// zenoh_opaque.h +// z_owned_fifo_handler_query_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedFifoHandlerQuery +{ + private fixed byte data[8]; +} + +// zenoh_opaque.h +// z_owned_fifo_handler_reply_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedFifoHandlerReply +{ + private fixed byte data[8]; +} + +// zenoh_opaque.h +// z_owned_fifo_handler_sample_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedFifoHandlerSample +{ + private fixed byte data[8]; +} + +// zenoh_opaque.h +// z_loaned_fifo_handler_query_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedFifoHandlerQuery +{ + private fixed byte data[8]; +} + +// zenoh_opaque.h +// z_loaned_fifo_handler_reply_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedFifoHandlerReply +{ + private fixed byte data[8]; +} + +// zenoh_opaque.h +// z_loaned_fifo_handler_sample_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedFifoHandlerSample +{ + private fixed byte data[8]; +} + +// zenoh_opaque.h +// z_owned_query_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedQuery +{ + private fixed byte data[136]; +} + +// zenoh_opaque.h +// z_owned_reply_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedReply +{ + private fixed byte data[184]; +} + +// zenoh_opaque.h +// z_owned_reply_err_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedReplyErr +{ + private fixed byte data[72]; +} + +// zenoh_opaque.h +// z_loaned_reply_err_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedReplyErr +{ + private fixed byte data[72]; +} + +// zenoh_opaque.h +// z_owned_sample_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedSample +{ + private fixed byte data[184]; +} + +// zenoh_opaque.h +// z_owned_hello_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedHello +{ + private fixed byte data[48]; +} + +// zenoh_opaque.h +// z_owned_string_array_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedStringArray +{ + private fixed byte data[24]; +} + +// zenoh_opaque.h +// z_loaned_string_array_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedStringArray +{ + private fixed byte data[24]; +} + +// zenoh_opaque.h +// z_owned_liveliness_token_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedLivelinessToken +{ + private fixed byte data[16]; +} + +// zenoh_opaque.h +// z_loaned_liveliness_token_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedLivelinessToken +{ + private fixed byte data[16]; +} + +// zenoh_opaque.h +// z_owned_mutex_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedMutex +{ + private fixed byte data[32]; +} + +// zenoh_opaque.h +// z_owned_ring_handler_query_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedRingHandlerQuery +{ + private fixed byte data[8]; +} + +// zenoh_opaque.h +// z_loaned_ring_handler_query_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedRingHandlerQuery +{ + private fixed byte data[8]; +} + +// zenoh_opaque.h +// z_owned_ring_handler_reply_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedRingHandlerReply +{ + private fixed byte data[8]; +} + +// zenoh_opaque.h +// z_loaned_ring_handler_reply_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedRingHandlerReply +{ + private fixed byte data[8]; +} + +// zenoh_opaque.h +// z_owned_ring_handler_sample_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedRingHandlerSample +{ + private fixed byte data[8]; +} + +// zenoh_opaque.h +// z_loaned_ring_handler_sample_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedRingHandlerSample +{ + private fixed byte data[8]; +} + +// zenoh_opaque.h +// z_owned_session_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedSession +{ + private fixed byte data[8]; +} + +// zenoh_opaque.h +// z_owned_task_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedTask +{ + private fixed byte data[24]; +} + // z_bytes_t // -------------------------------- @@ -103,18 +666,6 @@ internal unsafe struct ZBytes internal byte* start; } -// z_id_t -// -------------------------------- -// typedef struct z_id_t { -// uint8_t id[16]; -// } z_id_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal unsafe struct ZId -{ - // [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] - internal fixed byte id[16]; -} // z_owned_str_t // -------------------------------- @@ -156,21 +707,6 @@ internal struct ZStrArray internal nint val; } -// z_owned_encoding_t -// -------------------------------- -// typedef struct z_owned_encoding_t { -// enum z_encoding_prefix_t prefix; -// struct z_bytes_t suffix; -// bool _dropped; -// } z_owned_encoding_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal struct ZOwnedEncoding -{ - internal EncodingPrefix prefix; - internal ZBytes suffix; - internal sbyte _dropped; -} // z_encoding_t // -------------------------------- @@ -199,19 +735,6 @@ internal struct ZEncoding // [DllImport(ZenohC.DllName, EntryPoint = "z_encoding", CallingConvention = CallingConvention.Cdecl)] // internal static extern ZEncoding FnZEncoding(EncodingPrefix prefix, IntPtr suffix); -// z_timestamp_t -// -------------------------------- -// typedef struct z_timestamp_t { -// uint64_t time; -// struct z_id_t id; -// } z_timestamp_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal struct ZTimestamp -{ - internal ulong time; - internal ZId id; -} // z_keyexpr_t // -------------------------------- @@ -226,17 +749,6 @@ internal unsafe struct ZKeyexpr private fixed ulong _[4]; } -// -------------------------------- -// typedef struct z_owned_keyexpr_t { -// uint64_t _0[4]; -// } z_keyexpr_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZOwnedKeyexpr -{ - // [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] - private fixed ulong _[4]; -} // z_sample_t // -------------------------------- @@ -260,18 +772,6 @@ internal struct ZSample internal ZTimestamp timestamp; } -// z_owned_config_t -// -------------------------------- -// typedef struct z_owned_config_t { -// void *_0; -// } z_owned_config_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal struct ZOwnedConfig -{ - private nint _; -} - // z_config_t // -------------------------------- // typedef struct z_config_t { @@ -284,30 +784,7 @@ internal struct ZConfig private nint _; } -// z_owned_publisher_t -// -------------------------------- -// typedef struct ALIGN(8) z_owned_publisher_t { -// uint64_t _0[7]; -// } z_owned_publisher_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZOwnedPublisher -{ - // [MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)] - private fixed ulong _[7]; -} -// z_owned_queryable_t -// -------------------------------- -// typedef struct ALIGN(8) z_owned_queryable_t { -// uint64_t _0[4]; -// } z_owned_queryable_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZOwnedQueryable -{ - private fixed ulong _[4]; -} // z_publisher_t // -------------------------------- @@ -419,18 +896,6 @@ internal struct ZSubscriberOptions internal Reliability reliability; } -// z_owned_subscriber_t -// -------------------------------- -// typedef struct ALIGN(8) z_owned_subscriber_t { -// uint64_t _0[1]; -// } z_owned_subscriber_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential, Pack = 8)] -internal struct ZOwnedSubscriber -{ - private ulong _; -} - #if PLATFORM_ARM64 // -------------------------------- // typedef struct ALIGN(16) z_owned_reply_t { @@ -447,11 +912,11 @@ internal unsafe struct ZOwnedReply{ // uint64_t _0[22]; // } z_owned_reply_t; // -------------------------------- -[StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZOwnedReply -{ - private fixed ulong _[22]; -} +// [StructLayout(LayoutKind.Sequential, Pack = 8)] +// internal unsafe struct ZOwnedReply +// { +// private fixed ulong _[22]; +// } #else #error PLATFORM_ARM64 or PLATFORM_x64 #endif @@ -589,17 +1054,6 @@ internal struct ZQuery private nint _; } -// z_owned_session_t -// -------------------------------- -// typedef struct z_owned_session_t { -// uintptr_t _0; -// } z_owned_session_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal struct ZOwnedSession -{ - private nint _; -} // z_session_t // -------------------------------- @@ -750,18 +1204,15 @@ internal static unsafe class ZenohC internal static string ZOwnedStrToString(ZOwnedStr* zs) { - if (z_str_check(zs) != 1) - { - return ""; - } + if (z_str_check(zs) != 1) return ""; return Marshal.PtrToStringUTF8(zs->cstr) ?? ""; } internal static string ZKeyexprToString(ZKeyexpr keyexpr) { - ZOwnedStr str = z_keyexpr_to_string(keyexpr); - string o = ZOwnedStrToString(&str); + var str = z_keyexpr_to_string(keyexpr); + var o = ZOwnedStrToString(&str); z_str_drop(&str); return o; } diff --git a/examples/ZGet/ZGet.csproj b/examples/ZGet/ZGet.csproj index 5059234..ee8616b 100644 --- a/examples/ZGet/ZGet.csproj +++ b/examples/ZGet/ZGet.csproj @@ -4,8 +4,8 @@ ZGet Exe false - 10 - net7.0;net6.0 + 12 + net8.0 Release;Debug x64;ARM64 diff --git a/examples/ZInfo/ZInfo.csproj b/examples/ZInfo/ZInfo.csproj index 9372c12..4bb8056 100644 --- a/examples/ZInfo/ZInfo.csproj +++ b/examples/ZInfo/ZInfo.csproj @@ -4,8 +4,8 @@ ZInfo Exe false - 10 - net7.0;net6.0 + 12 + net8.0 Release;Debug x64;ARM64 diff --git a/examples/ZPub/ZPub.csproj b/examples/ZPub/ZPub.csproj index d9f5fc7..1446bdc 100644 --- a/examples/ZPub/ZPub.csproj +++ b/examples/ZPub/ZPub.csproj @@ -4,8 +4,8 @@ ZPub Exe false - 10 - net7.0;net6.0 + 12 + net8.0 Debug;Release ARM64;x64 diff --git a/examples/ZPut/ZPut.csproj b/examples/ZPut/ZPut.csproj index a41407c..34c47ea 100644 --- a/examples/ZPut/ZPut.csproj +++ b/examples/ZPut/ZPut.csproj @@ -4,8 +4,8 @@ ZPut Exe false - 10 - net7.0;net6.0 + 12 + net8.0 Release;Debug x64;ARM64 diff --git a/examples/ZQueryable/ZQueryable.csproj b/examples/ZQueryable/ZQueryable.csproj index c4f929f..4e7c52f 100644 --- a/examples/ZQueryable/ZQueryable.csproj +++ b/examples/ZQueryable/ZQueryable.csproj @@ -4,7 +4,8 @@ ZQueryable Exe false - net7.0;net6.0 + 12 + net8.0 enable Debug;Release ARM64;x64 diff --git a/examples/ZSub/ZSub.csproj b/examples/ZSub/ZSub.csproj index 72b301b..cb47a18 100644 --- a/examples/ZSub/ZSub.csproj +++ b/examples/ZSub/ZSub.csproj @@ -4,8 +4,8 @@ ZSub Exe false - 10 - net7.0;net6.0 + 12 + net8.0 Debug;Release ARM64;x64 From 26578850372c935f8205264f90c15dfe815ca5f2 Mon Sep 17 00:00:00 2001 From: sanri Date: Wed, 15 Jan 2025 18:49:46 +0800 Subject: [PATCH 31/34] 20250115 --- Zenoh/ZenohC.cs | 430 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 289 insertions(+), 141 deletions(-) diff --git a/Zenoh/ZenohC.cs b/Zenoh/ZenohC.cs index d13ca42..75ae116 100644 --- a/Zenoh/ZenohC.cs +++ b/Zenoh/ZenohC.cs @@ -155,18 +155,26 @@ internal unsafe struct ZLoanedBytes private fixed byte data[32]; } +// zenoh_commons.h +// z_moved_bytes_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedBytes +{ + private ZOwnedBytes ownedBytes; +} + // zenoh_opaque.h -// z_loaned_slice_t +// z_owned_slice_t [StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZLoanedSlice +internal unsafe struct ZOwnedSlice { private fixed byte data[32]; } // zenoh_opaque.h -// z_owned_slice_t +// z_loaned_slice_t [StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZOwnedSlice +internal unsafe struct ZLoanedSlice { private fixed byte data[32]; } @@ -179,18 +187,34 @@ internal unsafe struct ZViewSlice private fixed byte data[32]; } +// zenoh_commons.h +// z_moved_slice_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedSlice +{ + private ZOwnedSlice ownedSlice; +} + // zenoh_opaque.h -// z_loaned_string_t +// z_owned_string_t [StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZLoanedString +internal unsafe struct ZOwnedString { private fixed byte data[32]; } +// zenoh_commons.h +// z_bytes_slice_iterator_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZBytesSliceIterator +{ + private fixed byte data[24]; +} + // zenoh_opaque.h -// z_owned_string_t +// z_loaned_string_t [StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZOwnedString +internal unsafe struct ZLoanedString { private fixed byte data[32]; } @@ -203,6 +227,14 @@ internal unsafe struct ZViewString private fixed byte data[32]; } +// zenoh_commons.h +// z_moved_string_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedString +{ + private ZOwnedString ownedString; +} + // zenoh_opaque.h // z_bytes_reader_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -212,21 +244,29 @@ internal unsafe struct ZBytesReader } // zenoh_opaque.h -// z_loaned_bytes_writer_t +// z_owned_bytes_writer_t [StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZLoanedBytesWriter +internal unsafe struct ZOwnedBytesWriter { private fixed byte data[56]; } // zenoh_opaque.h -// z_owned_bytes_writer_t +// z_loaned_bytes_writer_t [StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZOwnedBytesWriter +internal unsafe struct ZLoanedBytesWriter { private fixed byte data[56]; } +// zenoh_commons.h +// z_moved_bytes_writer_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedBytesWriter +{ + internal ZOwnedBytesWriter ownedBytesWriter; +} + // zenoh_opaque.h // z_loaned_session_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -235,6 +275,14 @@ internal unsafe struct ZLoanedSession private fixed byte data[8]; } +// zenoh_opaque.h +// z_owned_hello_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedHello +{ + private fixed byte data[48]; +} + // zenoh_opaque.h // z_loaned_hello_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -243,6 +291,28 @@ internal unsafe struct ZLoanedHello private fixed byte data[48]; } +// zenoh_commons.h +// z_moved_hello_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedHello +{ + internal ZOwnedHello ownedHello; +} + + +// zenoh_commons.h +// z_owned_closure_hello_t +[StructLayout(LayoutKind.Sequential)] +internal unsafe struct ZOwnedClosureHello +{ + internal void* context; + internal ZOwnedClosureHelloCall call; + internal ZOwnedClosureHelloDrop? drop; +} + +internal unsafe delegate void ZOwnedClosureHelloCall(ZLoanedHello* hello, void* context); +internal unsafe delegate void ZOwnedClosureHelloDrop(void* context); + // zenoh_opaque.h // z_loaned_closure_hello_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -251,6 +321,22 @@ internal unsafe struct ZLoanedClosureHello private fixed byte data[3]; } +// zenoh_commons.h +// z_moved_closure_hello_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedClosureHello +{ + internal ZOwnedClosureHello _ownedClosureHello; +} + +// zenoh_opaque.h +// z_owned_query_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedQuery +{ + private fixed byte data[136]; +} + // zenoh_opaque.h // z_loaned_query_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -259,6 +345,19 @@ internal unsafe struct ZLoanedQuery private fixed byte data[136]; } +// zenoh_commons.h +// z_owned_closure_query_t +[StructLayout(LayoutKind.Sequential)] +internal unsafe struct ZOwnedClosureQuery +{ + internal void* context; + internal ZOwnedClosureQueryCall call; + internal ZOwnedClosureQueryDrop? drop; +} + +internal unsafe delegate void ZOwnedClosureQueryCall(ZLoanedQuery* query, void* context); +internal unsafe delegate void ZOwnedClosureQueryDrop(void* context); + // zenoh_opaque.h // z_loaned_closure_query_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -267,6 +366,22 @@ internal unsafe struct ZLoanedClosureQuery private fixed ulong data[3]; } +// zenoh_commons.h +// z_moved_closure_query_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedClosureQuery +{ + internal ZOwnedClosureQuery _ownedClosureQuery; +} + +// zenoh_opaque.h +// z_owned_reply_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedReply +{ + private fixed byte data[184]; +} + // zenoh_opaque.h // z_loaned_reply_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -275,6 +390,19 @@ internal unsafe struct ZLoanedReply private fixed byte data[184]; } +// zenoh_commons.h +// z_owned_closure_reply_t +[StructLayout(LayoutKind.Sequential)] +internal unsafe struct ZOwnedClosureReply +{ + internal void* context; + internal ZOwnedClosureReplyCall call; + internal ZOwnedClosureReplyDrop? drop; +} + +internal unsafe delegate void ZOwnedClosureReplyCall(ZLoanedReply* reply, void* context); +internal unsafe delegate void ZOwnedClosureReplyDrop(void* context); + // zenoh_opaque.h // z_loaned_closure_reply_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -283,6 +411,22 @@ internal unsafe struct ZLoanedClosureReply private fixed ulong data[3]; } +// zenoh_commons.h +// z_moved_closure_reply_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedClosureReply +{ + internal ZOwnedClosureReply _ownedClosureReply; +} + +// zenoh_opaque.h +// z_owned_sample_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedSample +{ + private fixed byte data[184]; +} + // zenoh_opaque.h // z_loaned_sample_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -291,6 +435,19 @@ internal unsafe struct ZLoanedSample private fixed byte data[184]; } +// zenoh_commons.h +// z_owned_closure_sample_t +[StructLayout(LayoutKind.Sequential)] +internal unsafe struct ZOwnedClosureSample +{ + internal void* context; + internal ZOwnedClosureSampleCall call; + internal ZOwnedClosureSampleDrop? drop; +} + +internal unsafe delegate void ZOwnedClosureSampleCall(ZLoanedSample* reply, void* context); +internal unsafe delegate void ZOwnedClosureSampleDrop(void* context); + // zenoh_opaque.h // z_loaned_closure_sample_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -299,6 +456,14 @@ internal unsafe struct ZLoanedClosureSample private fixed ulong data[3]; } +// zenoh_commons.h +// z_owned_moved_sample_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedClosureSample +{ + internal ZOwnedClosureSample _ownedClosureSample; +} + // zenoh_opaque.h // z_id_t [StructLayout(LayoutKind.Sequential, Pack = 1)] @@ -307,6 +472,19 @@ internal unsafe struct ZId private fixed byte id[16]; } +// zenoh_commons.h +// z_owned_closure_zid_t +[StructLayout(LayoutKind.Sequential)] +internal unsafe struct ZOwnedClosureZid +{ + internal void* context; + internal ZOwnedClosureZidCall call; + internal ZOwnedClosureZidDrop? drop; +} + +internal unsafe delegate void ZOwnedClosureZidCall(ZId* zId, void* context); +internal unsafe delegate void ZOwnedClosureZidDrop(void* context); + // zenoh_opaque.h // z_loaned_closure_zid_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -315,6 +493,14 @@ internal unsafe struct ZLoanedClosureZid private fixed ulong data[3]; } +// zenoh_commons.h +// z_moved_closure_zid_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedClosureZid +{ + internal ZOwnedClosureZid _ownedClosureZid; +} + // zenoh_opaque.h // z_owned_condvar_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -331,6 +517,15 @@ internal unsafe struct ZLoanedCondvar private fixed byte data[16]; } +// zenoh_commons.h +// z_moved_condvar_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedCondvar +{ + internal ZOwnedCondvar _ownedCondvar; +} + + // zenoh_opaque.h // z_loaned_mutex_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -352,7 +547,7 @@ internal unsafe struct ZTimestamp [StructLayout(LayoutKind.Sequential, Pack = 8)] internal unsafe struct ZOwnedConfig { - private fixed byte data[1736]; + private fixed byte data[1800]; } // zenoh_opaque.h @@ -360,7 +555,15 @@ internal unsafe struct ZOwnedConfig [StructLayout(LayoutKind.Sequential, Pack = 8)] internal unsafe struct ZLoanedConfig { - private fixed byte data[1736]; + private fixed byte data[1800]; +} + +// zenoh_commons.h +// z_moved_config_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedConfig +{ + internal ZOwnedConfig _ownedConfig; } // zenoh_opaque.h @@ -387,6 +590,14 @@ internal unsafe struct ZViewKeyexpr private fixed byte data[32]; } +// zenoh_commons.h +// z_moved_keyexpr_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedKeyexpr +{ + internal ZOwnedKeyexpr ownedKeyexpr; +} + // zenoh_opaque.h // z_owned_encoding_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -403,6 +614,14 @@ internal unsafe struct ZLoanedEncoding private fixed byte data[40]; } +// zenoh_commons.h +// z_moved_encoding_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedEncoding +{ + internal ZOwnedEncoding _ownedEncoding; +} + // zenoh_opaque.h // z_owned_publisher_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -460,25 +679,25 @@ internal unsafe struct ZOwnedFifoHandlerQuery } // zenoh_opaque.h -// z_owned_fifo_handler_reply_t +// z_loaned_fifo_handler_query_t [StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZOwnedFifoHandlerReply +internal unsafe struct ZLoanedFifoHandlerQuery { private fixed byte data[8]; } -// zenoh_opaque.h -// z_owned_fifo_handler_sample_t -[StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZOwnedFifoHandlerSample +// zenoh_commons.h +// z_moved_fifo_handler_query_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedFifoHandlerQuery { - private fixed byte data[8]; + internal ZOwnedFifoHandlerQuery _ownedFifoHandlerQuery; } // zenoh_opaque.h -// z_loaned_fifo_handler_query_t +// z_owned_fifo_handler_reply_t [StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZLoanedFifoHandlerQuery +internal unsafe struct ZOwnedFifoHandlerReply { private fixed byte data[8]; } @@ -491,28 +710,36 @@ internal unsafe struct ZLoanedFifoHandlerReply private fixed byte data[8]; } +// zenoh_commons.h +// z_moved_fifo_handler_reply_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedFifoHandlerReply +{ + internal ZOwnedFifoHandlerReply _ownedFifoHandlerReply; +} + // zenoh_opaque.h -// z_loaned_fifo_handler_sample_t +// z_owned_fifo_handler_sample_t [StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZLoanedFifoHandlerSample +internal unsafe struct ZOwnedFifoHandlerSample { private fixed byte data[8]; } // zenoh_opaque.h -// z_owned_query_t +// z_loaned_fifo_handler_sample_t [StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZOwnedQuery +internal unsafe struct ZLoanedFifoHandlerSample { - private fixed byte data[136]; + private fixed byte data[8]; } -// zenoh_opaque.h -// z_owned_reply_t -[StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZOwnedReply +// zenoh_commons.h +// z_moved_fifo_handler_sample_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedFifoHandlerSample { - private fixed byte data[184]; + internal ZOwnedFifoHandlerSample _ownedFifoHandlerSample; } // zenoh_opaque.h @@ -531,21 +758,7 @@ internal unsafe struct ZLoanedReplyErr private fixed byte data[72]; } -// zenoh_opaque.h -// z_owned_sample_t -[StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZOwnedSample -{ - private fixed byte data[184]; -} -// zenoh_opaque.h -// z_owned_hello_t -[StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZOwnedHello -{ - private fixed byte data[48]; -} // zenoh_opaque.h // z_owned_string_array_t @@ -579,6 +792,14 @@ internal unsafe struct ZLoanedLivelinessToken private fixed byte data[16]; } +// zenoh_commons.h +// z_liveliness_subscriber_options_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZLivelinessSubscriberOptions +{ + internal byte history; +} + // zenoh_opaque.h // z_owned_mutex_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -798,18 +1019,15 @@ internal struct ZPublisher private nint _; } +// zenoh_commons.h // z_publisher_options_t -// -------------------------------- -// typedef struct z_publisher_options_t { -// enum z_congestion_control_t congestion_control; -// enum z_priority_t priority; -// } z_publisher_options_t; -// -------------------------------- [StructLayout(LayoutKind.Sequential)] -internal struct ZPublisherOptions +internal unsafe struct ZPublisherOptions { + internal ZMovedEncoding *encoding; internal CongestionControl congestion_control; internal Priority priority; + internal byte is_express; } // z_publisher_delete_options_t @@ -872,28 +1090,20 @@ internal struct ZPullSubscriber internal nint _; } +// zenoh_commons.h // z_queryable_options_t -// -------------------------------- -// typedef struct z_queryable_options_t { -// bool complete; -// } z_queryable_options_t; -// -------------------------------- [StructLayout(LayoutKind.Sequential)] internal struct ZQueryableOptions { - internal sbyte complete; + internal byte complete; } +// zenoh_commons.h // z_subscriber_options_t -// -------------------------------- -// typedef struct z_subscriber_options_t { -// enum z_reliability_t reliability; -// } z_subscriber_options_t; -// -------------------------------- [StructLayout(LayoutKind.Sequential)] internal struct ZSubscriberOptions { - internal Reliability reliability; + internal byte option; } #if PLATFORM_ARM64 @@ -921,46 +1131,38 @@ internal unsafe struct ZOwnedReply{ #error PLATFORM_ARM64 or PLATFORM_x64 #endif +// zenoh_commons.h // z_delete_options_t -// -------------------------------- -// typedef struct z_delete_options_t { -// enum z_congestion_control_t congestion_control; -// enum z_priority_t priority; -// } z_delete_options_t; -// -------------------------------- [StructLayout(LayoutKind.Sequential)] -internal struct ZDeleteOptions +internal unsafe struct ZDeleteOptions { internal CongestionControl congestion_control; internal Priority priority; + internal byte is_express; + internal ZTimestamp *timestamp; } +// zenoh_commons.h // z_query_consolidation_t -// -------------------------------- -// typedef struct z_query_consolidation_t { -// enum z_consolidation_mode_t mode; -// } z_query_consolidation_t; -// -------------------------------- [StructLayout(LayoutKind.Sequential)] internal struct ZQueryConsolidation { internal ConsolidationMode mode; } +// zenoh_commons.h // z_get_options_t -// -------------------------------- -// typedef struct z_get_options_t { -// enum z_query_target_t target; -// struct z_query_consolidation_t consolidation; -// struct z_value_t value; -// } z_get_options_t; -// -------------------------------- [StructLayout(LayoutKind.Sequential)] -internal struct ZGetOptions +internal unsafe struct ZGetOptions { internal QueryTarget target; internal ZQueryConsolidation consolidation; - internal ZValue value; + internal ZMovedBytes *payload; + internal ZMovedEncoding *encoding; + internal CongestionControl congestion_control; + internal byte is_express; + internal ZMovedBytes* attachment; + internal ulong timeout_ms; } // z_value_t @@ -1067,25 +1269,6 @@ internal struct ZSession private nint _; } -// z_owned_closure_sample_t -// -------------------------------- -// typedef struct z_owned_closure_sample_t { -// void *context; -// void (*call)(const struct z_sample_t*, void *context); -// void (*drop)(void*); -// } z_owned_closure_sample_t; -// -------------------------------- -internal unsafe delegate void ZOwnedClosureSampleCall(ZSample* sample, void* context); - -internal unsafe delegate void ZOwnedClosureSampleDrop(void* context); - -[StructLayout(LayoutKind.Sequential)] -internal unsafe struct ZOwnedClosureSample -{ - internal void* context; - internal ZOwnedClosureSampleCall call; - internal ZOwnedClosureSampleDrop? drop; -} // -------------------------------- // typedef struct z_owned_closure_zid_t { @@ -1106,41 +1289,6 @@ internal unsafe struct ZOwnedClosureZId internal ZOwnedClosureZIdDrop? drop; } -internal unsafe delegate void ZOwnedClosureQueryCall(ZQuery* zQuery, void* context); - -internal unsafe delegate void ZOwnedClosureQueryDrop(void* context); - -// z_owned_closure_query_t -// -------------------------------- -// typedef struct z_owned_closure_query_t { -// void *context; -// void (*call)(const struct z_query_t*, void *context); -// void (*drop)(void*); -// } z_owned_closure_query_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal unsafe struct ZOwnedClosureQuery -{ - internal void* context; - internal ZOwnedClosureQueryCall call; - internal ZOwnedClosureQueryDrop? drop; -} - -// z_owned_closure_reply_t -// -------------------------------- -// typedef struct z_owned_closure_reply_t { -// void *context; -// void (*call)(struct z_owned_reply_t*, void*); -// void (*drop)(void*); -// } z_owned_closure_reply_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal unsafe struct ZOwnedClosureReply -{ - internal void* context; - internal delegate* unmanaged[Cdecl] call; - internal delegate* unmanaged[Cdecl] drop; -} // z_owned_reply_channel_closure_t // -------------------------------- From 66361db54c0afe1fe719e040da43b0b93ff73e19 Mon Sep 17 00:00:00 2001 From: sanri Date: Sat, 18 Jan 2025 12:20:24 +0800 Subject: [PATCH 32/34] 20250118 --- Zenoh/ZenohC.cs | 751 +++++++++++++++++++++++------------------------- 1 file changed, 358 insertions(+), 393 deletions(-) diff --git a/Zenoh/ZenohC.cs b/Zenoh/ZenohC.cs index 75ae116..268249e 100644 --- a/Zenoh/ZenohC.cs +++ b/Zenoh/ZenohC.cs @@ -8,15 +8,14 @@ namespace Zenoh; // zenoh_commons.h // z_congestion_control_t -public enum CongestionControl +public enum CongestionControl: uint { Block = 0, Drop = 1 } - // z_encoding_prefix_t -public enum EncodingPrefix +public enum EncodingPrefix: uint { Empty = 0, AppOctetStream = 1, @@ -43,7 +42,7 @@ public enum EncodingPrefix // zenoh_commons.h // z_sample_kind_t -public enum SampleKind +public enum SampleKind: uint { Put = 0, Delete = 1 @@ -51,7 +50,7 @@ public enum SampleKind // zenoh_commons.h // z_priority_t -public enum Priority +public enum Priority: uint { RealTime = 1, InteractiveHigh = 2, @@ -64,7 +63,7 @@ public enum Priority // zenoh_commons.h // z_consolidation_mode_t -public enum ConsolidationMode +public enum ConsolidationMode: int { Auto = -1, None = 0, @@ -73,7 +72,7 @@ public enum ConsolidationMode } // z_query_consolidation_t -public enum QueryConsolidation +public enum QueryConsolidation: int { Auto = -1, None = 0, @@ -82,7 +81,7 @@ public enum QueryConsolidation } // z_reliability_t -public enum Reliability +public enum Reliability: uint { BestEffort, Reliable @@ -90,7 +89,7 @@ public enum Reliability // zenoh_commons.h // z_query_target_t -public enum QueryTarget +public enum QueryTarget: uint { BestMatching = 0, All = 1, @@ -99,7 +98,7 @@ public enum QueryTarget // zenoh_commons.h // z_what_t -public enum What +public enum What: uint { Router = 1, Peer = 2, @@ -112,7 +111,7 @@ public enum What // zenoh_commons.h // z_whatami_t -public enum Whatami +public enum Whatami: uint { Router = 1, Peer = 2, @@ -121,7 +120,7 @@ public enum Whatami // zenoh_commons.h // zc_locality_t -public enum ZcLocality +public enum ZcLocality: uint { Any = 0, Local = 1, @@ -130,7 +129,7 @@ public enum ZcLocality // zenoh_commons.h // zc_log_severity_t -public enum ZcLogSeverity +public enum ZcLogSeverity: uint { Trace = 0, Debug = 1, @@ -145,7 +144,7 @@ public enum ZcLogSeverity internal unsafe struct ZOwnedBytes { private fixed byte data[32]; -} +} // zenoh_opaque.h // z_loaned_bytes_t @@ -153,7 +152,7 @@ internal unsafe struct ZOwnedBytes internal unsafe struct ZLoanedBytes { private fixed byte data[32]; -} +} // zenoh_commons.h // z_moved_bytes_t @@ -161,7 +160,7 @@ internal unsafe struct ZLoanedBytes internal struct ZMovedBytes { private ZOwnedBytes ownedBytes; -} +} // zenoh_opaque.h // z_owned_slice_t @@ -193,7 +192,7 @@ internal unsafe struct ZViewSlice internal struct ZMovedSlice { private ZOwnedSlice ownedSlice; -} +} // zenoh_opaque.h // z_owned_string_t @@ -233,7 +232,7 @@ internal unsafe struct ZViewString internal struct ZMovedString { private ZOwnedString ownedString; -} +} // zenoh_opaque.h // z_bytes_reader_t @@ -265,7 +264,7 @@ internal unsafe struct ZLoanedBytesWriter internal struct ZMovedBytesWriter { internal ZOwnedBytesWriter ownedBytesWriter; -} +} // zenoh_opaque.h // z_loaned_session_t @@ -299,19 +298,18 @@ internal struct ZMovedHello internal ZOwnedHello ownedHello; } - // zenoh_commons.h // z_owned_closure_hello_t [StructLayout(LayoutKind.Sequential)] internal unsafe struct ZOwnedClosureHello { - internal void* context; - internal ZOwnedClosureHelloCall call; - internal ZOwnedClosureHelloDrop? drop; + internal void* context; + internal delegate* unmanaged[Cdecl] call; + internal ZOwnedClosureHelloDrop? drop; } -internal unsafe delegate void ZOwnedClosureHelloCall(ZLoanedHello* hello, void* context); -internal unsafe delegate void ZOwnedClosureHelloDrop(void* context); +// internal unsafe delegate void ZOwnedClosureHelloCall(ZLoanedHello* hello, void* context); +internal unsafe delegate void ZOwnedClosureHelloDrop(void* context); // zenoh_opaque.h // z_loaned_closure_hello_t @@ -345,18 +343,27 @@ internal unsafe struct ZLoanedQuery private fixed byte data[136]; } +// zenoh_commons.h +// z_moved_query_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedQuery +{ + internal ZOwnedQuery ownedQuery; +} + // zenoh_commons.h // z_owned_closure_query_t [StructLayout(LayoutKind.Sequential)] internal unsafe struct ZOwnedClosureQuery { - internal void* context; - internal ZOwnedClosureQueryCall call; - internal ZOwnedClosureQueryDrop? drop; + internal void* context; + internal ZOwnedClosureQueryCall call; + internal ZOwnedClosureQueryDrop? drop; } -internal unsafe delegate void ZOwnedClosureQueryCall(ZLoanedQuery* query, void* context); -internal unsafe delegate void ZOwnedClosureQueryDrop(void* context); +internal unsafe delegate void ZOwnedClosureQueryCall(ZLoanedQuery* query, void* context); + +internal unsafe delegate void ZOwnedClosureQueryDrop(void* context); // zenoh_opaque.h // z_loaned_closure_query_t @@ -390,18 +397,51 @@ internal unsafe struct ZLoanedReply private fixed byte data[184]; } +// zenoh_commons.h +// z_moved_reply_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedReply +{ + internal ZOwnedReply ownedReply; +} + +// zenoh_opaque.h +// z_owned_reply_err_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZOwnedReplyErr +{ + private fixed byte data[72]; +} + +// zenoh_opaque.h +// z_loaned_reply_err_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZLoanedReplyErr +{ + private fixed byte data[72]; +} + +// zenoh_commons.h +// z_moved_reply_err_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedReplyErr +{ + internal ZOwnedReplyErr ownedReplyErr; +} + // zenoh_commons.h // z_owned_closure_reply_t [StructLayout(LayoutKind.Sequential)] internal unsafe struct ZOwnedClosureReply { - internal void* context; - internal ZOwnedClosureReplyCall call; - internal ZOwnedClosureReplyDrop? drop; + internal void* context; + internal ZOwnedClosureReplyCall call; + internal ZOwnedClosureReplyDrop? drop; } -internal unsafe delegate void ZOwnedClosureReplyCall(ZLoanedReply* reply, void* context); -internal unsafe delegate void ZOwnedClosureReplyDrop(void* context); +internal unsafe delegate void ZOwnedClosureReplyCall(ZLoanedReply* reply, void* context); + +internal unsafe delegate void ZOwnedClosureReplyDrop(void* context); // zenoh_opaque.h // z_loaned_closure_reply_t @@ -435,18 +475,27 @@ internal unsafe struct ZLoanedSample private fixed byte data[184]; } +// zenoh_commons.h +// z_moved_sample_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedSample +{ + internal ZOwnedSample ownedSample; +} + // zenoh_commons.h // z_owned_closure_sample_t [StructLayout(LayoutKind.Sequential)] internal unsafe struct ZOwnedClosureSample { - internal void* context; - internal ZOwnedClosureSampleCall call; - internal ZOwnedClosureSampleDrop? drop; + internal void* context; + internal ZOwnedClosureSampleCall call; + internal ZOwnedClosureSampleDrop? drop; } -internal unsafe delegate void ZOwnedClosureSampleCall(ZLoanedSample* reply, void* context); -internal unsafe delegate void ZOwnedClosureSampleDrop(void* context); +internal unsafe delegate void ZOwnedClosureSampleCall(ZLoanedSample* reply, void* context); + +internal unsafe delegate void ZOwnedClosureSampleDrop(void* context); // zenoh_opaque.h // z_loaned_closure_sample_t @@ -477,13 +526,14 @@ internal unsafe struct ZId [StructLayout(LayoutKind.Sequential)] internal unsafe struct ZOwnedClosureZid { - internal void* context; - internal ZOwnedClosureZidCall call; - internal ZOwnedClosureZidDrop? drop; + internal void* context; + internal ZOwnedClosureZidCall call; + internal ZOwnedClosureZidDrop? drop; } -internal unsafe delegate void ZOwnedClosureZidCall(ZId* zId, void* context); -internal unsafe delegate void ZOwnedClosureZidDrop(void* context); +internal unsafe delegate void ZOwnedClosureZidCall(ZId* zId, void* context); + +internal unsafe delegate void ZOwnedClosureZidDrop(void* context); // zenoh_opaque.h // z_loaned_closure_zid_t @@ -525,7 +575,6 @@ internal struct ZMovedCondvar internal ZOwnedCondvar _ownedCondvar; } - // zenoh_opaque.h // z_loaned_mutex_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -542,6 +591,14 @@ internal unsafe struct ZTimestamp private fixed byte data[24]; } +// zenoh_commons.h +// z_time_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZTime +{ + internal ulong t; +} + // zenoh_opaque.h // z_owned_config_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -619,7 +676,7 @@ internal unsafe struct ZLoanedEncoding [StructLayout(LayoutKind.Sequential)] internal struct ZMovedEncoding { - internal ZOwnedEncoding _ownedEncoding; + internal ZOwnedEncoding ownedEncoding; } // zenoh_opaque.h @@ -638,6 +695,14 @@ internal unsafe struct ZLoanedPublisher private fixed byte data[96]; } +// zenoh_commons.h +// z_moved_publisher_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedPublisher +{ + internal ZOwnedPublisher ownedPublisher; +} + // zenoh_opaque.h // z_owned_queryable_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -654,6 +719,14 @@ internal unsafe struct ZLoanedQueryable private fixed byte data[16]; } +// zenoh_commons.h +// z_moved_queryable_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedQueryable +{ + internal ZOwnedQueryable ownedQueryable; +} + // zenoh_opaque.h // z_owned_subscriber_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -670,6 +743,14 @@ internal unsafe struct ZLoanedSubscriber private fixed byte data[48]; } +// zenoh_commons.h +// z_moved_subscriber_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedSubscriber +{ + internal ZOwnedSubscriber ownedSubscriber; +} + // zenoh_opaque.h // z_owned_fifo_handler_query_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -742,24 +823,6 @@ internal struct ZMovedFifoHandlerSample internal ZOwnedFifoHandlerSample _ownedFifoHandlerSample; } -// zenoh_opaque.h -// z_owned_reply_err_t -[StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZOwnedReplyErr -{ - private fixed byte data[72]; -} - -// zenoh_opaque.h -// z_loaned_reply_err_t -[StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZLoanedReplyErr -{ - private fixed byte data[72]; -} - - - // zenoh_opaque.h // z_owned_string_array_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -776,6 +839,14 @@ internal unsafe struct ZLoanedStringArray private fixed byte data[24]; } +// zenoh_commons.h +// z_moved_string_array_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedStringArray +{ + internal ZOwnedStringArray ownedStringArray; +} + // zenoh_opaque.h // z_owned_liveliness_token_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -792,6 +863,14 @@ internal unsafe struct ZLoanedLivelinessToken private fixed byte data[16]; } +// zenoh_commons.h +// z_moved_liveliness_token_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedLivelinessToken +{ + internal ZOwnedLivelinessToken ownedLivelinessToken; +} + // zenoh_commons.h // z_liveliness_subscriber_options_t [StructLayout(LayoutKind.Sequential)] @@ -800,6 +879,22 @@ internal struct ZLivelinessSubscriberOptions internal byte history; } +// zenoh_commons.h +// z_liveliness_token_options_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZLivelinessTokenOptions +{ + internal byte dummy; +} + +// zenoh_commons.h +// z_liveliness_get_options_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZLivelinessGetOptions +{ + internal uint timeout_ms; +} + // zenoh_opaque.h // z_owned_mutex_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -808,6 +903,14 @@ internal unsafe struct ZOwnedMutex private fixed byte data[32]; } +// zenoh_commons.h +// z_moved_mutex_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedMutex +{ + internal ZOwnedMutex ownedMutex; +} + // zenoh_opaque.h // z_owned_ring_handler_query_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -824,6 +927,14 @@ internal unsafe struct ZLoanedRingHandlerQuery private fixed byte data[8]; } +// zenoh_commons.h +// z_moved_ring_handler_query_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedRingHandlerQuery +{ + internal ZOwnedRingHandlerQuery ownedRingHandlerQuery; +} + // zenoh_opaque.h // z_owned_ring_handler_reply_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -840,6 +951,14 @@ internal unsafe struct ZLoanedRingHandlerReply private fixed byte data[8]; } +// zenoh_commons.h +// z_moved_ring_handler_reply_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedRingHandlerReply +{ + internal ZOwnedRingHandlerReply ownedRingHandlerReply; +} + // zenoh_opaque.h // z_owned_ring_handler_sample_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -856,6 +975,14 @@ internal unsafe struct ZLoanedRingHandlerSample private fixed byte data[8]; } +// zenoh_commons.h +// z_moved_ring_handler_sample_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedRingHandlerSample +{ + internal ZOwnedRingHandlerSample ownedRingHandlerSample; +} + // zenoh_opaque.h // z_owned_session_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -864,6 +991,14 @@ internal unsafe struct ZOwnedSession private fixed byte data[8]; } +// zenoh_commons.h +// z_moved_session_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZMovedSession +{ + internal ZOwnedSession ownedSession; +} + // zenoh_opaque.h // z_owned_task_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -872,75 +1007,37 @@ internal unsafe struct ZOwnedTask private fixed byte data[24]; } - -// z_bytes_t -// -------------------------------- -// typedef struct z_bytes_t { -// size_t len; -// const uint8_t *start; -// } z_bytes_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal unsafe struct ZBytes -{ - internal nuint len; - internal byte* start; -} - - -// z_owned_str_t -// -------------------------------- -// typedef struct z_owned_str_t { -// char *_cstr; -// } z_owned_str_t; -// -------------------------------- +// zenoh_commons.h +// z_moved_task_t [StructLayout(LayoutKind.Sequential)] -internal struct ZOwnedStr +internal struct ZMovedTask { - internal nint cstr; + internal ZOwnedTask ownedTask; } -// z_owned_str_array_t -// -------------------------------- -// typedef struct z_owned_str_array_t { -// char **val; -// size_t len; -// } z_owned_str_array_t; -// -------------------------------- +// zenoh_commons.h +// z_task_attr_t [StructLayout(LayoutKind.Sequential)] -internal struct ZOwnedStrArray +internal struct ZTaskAttr { - internal nint val; - internal nuint len; + internal ulong attr; } -// z_str_array_t -// -------------------------------- -// typedef struct z_str_array_t { -// size_t len; -// const char *const *val; -// } z_str_array_t; -// -------------------------------- +// zenoh_commons.h +// z_open_options_t [StructLayout(LayoutKind.Sequential)] -internal struct ZStrArray +internal struct ZOpenOptions { - internal nuint len; - internal nint val; + internal byte dummy; } - -// z_encoding_t -// -------------------------------- -// typedef struct z_encoding_t { -// enum z_encoding_prefix_t prefix; -// struct z_bytes_t suffix; -// } z_encoding_t; -// -------------------------------- +// zenoh_commons.h +// z_scout_options_t [StructLayout(LayoutKind.Sequential)] -internal struct ZEncoding +internal struct ZScoutOptions { - internal EncodingPrefix prefix; - internal ZBytes suffix; + internal ulong timeout_ms; + internal What what; } // public string PrefixToString() @@ -956,138 +1053,34 @@ internal struct ZEncoding // [DllImport(ZenohC.DllName, EntryPoint = "z_encoding", CallingConvention = CallingConvention.Cdecl)] // internal static extern ZEncoding FnZEncoding(EncodingPrefix prefix, IntPtr suffix); - -// z_keyexpr_t -// -------------------------------- -// typedef struct z_keyexpr_t { -// uint64_t _0[4]; -// } z_keyexpr_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential, Pack = 8)] -internal unsafe struct ZKeyexpr -{ - // [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] - private fixed ulong _[4]; -} - - -// z_sample_t -// -------------------------------- -// typedef struct z_sample_t { -// struct z_keyexpr_t keyexpr; -// struct z_bytes_t payload; -// struct z_encoding_t encoding; -// const void *_zc_buf; -// enum z_sample_kind_t kind; -// struct z_timestamp_t timestamp; -// } z_sample_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal struct ZSample -{ - internal ZKeyexpr keyexpr; - internal ZBytes payload; - internal ZEncoding encoding; - private nint _zc_buf; - internal SampleKind kind; - internal ZTimestamp timestamp; -} - -// z_config_t -// -------------------------------- -// typedef struct z_config_t { -// const struct z_owned_config_t *_0; -// } z_config_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal struct ZConfig -{ - private nint _; -} - - - -// z_publisher_t -// -------------------------------- -// typedef struct z_publisher_t { -// const struct z_owned_publisher_t *_0; -// } z_publisher_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal struct ZPublisher -{ - private nint _; -} - // zenoh_commons.h // z_publisher_options_t [StructLayout(LayoutKind.Sequential)] internal unsafe struct ZPublisherOptions { - internal ZMovedEncoding *encoding; + internal ZMovedEncoding* encoding; internal CongestionControl congestion_control; internal Priority priority; - internal byte is_express; + [MarshalAs(UnmanagedType.U1)] + internal bool is_express; } -// z_publisher_delete_options_t -// -------------------------------- -// typedef struct z_publisher_delete_options_t { -// uint8_t __dummy; -// } z_publisher_delete_options_t; -// -------------------------------- +// zenoh_commons.h +// z_publisher_delete_options_t [StructLayout(LayoutKind.Sequential)] -internal struct ZPublisherDeleteOptions +internal unsafe struct ZPublisherDeleteOptions { - internal byte dummy; + internal ZTimestamp* timestamp; } +// zenoh_commons.h // z_publisher_put_options_t -// -------------------------------- -// typedef struct z_publisher_put_options_t { -// struct z_encoding_t encoding; -// } z_publisher_put_options_t; -// -------------------------------- [StructLayout(LayoutKind.Sequential)] -internal struct ZPublisherPutOptions +internal unsafe struct ZPublisherPutOptions { - internal ZEncoding encoding; -} - -// z_pull_subscriber_options_t -// -------------------------------- -// typedef struct z_pull_subscriber_options_t { -// enum z_reliability_t reliability; -// } z_pull_subscriber_options_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal struct ZPullSubscriberOptions -{ - internal Reliability reliability; -} - -// z_owned_pull_subscriber_t -// -------------------------------- -// typedef struct ALIGN(8) z_owned_pull_subscriber_t { -// uint64_t _0[1]; -// } z_owned_pull_subscriber_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential, Pack = 8)] -internal struct ZOwnedPullSubscriber -{ - private ulong _; -} - -// z_pull_subscriber_t -// -------------------------------- -// typedef struct z_pull_subscriber_t { -// const struct z_owned_pull_subscriber_t *_0; -// } z_pull_subscriber_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal struct ZPullSubscriber -{ - internal nint _; + internal ZMovedEncoding* encoding; + internal ZTimestamp* timestamp; + internal ZMovedBytes* attachment; } // zenoh_commons.h @@ -1138,8 +1131,9 @@ internal unsafe struct ZDeleteOptions { internal CongestionControl congestion_control; internal Priority priority; - internal byte is_express; - internal ZTimestamp *timestamp; + [MarshalAs(UnmanagedType.U1)] + internal bool is_express; + internal ZTimestamp* timestamp; } // zenoh_commons.h @@ -1157,179 +1151,102 @@ internal unsafe struct ZGetOptions { internal QueryTarget target; internal ZQueryConsolidation consolidation; - internal ZMovedBytes *payload; - internal ZMovedEncoding *encoding; + internal ZMovedBytes* payload; + internal ZMovedEncoding* encoding; internal CongestionControl congestion_control; - internal byte is_express; + [MarshalAs(UnmanagedType.U1)] + internal bool is_express; internal ZMovedBytes* attachment; internal ulong timeout_ms; } -// z_value_t -// -------------------------------- -// typedef struct z_value_t { -// struct z_bytes_t payload; -// struct z_encoding_t encoding; -// } z_value_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal struct ZValue -{ - internal ZBytes payload; - internal ZEncoding encoding; -} - -// zc_owned_payload_t -// -------------------------------- -// typedef struct zc_owned_payload_t { -// struct z_bytes_t payload; -// uintptr_t _owner[4]; -// } zc_owned_payload_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal unsafe struct ZcOwnedPayload -{ - private fixed ulong _[4]; -} - -// zc_owned_shmbuf_t -// -------------------------------- -// typedef struct zc_owned_shmbuf_t { -// uintptr_t _0[9]; -// } zc_owned_shmbuf_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal unsafe struct ZcOwnedShmbuf -{ - private fixed ulong _[9]; -} - -// zc_owned_shm_manager_t -// -------------------------------- -// typedef struct zc_owned_shm_manager_t { -// uintptr_t _0; -// } zc_owned_shm_manager_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal struct ZcOwnedShmManager -{ - private nint _; -} - +// zenoh_commons.h // z_put_options_t -// -------------------------------- -// typedef struct z_put_options_t { -// struct z_encoding_t encoding; -// enum z_congestion_control_t congestion_control; -// enum z_priority_t priority; -// } z_put_options_t; -// -------------------------------- [StructLayout(LayoutKind.Sequential)] -internal struct ZPutOptions +internal unsafe struct ZPutOptions { - internal ZEncoding encoding; + internal ZMovedEncoding* encoding; internal CongestionControl congestionControl; internal Priority priority; + [MarshalAs(UnmanagedType.U1)] + internal bool is_express; + internal ZTimestamp* timestamp; + internal ZMovedBytes* attachment; } +// zenoh_commons.h // z_query_reply_options_t -// -------------------------------- -// typedef struct z_query_reply_options_t { -// struct z_encoding_t encoding; -// } z_query_reply_options_t; -// -------------------------------- [StructLayout(LayoutKind.Sequential)] -internal struct ZQueryReplyOptions +internal unsafe struct ZQueryReplyOptions { - internal ZEncoding encoding; + internal ZMovedEncoding* encoding; + internal CongestionControl congestionControl; + internal Priority priority; + [MarshalAs(UnmanagedType.U1)] + internal bool is_express; + internal ZTimestamp* timestamp; + internal ZMovedBytes* attachment; } -// z_query_t -// -------------------------------- -// typedef struct z_query_t { -// void *_0; -// } z_query_t; -// -------------------------------- +// zenoh_commons.h +// z_query_reply_del_options_t [StructLayout(LayoutKind.Sequential)] -internal struct ZQuery +internal unsafe struct ZQueryReplyDelOptions { - private nint _; + internal CongestionControl congestionControl; + internal Priority priority; + [MarshalAs(UnmanagedType.U1)] + internal bool is_express; + internal ZTimestamp* timestamp; + internal ZMovedBytes* attachment; } - -// z_session_t -// -------------------------------- -// typedef struct z_session_t { -// uintptr_t _0; -// } z_session_t; -// -------------------------------- +// zenoh_commons.h +// z_query_reply_err_options_t [StructLayout(LayoutKind.Sequential)] -internal struct ZSession +internal unsafe struct ZQueryReplyErrOptions { - private nint _; + internal ZMovedEncoding* encoding; } - -// -------------------------------- -// typedef struct z_owned_closure_zid_t { -// void *context; -// void (*call)(const struct z_id_t*, void*); -// void (*drop)(void*); -// } z_owned_closure_zid_t; -// -------------------------------- -internal unsafe delegate void ZOwnedClosureZIdCall(ZId* zId, void* context); - -internal unsafe delegate void ZOwnedClosureZIdDrop(void* context); - -[StructLayout(LayoutKind.Sequential)] -internal unsafe struct ZOwnedClosureZId +// zenoh_opaque.h +// zc_loaned_closure_log_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZcLoanedClosureLog { - internal void* context; - internal ZOwnedClosureZIdCall call; - internal ZOwnedClosureZIdDrop? drop; + private fixed ulong data[3]; } +// zenoh_opaque.h +// ze_deserializer_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZeDeserializer +{ + private fixed byte data[24]; +} -// z_owned_reply_channel_closure_t -// -------------------------------- -// typedef struct z_owned_reply_channel_closure_t { -// void *context; -// bool (*call)(struct z_owned_reply_t*, void*); -// void (*drop)(void*); -// } z_owned_reply_channel_closure_t; -// -------------------------------- -internal unsafe delegate void ZOwnedReplayChannelClosureCall(ZOwnedReply* zOwnedReply, void* context); - -internal unsafe delegate void ZOwnedReplyChannelClosureDrop(void* context); - -[StructLayout(LayoutKind.Sequential)] -internal unsafe struct ZOwnedReplyChannelClosure +// zenoh_opaque.h +// ze_owned_serializer_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZeOwnedSerializer { - internal void* context; - internal ZOwnedReplayChannelClosureCall call; - internal ZOwnedReplyChannelClosureDrop drop; + private fixed byte data[56]; } -// z_owned_reply_channel_t -// -------------------------------- -// typedef struct z_owned_reply_channel_t { -// struct z_owned_closure_reply_t send; -// struct z_owned_reply_channel_closure_t recv; -// } z_owned_reply_channel_t; -// -------------------------------- -[StructLayout(LayoutKind.Sequential)] -internal struct ZOwnedReplyChannel +// zenoh_opaque.h +// ze_loaned_serializer_t +[StructLayout(LayoutKind.Sequential, Pack = 8)] +internal unsafe struct ZeLoanedSerializer { - internal ZOwnedClosureReply send; - internal ZOwnedReplyChannelClosure recv; + private fixed byte data[56]; } +// zenoh_commons.h +// ze_moved_serializer_t [StructLayout(LayoutKind.Sequential)] -public struct ConsolidationStrategy // z_consolidation_strategy_t +internal struct ZeMovedSerializer { - public ConsolidationMode firstRouters; - public ConsolidationMode lastRouters; - public ConsolidationMode reception; + internal ZeOwnedSerializer ownedSerializer; } internal static unsafe class ZenohC @@ -1348,25 +1265,72 @@ internal static unsafe class ZenohC internal static string zConfigMulticastIpv4AddressKey = "scouting/multicast/address"; internal static string zConfigScoutingTimeoutKey = "scouting/timeout"; internal static string zConfigScoutingDelayKey = "scouting/delay"; - internal static string zConfigAddTimestampKey = "add_timestamp"; - - internal static string ZOwnedStrToString(ZOwnedStr* zs) - { - if (z_str_check(zs) != 1) return ""; - - return Marshal.PtrToStringUTF8(zs->cstr) ?? ""; - } - - internal static string ZKeyexprToString(ZKeyexpr keyexpr) - { - var str = z_keyexpr_to_string(keyexpr); - var o = ZOwnedStrToString(&str); - z_str_drop(&str); - return o; - } - - [DllImport(DllName, EntryPoint = "z_bytes_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_bytes_check(ZBytes* b); + internal static string zConfigAddTimestampKey = "timestamping/enabled"; + + // internal static string ZOwnedStrToString(ZOwnedStr* zs) + // { + // if (z_str_check(zs) != 1) return ""; + // + // return Marshal.PtrToStringUTF8(zs->cstr) ?? ""; + // } + // + // internal static string ZKeyexprToString(ZKeyexpr keyexpr) + // { + // var str = z_keyexpr_to_string(keyexpr); + // var o = ZOwnedStrToString(&str); + // z_str_drop(&str); + // return o; + // } + + [DllImport(DllName, EntryPoint = "z_bytes_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + internal static extern void z_bytes_clone(ZOwnedBytes* dst, ZLoanedBytes* src); + + [DllImport(DllName, EntryPoint = "z_bytes_copy_from_buf", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + internal static extern void z_bytes_copy_from_buf(ZOwnedBytes* dst, byte* src, ulong len); + + [DllImport(DllName, EntryPoint = "z_bytes_copy_from_slice", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + internal static extern void z_bytes_copy_from_slice(ZOwnedBytes* dst, ZLoanedSlice* src); + + [DllImport(DllName, EntryPoint = "z_bytes_copy_from_str", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + internal static extern void z_bytes_copy_from_str(ZOwnedBytes* dst, byte* src); + + [DllImport(DllName, EntryPoint = "z_bytes_copy_from_string", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + internal static extern void z_bytes_copy_from_string(ZOwnedBytes* dst, ZLoanedString* src); + + [DllImport(DllName, EntryPoint = "z_bytes_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + internal static extern void z_bytes_drop(ZMovedBytes* t); + + [DllImport(DllName, EntryPoint = "z_bytes_empty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + internal static extern void z_bytes_empty(ZOwnedBytes* t); + + [DllImport(DllName, EntryPoint = "z_bytes_from_buf", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + internal static extern ZResult z_bytes_from_buf(ZOwnedBytes* dst, byte* src, ulong len, void* deleter, + void* context); + + [DllImport(DllName, EntryPoint = "z_bytes_from_slice", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + internal static extern void z_bytes_from_slice(ZOwnedBytes* dst, ZMovedSlice* src); + + [DllImport(DllName, EntryPoint = "z_bytes_from_static_buf", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZResult z_bytes_from_static_buf(ZOwnedBytes* dst, byte* src, ulong len); + + [DllImport(DllName, EntryPoint = "z_bytes_from_static_str", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZResult z_bytes_from_static_str(ZOwnedBytes* dst, byte* src); + + [DllImport(DllName, EntryPoint = "z_bytes_from_str", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZResult z_bytes_from_str(ZOwnedBytes* dst, byte* src, void* deleter, void* context); + + [DllImport(DllName, EntryPoint = "z_bytes_from_string", CallingConvention = CallingConvention.Cdecl)] + internal static extern void z_bytes_from_string(ZOwnedBytes* dst, ZMovedString* src); + + [DllImport(DllName, EntryPoint = "z_bytes_get_reader", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZBytesReader z_bytes_get_reader(ZLoanedBytes* data); + + [DllImport(DllName, EntryPoint = "z_bytes_get_slice_iterator", CallingConvention = CallingConvention.Cdecl)] + internal static extern ZBytesSliceIterator z_bytes_get_slice_iterator(ZLoanedBytes* data); + + [DllImport(DllName, EntryPoint = "z_bytes_is_empty", CallingConvention = CallingConvention.Cdecl)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_bytes_is_empty(ZLoanedBytes* data); [DllImport(DllName, EntryPoint = "z_str_array_check", CallingConvention = CallingConvention.Cdecl)] internal static extern sbyte z_str_array_check(ZOwnedStrArray* strs); @@ -1735,4 +1699,5 @@ internal static extern sbyte z_query_reply( [DllImport(DllName, EntryPoint = "z_reply_channel_closure_call", CallingConvention = CallingConvention.Cdecl)] internal static extern sbyte z_reply_channel_closure_call(ZOwnedReplyChannelClosure* closure, ZOwnedReply* reply); + } \ No newline at end of file From f5114fb50c262fe12747d61c488c97a7f2ccf2aa Mon Sep 17 00:00:00 2001 From: sanri Date: Thu, 23 Jan 2025 19:12:20 +0800 Subject: [PATCH 33/34] 20250123 --- Zenoh/ZenohC.cs | 1769 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 1439 insertions(+), 330 deletions(-) diff --git a/Zenoh/ZenohC.cs b/Zenoh/ZenohC.cs index 268249e..0254a7e 100644 --- a/Zenoh/ZenohC.cs +++ b/Zenoh/ZenohC.cs @@ -1,5 +1,11 @@ #pragma warning disable CS8500 +// Marshal.GetFunctionPointerForDelegate +// 将委托转换为可从非托管代码调用的函数指针 +// +// Marshal.GetDelegateForFunctionPointer +// 将非托管函数指针转换为委托 + using System.Runtime.InteropServices; namespace Zenoh; @@ -8,14 +14,14 @@ namespace Zenoh; // zenoh_commons.h // z_congestion_control_t -public enum CongestionControl: uint +public enum CongestionControl : uint { Block = 0, Drop = 1 } // z_encoding_prefix_t -public enum EncodingPrefix: uint +public enum EncodingPrefix : uint { Empty = 0, AppOctetStream = 1, @@ -42,7 +48,7 @@ public enum EncodingPrefix: uint // zenoh_commons.h // z_sample_kind_t -public enum SampleKind: uint +public enum SampleKind : uint { Put = 0, Delete = 1 @@ -50,7 +56,7 @@ public enum SampleKind: uint // zenoh_commons.h // z_priority_t -public enum Priority: uint +public enum Priority : uint { RealTime = 1, InteractiveHigh = 2, @@ -63,7 +69,7 @@ public enum Priority: uint // zenoh_commons.h // z_consolidation_mode_t -public enum ConsolidationMode: int +public enum ConsolidationMode : int { Auto = -1, None = 0, @@ -72,7 +78,7 @@ public enum ConsolidationMode: int } // z_query_consolidation_t -public enum QueryConsolidation: int +public enum QueryConsolidation : int { Auto = -1, None = 0, @@ -81,7 +87,7 @@ public enum QueryConsolidation: int } // z_reliability_t -public enum Reliability: uint +public enum Reliability : uint { BestEffort, Reliable @@ -89,7 +95,7 @@ public enum Reliability: uint // zenoh_commons.h // z_query_target_t -public enum QueryTarget: uint +public enum QueryTarget : uint { BestMatching = 0, All = 1, @@ -98,7 +104,7 @@ public enum QueryTarget: uint // zenoh_commons.h // z_what_t -public enum What: uint +public enum What : uint { Router = 1, Peer = 2, @@ -111,7 +117,7 @@ public enum What: uint // zenoh_commons.h // z_whatami_t -public enum Whatami: uint +public enum Whatami : uint { Router = 1, Peer = 2, @@ -120,7 +126,7 @@ public enum Whatami: uint // zenoh_commons.h // zc_locality_t -public enum ZcLocality: uint +public enum ZcLocality : uint { Any = 0, Local = 1, @@ -129,7 +135,7 @@ public enum ZcLocality: uint // zenoh_commons.h // zc_log_severity_t -public enum ZcLogSeverity: uint +public enum ZcLogSeverity : uint { Trace = 0, Debug = 1, @@ -305,18 +311,18 @@ internal unsafe struct ZOwnedClosureHello { internal void* context; internal delegate* unmanaged[Cdecl] call; - internal ZOwnedClosureHelloDrop? drop; + internal delegate* unmanaged[Cdecl] drop; } // internal unsafe delegate void ZOwnedClosureHelloCall(ZLoanedHello* hello, void* context); -internal unsafe delegate void ZOwnedClosureHelloDrop(void* context); +// internal unsafe delegate void ZOwnedClosureHelloDrop(void* context); // zenoh_opaque.h // z_loaned_closure_hello_t -[StructLayout(LayoutKind.Sequential, Pack = 8)] +[StructLayout(LayoutKind.Sequential)] internal unsafe struct ZLoanedClosureHello { - private fixed byte data[3]; + private fixed nuint data[3]; } // zenoh_commons.h @@ -357,20 +363,19 @@ internal struct ZMovedQuery internal unsafe struct ZOwnedClosureQuery { internal void* context; - internal ZOwnedClosureQueryCall call; - internal ZOwnedClosureQueryDrop? drop; + internal delegate* unmanaged[Cdecl] call; + internal delegate* unmanaged[Cdecl] drop; } -internal unsafe delegate void ZOwnedClosureQueryCall(ZLoanedQuery* query, void* context); - -internal unsafe delegate void ZOwnedClosureQueryDrop(void* context); +// internal unsafe delegate void ZOwnedClosureQueryCall(ZLoanedQuery* query, void* context); +// internal unsafe delegate void ZOwnedClosureQueryDrop(void* context); // zenoh_opaque.h // z_loaned_closure_query_t [StructLayout(LayoutKind.Sequential, Pack = 8)] internal unsafe struct ZLoanedClosureQuery { - private fixed ulong data[3]; + private fixed nuint data[3]; } // zenoh_commons.h @@ -435,20 +440,19 @@ internal struct ZMovedReplyErr internal unsafe struct ZOwnedClosureReply { internal void* context; - internal ZOwnedClosureReplyCall call; - internal ZOwnedClosureReplyDrop? drop; + internal delegate* unmanaged[Cdecl] call; + internal delegate* unmanaged[Cdecl] drop; } -internal unsafe delegate void ZOwnedClosureReplyCall(ZLoanedReply* reply, void* context); - -internal unsafe delegate void ZOwnedClosureReplyDrop(void* context); +// internal unsafe delegate void ZOwnedClosureReplyCall(ZLoanedReply* reply, void* context); +// internal unsafe delegate void ZOwnedClosureReplyDrop(void* context); // zenoh_opaque.h // z_loaned_closure_reply_t [StructLayout(LayoutKind.Sequential, Pack = 8)] internal unsafe struct ZLoanedClosureReply { - private fixed ulong data[3]; + private fixed nuint data[3]; } // zenoh_commons.h @@ -489,20 +493,19 @@ internal struct ZMovedSample internal unsafe struct ZOwnedClosureSample { internal void* context; - internal ZOwnedClosureSampleCall call; - internal ZOwnedClosureSampleDrop? drop; + internal delegate* unmanaged[Cdecl] call; + internal delegate* unmanaged[Cdecl] drop; } -internal unsafe delegate void ZOwnedClosureSampleCall(ZLoanedSample* reply, void* context); - -internal unsafe delegate void ZOwnedClosureSampleDrop(void* context); +// internal unsafe delegate void ZOwnedClosureSampleCall(ZLoanedSample* reply, void* context); +// internal unsafe delegate void ZOwnedClosureSampleDrop(void* context); // zenoh_opaque.h // z_loaned_closure_sample_t [StructLayout(LayoutKind.Sequential, Pack = 8)] internal unsafe struct ZLoanedClosureSample { - private fixed ulong data[3]; + private fixed nuint data[3]; } // zenoh_commons.h @@ -527,20 +530,19 @@ internal unsafe struct ZId internal unsafe struct ZOwnedClosureZid { internal void* context; - internal ZOwnedClosureZidCall call; - internal ZOwnedClosureZidDrop? drop; + internal delegate* unmanaged[Cdecl] call; + internal delegate* unmanaged[Cdecl] drop; } -internal unsafe delegate void ZOwnedClosureZidCall(ZId* zId, void* context); - -internal unsafe delegate void ZOwnedClosureZidDrop(void* context); +// internal unsafe delegate void ZOwnedClosureZidCall(ZId* zId, void* context); +// internal unsafe delegate void ZOwnedClosureZidDrop(void* context); // zenoh_opaque.h // z_loaned_closure_zid_t [StructLayout(LayoutKind.Sequential, Pack = 8)] internal unsafe struct ZLoanedClosureZid { - private fixed ulong data[3]; + private fixed nuint data[3]; } // zenoh_commons.h @@ -548,7 +550,7 @@ internal unsafe struct ZLoanedClosureZid [StructLayout(LayoutKind.Sequential)] internal struct ZMovedClosureZid { - internal ZOwnedClosureZid _ownedClosureZid; + internal ZOwnedClosureZid owned_closure_zid; } // zenoh_opaque.h @@ -572,7 +574,7 @@ internal unsafe struct ZLoanedCondvar [StructLayout(LayoutKind.Sequential)] internal struct ZMovedCondvar { - internal ZOwnedCondvar _ownedCondvar; + internal ZOwnedCondvar owned_condvar; } // zenoh_opaque.h @@ -599,6 +601,23 @@ internal struct ZTime internal ulong t; } +// zenoh_commons.h +// z_clock_t +[StructLayout(LayoutKind.Sequential)] +internal unsafe struct ZClock +{ + internal ulong t; + internal void* t_base; +} + +// zenoh_commons.h +// z_close_options_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZCloseOptions +{ + internal byte dummy; +} + // zenoh_opaque.h // z_owned_config_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -1020,7 +1039,7 @@ internal struct ZMovedTask [StructLayout(LayoutKind.Sequential)] internal struct ZTaskAttr { - internal ulong attr; + internal nuint attr; } // zenoh_commons.h @@ -1037,7 +1056,7 @@ internal struct ZOpenOptions internal struct ZScoutOptions { internal ulong timeout_ms; - internal What what; + internal What What; } // public string PrefixToString() @@ -1061,8 +1080,7 @@ internal unsafe struct ZPublisherOptions internal ZMovedEncoding* encoding; internal CongestionControl congestion_control; internal Priority priority; - [MarshalAs(UnmanagedType.U1)] - internal bool is_express; + [MarshalAs(UnmanagedType.U1)] internal bool is_express; } // zenoh_commons.h @@ -1131,8 +1149,7 @@ internal unsafe struct ZDeleteOptions { internal CongestionControl congestion_control; internal Priority priority; - [MarshalAs(UnmanagedType.U1)] - internal bool is_express; + [MarshalAs(UnmanagedType.U1)] internal bool is_express; internal ZTimestamp* timestamp; } @@ -1154,8 +1171,7 @@ internal unsafe struct ZGetOptions internal ZMovedBytes* payload; internal ZMovedEncoding* encoding; internal CongestionControl congestion_control; - [MarshalAs(UnmanagedType.U1)] - internal bool is_express; + [MarshalAs(UnmanagedType.U1)] internal bool is_express; internal ZMovedBytes* attachment; internal ulong timeout_ms; } @@ -1168,8 +1184,7 @@ internal unsafe struct ZPutOptions internal ZMovedEncoding* encoding; internal CongestionControl congestionControl; internal Priority priority; - [MarshalAs(UnmanagedType.U1)] - internal bool is_express; + [MarshalAs(UnmanagedType.U1)] internal bool is_express; internal ZTimestamp* timestamp; internal ZMovedBytes* attachment; } @@ -1182,8 +1197,7 @@ internal unsafe struct ZQueryReplyOptions internal ZMovedEncoding* encoding; internal CongestionControl congestionControl; internal Priority priority; - [MarshalAs(UnmanagedType.U1)] - internal bool is_express; + [MarshalAs(UnmanagedType.U1)] internal bool is_express; internal ZTimestamp* timestamp; internal ZMovedBytes* attachment; } @@ -1195,8 +1209,7 @@ internal unsafe struct ZQueryReplyDelOptions { internal CongestionControl congestionControl; internal Priority priority; - [MarshalAs(UnmanagedType.U1)] - internal bool is_express; + [MarshalAs(UnmanagedType.U1)] internal bool is_express; internal ZTimestamp* timestamp; internal ZMovedBytes* attachment; } @@ -1214,7 +1227,7 @@ internal unsafe struct ZQueryReplyErrOptions [StructLayout(LayoutKind.Sequential, Pack = 8)] internal unsafe struct ZcLoanedClosureLog { - private fixed ulong data[3]; + private fixed nuint data[3]; } // zenoh_opaque.h @@ -1282,422 +1295,1518 @@ internal static unsafe class ZenohC // return o; // } - [DllImport(DllName, EntryPoint = "z_bytes_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [DllImport(DllName, EntryPoint = "z_bytes_clone", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] internal static extern void z_bytes_clone(ZOwnedBytes* dst, ZLoanedBytes* src); - [DllImport(DllName, EntryPoint = "z_bytes_copy_from_buf", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - internal static extern void z_bytes_copy_from_buf(ZOwnedBytes* dst, byte* src, ulong len); + [DllImport(DllName, EntryPoint = "z_bytes_copy_from_buf", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_bytes_copy_from_buf(ZOwnedBytes* dst, byte* src, nuint len); - [DllImport(DllName, EntryPoint = "z_bytes_copy_from_slice", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [DllImport(DllName, EntryPoint = "z_bytes_copy_from_slice", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] internal static extern void z_bytes_copy_from_slice(ZOwnedBytes* dst, ZLoanedSlice* src); - [DllImport(DllName, EntryPoint = "z_bytes_copy_from_str", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [DllImport(DllName, EntryPoint = "z_bytes_copy_from_str", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] internal static extern void z_bytes_copy_from_str(ZOwnedBytes* dst, byte* src); - [DllImport(DllName, EntryPoint = "z_bytes_copy_from_string", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [DllImport(DllName, EntryPoint = "z_bytes_copy_from_string", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] internal static extern void z_bytes_copy_from_string(ZOwnedBytes* dst, ZLoanedString* src); [DllImport(DllName, EntryPoint = "z_bytes_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern void z_bytes_drop(ZMovedBytes* t); - [DllImport(DllName, EntryPoint = "z_bytes_empty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [DllImport(DllName, EntryPoint = "z_bytes_empty", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] internal static extern void z_bytes_empty(ZOwnedBytes* t); - [DllImport(DllName, EntryPoint = "z_bytes_from_buf", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] - internal static extern ZResult z_bytes_from_buf(ZOwnedBytes* dst, byte* src, ulong len, void* deleter, + [DllImport(DllName, EntryPoint = "z_bytes_from_buf", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_bytes_from_buf(ZOwnedBytes* dst, byte* src, nuint len, void* deleter, void* context); - [DllImport(DllName, EntryPoint = "z_bytes_from_slice", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [DllImport(DllName, EntryPoint = "z_bytes_from_slice", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] internal static extern void z_bytes_from_slice(ZOwnedBytes* dst, ZMovedSlice* src); - [DllImport(DllName, EntryPoint = "z_bytes_from_static_buf", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZResult z_bytes_from_static_buf(ZOwnedBytes* dst, byte* src, ulong len); - - [DllImport(DllName, EntryPoint = "z_bytes_from_static_str", CallingConvention = CallingConvention.Cdecl)] + [DllImport(DllName, EntryPoint = "z_bytes_from_static_buf", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_bytes_from_static_buf(ZOwnedBytes* dst, byte* src, nuint len); + + [DllImport(DllName, EntryPoint = "z_bytes_from_static_str", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] internal static extern ZResult z_bytes_from_static_str(ZOwnedBytes* dst, byte* src); - - [DllImport(DllName, EntryPoint = "z_bytes_from_str", CallingConvention = CallingConvention.Cdecl)] + + [DllImport(DllName, EntryPoint = "z_bytes_from_str", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] internal static extern ZResult z_bytes_from_str(ZOwnedBytes* dst, byte* src, void* deleter, void* context); - - [DllImport(DllName, EntryPoint = "z_bytes_from_string", CallingConvention = CallingConvention.Cdecl)] + + [DllImport(DllName, EntryPoint = "z_bytes_from_string", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] internal static extern void z_bytes_from_string(ZOwnedBytes* dst, ZMovedString* src); - - [DllImport(DllName, EntryPoint = "z_bytes_get_reader", CallingConvention = CallingConvention.Cdecl)] + + [DllImport(DllName, EntryPoint = "z_bytes_get_reader", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] internal static extern ZBytesReader z_bytes_get_reader(ZLoanedBytes* data); - - [DllImport(DllName, EntryPoint = "z_bytes_get_slice_iterator", CallingConvention = CallingConvention.Cdecl)] + + [DllImport(DllName, EntryPoint = "z_bytes_get_slice_iterator", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] internal static extern ZBytesSliceIterator z_bytes_get_slice_iterator(ZLoanedBytes* data); - - [DllImport(DllName, EntryPoint = "z_bytes_is_empty", CallingConvention = CallingConvention.Cdecl)] + + [DllImport(DllName, EntryPoint = "z_bytes_is_empty", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] [return: MarshalAs(UnmanagedType.U1)] internal static extern bool z_bytes_is_empty(ZLoanedBytes* data); - [DllImport(DllName, EntryPoint = "z_str_array_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_str_array_check(ZOwnedStrArray* strs); + [DllImport(DllName, EntryPoint = "z_bytes_len", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + internal static extern nuint z_bytes_len(ZLoanedBytes* data); - [DllImport(DllName, EntryPoint = "z_str_array_drop", CallingConvention = CallingConvention.Cdecl)] - internal static extern void z_str_array_drop(ZOwnedStrArray* strs); + [DllImport(DllName, EntryPoint = "z_bytes_loan", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + internal static extern ZLoanedBytes* z_bytes_loan(ZOwnedBytes* data); - [DllImport(DllName, EntryPoint = "z_str_array_loan", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZStrArray z_str_array_loan(ZOwnedStrArray* strs); + [DllImport(DllName, EntryPoint = "z_bytes_loan_mut", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedBytes* z_bytes_loan_mut(ZOwnedBytes* data); - [DllImport(DllName, EntryPoint = "z_str_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_str_check(ZOwnedStr* s); + [DllImport(DllName, EntryPoint = "z_bytes_reader_read", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern nuint z_bytes_reader_read(ZBytesReader* reader, byte* dst, nuint len); - [DllImport(DllName, EntryPoint = "z_str_drop", CallingConvention = CallingConvention.Cdecl)] - internal static extern void z_str_drop(ZOwnedStr* s); + [DllImport(DllName, EntryPoint = "z_bytes_reader_remaining", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern nuint z_bytes_reader_remaining(ZBytesReader* reader); - [DllImport(DllName, EntryPoint = "z_str_loan", CallingConvention = CallingConvention.Cdecl)] - internal static extern byte* z_str_loan(ZOwnedStr* s); + [DllImport(DllName, EntryPoint = "z_bytes_reader_seek", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_bytes_reader_seek(ZBytesReader* reader, long offset, int origin); - [DllImport(DllName, EntryPoint = "z_str_null", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedStr z_str_null(); + [DllImport(DllName, EntryPoint = "z_bytes_reader_tell", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern long z_bytes_reader_tell(ZBytesReader* reader); - [DllImport(DllName, EntryPoint = "z_config_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_config_check(ZOwnedConfig* config); + [DllImport(DllName, EntryPoint = "z_bytes_slice_iterator_next", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_bytes_slice_iterator_next(ZBytesSliceIterator* iter, ZViewSlice* slice); + + [DllImport(DllName, EntryPoint = "z_bytes_to_slice", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_bytes_to_slice(ZLoanedBytes* bytes, ZOwnedSlice* dst); + + [DllImport(DllName, EntryPoint = "z_bytes_to_string", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_bytes_to_string(ZLoanedBytes* bytes, ZOwnedString* dst); + + [DllImport(DllName, EntryPoint = "z_bytes_writer_append", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_bytes_writer_append(ZLoanedBytesWriter* writer, ZMovedBytes* bytes); + + [DllImport(DllName, EntryPoint = "z_bytes_writer_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_bytes_writer_drop(ZMovedBytesWriter* writer); + + [DllImport(DllName, EntryPoint = "z_bytes_writer_empty", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_bytes_writer_empty(ZOwnedBytesWriter* writer); + + [DllImport(DllName, EntryPoint = "z_bytes_writer_finish", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_bytes_writer_finish(ZMovedBytesWriter* writer, ZOwnedBytes* bytes); + + [DllImport(DllName, EntryPoint = "z_bytes_writer_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedBytesWriter* z_bytes_writer_loan(ZOwnedBytesWriter* writer); + + [DllImport(DllName, EntryPoint = "z_bytes_writer_loan_mut", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedBytesWriter* z_bytes_writer_loan_mut(ZOwnedBytesWriter* writer); + + [DllImport(DllName, EntryPoint = "z_bytes_writer_write_all", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_bytes_writer_write_all(ZLoanedBytesWriter* writer, byte* src, nuint len); + + [DllImport(DllName, EntryPoint = "z_clock_elapsed_s", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ulong z_clock_elapsed_s(ZClock* time); + + [DllImport(DllName, EntryPoint = "z_clock_elapsed_ms", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ulong z_clock_elapsed_ms(ZClock* time); + + [DllImport(DllName, EntryPoint = "z_clock_elapsed_us", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ulong z_clock_elapsed_us(ZClock* time); + + [DllImport(DllName, EntryPoint = "z_clock_now", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + internal static extern ZClock z_clock_now(); + + [DllImport(DllName, EntryPoint = "z_close", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + internal static extern ZResult z_close(ZLoanedSession* session, ZCloseOptions* options); + + [DllImport(DllName, EntryPoint = "z_close_options_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_close_options_default(ZCloseOptions* options); + + [DllImport(DllName, EntryPoint = "z_closure_hello", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_closure_hello( + ZOwnedClosureHello* closure, + delegate* call, + delegate* drop, + void* context + ); + + [DllImport(DllName, EntryPoint = "z_closure_hello_call", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_closure_hello_call(ZLoanedClosureHello* closure, ZLoanedHello* hello); + + [DllImport(DllName, EntryPoint = "z_closure_hello_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_closure_hello_drop(ZMovedClosureHello* closure); + + [DllImport(DllName, EntryPoint = "z_closure_hello_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedClosureHello* z_closure_hello_loan(ZOwnedClosureHello* closure); + + [DllImport(DllName, EntryPoint = "z_closure_hello_loan_mut", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedClosureHello* z_closure_hello_loan_mut(ZOwnedClosureHello* closure); + + [DllImport(DllName, EntryPoint = "z_closure_query", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_closure_query( + ZOwnedClosureQuery* closure, + delegate* call, + delegate* drop, + void* context + ); + + [DllImport(DllName, EntryPoint = "z_closure_query_call", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_closure_query_call(ZLoanedClosureQuery* closure, ZLoanedQuery* query); + + [DllImport(DllName, EntryPoint = "z_closure_query_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_closure_query_drop(ZMovedClosureQuery* closure); + + [DllImport(DllName, EntryPoint = "z_closure_query_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedClosureQuery* z_closure_query_loan(ZOwnedClosureQuery* closure); + + [DllImport(DllName, EntryPoint = "z_closure_query_loan_mut", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedClosureQuery* z_closure_query_loan_mut(ZOwnedClosureQuery* closure); + + [DllImport(DllName, EntryPoint = "z_closure_reply", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_closure_reply( + ZOwnedClosureReply* closure, + delegate* call, + delegate* drop, + void* context + ); + + [DllImport(DllName, EntryPoint = "z_closure_reply_call", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_closure_reply_call(ZLoanedClosureReply* closure, ZLoanedReply* reply); + + [DllImport(DllName, EntryPoint = "z_closure_reply_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_closure_reply_drop(ZMovedClosureReply* closure); + + [DllImport(DllName, EntryPoint = "z_closure_reply_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedClosureReply* z_closure_reply_loan(ZOwnedClosureReply* closure); + + [DllImport(DllName, EntryPoint = "z_closure_reply_loan_mut", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedClosureReply* z_closure_reply_loan_mut(ZOwnedClosureReply* closure); + + [DllImport(DllName, EntryPoint = "z_closure_sample", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_closure_sample( + ZOwnedClosureSample* closure, + delegate* call, + delegate* drop, + void* context + ); + + [DllImport(DllName, EntryPoint = "z_closure_sample_call", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_closure_sample_call(ZLoanedClosureSample* closure, ZLoanedSample* sample); + + [DllImport(DllName, EntryPoint = "z_closure_sample_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_closure_sample_drop(ZMovedClosureSample* closure); + + [DllImport(DllName, EntryPoint = "z_closure_sample_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedClosureSample* z_closure_sample_loan(ZOwnedClosureSample* closure); + + [DllImport(DllName, EntryPoint = "z_closure_sample_loan_mut", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedClosureSample* z_closure_sample_loan_mut(ZOwnedClosureSample* closure); + + [DllImport(DllName, EntryPoint = "z_closure_zid", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_closure_zid( + ZOwnedClosureZid* closure, + delegate* call, + delegate* drop, + void* context + ); + + [DllImport(DllName, EntryPoint = "z_closure_zid_call", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_closure_zid_call(ZLoanedClosureZid* closure, ZId* zId); + + [DllImport(DllName, EntryPoint = "z_closure_zid_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_closure_zid_drop(ZMovedClosureZid* closure); + + [DllImport(DllName, EntryPoint = "z_closure_zid_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedClosureZid* z_closure_zid_loan(ZOwnedClosureZid* closure); + + [DllImport(DllName, EntryPoint = "z_closure_zid_loan_mut", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedClosureZid* z_closure_zid_loan_mut(ZOwnedClosureZid* closure); + + [DllImport(DllName, EntryPoint = "z_condvar_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_condvar_drop(ZMovedCondvar* condvar); + + [DllImport(DllName, EntryPoint = "z_condvar_init", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_condvar_init(ZOwnedCondvar* condvar); + + [DllImport(DllName, EntryPoint = "z_condvar_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedCondvar* z_condvar_loan(ZOwnedCondvar* condvar); + + [DllImport(DllName, EntryPoint = "z_condvar_loan_mut", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedCondvar* z_condvar_loan_mut(ZOwnedCondvar* condvar); + + [DllImport(DllName, EntryPoint = "z_condvar_signal", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_condvar_signal(ZLoanedCondvar* condvar); + + [DllImport(DllName, EntryPoint = "z_condvar_wait", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_condvar_wait(ZLoanedCondvar* condvar, ZLoanedMutex* mutex); + + [DllImport(DllName, EntryPoint = "z_config_clone", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_config_clone(ZOwnedConfig* dst, ZLoanedConfig* src); + + [DllImport(DllName, EntryPoint = "z_config_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_config_default(ZOwnedConfig* config); + + [DllImport(DllName, EntryPoint = "z_config_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_config_drop(ZMovedConfig* config); + + [DllImport(DllName, EntryPoint = "z_config_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedConfig* z_config_loan(ZOwnedConfig* config); + + [DllImport(DllName, EntryPoint = "z_config_loan_mut", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedConfig* z_config_loan_mut(ZOwnedConfig* config); + + [DllImport(DllName, EntryPoint = "z_declare_background_queryable", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_declare_background_queryable( + ZLoanedSession* session, + ZLoanedKeyexpr* keyexpr, + ZMovedClosureQuery* callback, + ZQueryableOptions* options + ); + + [DllImport(DllName, EntryPoint = "z_declare_background_subscriber", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_declare_background_subscriber( + ZLoanedSession* session, + ZLoanedKeyexpr* keyexpr, + ZMovedClosureSample* callback, + ZSubscriberOptions* options + ); + + [DllImport(DllName, EntryPoint = "z_declare_keyexpr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_declare_keyexpr( + ZLoanedSession* session, + ZOwnedKeyexpr* declaredKeyexpr, + ZLoanedKeyexpr* keyexpr + ); + + [DllImport(DllName, EntryPoint = "z_declare_publisher", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_declare_publisher( + ZLoanedSession* session, + ZOwnedPublisher* publisher, + ZLoanedKeyexpr* keyexpr, + ZPublisherOptions* options + ); + + [DllImport(DllName, EntryPoint = "z_declare_queryable", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_declare_queryable( + ZLoanedSession* session, + ZOwnedQueryable* queryable, + ZLoanedKeyexpr* keyexpr, + ZMovedClosureQuery* callback, + ZQueryableOptions* options + ); + + [DllImport(DllName, EntryPoint = "z_declare_subscriber", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_declare_subscriber( + ZLoanedSession* session, + ZOwnedSubscriber* subscriber, + ZLoanedKeyexpr* keyexpr, + ZMovedClosureSample* callback, + ZSubscriberOptions* options + ); + + [DllImport(DllName, EntryPoint = "z_delete", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_delete(ZLoanedSession* session, ZLoanedKeyexpr* keyexpr, ZDeleteOptions* options); + + [DllImport(DllName, EntryPoint = "z_delete_options_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_delete_options_default(ZDeleteOptions* options); + + [DllImport(DllName, EntryPoint = "z_encoding_application_cbor", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_cbor(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_cdr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_cdr(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_coap_payload", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_coap_payload(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_java_serialized_object", + CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_java_serialized_object(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_json", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_json(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_json_patch_json", + CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_json_patch_json(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_json_seq", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_json_seq(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_jsonpath", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_jsonpath(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_jwt", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_jwt(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_mp4", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_mp4(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_octet_stream", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_octet_stream(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_openmetrics_text", + CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_openmetrics_text(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_protobuf", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_protobuf(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_python_serialized_object", + CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_python_serialized_object(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_soap_xml", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_soap_xml(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_sql", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_sql(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_x_www_form_urlencoded", + CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_x_www_form_urlencoded(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_xml", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_xml(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_yaml", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_yaml(); + + [DllImport(DllName, EntryPoint = "z_encoding_application_yang", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_application_yang(); + + [DllImport(DllName, EntryPoint = "z_encoding_audio_aac", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_audio_aac(); + + [DllImport(DllName, EntryPoint = "z_encoding_audio_flac", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_audio_flac(); + + [DllImport(DllName, EntryPoint = "z_encoding_audio_mp4", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_audio_mp4(); + + [DllImport(DllName, EntryPoint = "z_encoding_audio_ogg", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_audio_ogg(); + + [DllImport(DllName, EntryPoint = "z_encoding_audio_vorbis", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_audio_vorbis(); + + [DllImport(DllName, EntryPoint = "z_encoding_image_bmp", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_image_bmp(); + + [DllImport(DllName, EntryPoint = "z_encoding_image_gif", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_image_gif(); + + [DllImport(DllName, EntryPoint = "z_encoding_image_jpeg", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_image_jpeg(); - [DllImport(DllName, EntryPoint = "z_config_drop", CallingConvention = CallingConvention.Cdecl)] - internal static extern void z_config_drop(ZOwnedConfig* config); + [DllImport(DllName, EntryPoint = "z_encoding_image_png", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_image_png(); - [DllImport(DllName, EntryPoint = "z_config_loan", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZConfig z_config_loan(ZOwnedConfig* config); + [DllImport(DllName, EntryPoint = "z_encoding_image_webp", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_image_webp(); - [DllImport(DllName, EntryPoint = "z_config_default", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedConfig z_config_default(); + [DllImport(DllName, EntryPoint = "z_encoding_text_css", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_text_css(); - [DllImport(DllName, EntryPoint = "z_config_new", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedConfig z_config_new(); + [DllImport(DllName, EntryPoint = "z_encoding_text_csv", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_text_csv(); - [DllImport(DllName, EntryPoint = "z_config_null", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedConfig z_config_null(); + [DllImport(DllName, EntryPoint = "z_encoding_text_html", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_text_html(); - [DllImport(DllName, EntryPoint = "z_config_client", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedConfig z_config_client(string[] peers, nuint nPeers); + [DllImport(DllName, EntryPoint = "z_encoding_text_javascript", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_text_javascript(); - [DllImport(DllName, EntryPoint = "z_config_peer", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedConfig z_config_peer(); + [DllImport(DllName, EntryPoint = "z_encoding_text_json", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_text_json(); - [DllImport(DllName, EntryPoint = "zc_config_from_file", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedConfig zc_config_from_file([MarshalAs(UnmanagedType.LPStr)] string path); + [DllImport(DllName, EntryPoint = "z_encoding_text_json5", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_text_json5(); - [DllImport(DllName, EntryPoint = "zc_config_from_str", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedConfig zc_config_from_str([MarshalAs(UnmanagedType.LPStr)] string s); + [DllImport(DllName, EntryPoint = "z_encoding_text_markdown", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_text_markdown(); - [DllImport(DllName, EntryPoint = "zc_config_get", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedStr zc_config_get(ZConfig config, [MarshalAs(UnmanagedType.LPStr)] string key); + [DllImport(DllName, EntryPoint = "z_encoding_text_plain", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_text_plain(); - [DllImport(DllName, EntryPoint = "zc_config_insert_json", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte zc_config_insert_json( - ZConfig config, [MarshalAs(UnmanagedType.LPStr)] string key, [MarshalAs(UnmanagedType.LPStr)] string value); + [DllImport(DllName, EntryPoint = "z_encoding_text_xml", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_text_xml(); - [DllImport(DllName, EntryPoint = "zc_config_to_string", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedStr zc_config_to_string(ZConfig config); + [DllImport(DllName, EntryPoint = "z_encoding_text_yaml", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_text_yaml(); - [DllImport(DllName, EntryPoint = "z_encoding", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZEncoding z_encoding(EncodingPrefix prefix, byte* suffix); + [DllImport(DllName, EntryPoint = "z_encoding_video_h261", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_video_h261(); - [DllImport(DllName, EntryPoint = "z_encoding_default", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZEncoding z_encoding_default(); + [DllImport(DllName, EntryPoint = "z_encoding_video_h263", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_video_h263(); - [DllImport(DllName, EntryPoint = "z_encoding_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_encoding_check(ZOwnedEncoding* encoding); + [DllImport(DllName, EntryPoint = "z_encoding_video_h264", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_video_h264(); - [DllImport(DllName, EntryPoint = "z_encoding_drop", CallingConvention = CallingConvention.Cdecl)] - internal static extern void z_encoding_drop(ZOwnedEncoding* encoding); + [DllImport(DllName, EntryPoint = "z_encoding_video_h265", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_video_h265(); - [DllImport(DllName, EntryPoint = "z_encoding_loan", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZEncoding z_encoding_loan(ZOwnedEncoding* encoding); + [DllImport(DllName, EntryPoint = "z_encoding_video_h266", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_video_h266(); - [DllImport(DllName, EntryPoint = "z_encoding_null", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedEncoding z_encoding_null(); + [DllImport(DllName, EntryPoint = "z_encoding_video_mp4", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_video_mp4(); - [DllImport(DllName, EntryPoint = "z_keyexpr_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_keyexpr_check(ZOwnedKeyexpr* keyexpr); + [DllImport(DllName, EntryPoint = "z_encoding_video_ogg", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_video_ogg(); - [DllImport(DllName, EntryPoint = "z_keyexpr", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZKeyexpr z_keyexpr(byte* name); - // internal static extern ZKeyexpr z_keyexpr([MarshalAs(UnmanagedType.LPStr)] string name); + [DllImport(DllName, EntryPoint = "z_encoding_video_raw", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_video_raw(); - [DllImport(DllName, EntryPoint = "z_keyexpr_as_bytes", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZBytes z_keyexpr_as_bytes(ZKeyexpr keyexpr); + [DllImport(DllName, EntryPoint = "z_encoding_video_vp8", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_video_vp8(); - [DllImport(DllName, EntryPoint = "z_keyexpr_canonize", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_keyexpr_canonize(byte* start, nuint* len); + [DllImport(DllName, EntryPoint = "z_encoding_video_vp9", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_video_vp9(); - [DllImport(DllName, EntryPoint = "z_keyexpr_canonize_null_terminated", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_keyexpr_canonize_null_terminated(byte* start); + [DllImport(DllName, EntryPoint = "z_encoding_zenoh_bytes", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_zenoh_bytes(); - [DllImport(DllName, EntryPoint = "z_keyexpr_drop", CallingConvention = CallingConvention.Cdecl)] - internal static extern void z_keyexpr_drop(ZOwnedKeyexpr* keyexpr); + [DllImport(DllName, EntryPoint = "z_encoding_zenoh_serialized", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_zenoh_serialized(); - [DllImport(DllName, EntryPoint = "z_keyexpr_concat", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedKeyexpr z_keyexpr_concat(ZKeyexpr left, byte* rightStart, nuint rightLen); + [DllImport(DllName, EntryPoint = "z_encoding_zenoh_string", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_zenoh_string(); - [DllImport(DllName, EntryPoint = "z_keyexpr_equals", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_keyexpr_equals(ZKeyexpr left, ZKeyexpr right); + [DllImport(DllName, EntryPoint = "z_encoding_clone", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_encoding_clone(ZOwnedEncoding* dst, ZLoanedEncoding* src); - [DllImport(DllName, EntryPoint = "z_keyexpr_includes", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_keyexpr_includes(ZKeyexpr left, ZKeyexpr right); + [DllImport(DllName, EntryPoint = "z_encoding_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_encoding_drop(ZMovedEncoding* encoding); - [DllImport(DllName, EntryPoint = "z_keyexpr_intersects", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_keyexpr_intersects(ZKeyexpr left, ZKeyexpr right); + [DllImport(DllName, EntryPoint = "z_encoding_equals", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_encoding_equals(ZLoanedEncoding* encoding, ZLoanedEncoding* other); + + [DllImport(DllName, EntryPoint = "z_encoding_from_str", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_encoding_from_str(ZOwnedEncoding* encoding, byte* s); + + [DllImport(DllName, EntryPoint = "z_encoding_from_substr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_encoding_from_substr(ZOwnedEncoding* encoding, byte* s, nuint len); + + [DllImport(DllName, EntryPoint = "z_encoding_loan_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_loan_default(); + + [DllImport(DllName, EntryPoint = "z_encoding_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_loan(ZOwnedEncoding* encoding); + + [DllImport(DllName, EntryPoint = "z_encoding_loan_mut", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_encoding_loan_mut(ZOwnedEncoding* encoding); + + [DllImport(DllName, EntryPoint = "z_encoding_set_schema_from_str", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_encoding_set_schema_from_str(ZLoanedEncoding* encoding, byte* s); + + [DllImport(DllName, EntryPoint = "z_encoding_set_schema_from_substr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_encoding_set_schema_from_substr(ZLoanedEncoding* encoding, byte* s, nuint len); + + [DllImport(DllName, EntryPoint = "z_encoding_to_string", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_encoding_to_string(ZLoanedEncoding* encoding, ZOwnedString* outStr); + + [DllImport(DllName, EntryPoint = "z_fifo_channel_query_new", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_fifo_channel_query_new(ZOwnedClosureQuery* callback, ZOwnedFifoHandlerQuery* handler, + nuint capacity); + + [DllImport(DllName, EntryPoint = "z_fifo_handler_query_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_fifo_handler_query_drop(ZMovedFifoHandlerQuery* handler); + + [DllImport(DllName, EntryPoint = "z_fifo_handler_query_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedFifoHandlerQuery* z_fifo_handler_query_loan(ZOwnedFifoHandlerQuery* handler); + + [DllImport(DllName, EntryPoint = "z_fifo_handler_query_recv", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_fifo_handler_query_recv(ZLoanedFifoHandlerQuery* handler, ZOwnedQuery* query); + + [DllImport(DllName, EntryPoint = "z_fifo_handler_query_try_recv", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_fifo_handler_query_try_recv(ZLoanedFifoHandlerQuery* handler, ZOwnedQuery* query); + + [DllImport(DllName, EntryPoint = "z_fifo_channel_reply_new", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_fifo_channel_reply_new(ZOwnedClosureReply* callback, ZOwnedFifoHandlerReply* handler, + nuint capacity); + + [DllImport(DllName, EntryPoint = "z_fifo_handler_reply_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_fifo_handler_reply_drop(ZMovedFifoHandlerReply* handler); + + [DllImport(DllName, EntryPoint = "z_fifo_handler_reply_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedFifoHandlerReply* z_fifo_handler_reply_loan(ZOwnedFifoHandlerReply* handler); + + [DllImport(DllName, EntryPoint = "z_fifo_handler_reply_recv", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_fifo_handler_reply_recv(ZLoanedFifoHandlerReply* handler, ZOwnedReply* reply); + + [DllImport(DllName, EntryPoint = "z_fifo_handler_reply_try_recv", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_fifo_handler_reply_try_recv(ZLoanedFifoHandlerReply* handler, ZOwnedReply* reply); + + [DllImport(DllName, EntryPoint = "z_fifo_channel_sample_new", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_fifo_channel_sample_new(ZOwnedClosureSample* callback, + ZOwnedFifoHandlerSample* handler, nuint capacity); + + [DllImport(DllName, EntryPoint = "z_fifo_handler_sample_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_fifo_handler_sample_drop(ZMovedFifoHandlerSample* handler); + + [DllImport(DllName, EntryPoint = "z_fifo_handler_sample_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedFifoHandlerSample* z_fifo_handler_sample_loan(ZOwnedFifoHandlerSample* handler); + + [DllImport(DllName, EntryPoint = "z_fifo_handler_sample_recv", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_fifo_handler_sample_recv(ZLoanedFifoHandlerSample* handler, ZOwnedSample* sample); + + [DllImport(DllName, EntryPoint = "z_fifo_handler_sample_try_recv", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_fifo_handler_sample_try_recv(ZLoanedFifoHandlerSample* handler, + ZOwnedSample* sample); + + [DllImport(DllName, EntryPoint = "z_get", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + internal static extern ZResult z_get( + ZLoanedSession* session, + ZLoanedKeyexpr* keyexpr, + byte* parameters, + ZMovedClosureReply* callback, + ZGetOptions* options + ); + + [DllImport(DllName, EntryPoint = "z_get_options_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_get_options_default(ZGetOptions* options); + + [DllImport(DllName, EntryPoint = "z_hello_clone", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_hello_clone(ZOwnedHello* dst, ZLoanedHello* src); + + [DllImport(DllName, EntryPoint = "z_hello_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_hello_drop(ZMovedHello* hello); + + [DllImport(DllName, EntryPoint = "z_hello_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedHello* z_hello_loan(ZOwnedHello* hello); + + [DllImport(DllName, EntryPoint = "z_hello_locators", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_hello_locators(ZLoanedHello* hello, ZOwnedStringArray* locatorsOut); + + [DllImport(DllName, EntryPoint = "z_hello_whatami", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern Whatami z_hello_whatami(ZLoanedHello* hello); + + [DllImport(DllName, EntryPoint = "z_hello_zid", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZId z_hello_zid(ZLoanedHello* hello); + + [DllImport(DllName, EntryPoint = "z_id_to_string", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_id_to_string(ZId* id, ZOwnedString* dst); + + [DllImport(DllName, EntryPoint = "z_info_peers_zid", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_info_peers_zid(ZLoanedSession* session, ZMovedClosureZid* callback); + + [DllImport(DllName, EntryPoint = "z_info_routers_zid", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_info_routers_zid(ZLoanedSession* session, ZMovedClosureZid* callback); + + [DllImport(DllName, EntryPoint = "z_info_zid", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZId z_info_zid(ZLoanedSession* session); + + [DllImport(DllName, EntryPoint = "z_internal_bytes_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_bytes_check(ZOwnedBytes* b); - [DllImport(DllName, EntryPoint = "z_keyexpr_is_canon", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_keyexpr_is_canon(byte* start, nuint len); + [DllImport(DllName, EntryPoint = "z_internal_bytes_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_bytes_null(ZOwnedBytes* b); - [DllImport(DllName, EntryPoint = "z_keyexpr_is_initialized", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_keyexpr_is_initialized(ZKeyexpr* keyexpr); + [DllImport(DllName, EntryPoint = "z_internal_bytes_writer_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_bytes_writer_check(ZOwnedBytesWriter* writer); - [DllImport(DllName, EntryPoint = "z_keyexpr_join", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedKeyexpr z_keyexpr_join(ZKeyexpr left, ZKeyexpr right); + [DllImport(DllName, EntryPoint = "z_internal_bytes_writer_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_bytes_writer_null(ZOwnedBytesWriter* writer); - [DllImport(DllName, EntryPoint = "z_keyexpr_loan", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZKeyexpr z_keyexpr_loan(ZOwnedKeyexpr* keyexpr); + [DllImport(DllName, EntryPoint = "z_internal_closure_hello_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_closure_hello_check(ZOwnedClosureHello* hello); - [DllImport(DllName, EntryPoint = "z_keyexpr_new", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedKeyexpr z_keyexpr_new(byte* name); + [DllImport(DllName, EntryPoint = "z_internal_closure_hello_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_closure_hello_null(ZOwnedClosureHello* hello); - [DllImport(DllName, EntryPoint = "z_keyexpr_unchecked", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZKeyexpr z_keyexpr_unchecked(byte* name); + [DllImport(DllName, EntryPoint = "z_internal_closure_query_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_closure_query_check(ZOwnedClosureQuery* query); - [DllImport(DllName, EntryPoint = "z_keyexpr_null", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedKeyexpr z_keyexpr_null(); + [DllImport(DllName, EntryPoint = "z_internal_closure_query_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_closure_query_null(ZOwnedClosureQuery* query); - [DllImport(DllName, EntryPoint = "z_keyexpr_to_string", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedStr z_keyexpr_to_string(ZKeyexpr keyexpr); + [DllImport(DllName, EntryPoint = "z_internal_closure_reply_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_closure_reply_check(ZOwnedClosureReply* reply); - [DllImport(DllName, EntryPoint = "z_declare_keyexpr", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedKeyexpr z_declare_keyexpr(ZSession session, ZKeyexpr keyexpr); + [DllImport(DllName, EntryPoint = "z_internal_closure_reply_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_closure_reply_null(ZOwnedClosureReply* reply); - [DllImport(DllName, EntryPoint = "z_undeclare_keyexpr", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_undeclare_keyexpr(ZSession session, ZOwnedKeyexpr* keyexpr); + [DllImport(DllName, EntryPoint = "z_internal_closure_sample_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_closure_sample_check(ZOwnedClosureSample* sample); - [DllImport(DllName, EntryPoint = "z_timestamp_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_timestamp_check(ZTimestamp ts); + [DllImport(DllName, EntryPoint = "z_internal_closure_sample_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_closure_sample_null(ZOwnedClosureSample* sample); - [DllImport(DllName, EntryPoint = "z_subscriber_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_subscriber_check(ZOwnedSubscriber* sub); + [DllImport(DllName, EntryPoint = "z_internal_closure_zid_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_closure_zid_check(ZOwnedClosureZid* zid); - [DllImport(DllName, EntryPoint = "z_subscriber_null", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedSubscriber z_subscriber_null(); + [DllImport(DllName, EntryPoint = "z_internal_closure_zid_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_closure_zid_null(ZOwnedClosureZid* zid); - [DllImport(DllName, EntryPoint = "z_subscriber_options_default", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZSubscriberOptions z_subscriber_options_default(); + [DllImport(DllName, EntryPoint = "z_internal_condvar_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_condvar_check(ZOwnedCondvar* condvar); - [DllImport(DllName, EntryPoint = "z_subscriber_pull", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_subscriber_pull(ZPullSubscriber* sub); + [DllImport(DllName, EntryPoint = "z_internal_condvar_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_condvar_null(ZOwnedCondvar* condvar); - [DllImport(DllName, EntryPoint = "z_declare_subscriber", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedSubscriber z_declare_subscriber( - ZSession session, ZKeyexpr keyexpr, ZOwnedClosureSample* callback, ZSubscriberOptions* options); + [DllImport(DllName, EntryPoint = "z_internal_config_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_config_check(ZOwnedConfig* config); - [DllImport(DllName, EntryPoint = "z_undeclare_subscriber", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_undeclare_subscriber(ZOwnedSubscriber* sub); + [DllImport(DllName, EntryPoint = "z_internal_config_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_config_null(ZOwnedConfig* config); - [DllImport(DllName, EntryPoint = "z_pull_subscriber_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_pull_subscriber_check(ZOwnedPullSubscriber* sub); + [DllImport(DllName, EntryPoint = "z_internal_encoding_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_encoding_check(ZOwnedEncoding* encoding); - [DllImport(DllName, EntryPoint = "z_pull_subscriber_loan", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZPullSubscriber z_pull_subscriber_loan(ZOwnedPullSubscriber* sub); + [DllImport(DllName, EntryPoint = "z_internal_encoding_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_encoding_null(ZOwnedEncoding* encoding); - [DllImport(DllName, EntryPoint = "z_pull_subscriber_null", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedPullSubscriber z_pull_subscriber_null(); + [DllImport(DllName, EntryPoint = "z_internal_fifo_handler_query_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_fifo_handler_query_check(ZOwnedFifoHandlerQuery* handler); - [DllImport(DllName, EntryPoint = "z_pull_subscriber_options_default", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZPullSubscriberOptions z_pull_subscriber_options_default(); + [DllImport(DllName, EntryPoint = "z_internal_fifo_handler_query_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_fifo_handler_query_null(ZOwnedFifoHandlerQuery* handler); - [DllImport(DllName, EntryPoint = "z_declare_pull_subscriber", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedPullSubscriber z_declare_pull_subscriber( - ZSession session, ZKeyexpr keyexpr, ZOwnedClosureSample* callback, ZPullSubscriberOptions* options); + [DllImport(DllName, EntryPoint = "z_internal_fifo_handler_reply_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_fifo_handler_reply_check(ZOwnedFifoHandlerReply* handler); - [DllImport(DllName, EntryPoint = "z_undeclare_pull_subscriber", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_undeclare_pull_subscriber(ZOwnedPullSubscriber* sub); + [DllImport(DllName, EntryPoint = "z_internal_fifo_handler_reply_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_fifo_handler_reply_null(ZOwnedFifoHandlerReply* handler); - [DllImport(DllName, EntryPoint = "z_declare_queryable", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedQueryable z_declare_queryable( - ZSession session, ZKeyexpr keyexpr, ZOwnedClosureQuery* callback, ZQueryableOptions* options); + [DllImport(DllName, EntryPoint = "z_internal_fifo_handler_sample_check", + CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_fifo_handler_sample_check(ZOwnedFifoHandlerSample* handler); - [DllImport(DllName, EntryPoint = "z_undeclare_queryable", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_undeclare_queryable(ZOwnedQueryable* queryable); + [DllImport(DllName, EntryPoint = "z_internal_fifo_handler_sample_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_fifo_handler_sample_null(ZOwnedFifoHandlerSample* handler); - [DllImport(DllName, EntryPoint = "z_delete", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_delete(ZSession session, ZKeyexpr keyexpr, ZDeleteOptions* options); + [DllImport(DllName, EntryPoint = "z_internal_hello_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_hello_check(ZOwnedHello* hello); - [DllImport(DllName, EntryPoint = "z_delete_options_default", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZDeleteOptions z_delete_options_default(); + [DllImport(DllName, EntryPoint = "z_internal_hello_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_hello_null(ZOwnedHello* hello); - [DllImport(DllName, EntryPoint = "z_get", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_get( - ZSession session, ZKeyexpr keyexpr, byte* parameters, ZOwnedClosureReply* callback, ZGetOptions* options); + [DllImport(DllName, EntryPoint = "z_internal_keyexpr_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_keyexpr_check(ZOwnedKeyexpr* keyexpr); - [DllImport(DllName, EntryPoint = "z_get_options_default", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZGetOptions z_get_options_default(); + [DllImport(DllName, EntryPoint = "z_internal_keyexpr_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_keyexpr_null(ZOwnedKeyexpr* keyexpr); - [DllImport(DllName, EntryPoint = "z_info_peers_zid", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_info_peers_zid(ZSession session, ZOwnedClosureZId* callback); + [DllImport(DllName, EntryPoint = "z_internal_liveliness_token_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_liveliness_token_check(ZOwnedLivelinessToken* token); - [DllImport(DllName, EntryPoint = "z_info_routers_zid", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_info_routers_zid(ZSession session, ZOwnedClosureZId* callback); + [DllImport(DllName, EntryPoint = "z_internal_liveliness_token_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_liveliness_token_null(ZOwnedLivelinessToken* token); - [DllImport(DllName, EntryPoint = "z_info_zid", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZId z_info_zid(ZSession session); + [DllImport(DllName, EntryPoint = "z_internal_mutex_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_mutex_check(ZOwnedMutex* mutex); - [DllImport(DllName, EntryPoint = "z_declare_publisher", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedPublisher z_declare_publisher( - ZSession session, ZKeyexpr keyexpr, ZPublisherOptions* options); + [DllImport(DllName, EntryPoint = "z_internal_mutex_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_mutex_null(ZOwnedMutex* mutex); - [DllImport(DllName, EntryPoint = "z_undeclare_publisher", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_undeclare_publisher(ZOwnedPublisher* publisher); + [DllImport(DllName, EntryPoint = "z_internal_publisher_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_publisher_check(ZOwnedPublisher* publisher); - [DllImport(DllName, EntryPoint = "z_publisher_options_default", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZPublisherOptions z_publisher_options_default(); + [DllImport(DllName, EntryPoint = "z_internal_publisher_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_publisher_null(ZOwnedPublisher* publisher); - [DllImport(DllName, EntryPoint = "z_publisher_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_publisher_check(ZOwnedPublisher* pbl); + [DllImport(DllName, EntryPoint = "z_internal_query_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_query_check(ZOwnedQuery* query); - [DllImport(DllName, EntryPoint = "z_publisher_delete", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_publisher_delete(ZPublisher publisher, ZPublisherDeleteOptions* options); + [DllImport(DllName, EntryPoint = "z_internal_query_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_query_null(ZOwnedQuery* query); - [DllImport(DllName, EntryPoint = "z_publisher_delete_options_default", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZPublisherDeleteOptions z_publisher_delete_options_default(); + [DllImport(DllName, EntryPoint = "z_internal_queryable_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_queryable_check(ZOwnedQueryable* queryable); - [DllImport(DllName, EntryPoint = "z_publisher_loan", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZPublisher z_publisher_loan(ZOwnedPublisher* pbl); + [DllImport(DllName, EntryPoint = "z_internal_queryable_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_queryable_null(ZOwnedQueryable* queryable); - [DllImport(DllName, EntryPoint = "z_publisher_null", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedPublisher z_publisher_null(); + [DllImport(DllName, EntryPoint = "z_internal_reply_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_reply_check(ZOwnedReply* reply); - [DllImport(DllName, EntryPoint = "z_publisher_put", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_publisher_put( - ZPublisher publisher, byte* payload, nuint len, ZPublisherPutOptions* options); + [DllImport(DllName, EntryPoint = "z_internal_reply_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_reply_null(ZOwnedReply* reply); - [DllImport(DllName, EntryPoint = "zc_publisher_put_owned", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte zc_publisher_put_owned( - ZPublisher publisher, ZcOwnedPayload* payload, ZPublisherPutOptions* options); + [DllImport(DllName, EntryPoint = "z_internal_reply_err_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_reply_err_check(ZOwnedReplyErr* replyErr); - [DllImport(DllName, EntryPoint = "z_publisher_put_options_default", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZPublisherOptions z_publisher_put_options_default(); + [DllImport(DllName, EntryPoint = "z_internal_reply_err_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_reply_err_null(ZOwnedReplyErr* replyErr); - [DllImport(DllName, EntryPoint = "z_put", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_put(ZSession session, ZKeyexpr keyexpr, byte* payload, nuint len, ZPutOptions* opts); + [DllImport(DllName, EntryPoint = "z_internal_ring_handler_query_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_ring_handler_query_check(ZOwnedRingHandlerQuery* handler); - [DllImport(DllName, EntryPoint = "zc_put_owned", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte zc_put_owned( - ZSession session, ZKeyexpr keyexpr, ZcOwnedPayload* payload, ZPutOptions* opts); + [DllImport(DllName, EntryPoint = "z_internal_ring_handler_query_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_ring_handler_query_null(ZOwnedRingHandlerQuery* handler); - [DllImport(DllName, EntryPoint = "z_put_options_default", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZPutOptions z_put_options_default(); + [DllImport(DllName, EntryPoint = "z_internal_ring_handler_reply_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_ring_handler_reply_check(ZOwnedRingHandlerReply* handler); - [DllImport(DllName, EntryPoint = "z_query_consolidation_auto", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZQueryConsolidation z_query_consolidation_auto(); + [DllImport(DllName, EntryPoint = "z_internal_ring_handler_reply_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_ring_handler_reply_null(ZOwnedRingHandlerReply* handler); - [DllImport(DllName, EntryPoint = "z_query_consolidation_default", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZQueryConsolidation z_query_consolidation_default(); + [DllImport(DllName, EntryPoint = "z_internal_ring_handler_sample_check", + CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_ring_handler_sample_check(ZOwnedRingHandlerSample* handler); - [DllImport(DllName, EntryPoint = "z_query_consolidation_latest", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZQueryConsolidation z_query_consolidation_latest(); + [DllImport(DllName, EntryPoint = "z_internal_ring_handler_sample_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_ring_handler_sample_null(ZOwnedRingHandlerSample* handler); - [DllImport(DllName, EntryPoint = "z_query_consolidation_monotonic", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZQueryConsolidation z_query_consolidation_monotonic(); + [DllImport(DllName, EntryPoint = "z_internal_sample_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_sample_check(ZOwnedSample* sample); - [DllImport(DllName, EntryPoint = "z_query_consolidation_none", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZQueryConsolidation z_query_consolidation_none(); + [DllImport(DllName, EntryPoint = "z_internal_sample_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_sample_null(ZOwnedSample* sample); - [DllImport(DllName, EntryPoint = "z_query_keyexpr", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZKeyexpr z_query_keyexpr(ZQuery* query); + [DllImport(DllName, EntryPoint = "z_internal_session_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_session_check(ZOwnedSession* session); - [DllImport(DllName, EntryPoint = "z_query_parameters", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZBytes z_query_parameters(ZQuery* query); + [DllImport(DllName, EntryPoint = "z_internal_session_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_session_null(ZOwnedSession* session); - [DllImport(DllName, EntryPoint = "z_query_reply", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_query_reply( - ZQuery* query, ZKeyexpr key, byte* payload, nuint len, ZQueryReplyOptions* options); + [DllImport(DllName, EntryPoint = "z_internal_slice_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_slice_check(ZOwnedSlice* slice); - [DllImport(DllName, EntryPoint = "z_query_parameters", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZQueryReplyOptions z_query_reply_options_default(); + [DllImport(DllName, EntryPoint = "z_internal_slice_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_slice_null(ZOwnedSlice* slice); - [DllImport(DllName, EntryPoint = "z_query_target_default", CallingConvention = CallingConvention.Cdecl)] - internal static extern QueryTarget z_query_target_default(); + [DllImport(DllName, EntryPoint = "z_internal_string_array_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_string_array_check(ZOwnedStringArray* strings); - [DllImport(DllName, EntryPoint = "z_query_value", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZValue z_query_value(ZQuery* query); + [DllImport(DllName, EntryPoint = "z_internal_string_array_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_string_array_null(ZOwnedStringArray* strings); - [DllImport(DllName, EntryPoint = "z_queryable_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_queryable_check(ZOwnedQueryable* queryable); + [DllImport(DllName, EntryPoint = "z_internal_string_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_string_check(ZOwnedString* str); - [DllImport(DllName, EntryPoint = "z_queryable_null", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedQueryable z_queryable_null(); + [DllImport(DllName, EntryPoint = "z_internal_string_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_string_null(ZOwnedString* str); - [DllImport(DllName, EntryPoint = "z_queryable_options_default", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZQueryableOptions z_queryable_options_default(); + [DllImport(DllName, EntryPoint = "z_internal_subscriber_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_subscriber_check(ZOwnedSubscriber* subscriber); - [DllImport(DllName, EntryPoint = "z_reply_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_reply_check(ZOwnedReply* replyData); + [DllImport(DllName, EntryPoint = "z_internal_subscriber_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_subscriber_null(ZOwnedSubscriber* subscriber); - [DllImport(DllName, EntryPoint = "z_reply_is_ok", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_reply_is_ok(ZOwnedReply* reply); + [DllImport(DllName, EntryPoint = "z_internal_task_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_internal_task_check(ZOwnedTask* task); - [DllImport(DllName, EntryPoint = "z_reply_drop", CallingConvention = CallingConvention.Cdecl)] - internal static extern void z_reply_drop(ZOwnedReply* replyData); + [DllImport(DllName, EntryPoint = "z_internal_task_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_internal_task_null(ZOwnedTask* task); - [DllImport(DllName, EntryPoint = "z_reply_err", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZValue z_reply_err(ZOwnedReply* replyData); + [DllImport(DllName, EntryPoint = "z_keyexpr_as_view_string", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_keyexpr_as_view_string(ZLoanedKeyexpr* keyexpr, ZViewString* outString); - [DllImport(DllName, EntryPoint = "z_reply_null", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedReply z_reply_null(); + [DllImport(DllName, EntryPoint = "z_keyexpr_canonize", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_keyexpr_canonize(byte* start, nuint* len); - [DllImport(DllName, EntryPoint = "z_reply_ok", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZSample z_reply_ok(ZOwnedReply* reply); + [DllImport(DllName, EntryPoint = "z_keyexpr_canonize_null_terminated", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_keyexpr_canonize_null_terminated(byte* start); - [DllImport(DllName, EntryPoint = "z_open", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedSession z_open(ZOwnedConfig* config); + [DllImport(DllName, EntryPoint = "z_keyexpr_clone", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_keyexpr_clone(ZOwnedKeyexpr* dst, ZLoanedKeyexpr* src); - [DllImport(DllName, EntryPoint = "z_close", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_close(ZOwnedSession* session); + [DllImport(DllName, EntryPoint = "z_keyexpr_concat", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_keyexpr_concat(ZOwnedKeyexpr* keyexpr, ZLoanedKeyexpr* left, byte* rightStart, + nuint rightLen); - [DllImport(DllName, EntryPoint = "z_session_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_session_check(ZOwnedSession* session); + [DllImport(DllName, EntryPoint = "z_keyexpr_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_keyexpr_drop(ZMovedKeyexpr* keyexpr); - [DllImport(DllName, EntryPoint = "z_session_loan", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZSession z_session_loan(ZOwnedSession* session); + [DllImport(DllName, EntryPoint = "z_keyexpr_equals", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_keyexpr_equals(ZLoanedKeyexpr* left, ZLoanedKeyexpr* right); - [DllImport(DllName, EntryPoint = "z_session_null", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedSession z_session_null(); + [DllImport(DllName, EntryPoint = "z_keyexpr_from_str", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_keyexpr_from_str(ZOwnedKeyexpr* keyexpr, byte* str); - [DllImport(DllName, EntryPoint = "zc_session_rcinc", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedSession zc_session_rcinc(ZSession session); + [DllImport(DllName, EntryPoint = "z_keyexpr_from_str_autocanonize", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_keyexpr_from_str_autocanonize(ZOwnedKeyexpr* keyexpr, byte* str); - [DllImport(DllName, EntryPoint = "zc_init_logger", CallingConvention = CallingConvention.Cdecl)] - internal static extern void zc_init_logger(); + [DllImport(DllName, EntryPoint = "z_keyexpr_from_substr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_keyexpr_from_substr(ZOwnedKeyexpr* keyexpr, byte* str, nuint len); - [DllImport(DllName, EntryPoint = "zc_payload_check", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte zc_payload_check(ZcOwnedPayload* payload); + [DllImport(DllName, EntryPoint = "z_keyexpr_from_substr_autocanonize", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_keyexpr_from_substr_autocanonize(ZOwnedKeyexpr* keyexpr, byte* str, nuint len); - [DllImport(DllName, EntryPoint = "zc_payload_drop", CallingConvention = CallingConvention.Cdecl)] - internal static extern void zc_payload_drop(ZcOwnedPayload* payload); + [DllImport(DllName, EntryPoint = "z_keyexpr_includes", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_keyexpr_includes(ZLoanedKeyexpr* left, ZLoanedKeyexpr* right); - [DllImport(DllName, EntryPoint = "zc_payload_null", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZcOwnedPayload zc_payload_null(); + [DllImport(DllName, EntryPoint = "z_keyexpr_intersects", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_keyexpr_intersects(ZLoanedKeyexpr* left, ZLoanedKeyexpr* right); + + [DllImport(DllName, EntryPoint = "z_keyexpr_is_canon", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_keyexpr_is_canon(byte* start, nuint len); + + [DllImport(DllName, EntryPoint = "z_keyexpr_join", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_keyexpr_join(ZOwnedKeyexpr* keyexpr, ZLoanedKeyexpr* left, ZLoanedKeyexpr* right); + + [DllImport(DllName, EntryPoint = "z_keyexpr_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedKeyexpr* z_keyexpr_loan(ZOwnedKeyexpr* keyexpr); + + [DllImport(DllName, EntryPoint = "z_liveliness_declare_subscriber", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_liveliness_declare_subscriber( + ZLoanedSession* session, + ZOwnedSubscriber* subscriber, + ZLoanedKeyexpr* keyexpr, + ZMovedClosureSample* callback, + ZLivelinessSubscriberOptions* options + ); + + [DllImport(DllName, EntryPoint = "z_liveliness_declare_token", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_liveliness_declare_token( + ZLoanedSession* session, + ZOwnedLivelinessToken* token, + ZLoanedKeyexpr* keyexpr, + ZLivelinessTokenOptions* options + ); + + [DllImport(DllName, EntryPoint = "z_liveliness_get", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_liveliness_get( + ZLoanedSession* session, + ZLoanedKeyexpr* keyexpr, + ZMovedClosureReply* callback, + ZLivelinessGetOptions* options + ); + + [DllImport(DllName, EntryPoint = "z_liveliness_get_options_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_liveliness_get_options_default(ZLivelinessGetOptions* options); + + [DllImport(DllName, EntryPoint = "z_liveliness_subscriber_options_default", + CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_liveliness_subscriber_options_default(ZLivelinessSubscriberOptions* options); + + [DllImport(DllName, EntryPoint = "z_liveliness_token_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_liveliness_token_drop(ZMovedLivelinessToken* token); + + [DllImport(DllName, EntryPoint = "z_liveliness_token_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedLivelinessToken* z_liveliness_token_loan(ZOwnedLivelinessToken* token); + + [DllImport(DllName, EntryPoint = "z_liveliness_token_options_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_liveliness_token_options_default(ZLivelinessTokenOptions* options); + + [DllImport(DllName, EntryPoint = "z_liveliness_undeclare_token", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_liveliness_undeclare_token(ZMovedLivelinessToken* token); + + [DllImport(DllName, EntryPoint = "z_mutex_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_mutex_drop(ZMovedMutex* mutex); + + [DllImport(DllName, EntryPoint = "z_mutex_init", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_mutex_init(ZOwnedMutex* mutex); + + [DllImport(DllName, EntryPoint = "z_mutex_loan_mut", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedMutex* z_mutex_loan_mut(ZOwnedMutex* mutex); + + [DllImport(DllName, EntryPoint = "z_mutex_lock", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_mutex_lock(ZLoanedMutex* mutex); + + [DllImport(DllName, EntryPoint = "z_mutex_try_lock", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_mutex_try_lock(ZLoanedMutex* mutex); + + [DllImport(DllName, EntryPoint = "z_mutex_unlock", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_mutex_unlock(ZLoanedMutex* mutex); + + [DllImport(DllName, EntryPoint = "z_open", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_open(ZOwnedSession* session, ZMovedConfig* config, ZOpenOptions* options); + + [DllImport(DllName, EntryPoint = "z_open_options_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_open_options_default(ZOpenOptions* options); + + [DllImport(DllName, EntryPoint = "z_priority_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern Priority z_priority_default(); + + [DllImport(DllName, EntryPoint = "z_publisher_delete", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_publisher_delete(ZLoanedPublisher* publisher, ZPublisherDeleteOptions* options); + + [DllImport(DllName, EntryPoint = "z_publisher_delete_options_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_publisher_delete_options_default(ZPublisherDeleteOptions* options); + + [DllImport(DllName, EntryPoint = "z_publisher_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_publisher_drop(ZMovedPublisher* publisher); + + [DllImport(DllName, EntryPoint = "z_publisher_keyexpr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedKeyexpr* z_publisher_keyexpr(ZLoanedPublisher* publisher); + + [DllImport(DllName, EntryPoint = "z_publisher_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedPublisher* z_publisher_loan(ZOwnedPublisher* publisher); + + [DllImport(DllName, EntryPoint = "z_publisher_loan_mut", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedPublisher* z_publisher_loan_mut(ZOwnedPublisher* publisher); + + [DllImport(DllName, EntryPoint = "z_publisher_options_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_publisher_options_default(ZPublisherOptions* options); + + [DllImport(DllName, EntryPoint = "z_publisher_put", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_publisher_put(ZLoanedPublisher* publisher, ZMovedBytes* payload, + ZPublisherPutOptions* options); + + [DllImport(DllName, EntryPoint = "z_publisher_put_options_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_publisher_put_options_default(ZPublisherPutOptions* options); + + [DllImport(DllName, EntryPoint = "z_put", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_put( + ZLoanedSession* session, + ZLoanedKeyexpr* keyexpr, + ZMovedBytes* payload, + ZPutOptions* options + ); + + [DllImport(DllName, EntryPoint = "z_put_options_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_put_options_default(ZPutOptions* options); + + [DllImport(DllName, EntryPoint = "z_query_attachment", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedBytes* z_query_attachment(ZLoanedQuery* query); + + [DllImport(DllName, EntryPoint = "z_query_clone", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_query_clone(ZOwnedQuery* dst, ZLoanedQuery* src); + + [DllImport(DllName, EntryPoint = "z_query_consolidation_auto", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZQueryConsolidation z_query_consolidation_auto(); + + [DllImport(DllName, EntryPoint = "z_query_consolidation_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZQueryConsolidation z_query_consolidation_default(); + + [DllImport(DllName, EntryPoint = "z_query_consolidation_latest", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZQueryConsolidation z_query_consolidation_latest(); + + [DllImport(DllName, EntryPoint = "z_query_consolidation_monotonic", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZQueryConsolidation z_query_consolidation_monotonic(); + + [DllImport(DllName, EntryPoint = "z_query_consolidation_none", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZQueryConsolidation z_query_consolidation_none(); + + [DllImport(DllName, EntryPoint = "z_query_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_query_drop(ZMovedQuery* query); + + [DllImport(DllName, EntryPoint = "z_query_encoding", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_query_encoding(ZLoanedQuery* query); + + [DllImport(DllName, EntryPoint = "z_query_keyexpr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedKeyexpr* z_query_keyexpr(ZLoanedQuery* query); + + [DllImport(DllName, EntryPoint = "z_query_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedQuery* z_query_loan(ZLoanedQuery* query); + + [DllImport(DllName, EntryPoint = "z_query_parameters", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_query_parameters(ZLoanedQuery* query, ZViewString* parameters); + + [DllImport(DllName, EntryPoint = "z_query_payload", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedBytes* z_query_payload(ZLoanedQuery* query); + + [DllImport(DllName, EntryPoint = "z_query_reply", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_query_reply( + ZLoanedQuery* query, + ZLoanedKeyexpr* keyexpr, + ZMovedBytes* payload, + ZQueryReplyOptions* options + ); + + [DllImport(DllName, EntryPoint = "z_query_reply_del", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_query_reply_del(ZLoanedQuery* query, ZLoanedKeyexpr* keyexpr, + ZQueryReplyDelOptions* options); + + [DllImport(DllName, EntryPoint = "z_query_reply_del_options_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_query_reply_del_options_default(ZQueryReplyDelOptions* options); + + [DllImport(DllName, EntryPoint = "z_query_reply_err", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_query_reply_err(ZLoanedQuery* query, ZMovedBytes* payload, + ZQueryReplyErrOptions* options); + + [DllImport(DllName, EntryPoint = "z_query_reply_options_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_query_reply_options_default(ZQueryReplyOptions* options); + + [DllImport(DllName, EntryPoint = "z_query_target_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern QueryTarget z_query_target_default(); - [DllImport(DllName, EntryPoint = "zc_payload_rcinc", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZcOwnedPayload zc_payload_rcinc(ZcOwnedPayload* payload); + [DllImport(DllName, EntryPoint = "z_queryable_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_queryable_drop(ZMovedQueryable* queryable); - [DllImport(DllName, EntryPoint = "zc_sample_payload_rcinc", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZcOwnedPayload zc_sample_payload_rcinc(ZSample* sample); + [DllImport(DllName, EntryPoint = "z_queryable_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedQueryable* z_queryable_loan(ZOwnedQueryable* queryable); - [DllImport(DllName, EntryPoint = "z_closure_sample_drop", CallingConvention = CallingConvention.Cdecl)] - internal static extern void z_closure_sample_drop(ZOwnedClosureSample* closure); + [DllImport(DllName, EntryPoint = "z_queryable_options_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_queryable_options_default(ZQueryableOptions* options); - [DllImport(DllName, EntryPoint = "z_closure_reply_call", CallingConvention = CallingConvention.Cdecl)] - internal static extern void z_closure_reply_call(ZOwnedClosureReply* closure, ZOwnedReply* sample); + [DllImport(DllName, EntryPoint = "z_random_fill", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_random_fill(byte* buf, nuint len); - [DllImport(DllName, EntryPoint = "zc_reply_fifo_new", CallingConvention = CallingConvention.Cdecl)] - internal static extern ZOwnedReplyChannel zc_reply_fifo_new(nuint bound); + [DllImport(DllName, EntryPoint = "z_random_u8", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern byte z_random_u8(); + + [DllImport(DllName, EntryPoint = "z_random_u16", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U2)] + internal static extern ushort z_random_u16(); + + [DllImport(DllName, EntryPoint = "z_random_u32", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U4)] + internal static extern uint z_random_u32(); + + [DllImport(DllName, EntryPoint = "z_random_u64", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U8)] + internal static extern ulong z_random_u64(); + + [DllImport(DllName, EntryPoint = "z_reply_clone", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_reply_clone(ZOwnedReply* dst, ZLoanedReply* src); + + [DllImport(DllName, EntryPoint = "z_reply_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_reply_drop(ZMovedReply* reply); - [DllImport(DllName, EntryPoint = "z_reply_channel_drop", CallingConvention = CallingConvention.Cdecl)] - internal static extern void z_reply_channel_drop(ZOwnedReplyChannel* channel); + [DllImport(DllName, EntryPoint = "z_reply_err", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedReplyErr* z_reply_err(ZLoanedReply* reply); - [DllImport(DllName, EntryPoint = "z_reply_channel_closure_call", CallingConvention = CallingConvention.Cdecl)] - internal static extern sbyte z_reply_channel_closure_call(ZOwnedReplyChannelClosure* closure, ZOwnedReply* reply); + [DllImport(DllName, EntryPoint = "z_reply_err_clone", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_reply_err_clone(ZOwnedReplyErr* dst, ZLoanedReplyErr* src); + [DllImport(DllName, EntryPoint = "z_reply_err_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_reply_err_drop(ZMovedReplyErr* replyErr); + + [DllImport(DllName, EntryPoint = "z_reply_err_encoding", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_reply_err_encoding(ZLoanedReplyErr* replyErr); + + [DllImport(DllName, EntryPoint = "z_reply_err_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedReplyErr* z_reply_err_loan(ZOwnedReplyErr* replyErr); + + [DllImport(DllName, EntryPoint = "z_reply_err_payload", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedBytes* z_reply_err_payload(ZLoanedReplyErr* replyErr); + + [DllImport(DllName, EntryPoint = "z_reply_is_ok", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_reply_is_ok(ZLoanedReply* reply); + + [DllImport(DllName, EntryPoint = "z_reply_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedReply* z_reply_loan(ZOwnedReply* reply); + + [DllImport(DllName, EntryPoint = "z_reply_ok", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedSample* z_reply_ok(ZLoanedReply* reply); + + [DllImport(DllName, EntryPoint = "z_ring_channel_query_new", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_ring_channel_query_new( + ZOwnedClosureQuery* callback, + ZOwnedRingHandlerQuery* handler, + nuint capacity + ); + + [DllImport(DllName, EntryPoint = "z_ring_channel_reply_new", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_ring_channel_reply_new( + ZOwnedClosureReply* callback, + ZOwnedRingHandlerReply* handler, + nuint capacity + ); + + [DllImport(DllName, EntryPoint = "z_ring_channel_sample_new", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_ring_channel_sample_new( + ZOwnedClosureSample* callback, + ZOwnedRingHandlerSample* handler, + nuint capacity + ); + + [DllImport(DllName, EntryPoint = "z_ring_handler_query_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_ring_handler_query_drop(ZMovedRingHandlerQuery* handler); + + [DllImport(DllName, EntryPoint = "z_ring_handler_query_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedRingHandlerQuery* z_ring_handler_query_loan(ZOwnedRingHandlerQuery* handler); + + [DllImport(DllName, EntryPoint = "z_ring_handler_query_recv", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_ring_handler_query_recv(ZLoanedRingHandlerQuery* handler, ZOwnedQuery* query); + + [DllImport(DllName, EntryPoint = "z_ring_handler_query_try_recv", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_ring_handler_query_try_recv(ZLoanedRingHandlerQuery* handler, ZOwnedQuery* query); + + [DllImport(DllName, EntryPoint = "z_ring_handler_reply_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_ring_handler_reply_drop(ZMovedRingHandlerReply* handler); + + [DllImport(DllName, EntryPoint = "z_ring_handler_reply_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedRingHandlerReply* z_ring_handler_reply_loan(ZOwnedRingHandlerReply* handler); + + [DllImport(DllName, EntryPoint = "z_ring_handler_reply_recv", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_ring_handler_reply_recv(ZLoanedRingHandlerReply* handler, ZOwnedReply* reply); + + [DllImport(DllName, EntryPoint = "z_ring_handler_reply_try_recv", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_ring_handler_reply_try_recv(ZLoanedRingHandlerReply* handler, ZOwnedReply* reply); + + [DllImport(DllName, EntryPoint = "z_ring_handler_sample_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_ring_handler_sample_drop(ZMovedRingHandlerSample* handler); + + [DllImport(DllName, EntryPoint = "z_ring_handler_sample_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedRingHandlerSample* z_ring_handler_sample_loan(ZOwnedRingHandlerSample* handler); + + [DllImport(DllName, EntryPoint = "z_ring_handler_sample_recv", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_ring_handler_sample_recv(ZLoanedRingHandlerSample* handler, ZOwnedSample* sample); + + [DllImport(DllName, EntryPoint = "z_ring_handler_sample_try_recv", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_ring_handler_sample_try_recv(ZLoanedRingHandlerSample* handler, + ZOwnedSample* sample); + + [DllImport(DllName, EntryPoint = "z_sample_attachment", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedBytes* z_sample_attachment(ZLoanedSample* sample); + + [DllImport(DllName, EntryPoint = "z_sample_clone", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_sample_clone(ZLoanedSample* sample); + + [DllImport(DllName, EntryPoint = "z_sample_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_sample_drop(ZMovedSample* sample); + + [DllImport(DllName, EntryPoint = "z_sample_congestion_control", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern CongestionControl z_sample_congestion_control(ZLoanedSample* sample); } \ No newline at end of file From 1d9525b63cb087420d222fb5e31cf5b4e87cf025 Mon Sep 17 00:00:00 2001 From: sanri Date: Fri, 24 Jan 2025 17:32:33 +0800 Subject: [PATCH 34/34] 20250124 --- Zenoh/ZenohC.cs | 776 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 759 insertions(+), 17 deletions(-) diff --git a/Zenoh/ZenohC.cs b/Zenoh/ZenohC.cs index 0254a7e..5ccb83a 100644 --- a/Zenoh/ZenohC.cs +++ b/Zenoh/ZenohC.cs @@ -10,7 +10,7 @@ namespace Zenoh; -using ZResult = System.SByte; +using ZResult = sbyte; // zenoh_commons.h // z_congestion_control_t @@ -69,7 +69,7 @@ public enum Priority : uint // zenoh_commons.h // z_consolidation_mode_t -public enum ConsolidationMode : int +public enum ConsolidationMode { Auto = -1, None = 0, @@ -78,7 +78,7 @@ public enum ConsolidationMode : int } // z_query_consolidation_t -public enum QueryConsolidation : int +public enum QueryConsolidation { Auto = -1, None = 0, @@ -1222,6 +1222,24 @@ internal unsafe struct ZQueryReplyErrOptions internal ZMovedEncoding* encoding; } +// zenoh_commons.h +// zc_owned_closure_log_t +[StructLayout(LayoutKind.Sequential)] +internal unsafe struct ZcOwnedClosureLog +{ + internal void* context; + internal delegate* call; + internal delegate* drop; +} + +// zenoh_commons.h +// zc_moved_closure_log_t +[StructLayout(LayoutKind.Sequential)] +internal struct ZcMovedClosureLog +{ + internal ZcOwnedClosureLog owned_closure_log; +} + // zenoh_opaque.h // zc_loaned_closure_log_t [StructLayout(LayoutKind.Sequential, Pack = 8)] @@ -1265,20 +1283,20 @@ internal struct ZeMovedSerializer internal static unsafe class ZenohC { internal const string DllName = "zenohc"; - internal static uint zRouter = 1; - internal static uint zPeer = 2; - internal static uint zClient = 4; - internal static string zConfigModeKey = "mode"; - internal static string zConfigConnectKey = "connect/endpoints"; - internal static string zConfigListenKey = "listen/endpoints"; - internal static string zConfigUserKey = "transport/auth/usrpwd/user"; - internal static string zConfigPasswordKey = "transport/auth/usrpwd/password"; - internal static string zConfigMulticastScoutingKey = "scouting/multicast/enabled"; - internal static string zConfigMulticastInterfaceKey = "scouting/multicast/interface"; - internal static string zConfigMulticastIpv4AddressKey = "scouting/multicast/address"; - internal static string zConfigScoutingTimeoutKey = "scouting/timeout"; - internal static string zConfigScoutingDelayKey = "scouting/delay"; - internal static string zConfigAddTimestampKey = "timestamping/enabled"; + internal static uint Router = 1; + internal static uint Peer = 2; + internal static uint Client = 4; + internal static string ConfigModeKey = "mode"; + internal static string ConfigConnectKey = "connect/endpoints"; + internal static string ConfigListenKey = "listen/endpoints"; + internal static string ConfigUserKey = "transport/auth/usrpwd/user"; + internal static string ConfigPasswordKey = "transport/auth/usrpwd/password"; + internal static string ConfigMulticastScoutingKey = "scouting/multicast/enabled"; + internal static string ConfigMulticastInterfaceKey = "scouting/multicast/interface"; + internal static string ConfigMulticastIpv4AddressKey = "scouting/multicast/address"; + internal static string ConfigScoutingTimeoutKey = "scouting/timeout"; + internal static string ConfigScoutingDelayKey = "scouting/delay"; + internal static string ConfigAddTimestampKey = "timestamping/enabled"; // internal static string ZOwnedStrToString(ZOwnedStr* zs) // { @@ -2809,4 +2827,728 @@ internal static extern ZResult z_ring_handler_sample_try_recv(ZLoanedRingHandler [DllImport(DllName, EntryPoint = "z_sample_congestion_control", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] internal static extern CongestionControl z_sample_congestion_control(ZLoanedSample* sample); + + [DllImport(DllName, EntryPoint = "z_sample_encoding", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedEncoding* z_sample_encoding(ZLoanedSample* sample); + + [DllImport(DllName, EntryPoint = "z_sample_express", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_sample_express(ZLoanedSample* sample); + + [DllImport(DllName, EntryPoint = "z_sample_keyexpr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedKeyexpr* z_sample_keyexpr(ZLoanedSample* sample); + + [DllImport(DllName, EntryPoint = "z_sample_kind", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern SampleKind z_sample_kind(ZLoanedSample* sample); + + [DllImport(DllName, EntryPoint = "z_sample_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedSample* z_sample_loan(ZOwnedSample* sample); + + [DllImport(DllName, EntryPoint = "z_sample_payload", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedBytes* z_sample_payload(ZLoanedSample* sample); + + [DllImport(DllName, EntryPoint = "z_sample_priority", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern Priority z_sample_priority(ZLoanedSample* sample); + + [DllImport(DllName, EntryPoint = "z_sample_timestamp", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZTimestamp* z_sample_timestamp(ZLoanedSample* sample); + + [DllImport(DllName, EntryPoint = "z_scout", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_scout(ZMovedConfig* config, ZMovedClosureHello* callback, ZScoutOptions* options); + + [DllImport(DllName, EntryPoint = "z_scout_options_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_scout_options_default(ZScoutOptions* options); + + [DllImport(DllName, EntryPoint = "z_session_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_session_drop(ZMovedSession* session); + + [DllImport(DllName, EntryPoint = "z_session_is_closed", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_session_is_closed(ZLoanedSession* session); + + [DllImport(DllName, EntryPoint = "z_session_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedSession* z_session_loan(ZOwnedSession* session); + + [DllImport(DllName, EntryPoint = "z_session_loan_mut", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedSession* z_session_loan_mut(ZOwnedSession* session); + + [DllImport(DllName, EntryPoint = "z_sleep_s", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_sleep_s(nuint time); + + [DllImport(DllName, EntryPoint = "z_sleep_ms", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_sleep_ms(nuint time); + + [DllImport(DllName, EntryPoint = "z_sleep_us", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_sleep_us(nuint time); + + [DllImport(DllName, EntryPoint = "z_slice_clone", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_slice_clone(ZOwnedSlice* dst, ZLoanedSlice* src); + + [DllImport(DllName, EntryPoint = "z_slice_copy_from_buf", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_slice_copy_from_buf(ZOwnedSlice* slice, byte* start, nuint len); + + [DllImport(DllName, EntryPoint = "z_slice_data", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern byte* z_slice_data(ZLoanedSlice* slice); + + [DllImport(DllName, EntryPoint = "z_slice_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_slice_drop(ZMovedSlice* slice); + + [DllImport(DllName, EntryPoint = "z_slice_empty", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_slice_empty(ZOwnedSlice* slice); + + [DllImport(DllName, EntryPoint = "z_slice_from_buf", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_slice_from_buf( + ZOwnedSlice* slice, byte* data, nuint len, delegate* drop, void* context); + + [DllImport(DllName, EntryPoint = "z_slice_is_empty", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_slice_is_empty(ZLoanedSlice* slice); + + [DllImport(DllName, EntryPoint = "z_slice_len", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern nuint z_slice_len(ZLoanedSlice* slice); + + [DllImport(DllName, EntryPoint = "z_slice_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedSlice* z_slice_loan(ZOwnedSlice* slice); + + [DllImport(DllName, EntryPoint = "z_string_array_clone", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_string_array_clone(ZOwnedStringArray* dst, ZLoanedStringArray* src); + + [DllImport(DllName, EntryPoint = "z_string_array_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_string_array_drop(ZMovedStringArray* stringArray); + + [DllImport(DllName, EntryPoint = "z_string_array_get", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedString* z_string_array_get(ZLoanedStringArray* stringArray, nuint index); + + [DllImport(DllName, EntryPoint = "z_string_array_is_empty", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_string_array_is_empty(ZLoanedStringArray* stringArray); + + [DllImport(DllName, EntryPoint = "z_string_array_len", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern nuint z_string_array_len(ZLoanedStringArray* stringArray); + + [DllImport(DllName, EntryPoint = "z_string_array_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedStringArray* z_string_array_loan(ZOwnedStringArray* stringArray); + + [DllImport(DllName, EntryPoint = "z_string_array_loan_mut", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedStringArray* z_string_array_loan_mut(ZOwnedStringArray* stringArray); + + [DllImport(DllName, EntryPoint = "z_string_array_new", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_string_array_new(ZOwnedStringArray* stringArray); + + [DllImport(DllName, EntryPoint = "z_string_push_by_alias", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern nuint z_string_array_push_by_alias(ZLoanedStringArray* stringArray, ZLoanedString* value); + + [DllImport(DllName, EntryPoint = "z_string_push_by_copy", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern nuint z_string_array_push_by_copy(ZLoanedStringArray* stringArray, ZLoanedString* value); + + [DllImport(DllName, EntryPoint = "z_string_as_slice", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedSlice* z_string_as_slice(ZLoanedString* str); + + [DllImport(DllName, EntryPoint = "z_string_clone", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_string_clone(ZOwnedString* dst, ZLoanedString* src); + + [DllImport(DllName, EntryPoint = "z_string_copy_from_str", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_string_copy_from_str(ZOwnedString* dst, byte* str); + + [DllImport(DllName, EntryPoint = "z_string_copy_from_substr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_string_copy_from_substr(ZOwnedString* dst, byte* str, nuint len); + + [DllImport(DllName, EntryPoint = "z_string_data", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern byte* z_string_data(ZLoanedString* str); + + [DllImport(DllName, EntryPoint = "z_string_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_string_drop(ZMovedString* str); + + [DllImport(DllName, EntryPoint = "z_string_empty", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_string_empty(ZOwnedString* str); + + [DllImport(DllName, EntryPoint = "z_string_from_str", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_string_from_str( + ZOwnedString* dst, byte* str, delegate* drop, void* context); + + [DllImport(DllName, EntryPoint = "z_string_is_empty", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_string_is_empty(ZLoanedString* str); + + [DllImport(DllName, EntryPoint = "z_string_len", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern nuint z_string_len(ZLoanedString* str); + + [DllImport(DllName, EntryPoint = "z_string_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedString* z_string_loan(ZOwnedString* str); + + [DllImport(DllName, EntryPoint = "z_subscriber_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_subscriber_drop(ZMovedSubscriber* subscriber); + + [DllImport(DllName, EntryPoint = "z_subscriber_keyexpr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedKeyexpr* z_subscriber_keyexpr(ZLoanedSubscriber* subscriber); + + [DllImport(DllName, EntryPoint = "z_subscriber_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedSubscriber* z_subscriber_loan(ZOwnedSubscriber* subscriber); + + [DllImport(DllName, EntryPoint = "z_subscriber_options_default", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_subscriber_options_default(ZSubscriberOptions* options); + + [DllImport(DllName, EntryPoint = "z_task_detach", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_task_detach(ZMovedTask* task); + + [DllImport(DllName, EntryPoint = "z_task_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_task_drop(ZMovedTask* task); + + [DllImport(DllName, EntryPoint = "z_task_init", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_task_init(ZOwnedTask* task, ZTaskAttr* attr, delegate* fun, + void* arg); + + [DllImport(DllName, EntryPoint = "z_task_join", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_task_join(ZMovedTask* task); + + [DllImport(DllName, EntryPoint = "z_time_elapsed_s", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U8)] + internal static extern ulong z_time_elapsed_s(ZTime* time); + + [DllImport(DllName, EntryPoint = "z_time_elapsed_ms", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U8)] + internal static extern ulong z_time_elapsed_ms(ZTime* time); + + [DllImport(DllName, EntryPoint = "z_time_elapsed_us", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U8)] + internal static extern ulong z_time_elapsed_us(ZTime* time); + + [DllImport(DllName, EntryPoint = "z_time_now", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZTime z_time_now(); + + [DllImport(DllName, EntryPoint = "z_time_now_as_str", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern byte* z_time_now_as_str(byte* buf, nuint len); + + [DllImport(DllName, EntryPoint = "z_timestamp_id", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZId z_timestamp_id(ZTimestamp* timestamp); + + [DllImport(DllName, EntryPoint = "z_timestamp_new", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_timestamp_new(ZTimestamp* timestamp, ZLoanedSession* session); + + [DllImport(DllName, EntryPoint = "z_timestamp_ntp64_time", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U8)] + internal static extern ulong z_timestamp_ntp64_time(ZTimestamp* timestamp); + + [DllImport(DllName, EntryPoint = "z_undeclare_keyexpr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_undeclare_keyexpr(ZLoanedSession* session, ZMovedKeyexpr* keyexpr); + + [DllImport(DllName, EntryPoint = "z_undeclare_publisher", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_undeclare_publisher(ZMovedPublisher* publisher); + + [DllImport(DllName, EntryPoint = "z_undeclare_queryable", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_undeclare_queryable(ZMovedQueryable* queryable); + + [DllImport(DllName, EntryPoint = "z_undeclare_subscriber", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_undeclare_subscriber(ZMovedSubscriber* subscriber); + + [DllImport(DllName, EntryPoint = "z_view_keyexpr_empty", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_view_keyexpr_empty(ZViewKeyexpr* viewKeyexpr); + + [DllImport(DllName, EntryPoint = "z_view_keyexpr_from_str", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_view_keyexpr_from_str(ZViewKeyexpr* viewKeyexpr, byte* str); + + [DllImport(DllName, EntryPoint = "z_view_keyexpr_from_str_autocanonize", + CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_view_keyexpr_from_str_autocanonize(ZViewKeyexpr* viewKeyexpr, byte* str); + + [DllImport(DllName, EntryPoint = "z_view_keyexpr_from_str_unchecked", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_view_keyexpr_from_str_unchecked(ZViewKeyexpr* viewKeyexpr, byte* str); + + [DllImport(DllName, EntryPoint = "z_view_keyexpr_from_substr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_view_keyexpr_from_substr(ZViewKeyexpr* viewKeyexpr, byte* str, nuint len); + + [DllImport(DllName, EntryPoint = "z_view_keyexpr_from_substr_autocanonize", + CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_view_keyexpr_from_substr_autocanonize(ZViewKeyexpr* viewKeyexpr, byte* start, + nuint len); + + [DllImport(DllName, EntryPoint = "z_view_keyexpr_from_substr_unchecked", + CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_view_keyexpr_from_substr_unchecked(ZViewKeyexpr* viewKeyexpr, byte* start, nuint len); + + [DllImport(DllName, EntryPoint = "z_view_keyexpr_is_empty", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_view_keyexpr_is_empty(ZViewKeyexpr* viewKeyexpr); + + [DllImport(DllName, EntryPoint = "z_view_keyexpr_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedKeyexpr* z_view_keyexpr_loan(ZViewKeyexpr* viewKeyexpr); + + [DllImport(DllName, EntryPoint = "z_view_slice_empty", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_view_slice_empty(ZViewSlice* viewSlice); + + [DllImport(DllName, EntryPoint = "z_view_slice_from_buf", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_view_slice_from_buf(ZViewSlice* viewSlice, byte* start, nuint len); + + [DllImport(DllName, EntryPoint = "z_view_slice_is_empty", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_view_slice_is_empty(ZViewSlice* viewSlice); + + [DllImport(DllName, EntryPoint = "z_view_slice_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedSlice* z_view_slice_loan(ZViewSlice* viewSlice); + + [DllImport(DllName, EntryPoint = "z_view_string_empty", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void z_view_string_empty(ZViewString* viewString); + + [DllImport(DllName, EntryPoint = "z_view_string_from_str", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_view_string_from_str(ZViewString* viewString, byte* str); + + [DllImport(DllName, EntryPoint = "z_view_string_from_substr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_view_string_from_substr(ZViewString* viewString, byte* str, nuint len); + + [DllImport(DllName, EntryPoint = "z_view_string_is_empty", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool z_view_string_is_empty(ZViewString* viewString); + + [DllImport(DllName, EntryPoint = "z_view_string_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZLoanedString* z_view_string_loan(ZViewString* viewString); + + [DllImport(DllName, EntryPoint = "z_whatami_to_view_string", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult z_whatami_to_view_string(Whatami whatami, ZViewString* strOut); + + [DllImport(DllName, EntryPoint = "zc_closure_log", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void zc_closure_log( + ZcOwnedClosureLog* closure, + delegate* call, + delegate* drop, + void* context + ); + + [DllImport(DllName, EntryPoint = "zc_closure_log_call", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void zc_closure_log_call(ZcLoanedClosureLog* closure, ZcLogSeverity serverity, + ZLoanedString* msg); + + [DllImport(DllName, EntryPoint = "zc_closure_log_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void zc_closure_log_call_drop(ZcMovedClosureLog* closure); + + [DllImport(DllName, EntryPoint = "zc_closure_log_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZcLoanedClosureLog* zc_closure_log_call_loan(ZcOwnedClosureLog* closure); + + [DllImport(DllName, EntryPoint = "zc_config_from_env", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult zc_config_from_env(ZOwnedConfig* config); + + [DllImport(DllName, EntryPoint = "zc_config_from_file", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult zc_config_from_file(ZOwnedConfig* config, byte* path); + + [DllImport(DllName, EntryPoint = "zc_config_from_str", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult zc_config_from_str(ZOwnedConfig* config, byte* str); + + [DllImport(DllName, EntryPoint = "zc_config_get_from_str", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult zc_config_get_from_str(ZLoanedConfig* config, byte* key, + ZOwnedString* outValueString); + + [DllImport(DllName, EntryPoint = "zc_config_get_from_substr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult zc_config_get_from_substr(ZLoanedConfig* config, byte* key, nuint keyLen, + ZOwnedString* outValueString); + + [DllImport(DllName, EntryPoint = "zc_config_insert_json5", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult zc_config_insert_json5(ZLoanedConfig* config, byte* key, byte* value); + + [DllImport(DllName, EntryPoint = "zc_config_insert_json5_from_substr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult zc_config_insert_json5_from_substr(ZLoanedConfig* config, byte* key, nuint keyLen, + byte* value, nuint valueLen); + + [DllImport(DllName, EntryPoint = "zc_config_to_string", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult zc_config_to_string(ZLoanedConfig* config, ZOwnedString* outConfigString); + + [DllImport(DllName, EntryPoint = "zc_try_init_log_from_env", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void zc_try_init_log_from_env(); + + [DllImport(DllName, EntryPoint = "zc_init_log_from_env_or", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult zc_init_log_from_env_or(byte* fallbackFilter); + + [DllImport(DllName, EntryPoint = "zc_init_log_with_callback", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void zc_init_log_with_callback(ZcLogSeverity minSeverity, ZcMovedClosureLog* callback); + + [DllImport(DllName, EntryPoint = "zc_internal_closure_log_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool zc_internal_closure_log_check(ZcOwnedClosureLog* closure); + + [DllImport(DllName, EntryPoint = "zc_internal_closure_log_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void zc_internal_closure_log_null(ZcOwnedClosureLog* closure); + + [DllImport(DllName, EntryPoint = "zc_stop_z_runtime", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void zc_stop_z_runtime(); + + [DllImport(DllName, EntryPoint = "ze_deserialize_bool", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserialize_bool(ZLoanedBytes* bytes, byte* dst); + + [DllImport(DllName, EntryPoint = "ze_deserialize_double", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserialize_double(ZLoanedBytes* bytes, double* dst); + + [DllImport(DllName, EntryPoint = "ze_deserialize_float", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserialize_float(ZLoanedBytes* bytes, float* dst); + + [DllImport(DllName, EntryPoint = "ze_deserialize_int8", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserialize_int8(ZLoanedBytes* bytes, sbyte* dst); + + [DllImport(DllName, EntryPoint = "ze_deserialize_int16", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserialize_int16(ZLoanedBytes* bytes, short* dst); + + [DllImport(DllName, EntryPoint = "ze_deserialize_int32", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserialize_int32(ZLoanedBytes* bytes, int* dst); + + [DllImport(DllName, EntryPoint = "ze_deserialize_int64", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserialize_int64(ZLoanedBytes* bytes, long* dst); + + [DllImport(DllName, EntryPoint = "ze_deserialize_uint8", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserialize_uint8(ZLoanedBytes* bytes, byte* dst); + + [DllImport(DllName, EntryPoint = "ze_deserialize_uint16", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserialize_uint16(ZLoanedBytes* bytes, ushort* dst); + + [DllImport(DllName, EntryPoint = "ze_deserialize_uint32", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserialize_uint32(ZLoanedBytes* bytes, uint* dst); + + [DllImport(DllName, EntryPoint = "ze_deserialize_uint64", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserialize_uint64(ZLoanedBytes* bytes, ulong* dst); + + [DllImport(DllName, EntryPoint = "ze_deserialize_slice", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserialize_slice(ZLoanedBytes* bytes, ZOwnedSlice* slice); + + [DllImport(DllName, EntryPoint = "ze_deserialize_string", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserialize_string(ZLoanedBytes* bytes, ZOwnedString* str); + + [DllImport(DllName, EntryPoint = "ze_deserializer_deserialize_bool", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserializer_deserialize_bool(ZeDeserializer* deserializer, byte* dst); + + [DllImport(DllName, EntryPoint = "ze_deserializer_deserialize_double", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserializer_deserialize_double(ZeDeserializer* deserializer, double* dst); + + [DllImport(DllName, EntryPoint = "ze_deserializer_deserialize_float", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserializer_deserialize_float(ZeDeserializer* deserializer, float* dst); + + [DllImport(DllName, EntryPoint = "ze_deserializer_deserialize_int8", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserializer_deserialize_int8(ZeDeserializer* deserializer, sbyte* dst); + + [DllImport(DllName, EntryPoint = "ze_deserializer_deserialize_int16", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserializer_deserialize_int16(ZeDeserializer* deserializer, short* dst); + + [DllImport(DllName, EntryPoint = "ze_deserializer_deserialize_int32", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserializer_deserialize_int32(ZeDeserializer* deserializer, int* dst); + + [DllImport(DllName, EntryPoint = "ze_deserializer_deserialize_int64", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserializer_deserialize_int64(ZeDeserializer* deserializer, long* dst); + + [DllImport(DllName, EntryPoint = "ze_deserializer_deserialize_uint8", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserializer_deserialize_uint8(ZeDeserializer* deserializer, byte* dst); + + [DllImport(DllName, EntryPoint = "ze_deserializer_deserialize_uint16", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserializer_deserialize_uint16(ZeDeserializer* deserializer, ushort* dst); + + [DllImport(DllName, EntryPoint = "ze_deserializer_deserialize_uint32", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserializer_deserialize_uint32(ZeDeserializer* deserializer, uint* dst); + + [DllImport(DllName, EntryPoint = "ze_deserializer_deserialize_uint64", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserializer_deserialize_uint64(ZeDeserializer* deserializer, ulong* dst); + + [DllImport(DllName, EntryPoint = "ze_deserializer_deserialize_sequence_length", + CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult + ze_deserializer_deserialize_sequence_length(ZeDeserializer* deserializer, nuint* len); + + [DllImport(DllName, EntryPoint = "ze_deserializer_deserialize_slice", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserializer_deserialize_slice(ZeDeserializer* deserializer, ZOwnedSlice* slice); + + [DllImport(DllName, EntryPoint = "ze_deserializer_deserialize_string", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_deserializer_deserialize_string(ZeDeserializer* deserializer, ZOwnedString* str); + + [DllImport(DllName, EntryPoint = "ze_deserializer_from_bytes", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZeDeserializer ze_deserializer_from_bytes(ZLoanedBytes* bytes); + + [DllImport(DllName, EntryPoint = "ze_deserializer_is_done", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool ze_deserializer_is_done(ZeDeserializer* deserializer); + + [DllImport(DllName, EntryPoint = "ze_internal_serializer_check", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + [return: MarshalAs(UnmanagedType.U1)] + internal static extern bool ze_internal_serializer_check(ZeOwnedSerializer* serializer); + + [DllImport(DllName, EntryPoint = "ze_internal_serializer_null", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void ze_internal_serializer_null(ZeOwnedSerializer* serializer); + + [DllImport(DllName, EntryPoint = "ze_serialize_buf", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serialize_buf(ZOwnedBytes* bytes, byte* data, nuint len); + + [DllImport(DllName, EntryPoint = "ze_serialize_bool", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serialize_bool(ZOwnedBytes* bytes, byte val); + + [DllImport(DllName, EntryPoint = "ze_serialize_double", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serialize_double(ZOwnedBytes* bytes, double val); + + [DllImport(DllName, EntryPoint = "ze_serialize_float", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serialize_float(ZOwnedBytes* bytes, float val); + + [DllImport(DllName, EntryPoint = "ze_serialize_int8", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serialize_int8(ZOwnedBytes* bytes, sbyte val); + + [DllImport(DllName, EntryPoint = "ze_serialize_int16", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serialize_int16(ZOwnedBytes* bytes, short val); + + [DllImport(DllName, EntryPoint = "ze_serialize_int32", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serialize_int32(ZOwnedBytes* bytes, int val); + + [DllImport(DllName, EntryPoint = "ze_serialize_int64", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serialize_int64(ZOwnedBytes* bytes, long val); + + [DllImport(DllName, EntryPoint = "ze_serialize_uint8", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serialize_uint8(ZOwnedBytes* bytes, byte val); + + [DllImport(DllName, EntryPoint = "ze_serialize_uint16", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serialize_uint16(ZOwnedBytes* bytes, ushort val); + + [DllImport(DllName, EntryPoint = "ze_serialize_uint32", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serialize_uint32(ZOwnedBytes* bytes, uint val); + + [DllImport(DllName, EntryPoint = "ze_serialize_uint64", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serialize_uint64(ZOwnedBytes* bytes, ulong val); + + [DllImport(DllName, EntryPoint = "ze_serialize_slice", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serialize_slice(ZOwnedBytes* bytes, ZLoanedSlice* slice); + + [DllImport(DllName, EntryPoint = "ze_serialize_str", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serialize_str(ZOwnedBytes* bytes, byte* str); + + [DllImport(DllName, EntryPoint = "ze_serialize_substr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serialize_substr(ZOwnedBytes* bytes, byte* start, nuint len); + + [DllImport(DllName, EntryPoint = "ze_serialize_string", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serialize_string(ZOwnedBytes* bytes, ZLoanedString* str); + + [DllImport(DllName, EntryPoint = "ze_serializer_drop", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void ze_serializer_drop(ZeMovedSerializer* serializer); + + [DllImport(DllName, EntryPoint = "ze_serializer_empty", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_empty(ZeOwnedSerializer* serializer); + + [DllImport(DllName, EntryPoint = "ze_serializer_finish", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern void ze_serializer_finish(ZeMovedSerializer* serializer, ZOwnedBytes* bytes); + + [DllImport(DllName, EntryPoint = "ze_serializer_loan", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZeLoanedSerializer* ze_serializer_loan(ZeOwnedSerializer* serializer); + + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_bool", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_bool(ZeLoanedSerializer* serializer, byte val); + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_double", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_double(ZeLoanedSerializer* serializer, double val); + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_float", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_float(ZeLoanedSerializer* serializer, float val); + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_int8", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_int8(ZeLoanedSerializer* serializer, sbyte val); + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_int16", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_int16(ZeLoanedSerializer* serializer, short val); + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_int32", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_int32(ZeLoanedSerializer* serializer, int val); + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_int64", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_int64(ZeLoanedSerializer* serializer, long val); + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_uint8", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_uint8(ZeLoanedSerializer* serializer, byte val); + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_uint16", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_uint16(ZeLoanedSerializer* serializer, ushort val); + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_uint32", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_uint32(ZeLoanedSerializer* serializer, uint val); + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_uint64", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_uint64(ZeLoanedSerializer* serializer, ulong val); + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_buf", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_buf(ZeLoanedSerializer* serializer, byte* data, nuint len); + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_sequence_length", + CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_sequence_length(ZeLoanedSerializer* serializer, nuint len); + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_slice", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_slice(ZeLoanedSerializer* serializer, ZLoanedSlice* slice); + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_str", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_str(ZeLoanedSerializer* serializer, byte* str); + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_substr", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_substr(ZeLoanedSerializer* serializer, byte* start, + nuint len); + + [DllImport(DllName, EntryPoint = "ze_serializer_serialize_string", CallingConvention = CallingConvention.Cdecl, + ExactSpelling = true)] + internal static extern ZResult ze_serializer_serialize_string(ZeLoanedSerializer* serializer, ZLoanedString* str); } \ No newline at end of file