Skip to content

Commit

Permalink
Don't throw InvalidOperationException when native function is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
mgnsm committed Apr 17, 2023
1 parent 3d3a82b commit d7371ef
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 184 deletions.
22 changes: 4 additions & 18 deletions Source/Millistream.Streaming/MarketDataFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,28 +303,14 @@ public bool HandleDelay
/// <summary>
/// Gets the intended delay of the current message if delay-mode have been activated by setting the <see cref="HandleDelay"/> property. Note that this is the intended delay of the message and not necessarily the real delay, network latency, server latency and so on are not included.
/// </summary>
/// <exception cref="InvalidOperationException">The installed version of the native library doesn't include the mdf_get_delay function.</exception>
public byte Delay
{
get
{
Message.ThrowIfNativeFunctionIsMissing(_nativeImplementation.mdf_get_delay, nameof(_nativeImplementation.mdf_get_delay));
return _nativeImplementation.mdf_get_delay(_feedHandle);
}
}
/// <remarks>The corresponding native function is mdf_get_delay.</remarks>
public byte Delay => _nativeImplementation.mdf_get_delay != default ? _nativeImplementation.mdf_get_delay(_feedHandle) : default;

/// <summary>
/// Gets the message class of the current received message.
/// </summary>
/// <exception cref="InvalidOperationException">The installed version of the native library doesn't include the mdf_get_mclass function.</exception>
public ulong MessageClass
{
get
{
Message.ThrowIfNativeFunctionIsMissing(_nativeImplementation.mdf_get_mclass, nameof(_nativeImplementation.mdf_get_mclass));
return _nativeImplementation.mdf_get_mclass(_feedHandle);
}
}
/// <remarks>The corresponding native function is mdf_get_mclass.</remarks>
public ulong MessageClass => _nativeImplementation.mdf_get_mclass != default ? _nativeImplementation.mdf_get_mclass(_feedHandle) : default;
#endregion

#region Methods
Expand Down
11 changes: 4 additions & 7 deletions Source/Millistream.Streaming/Message.ByteOverloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ public bool AddString(uint tag, ReadOnlySpan<byte> value)
/// <param name="value">The field value as a memory span that contains a null-terminated sequence of UTF-8 encoded bytes.</param>
/// <param name="length">The number of bytes in <paramref name="value"/> to be added to the message.</param>
/// <returns><see langword="true" /> if the field was successfully added, or <see langword="false" /> if the value could not be added (because there was no more memory, the message handle does not contain any messages, or the supplied value is not of the type specified).</returns>
/// <exception cref="InvalidOperationException">The installed version of the native library doesn't include the mdf_message_add_string2 function.</exception>
/// <remarks>The corresponding native function is mdf_message_add_string2.</remarks>
public bool AddString(uint tag, ReadOnlySpan<byte> value, int length)
{
if (value != null && length < 0)
if (_nativeImplementation.mdf_message_add_string2 == default || (value != null && length < 0))
return false;
ThrowIfNativeFunctionIsMissing(_nativeImplementation.mdf_message_add_string2, nameof(_nativeImplementation.mdf_message_add_string2));

fixed (byte* bytes = value)
return _nativeImplementation.mdf_message_add_string2(Handle, tag, (IntPtr)bytes, length) == 1;
}
Expand All @@ -78,7 +77,6 @@ public bool AddString(Field tag, ReadOnlySpan<byte> value) =>
/// <param name="value">The field value as a memory span that contains a null-terminated sequence of UTF-8 encoded bytes.</param>
/// <param name="length">The number of bytes in <paramref name="value"/> to be added to the message.</param>
/// <returns><see langword="true" /> if the field was successfully added, or <see langword="false" /> if the value could not be added (because there was no more memory, the message handle does not contain any messages, or the supplied value is not of the type specified).</returns>
/// <exception cref="InvalidOperationException">The installed version of the native library doesn't include the mdf_message_add_string2 function.</exception>
/// <remarks>The corresponding native function is mdf_message_add_string2.</remarks>
public bool AddString(Field tag, ReadOnlySpan<byte> value, int length) =>
AddString((uint)tag, value, length);
Expand Down Expand Up @@ -169,13 +167,12 @@ public bool AddList(Field tag, ReadOnlySpan<byte> value) =>
/// </summary>
/// <param name="data">A memory span that contains a base64 encoded (serialized) message chain.</param>
/// <returns><see langword="true" /> if the message chain was successfully deserialized, or <see langword="false" /> if the deserialization failed (if so the current message chain in the message handler is left untouched).</returns>
/// <exception cref="InvalidOperationException">The installed version of the native library doesn't include the mdf_message_deserialize function.</exception>
/// <remarks>The corresponding native function is mdf_message_deserialize.</remarks>
public bool Deserialize(ReadOnlySpan<byte> data)
{
ThrowIfNativeFunctionIsMissing(_nativeImplementation.mdf_message_deserialize, nameof(_nativeImplementation.mdf_message_deserialize));
fixed (byte* bytes = data)
return _nativeImplementation.mdf_message_deserialize(Handle, (IntPtr)bytes) == 1;
return _nativeImplementation.mdf_message_deserialize != default
&& _nativeImplementation.mdf_message_deserialize(Handle, (IntPtr)bytes) == 1;
}
}
}
Loading

0 comments on commit d7371ef

Please sign in to comment.