From 155386abe0f11868f934a144a599453bbbc907b2 Mon Sep 17 00:00:00 2001 From: barth Date: Sat, 18 Nov 2023 09:54:25 +0100 Subject: [PATCH 1/2] Add factory method to create a Message by from tag and IMessageBuffer --- DarkRift/Message.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/DarkRift/Message.cs b/DarkRift/Message.cs index 44cc453..e6e6e0d 100644 --- a/DarkRift/Message.cs +++ b/DarkRift/Message.cs @@ -189,13 +189,23 @@ public static Message CreateEmpty(ushort tag) /// The tag the message has. /// The initial data in the message. public static Message Create(ushort tag, DarkRiftWriter writer) + { + return Create(tag, writer.ToBuffer()); + } + + /// + /// Creates a new message with the given tag and message buffer. + /// + /// The tag the message has. + /// The initial data in the message. + public static Message Create(ushort tag, IMessageBuffer buffer) { Message message = ObjectCache.GetMessage(); message.isCurrentlyLoungingInAPool = false; message.IsReadOnly = false; - message.buffer = writer.ToBuffer(); + message.buffer = buffer; message.tag = tag; message.flags = 0; message.PingCode = 0; @@ -268,7 +278,7 @@ internal static Message Create(IMessageBuffer buffer, bool isReadOnly) // We clone the message buffer so we can modify it's properties safely message.buffer = buffer.Clone(); - + //Get flags first so we can query it message.flags = buffer.Buffer[buffer.Offset]; @@ -289,7 +299,7 @@ internal static Message Create(IMessageBuffer buffer, bool isReadOnly) /// internal Message() { - + } /// @@ -448,7 +458,7 @@ public Message Clone() //We don't want to give a reference to our buffer so we need to clone it message.buffer = buffer.Clone(); - + message.flags = flags; message.tag = tag; message.PingCode = PingCode; From 6d89303a2513e76583193e3954aa7e3e10f9b13e Mon Sep 17 00:00:00 2001 From: barth Date: Sun, 19 Nov 2023 09:57:19 +0100 Subject: [PATCH 2/2] Hide factory method to create Message from buffer and added a method allowing to create one from byte[] directly --- DarkRift/Message.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/DarkRift/Message.cs b/DarkRift/Message.cs index e6e6e0d..8680dd1 100644 --- a/DarkRift/Message.cs +++ b/DarkRift/Message.cs @@ -193,12 +193,22 @@ public static Message Create(ushort tag, DarkRiftWriter writer) return Create(tag, writer.ToBuffer()); } + /// + /// Creates a new message with the given tag and data. + /// + /// The tag the message has. + /// The initial data in the message. + public static Message Create(ushort tag, byte[] data) + { + return Create(tag, new UnmanagedMemoryBuffer(data, 0, data.Length)); + } + /// /// Creates a new message with the given tag and message buffer. /// /// The tag the message has. /// The initial data in the message. - public static Message Create(ushort tag, IMessageBuffer buffer) + private static Message Create(ushort tag, IMessageBuffer buffer) { Message message = ObjectCache.GetMessage();