-
Notifications
You must be signed in to change notification settings - Fork 572
feat: networked message channel #605
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e526738
Creating NetworkedMessageChannel
LPLafontaineB 1cb14bb
adding class description to NetworkedMessageChannel
LPLafontaineB 18cf7c9
Now only clients register message handlers and unregister them when d…
LPLafontaineB 088e063
adding basic usage example
LPLafontaineB 1c935fd
Revert "adding basic usage example"
LPLafontaineB 43c8456
inferring name from type T
LPLafontaineB 30cbf17
only register to the custom networked messages on clients
LPLafontaineB e16ffb3
Merge branch 'develop' into feature/networked-message-channel
LPLafontaineB c2e0cdb
replaced m_BufferSize field with call to FastBufferWriter.GetWriteSize()
LPLafontaineB 16bcc14
Merge branch 'develop' into feature/networked-message-channel
LPLafontaineB 4019e5f
fixing wrong name used for custom named message registering
LPLafontaineB 59139d7
added unregistering of custom named message on dispose
LPLafontaineB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
Assets/BossRoom/Scripts/Shared/Infrastructure/PubSub/NetworkedMessageChannel.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using Unity.Collections; | ||
using Unity.Netcode; | ||
using UnityEngine; | ||
|
||
namespace Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure | ||
{ | ||
/// <summary> | ||
/// This type of message channel allows the server to publish a message that will be sent to clients as well as | ||
/// being published locally. Clients and the server both can subscribe to it. However, that subscription needs to be | ||
/// done after the NetworkManager has initialized. On objects whose lifetime is bigger than a networked session, | ||
/// subscribing will be required each time a new session starts. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
public class NetworkedMessageChannel<T> : MessageChannel<T> where T : unmanaged | ||
{ | ||
string m_Name; | ||
|
||
int m_BufferSize; | ||
|
||
bool m_HasRegisteredHandler; | ||
|
||
public NetworkedMessageChannel(int bufferSize) | ||
{ | ||
m_Name = $"{nameof(T)}NetworkMessageChannel"; | ||
m_BufferSize = bufferSize; | ||
} | ||
|
||
~NetworkedMessageChannel() | ||
{ | ||
if (NetworkManager.Singleton != null && NetworkManager.Singleton.CustomMessagingManager != null) | ||
{ | ||
NetworkManager.Singleton.CustomMessagingManager.UnregisterNamedMessageHandler(m_Name); | ||
} | ||
} | ||
|
||
public override IDisposable Subscribe(Action<T> handler) | ||
{ | ||
if (NetworkManager.Singleton != null && NetworkManager.Singleton.IsListening) | ||
{ | ||
// Only register message handler on clients | ||
if (!m_HasRegisteredHandler && !NetworkManager.Singleton.IsServer) | ||
{ | ||
NetworkManager.Singleton.CustomMessagingManager.RegisterNamedMessageHandler(m_Name, ReceiveMessageThroughNetwork); | ||
SamuelBellomo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
m_HasRegisteredHandler = true; | ||
NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnect; | ||
} | ||
|
||
return base.Subscribe(handler); | ||
} | ||
|
||
Debug.LogError("Cannot subscribe to NetworkedMessageChannel. NetworkManager is not initialized."); | ||
return null; | ||
} | ||
|
||
void OnClientDisconnect(ulong clientId) | ||
SamuelBellomo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
m_HasRegisteredHandler = false; | ||
NetworkManager.Singleton.OnClientConnectedCallback -= OnClientDisconnect; | ||
NetworkManager.Singleton.CustomMessagingManager.UnregisterNamedMessageHandler(m_Name); | ||
} | ||
|
||
public override void Publish(T message) | ||
{ | ||
if (NetworkManager.Singleton.IsServer) | ||
{ | ||
// send message to clients, then publish locally | ||
SendMessageThroughNetwork(message); | ||
base.Publish(message); | ||
} | ||
else | ||
{ | ||
Debug.LogError("Only a server can publish in a NetworkedMessageChannel"); | ||
} | ||
} | ||
|
||
void SendMessageThroughNetwork(T message) | ||
{ | ||
var writer = new FastBufferWriter(m_BufferSize, Allocator.Temp); | ||
writer.WriteValueSafe(message); | ||
NetworkManager.Singleton.CustomMessagingManager.SendNamedMessageToAll(m_Name, writer); | ||
} | ||
|
||
void ReceiveMessageThroughNetwork(ulong clientID, FastBufferReader reader) | ||
{ | ||
reader.ReadValueSafe(out T message); | ||
base.Publish(message); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Assets/BossRoom/Scripts/Shared/Infrastructure/PubSub/NetworkedMessageChannel.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.