Skip to content

Commit

Permalink
Added: Missing documentation from functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Jul 5, 2022
1 parent a63c8eb commit a129ea5
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace Reloaded.Messaging.Serializer.ReloadedMemory
{
/// <summary>
/// Serializes messages using raw byte conversion with Reloaded.Memory.
/// </summary>
public class ReloadedMemorySerializer : ISerializer
{
/// <summary>
Expand Down
10 changes: 6 additions & 4 deletions Source/Reloaded.Messaging/MessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ namespace Reloaded.Messaging
{
/// <summary>
/// Provides a generic mechanism for dispatching messages received from a client or server.
/// Works by assigning functions to specified message "types", declared by <see cref="TMessageType"/>.
/// Works by assigning functions to specified message "types", declared by TMessageType.
/// </summary>
/// <typeparam name="TMessageType">Type of value to map to individual message handlers.</typeparam>
public class MessageHandler<TMessageType> where TMessageType : unmanaged
{
private Dictionary<TMessageType, RawNetMessageHandler> _mapping;

/// <summary/>
public MessageHandler()
{
_mapping = new Dictionary<TMessageType, RawNetMessageHandler>();
Expand All @@ -32,7 +33,7 @@ public void Handle(ref RawNetMessage parameters)
}

/// <summary>
/// Sets a method to execute handling a specific <see cref="TMessageType"/>
/// Sets a method to execute handling a specific <typeparamref name="TMessageType"/>
/// </summary>
public void AddOrOverrideHandler<TStruct>(Handler<TStruct> handler) where TStruct : IMessage<TMessageType>, new()
{
Expand All @@ -41,7 +42,7 @@ public void Handle(ref RawNetMessage parameters)
}

/// <summary>
/// Sets a method to execute handling a specific <see cref="TMessageType"/>
/// Sets a method to execute handling a specific <typeparamref name="TMessageType"/>
/// </summary>
public void AddOrOverrideHandler<TStruct>(TMessageType messageType, Handler<TStruct> handler) where TStruct : IMessage<TMessageType>, new()
{
Expand All @@ -56,13 +57,14 @@ public void Handle(ref RawNetMessage parameters)
}

/// <summary>
/// Removes the current method assigned to a handle a message of a specific <see cref="TMessageType"/>
/// Removes the current method assigned to a handle a message of a specific <typeparamref name="TMessageType"/>
/// </summary>
public void RemoveHandler(TMessageType messageType)
{
_mapping.Remove(messageType);
}

/// <summary/>
public delegate void Handler<TStruct>(ref NetMessage<TStruct> netMessage);
private delegate void RawNetMessageHandler(ref RawNetMessage rawNetMessage);
}
Expand Down
8 changes: 8 additions & 0 deletions Source/Reloaded.Messaging/Messages/IMessageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

namespace Reloaded.Messaging.Messages
{
/// <summary>
/// Hosts various extension methods related to the <see cref="IMessage{TMessageType}"/> interface.
/// </summary>
public static class MessageExtensions
{
/// <summary>
/// Retrieves the message type (key) for a given type.
/// </summary>
/// <param name="message">The message to get type for.</param>
/// <returns>Used to instantiate and get message type (key) as single liner in source code.</returns>
public static TMessageType GetMessageType<TMessageType>(this IMessage<TMessageType> message) where TMessageType : unmanaged
{
return message.GetMessageType();
Expand Down
16 changes: 14 additions & 2 deletions Source/Reloaded.Messaging/Messages/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,29 @@

namespace Reloaded.Messaging.Messages
{
/// <summary>
///
/// </summary>
/// <typeparam name="TMessageType">The key for the message.</typeparam>
/// <typeparam name="TStruct"></typeparam>
public unsafe class Message<TMessageType, TStruct> : MessageBase<TMessageType> where TStruct : IMessage<TMessageType>, new() where TMessageType : unmanaged
{
// ReSharper disable StaticMemberInGenericType
private static ISerializer DefaultSerializer { get; set; }
private static ICompressor DefaultCompressor { get; set; }
private static bool DefaultCompressorSet { get; set; }

// ReSharper restore StaticMemberInGenericType

/// <summary>
/// The actual message backed by this class.
/// </summary>
public TStruct ActualMessage { get; set; }

/// <summary>
///
/// </summary>
/// <param name="message"></param>
public Message(TStruct message)
{
ActualMessage = message;
Expand Down Expand Up @@ -69,10 +82,9 @@ public static TStruct Deserialize(byte[] serializedBytes)
// Get serializer
var serializer = GetSerializer();

// Read messagepack message.
// Read message.
var messageSegment = serializedBytes.AsSpan(sizeof(TMessageType)).ToArray();
var message = serializer.Deserialize<TStruct>(messageSegment);

return message;

// Note: No need to read MessageType. MessageType was only necessary to link a message to correct handler.
Expand Down
9 changes: 9 additions & 0 deletions Source/Reloaded.Messaging/Messages/MessageBase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
namespace Reloaded.Messaging.Messages
{
/// <summary>
/// The base class for the <see cref="Message{TMessageType,TStruct}"/> class, used for dealing with message type specific things.
/// </summary>
/// <typeparam name="TMessageType"></typeparam>
public unsafe class MessageBase<TMessageType> where TMessageType : unmanaged
{
/// <summary>
/// Retrieves the message type by raw array of values.
/// </summary>
/// <param name="serializedBytes">The raw serialized bytes containing the message.</param>
/// <returns>The type of the message.</returns>
public static TMessageType GetMessageType(byte[] serializedBytes)
{
fixed (byte* arrayPtr = serializedBytes)
Expand Down
7 changes: 7 additions & 0 deletions Source/Reloaded.Messaging/Overrides.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ namespace Reloaded.Messaging
/// </summary>
public static class Overrides
{
/// <summary>
/// Allows you to override the serializer for a specific type.
/// </summary>
public static Dictionary<Type, ISerializer> SerializerOverride { get; } = new Dictionary<Type, ISerializer>();

/// <summary>
/// Allows you to override the compressor for a specific type.
/// </summary>
public static Dictionary<Type, ICompressor> CompressorOverride { get; } = new Dictionary<Type, ICompressor>();
}
}
8 changes: 6 additions & 2 deletions Source/Reloaded.Messaging/SimpleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class SimpleHost<TMessageType> : IDisposable where TMessageType : unmanag
public event EventBasedNetListener.OnConnectionRequest ConnectionRequestEvent;

/// <summary>
/// Dispatcher for individual <see cref="TMessageType"/>(s) to your events.
/// Dispatcher for individual <typeparamref name="TMessageType"/>(s) to your events.
/// </summary>
public MessageHandler<TMessageType> MessageHandler { get; private set; }

Expand All @@ -35,7 +35,11 @@ public class SimpleHost<TMessageType> : IDisposable where TMessageType : unmanag
/// <summary/>
public NetManager NetManager { get; private set; }


/// <summary>
/// Provides a simple client or host based off of LiteNetLib.
/// </summary>
/// <param name="acceptClients">Set to true to accept incoming clients, else reject all requests.</param>
/// <param name="password">The password necessary to join.</param>
public SimpleHost(bool acceptClients, string password = "")
{
Password = password;
Expand Down
23 changes: 23 additions & 0 deletions Source/Reloaded.Messaging/Structs/NetMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@

namespace Reloaded.Messaging.Structs
{
/// <summary>
/// Encapsulates a message with a specific structure received from the network as a raw array of bytes.
/// </summary>
public struct NetMessage<TStruct>
{
/// <summary>
/// The message received from the peer.
/// </summary>
public TStruct Message { get; private set; }

/// <summary>
/// The peer which has sent you the message.
/// </summary>
public NetPeer Peer { get; private set; }

/// <summary>
/// Can be used to read the message, if desired.
/// </summary>
public NetPacketReader PacketReader { get; private set; }

/// <summary>
/// The method via which the package was delivered.
/// </summary>
public DeliveryMethod DeliveryMethod { get; private set; }

/// <summary>
/// Encapsulates a raw message received from the network.
/// </summary>
/// <param name="message">The message in question.</param>
/// <param name="rawMessage">The raw message from which this message should be constructed with.</param>
public NetMessage(ref TStruct message, ref RawNetMessage rawMessage)
{
Message = message;
Expand Down
25 changes: 25 additions & 0 deletions Source/Reloaded.Messaging/Structs/RawNetMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,38 @@

namespace Reloaded.Messaging.Structs
{
/// <summary>
/// Encapsulates a message received from the network as a raw array of bytes.
/// </summary>
public struct RawNetMessage
{
/// <summary>
/// The contents of the message.
/// </summary>
public byte[] Message { get; private set; }

/// <summary>
/// The peer from whom the message was received from.
/// </summary>
public NetPeer Peer { get; private set; }

/// <summary>
/// Used to read the packet internals, if desired.
/// </summary>
public NetPacketReader PacketReader { get; private set; }

/// <summary>
/// The method via which this message was delivered.
/// </summary>
public DeliveryMethod DeliveryMethod { get; private set; }

/// <summary>
/// Encapsulates the message received from the network as a raw array of bytes.
/// </summary>
/// <param name="message">The message in question.</param>
/// <param name="peer">The peer from whom the message was received from.</param>
/// <param name="packetReader">Used to read packet internals, if desired.</param>
/// <param name="deliveryMethod">The method via which the message was delivered.</param>
public RawNetMessage(byte[] message, NetPeer peer, NetPacketReader packetReader, DeliveryMethod deliveryMethod)
{
Message = message;
Expand Down

0 comments on commit a129ea5

Please sign in to comment.