Skip to content

Commit

Permalink
refactor: rename connection to player (MirageNet#706)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Connection renamed to player
  • Loading branch information
paulpach authored Mar 15, 2021
1 parent 51cf098 commit 03e8cfa
Show file tree
Hide file tree
Showing 55 changed files with 458 additions and 459 deletions.
30 changes: 15 additions & 15 deletions Assets/Mirage/Authenticators/BasicAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ public struct AuthResponseMessage
public string Message;
}

public override void OnServerAuthenticate(INetworkPlayer conn)
public override void OnServerAuthenticate(INetworkPlayer player)
{
// wait for AuthRequestMessage from client
conn.RegisterHandler<AuthRequestMessage>(OnAuthRequestMessage);
player.RegisterHandler<AuthRequestMessage>(OnAuthRequestMessage);
}

public override void OnClientAuthenticate(INetworkPlayer conn)
public override void OnClientAuthenticate(INetworkPlayer player)
{
conn.RegisterHandler<AuthResponseMessage>(OnAuthResponseMessage);
player.RegisterHandler<AuthResponseMessage>(OnAuthResponseMessage);

var authRequestMessage = new AuthRequestMessage
{
AuthUsername = Username,
AuthPassword = Password
};

conn.Send(authRequestMessage);
player.Send(authRequestMessage);
}

public void OnAuthRequestMessage(INetworkPlayer conn, AuthRequestMessage msg)
public void OnAuthRequestMessage(INetworkPlayer player, AuthRequestMessage msg)
{
if (logger.LogEnabled()) logger.LogFormat(LogType.Log, "Authentication Request: {0} {1}", msg.AuthUsername, msg.AuthPassword);

Expand All @@ -60,10 +60,10 @@ public void OnAuthRequestMessage(INetworkPlayer conn, AuthRequestMessage msg)
Message = "Success"
};

conn.Send(authResponseMessage);
player.Send(authResponseMessage);

// Invoke the event to complete a successful authentication
base.OnServerAuthenticate(conn);
base.OnServerAuthenticate(player);
}
else
{
Expand All @@ -74,33 +74,33 @@ public void OnAuthRequestMessage(INetworkPlayer conn, AuthRequestMessage msg)
Message = "Invalid Credentials"
};

conn.Send(authResponseMessage);
player.Send(authResponseMessage);

// disconnect the client after 1 second so that response message gets delivered
StartCoroutine(DelayedDisconnect(conn, 1));
StartCoroutine(DelayedDisconnect(player, 1));
}
}

public IEnumerator DelayedDisconnect(INetworkPlayer conn, float waitTime)
public IEnumerator DelayedDisconnect(INetworkPlayer player, float waitTime)
{
yield return new WaitForSeconds(waitTime);
conn.Connection?.Disconnect();
player.Connection?.Disconnect();
}

public void OnAuthResponseMessage(INetworkPlayer conn, AuthResponseMessage msg)
public void OnAuthResponseMessage(INetworkPlayer player, AuthResponseMessage msg)
{
if (msg.Code == 100)
{
if (logger.LogEnabled()) logger.LogFormat(LogType.Log, "Authentication Response: {0}", msg.Message);

// Invoke the event to complete a successful authentication
base.OnClientAuthenticate(conn);
base.OnClientAuthenticate(player);
}
else
{
logger.LogFormat(LogType.Error, "Authentication Response: {0}", msg.Message);
// disconnect the client
conn.Connection?.Disconnect();
player.Connection?.Disconnect();
}
}
}
Expand Down
40 changes: 20 additions & 20 deletions Assets/Mirage/Authenticators/TimeoutAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,47 +27,47 @@ public void Awake()

private readonly HashSet<INetworkPlayer> pendingAuthentication = new HashSet<INetworkPlayer>();

private void HandleServerAuthenticated(INetworkPlayer connection)
private void HandleServerAuthenticated(INetworkPlayer player)
{
pendingAuthentication.Remove(connection);
base.OnClientAuthenticate(connection);
pendingAuthentication.Remove(player);
base.OnClientAuthenticate(player);
}

private void HandleClientAuthenticated(INetworkPlayer connection)
private void HandleClientAuthenticated(INetworkPlayer player)
{
pendingAuthentication.Remove(connection);
base.OnServerAuthenticate(connection);
pendingAuthentication.Remove(player);
base.OnServerAuthenticate(player);
}

public override void OnClientAuthenticate(INetworkPlayer conn)
public override void OnClientAuthenticate(INetworkPlayer player)
{
pendingAuthentication.Add(conn);
Authenticator.OnClientAuthenticate(conn);
pendingAuthentication.Add(player);
Authenticator.OnClientAuthenticate(player);

if (Timeout > 0)
StartCoroutine(BeginAuthentication(conn));
StartCoroutine(BeginAuthentication(player));
}

public override void OnServerAuthenticate(INetworkPlayer conn)
public override void OnServerAuthenticate(INetworkPlayer player)
{
pendingAuthentication.Add(conn);
Authenticator.OnServerAuthenticate(conn);
pendingAuthentication.Add(player);
Authenticator.OnServerAuthenticate(player);
if (Timeout > 0)
StartCoroutine(BeginAuthentication(conn));
StartCoroutine(BeginAuthentication(player));
}

