From 3a5d05cb21e534552e72e0e748784897257cb0d6 Mon Sep 17 00:00:00 2001 From: NZSmartie Date: Sun, 3 Dec 2017 19:57:10 +1300 Subject: [PATCH] Return a placeholder option to give the application chance at reading them --- src/CoAPNet/Options/OptionFactory.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/CoAPNet/Options/OptionFactory.cs b/src/CoAPNet/Options/OptionFactory.cs index 709dbc5..43c5150 100644 --- a/src/CoAPNet/Options/OptionFactory.cs +++ b/src/CoAPNet/Options/OptionFactory.cs @@ -85,16 +85,22 @@ public void Register(params Type[] addtionalOptions) /// If the option number is unsuppported and is critical (See RFC 7252 Section 5.4.1) public CoapOption Create(int number, byte[] data = null) { + CoapOption option; // Let the exception get thrown if index is out of range - Type type = null; - if (!_options.TryGetValue(number, out type)) + if (_options.TryGetValue(number, out var type)) { + option = (CoapOption)Activator.CreateInstance(type); + } + else + { + // Critial option must be registered as they're esssential for understanding the message if (number % 2 == 1) throw new CoapOptionException($"Unsupported critical option ({number})", new ArgumentOutOfRangeException(nameof(number))); - return null; + + // Return a placeholder option to give the application chance at reading them + option = new CoapOption(number, type: OptionType.Opaque); } - var option = (CoapOption)Activator.CreateInstance(type); if (data != null) option.FromBytes(data);