Skip to content

Commit

Permalink
feat: Add IncludeOwner option to SendToAll (#387)
Browse files Browse the repository at this point in the history
* feat: Add IncludeOwner option to SendToAll

* fix code smell

* SendRPCInternal use SendToObservers

* SendUpdateVarsMessage to use SendToObservers
  • Loading branch information
uweeby committed Oct 6, 2020
1 parent d3f7209 commit 6b0a005
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Assets/Mirror/Runtime/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ protected void SendRPCInternal(Type invokeClass, string rpcName, NetworkWriter w
// The public facing parameter is excludeOwner in [ClientRpc]
// so we negate it here to logically align with SendToReady.
bool includeOwner = !excludeOwner;
Server.SendToReady(NetIdentity, message, includeOwner, channelId);
Server.SendToObservers(NetIdentity, message, includeOwner, channelId);
}

[EditorBrowsable(EditorBrowsableState.Never)]
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirror/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@ void SendUpdateVarsMessage()
if (observersWritten > 0)
{
varsMessage.payload = observersWriter.ToArraySegment();
Server.SendToReady(this, varsMessage, false);
Server.SendToObservers(this, varsMessage, false);
}

// clear dirty bits only for the components that we serialized
Expand Down
21 changes: 19 additions & 2 deletions Assets/Mirror/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,29 @@ internal void ActivateHostScene()
/// <param name="identity"></param>
/// <param name="msg"></param>
/// <param name="channelId"></param>
void SendToObservers<T>(NetworkIdentity identity, T msg, int channelId = Channels.DefaultReliable)
internal void SendToObservers<T>(NetworkIdentity identity, T msg, bool includeOwner = true, int channelId = Channels.DefaultReliable)
{
if (logger.LogEnabled()) logger.Log("Server.SendToObservers id:" + typeof(T));

if (identity.observers.Count > 0)
if (identity.observers.Count == 0)
return;

if(includeOwner)
{
NetworkConnection.Send(identity.observers, msg, channelId);
}
else
{
HashSet<INetworkConnection> connectionsExcludeSelf = new HashSet<INetworkConnection>();
foreach(INetworkConnection conn in identity.observers)
{
if(identity.ConnectionToClient != conn)
{
connectionsExcludeSelf.Add(conn);
}
}
NetworkConnection.Send(connectionsExcludeSelf, msg, channelId);
}
}

/// <summary>
Expand Down

0 comments on commit 6b0a005

Please sign in to comment.