IEnumerator BeginAuthentication(INetworkPlayer conn)
IEnumerator BeginAuthentication(INetworkPlayer player)
{
if (logger.LogEnabled()) logger.Log($"Authentication countdown started {conn} {Timeout}");
if (logger.LogEnabled()) logger.Log($"Authentication countdown started {player} {Timeout}");

yield return new WaitForSecondsRealtime(Timeout);

if (pendingAuthentication.Contains(conn))
if (pendingAuthentication.Contains(player))
{
if (logger.LogEnabled()) logger.Log($"Authentication Timeout {conn}");
if (logger.LogEnabled()) logger.Log($"Authentication Timeout {player}");

pendingAuthentication.Remove(conn);
conn.Connection?.Disconnect();
pendingAuthentication.Remove(player);
player.Connection?.Disconnect();
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Assets/Mirage/Components/LobbyReady.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class LobbyReady : MonoBehaviour

// just a cached memory area where we can collect connections
// for broadcasting messages
private static readonly List<INetworkPlayer> connectionsCache = new List<INetworkPlayer>();
private static readonly List<INetworkPlayer> playerCache = new List<INetworkPlayer>();

public void SetAllClientsNotReady()
{
Expand All @@ -26,18 +26,18 @@ public void SendToReady<T>(NetworkIdentity identity, T msg, bool includeOwner =
{
if (logger.LogEnabled()) logger.Log("Server.SendToReady msgType:" + typeof(T));

connectionsCache.Clear();
playerCache.Clear();

foreach (ObjectReady objectReady in ObjectReadyList)
{
bool isOwner = objectReady.NetIdentity == identity;
if ((!isOwner || includeOwner) && objectReady.IsReady)
{
connectionsCache.Add(objectReady.NetIdentity.ConnectionToClient);
playerCache.Add(objectReady.NetIdentity.ConnectionToClient);
}
}

NetworkServer.SendToMany(connectionsCache, msg, channelId);
NetworkServer.SendToMany(playerCache, msg, channelId);
}
}
}
8 changes: 4 additions & 4 deletions Assets/Mirage/Components/NetworkAnimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void SendAnimationMessage(int stateHash, float normalizedTime, int layerId, floa
{
RpcOnAnimationClientMessage(stateHash, normalizedTime, layerId, weight, parameters);
}
else if (Client.Connection != null)
else if (Client.Player != null)
{
CmdOnAnimationServerMessage(stateHash, normalizedTime, layerId, weight, parameters);
}
Expand All @@ -185,7 +185,7 @@ void SendAnimationParametersMessage(byte[] parameters)
{
RpcOnAnimationParametersClientMessage(parameters);
}
else if (Client.Connection != null)
else if (Client.Player != null)
{
CmdOnAnimationParametersServerMessage(parameters);
}
Expand Down Expand Up @@ -439,7 +439,7 @@ public void SetTrigger(int hash)
return;
}

if (Client.Connection != null)
if (Client.Player != null)
CmdOnAnimationTriggerServerMessage(hash);

// call on client right away
Expand Down Expand Up @@ -488,7 +488,7 @@ public void ResetTrigger(int hash)
return;
}

if (Client.Connection != null)
if (Client.Player != null)
CmdOnAnimationResetTriggerServerMessage(hash);

// call on client right away
Expand Down
6 changes: 3 additions & 3 deletions Assets/Mirage/Components/NetworkMatchChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ void RebuildMatchObservers(Guid specificMatch)
/// Callback used by the visibility system to determine if an observer (player) can see this object.
/// <para>If this function returns true, the network connection will be added as an observer.</para>
/// </summary>
/// <param name="conn">Network connection of a player.</param>
/// <param name="player">Network connection of a player.</param>
/// <returns>True if the player can see this object.</returns>
public override bool OnCheckObserver(INetworkPlayer conn)
public override bool OnCheckObserver(INetworkPlayer player)
{
// Not Visible if not in a match
if (MatchId == Guid.Empty)
return false;

NetworkMatchChecker networkMatchChecker = conn.Identity.GetComponent<NetworkMatchChecker>();
NetworkMatchChecker networkMatchChecker = player.Identity.GetComponent<NetworkMatchChecker>();

if (networkMatchChecker == null)
return false;
Expand Down
12 changes: 6 additions & 6 deletions Assets/Mirage/Components/NetworkProximityChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ void RebuildObservers()
/// <para>If this function returns true, the network connection will be added as an observer.</para>
/// </summary>

/// <param name="conn">Network connection of a player.</param>
/// <param name="player">Network connection of a player.</param>
/// <returns>True if the player can see this object.</returns>
public override bool OnCheckObserver(INetworkPlayer conn)
public override bool OnCheckObserver(INetworkPlayer player)
{
if (ForceHidden)
return false;

return Vector3.Distance(conn.Identity.transform.position, transform.position) < VisibilityRange;
return Vector3.Distance(player.Identity.transform.position, transform.position) < VisibilityRange;
}

/// <summary>
Expand All @@ -89,12 +89,12 @@ public override void OnRebuildObservers(HashSet<INetworkPlayer> observers, bool
// magnitude faster. if we have 10k monsters and run a sphere
// cast 10k times, we will see a noticeable lag even with physics
// layers. but checking to every connection is fast.
foreach (INetworkPlayer conn in Server.connections)
foreach (INetworkPlayer player in Server.Players)
{
// check distance
if (conn != null && conn.Identity != null && Vector3.Distance(conn.Identity.transform.position, position) < VisibilityRange)
if (player != null && player.Identity != null && Vector3.Distance(player.Identity.transform.position, position) < VisibilityRange)
{
observers.Add(conn);
observers.Add(player);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Assets/Mirage/Components/NetworkSceneChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ void RebuildSceneObservers()
/// Callback used by the visibility system to determine if an observer (player) can see this object.
/// <para>If this function returns true, the network connection will be added as an observer.</para>
/// </summary>
/// <param name="conn">Network connection of a player.</param>
/// <param name="player">Network connection of a player.</param>
/// <returns>True if the player can see this object.</returns>
public override bool OnCheckObserver(INetworkPlayer conn)
public override bool OnCheckObserver(INetworkPlayer player)
{
if (forceHidden)
return false;

return conn.Identity.gameObject.scene == gameObject.scene;
return player.Identity.gameObject.scene == gameObject.scene;
}

/// <summary>
Expand Down
5 changes: 2 additions & 3 deletions Assets/Mirage/Editor/NetworkInformationPreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,9 @@ float DrawObservers(NetworkIdentity identity, float initialX, float Y)
observerRect.x += 20;
observerRect.y += observerRect.height;

foreach (INetworkPlayer conn in identity.observers)
foreach (INetworkPlayer player in identity.observers)
{

GUI.Label(observerRect, conn.Connection.GetEndPointAddress() + ":" + conn, styles.ComponentName);
GUI.Label(observerRect, player.Connection.GetEndPointAddress() + ":" + player, styles.ComponentName);
observerRect.y += observerRect.height;
Y = observerRect.y;
}
Expand Down
24 changes: 12 additions & 12 deletions Assets/Mirage/Runtime/CharacterSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public virtual void Start()
}
else
{
Client.Authenticated.AddListener(c => Client.Send(new AddPlayerMessage()));
Client.Authenticated.AddListener(c => Client.Send(new AddCharacterMessage()));
}

if (ClientObjectManager != null)
Expand All @@ -74,18 +74,18 @@ void OnDestroy()
if (Client != null && SceneManager != null)
{
SceneManager.ClientSceneChanged.RemoveListener(OnClientSceneChanged);
Client.Authenticated.RemoveListener(c => Client.Send(new AddPlayerMessage()));
Client.Authenticated.RemoveListener(c => Client.Send(new AddCharacterMessage()));
}
if (Server != null)
{
Server.Authenticated.RemoveListener(OnServerAuthenticated);
}
}

private void OnServerAuthenticated(INetworkPlayer connection)
private void OnServerAuthenticated(INetworkPlayer player)
{
// wait for client to send us an AddPlayerMessage
connection.RegisterHandler<AddPlayerMessage>(OnServerAddPlayerInternal);
player.RegisterHandler<AddCharacterMessage>(OnServerAddPlayerInternal);
}

/// <summary>
Expand All @@ -101,34 +101,34 @@ private void OnClientSceneChanged(string sceneName, SceneOperation sceneOperatio

public virtual void RequestServerSpawnPlayer()
{
Client.Send(new AddPlayerMessage());
Client.Send(new AddCharacterMessage());
}

void OnServerAddPlayerInternal(INetworkPlayer conn, AddPlayerMessage msg)
void OnServerAddPlayerInternal(INetworkPlayer player, AddCharacterMessage msg)
{
logger.Log("CharacterSpawner.OnServerAddPlayer");

if (conn.Identity != null)
if (player.Identity != null)
{
throw new InvalidOperationException("There is already a player for this connection.");
}

OnServerAddPlayer(conn);
OnServerAddPlayer(player);
}

/// <summary>
/// Called on the server when a client adds a new player with ClientScene.AddPlayer.
/// <para>The default implementation for this function creates a new player object from the playerPrefab.</para>
/// </summary>
/// <param name="conn">Connection from client.</param>
public virtual void OnServerAddPlayer(INetworkPlayer conn)
/// <param name="player">Connection from client.</param>
public virtual void OnServerAddPlayer(INetworkPlayer player)
{
Transform startPos = GetStartPosition();
NetworkIdentity player = startPos != null
NetworkIdentity character = startPos != null
? Instantiate(PlayerPrefab, startPos.position, startPos.rotation)
: Instantiate(PlayerPrefab);

ServerObjectManager.AddPlayerForConnection(conn, player.gameObject);
ServerObjectManager.AddCharacter(player, character.gameObject);
}

/// <summary>
Expand Down
Loading

0 comments on commit 03e8cfa

Please sign in to comment.