Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change how MessageReader assigns its type #9

Merged
merged 2 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions QuickLink/Messages/MessageReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public class MessageReader
/// <param name="buffer">The byte buffer to read from.</param>
public MessageReader(byte[] buffer)
{
_buffer = buffer;
_buffer = new byte[buffer.Length - 4];
Array.Copy(buffer, 4, _buffer, 0, _buffer.Length);
_offset = 0;

Type = MessageType.Get(ReadUInt32());
Type = MessageType.Get(BitConverter.ToUInt32(buffer, 0));
}

private void EnsureCanReadLength(int length)
Expand Down
28 changes: 19 additions & 9 deletions QuickLink/Messages/MessageType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,28 @@ public class MessageType
/// </summary>
public string Name { get; private set; }

private MessageType(uint id)
{
Id = id;
Name = "UNKNOWN";
}

/// <summary>
/// Gets the message type with the specified identifier.
/// </summary>
/// <param name="id">The identifier of the message type.</param>
/// <returns>The message type with the specified identifier.</returns>
public static MessageType Get(uint id)
{
return _messageTypes[id];
}
lock (_lock)
{
if (!_messageTypes.ContainsKey(id))
{
_messageTypes.Add(id, new MessageType(id));
}

private MessageType(uint id, string name)
{
Id = id;
Name = name;
return _messageTypes[id];
}
}

/// <summary>
Expand All @@ -48,12 +56,14 @@ public static MessageType Get(string name)
lock (_lock)
{
uint id = CRC32.GenerateHash(name);
if (!_messageTypes.ContainsKey(id))
MessageType messageType = Get(id);

if (messageType.Name == "UNKNOWN")
{
_messageTypes.Add(id, new MessageType(id, name));
messageType.Name = name;
}

return _messageTypes[id];
return messageType;
}
}
}
Expand Down