private void GetTxtRecord(out ushort length, out byte[] value) { int ret = Interop.Nsd.Dnssd.GetAllTxtRecord(_serviceHandle, out length, out value); if (ret != (int)DnssdError.None) { Log.Error(Globals.LogTag, "Failed to get the TXT record, Error: " + (DnssdError)ret); NsdErrorFactory.ThrowDnssdException(ret); } }
/// <summary> /// Deregisters the DNS-SD local service. /// </summary> /// <remarks> /// A local service registered using RegisterService() must be passed. /// </remarks> /// <since_tizen> 4 </since_tizen> /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature> /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception> /// <exception cref="NotSupportedException">Thrown when DNS-SD is not supported.</exception> public void DeregisterService() { int ret = Interop.Nsd.Dnssd.DeregisterService(_serviceHandle); if (ret != (int)DnssdError.None) { Log.Error(Globals.LogTag, "Failed to deregister the Dnssd local service, Error: " + (DnssdError)ret); NsdErrorFactory.ThrowDnssdException(ret); } }
/// <summary> /// Stops browsing the DNSSD remote service. /// </summary> /// <since_tizen> 4 </since_tizen> /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature> /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception> /// <exception cref="NotSupportedException">Thrown when DNSSD is not supported.</exception> public void StopDiscovery() { int ret = Interop.Nsd.Dnssd.StopBrowsing(_browserHandle); if (ret != (int)DnssdError.None) { Log.Error(Globals.LogTag, "Failed to stop discovery of Dnssd remote service, Error - " + (DnssdError)ret); NsdErrorFactory.ThrowDnssdException(ret); } }
internal static void DnssdInitialize() { int ret = Interop.Nsd.Dnssd.Initialize(); if (ret != (int)DnssdError.None) { Log.Error(LogTag, "Failed to initialize Dnssd, Error - " + (DnssdError)ret); NsdErrorFactory.ThrowDnssdException(ret); } }
/// <summary> /// Removes the TXT record. /// </summary> /// <since_tizen> 4 </since_tizen> /// <param name="key">The key of the TXT record to be removed.</param> /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature> /// <exception cref="NotSupportedException">Thrown when DNS-SD is not supported.</exception> /// <exception cref="ArgumentException">Thrown when the value of the key is null.</exception> /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception> public void RemoveTXTRecord(string key) { int ret = Interop.Nsd.Dnssd.RemoveTxtRecord(_serviceHandle, key); if (ret != (int)DnssdError.None) { Log.Error(Globals.LogTag, "Failed to remove the TXT record, Error: " + (DnssdError)ret); NsdErrorFactory.ThrowDnssdException(ret); } }
internal void DnssdInitializeCreateService() { DnssdInitializer dnssdInit = Globals.s_threadDns.Value; Log.Info(Globals.LogTag, "Initialize ThreadLocal<DnssdInitializer> instance = " + dnssdInit); int ret = Interop.Nsd.Dnssd.CreateService(_serviceType, out _serviceHandle); if (ret != (int)DnssdError.None) { Log.Error(Globals.LogTag, "Failed to create a local Dnssd service handle, Error - " + (DnssdError)ret); NsdErrorFactory.ThrowDnssdException(ret); } }
/// <summary> /// A public constructor for DnssdBrowser class to create a DnssdBrowser instance for the given service type. /// </summary> /// <param name="serviceType">The DNSSD service type</param> /// <since_tizen> 4 </since_tizen> /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature> /// <exception cref="ArgumentException">Thrown when serviceType is null.</exception> /// <exception cref="NotSupportedException">Thrown when DNSSD is not supported.</exception> public DnssdBrowser(string serviceType) { DnssdInitializer dnssdInit = Globals.s_threadDns.Value; Log.Info(Globals.LogTag, "Initialize ThreadLocal<DnssdInitializer> instance = " + dnssdInit); if (serviceType == null) { Log.Debug(Globals.LogTag, "serviceType is null"); NsdErrorFactory.ThrowDnssdException((int)DnssdError.InvalidParameter); } _serviceType = serviceType; }
/// <summary> /// Returns raw TXT records. /// </summary> /// <returns>Returns empty bytes array in case TXT record has not been set, else returns raw TXT record.</returns> /// <since_tizen> 9 </since_tizen> /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature> /// <exception cref="NotSupportedException">Thrown when DNS-SD is not supported.</exception> /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception> public byte[] GetRawTXTRecords() { int ret = Interop.Nsd.Dnssd.GetAllTxtRecord(_serviceHandle, out ushort length, out IntPtr data); if (ret != (int)DnssdError.None) { Log.Error(Globals.LogTag, "Failed to get the TXT record, Error: " + (DnssdError)ret); NsdErrorFactory.ThrowDnssdException(ret); } byte[] value = Array.Empty <byte>(); if (length > 0) { value = new byte[length]; Marshal.Copy(data, value, 0, length); Interop.Libc.Free(data); } return(value); }
/// <summary> /// Adds the TXT record. /// </summary> /// <remarks> /// TXT record should be added after registering the local service using RegisterService(). /// </remarks> /// <since_tizen> 4 </since_tizen> /// <param name="key">The key of the TXT record. It must be a null-terminated string with 9 characters or fewer excluding null. It is case insensitive.</param> /// <param name="value">The value of the TXT record. If null, then "key" will be added with no value. If non-null but the value_length is zero, then "key=" will be added with an empty value.</param> /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature> /// <exception cref="NotSupportedException">Thrown when DNS-SD is not supported.</exception> /// <exception cref="ArgumentException">Thrown when the value of the key is null.</exception> /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception> public void AddTXTRecord(string key, string value) { byte[] byteValue = Encoding.UTF8.GetBytes(value); ushort length = Convert.ToUInt16(byteValue.Length); int ret = Interop.Nsd.Dnssd.AddTxtRecord(_serviceHandle, key, length, byteValue); if (ret != (int)DnssdError.None) { Log.Error(Globals.LogTag, "Failed to add the TXT record, Error: " + (DnssdError)ret); NsdErrorFactory.ThrowDnssdException(ret); } byte[] txtValue = GetRawTXTRecords(); ret = Interop.Nsd.Dnssd.SetRecord(_serviceHandle, _dnsRecordtype, (ushort)txtValue.Length, txtValue); if (ret != (int)DnssdError.None) { Log.Error(Globals.LogTag, "Failed to set the DNS resource record, Error: " + (DnssdError)ret); NsdErrorFactory.ThrowDnssdException(ret); } }
/// <summary> /// Registers the DNS-SD local service for publishing. /// </summary> /// Name of the service must be set. /// <since_tizen> 4 </since_tizen> /// <privilege>http://tizen.org/privilege/internet</privilege> /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature> /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception> /// <exception cref="NotSupportedException">Thrown when DNS-SD is not supported.</exception> /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception> public void RegisterService() { if (!Globals.s_threadDns.IsValueCreated) { DnssdInitializeCreateService(); } _serviceRegisteredCallback = (DnssdError result, uint service, IntPtr userData) => { if (result != DnssdError.None) { Log.Error(Globals.LogTag, "Failed to finish the registration of Dnssd local service, Error: " + result); NsdErrorFactory.ThrowDnssdException((int)result); } }; int ret = Interop.Nsd.Dnssd.RegisterService(_serviceHandle, _serviceRegisteredCallback, IntPtr.Zero); if (ret != (int)DnssdError.None) { Log.Error(Globals.LogTag, "Failed to register the Dnssd local service, Error: " + (DnssdError)ret); NsdErrorFactory.ThrowDnssdException(ret); } }
/// <summary> /// Removes the TXT record. /// </summary> /// <since_tizen> 4 </since_tizen> /// <param name="key">The key of the TXT record to be removed.</param> /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature> /// <exception cref="NotSupportedException">Thrown when DNS-SD is not supported.</exception> /// <exception cref="ArgumentException">Thrown when the value of the key is null.</exception> /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception> public void RemoveTXTRecord(string key) { int ret = Interop.Nsd.Dnssd.RemoveTxtRecord(_serviceHandle, key); if (ret != (int)DnssdError.None) { Log.Error(Globals.LogTag, "Failed to remove the TXT record, Error: " + (DnssdError)ret); NsdErrorFactory.ThrowDnssdException(ret); } byte[] txtValue; ushort txtLength; GetTxtRecord(out txtLength, out txtValue); if (txtLength == 0) { ret = Interop.Nsd.Dnssd.UnsetRecord(_serviceHandle, _dnsRecordtype); if (ret != (int)DnssdError.None) { Log.Error(Globals.LogTag, "Failed to unset the DNS resource record, Error: " + (DnssdError)ret); NsdErrorFactory.ThrowDnssdException(ret); } } }
/// <summary> /// Starts browsing the DNSSD remote service. /// </summary> /// <remarks> /// If there are any services available, ServiceFound event will be invoked. /// Application will keep browsing for available/unavailable services until it calls StopDiscovery(). /// </remarks> /// <since_tizen> 4 </since_tizen> /// <privilege>http://tizen.org/privilege/internet</privilege> /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature> /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception> /// <exception cref="NotSupportedException">Thrown when DNSSD is not supported.</exception> /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception> public void StartDiscovery() { DnssdInitializer dnssdInit = Globals.s_threadDns.Value; Log.Info(Globals.LogTag, "Initialize ThreadLocal<DnssdInitializer> instance = " + dnssdInit); _serviceFoundCallback = (DnssdServiceState state, uint service, IntPtr userData) => { if (_serviceFound != null) { Log.Info(Globals.LogTag, "Inside Service found callback"); DnssdService dnsService = new DnssdService(service); _serviceFound(null, new DnssdServiceFoundEventArgs(state, dnsService)); } }; int ret = Interop.Nsd.Dnssd.StartBrowsing(_serviceType, out _browserHandle, _serviceFoundCallback, IntPtr.Zero); if (ret != (int)DnssdError.None) { Log.Error(Globals.LogTag, "Failed to discover Dnssd remote service, Error - " + (DnssdError)ret); NsdErrorFactory.ThrowDnssdException(ret); } }