Skip to content

Commit

Permalink
Updated to v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-weiland committed Dec 31, 2021
1 parent 7b489c8 commit 75068e5
Show file tree
Hide file tree
Showing 10 changed files with 482 additions and 46 deletions.
43 changes: 26 additions & 17 deletions UnityPackage/Runtime/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void ChangeTransport(IClient client)
this.client = client;
}

/// <summary>Attempts connect to the given host address.</summary>
/// <summary>Attempts to connect to the given host address.</summary>
/// <param name="hostAddress">The host address to connect to.</param>
/// <param name="messageHandlerGroupId">The ID of the group of message handler methods to use when building <see cref="messageHandlers"/>.</param>
/// <remarks>
Expand All @@ -82,20 +82,20 @@ public void Connect(string hostAddress, byte messageHandlerGroupId = 0)

CreateMessageHandlersDictionary(Assembly.GetCallingAssembly(), messageHandlerGroupId);

client.Connected += Connected;
client.Connected += OnConnected;
client.ConnectionFailed += OnConnectionFailed;
client.MessageReceived += OnMessageReceived;
client.Disconnected += OnDisconnected;
client.ClientConnected += ClientConnected;
client.ClientDisconnected += ClientDisconnected;
client.ClientConnected += OnClientConnected;
client.ClientDisconnected += OnClientDisconnected;
client.Connect(hostAddress);
}

/// <inheritdoc/>
protected override void CreateMessageHandlersDictionary(Assembly assembly, byte messageHandlerGroupId)
{
MethodInfo[] methods = assembly.GetTypes()
.SelectMany(t => t.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static))
.SelectMany(t => t.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)) // Include instance methods in the search so we can show the developer an error instead of silently not adding instance methods to the dictionary
.Where(m => m.GetCustomAttributes(typeof(MessageHandlerAttribute), false).Length > 0)
.ToArray();

Expand All @@ -107,17 +107,17 @@ protected override void CreateMessageHandlersDictionary(Assembly assembly, byte
break;

if (!methods[i].IsStatic)
{
RiptideLogger.Log(LogType.error, $"Message handler methods should be static, but '{methods[i].DeclaringType}.{methods[i].Name}' is an instance method!");
break;
}
throw new Exception($"Message handler methods should be static, but '{methods[i].DeclaringType}.{methods[i].Name}' is an instance method!");

Delegate clientMessageHandler = Delegate.CreateDelegate(typeof(MessageHandler), methods[i], false);
if (clientMessageHandler != null)
{
// It's a message handler for Client instances
if (messageHandlers.ContainsKey(attribute.MessageId))
RiptideLogger.Log(LogType.error, $"Message handler method (type: client) already exists for message ID {attribute.MessageId}! Only one handler method is allowed per ID!");
{
MethodInfo otherMethodWithId = messageHandlers[attribute.MessageId].GetMethodInfo();
throw new Exception($"Client-side message handler methods '{methods[i].DeclaringType}.{methods[i].Name}' and '{otherMethodWithId.DeclaringType}.{otherMethodWithId.Name}' are both set to handle messages with ID {attribute.MessageId}! Only one handler method is allowed per message ID!");
}
else
messageHandlers.Add(attribute.MessageId, (MessageHandler)clientMessageHandler);
}
Expand All @@ -126,7 +126,7 @@ protected override void CreateMessageHandlersDictionary(Assembly assembly, byte
// It's not a message handler for Client instances, but it might be one for Server instances
Delegate serverMessageHandler = Delegate.CreateDelegate(typeof(Server.MessageHandler), methods[i], false);
if (serverMessageHandler == null)
RiptideLogger.Log(LogType.error, $"'{methods[i].DeclaringType}.{methods[i].Name}' doesn't match any acceptable message handler method signatures, double-check its parameters!");
throw new Exception($"'{methods[i].DeclaringType}.{methods[i].Name}' doesn't match any acceptable message handler method signatures, double-check its parameters!");
}
}
}
Expand All @@ -149,14 +149,17 @@ public void Disconnect()

private void LocalDisconnect()
{
client.Connected -= Connected;
client.ConnectionFailed -= ConnectionFailed;
client.Connected -= OnConnected;
client.ConnectionFailed -= OnConnectionFailed;
client.MessageReceived -= OnMessageReceived;
client.Disconnected -= Disconnected;
client.ClientConnected -= ClientConnected;
client.ClientDisconnected -= ClientDisconnected;
client.Disconnected -= OnDisconnected;
client.ClientConnected -= OnClientConnected;
client.ClientDisconnected -= OnClientDisconnected;
}

/// <summary>Invokes the <see cref="Connected"/> event.</summary>
private void OnConnected(object s, EventArgs e) => Connected?.Invoke(this, e);

/// <summary>Invokes the <see cref="ConnectionFailed"/> event.</summary>
private void OnConnectionFailed(object s, EventArgs e)
{
Expand All @@ -172,7 +175,7 @@ private void OnMessageReceived(object s, ClientMessageReceivedEventArgs e)
if (messageHandlers.TryGetValue(e.MessageId, out MessageHandler messageHandler))
messageHandler(e.Message);
else
RiptideLogger.Log(LogType.warning, $"No handler method (type: client) found for message ID {e.MessageId}!");
RiptideLogger.Log(LogType.warning, $"No client-side handler method found for message ID {e.MessageId}!");
}

/// <summary>Invokes the <see cref="Disconnected"/> event.</summary>
Expand All @@ -181,5 +184,11 @@ private void OnDisconnected(object s, EventArgs e)
LocalDisconnect();
Disconnected?.Invoke(this, e);
}

/// <summary>Invokes the <see cref="ClientConnected"/> event.</summary>
private void OnClientConnected(object s, ClientConnectedEventArgs e) => ClientConnected?.Invoke(this, e);

/// <summary>Invokes the <see cref="ClientDisconnected"/> event.</summary>
private void OnClientDisconnected(object s, ClientDisconnectedEventArgs e) => ClientDisconnected?.Invoke(this, e);
}
}
Loading

0 comments on commit 75068e5

Please sign in to comment.