feat: NetworkConnection is optional for handlers (#1202)

* feat: NetworkConnection is optional for handlers

* make NetworkConnction optional in handlers
This commit is contained in:
Paul Pacheco 2019-11-03 12:24:34 -06:00 committed by vis2k
parent 12fd2ae6fc
commit bf9eb610dc
4 changed files with 37 additions and 13 deletions

View File

@ -473,7 +473,7 @@ static void ApplySpawnPayload(NetworkIdentity identity, SpawnMessage msg)
}
}
internal static void OnSpawn(NetworkConnection _, SpawnMessage msg)
internal static void OnSpawn(SpawnMessage msg)
{
if (msg.assetId == Guid.Empty && msg.sceneId == 0)
{
@ -556,7 +556,7 @@ static NetworkIdentity SpawnSceneObject(SpawnMessage msg)
return spawnedId;
}
internal static void OnObjectSpawnStarted(NetworkConnection _, ObjectSpawnStartedMessage msg)
internal static void OnObjectSpawnStarted(ObjectSpawnStartedMessage _)
{
if (LogFilter.Debug) Debug.Log("SpawnStarted");
@ -564,7 +564,7 @@ internal static void OnObjectSpawnStarted(NetworkConnection _, ObjectSpawnStarte
isSpawnFinished = false;
}
internal static void OnObjectSpawnFinished(NetworkConnection _, ObjectSpawnFinishedMessage msg)
internal static void OnObjectSpawnFinished(ObjectSpawnFinishedMessage _)
{
if (LogFilter.Debug) Debug.Log("SpawnFinished");
@ -582,12 +582,12 @@ internal static void OnObjectSpawnFinished(NetworkConnection _, ObjectSpawnFinis
isSpawnFinished = true;
}
internal static void OnObjectHide(NetworkConnection _, ObjectHideMessage msg)
internal static void OnObjectHide(ObjectHideMessage msg)
{
DestroyObject(msg.netId);
}
internal static void OnObjectDestroy(NetworkConnection _, ObjectDestroyMessage msg)
internal static void OnObjectDestroy(ObjectDestroyMessage msg)
{
DestroyObject(msg.netId);
}
@ -623,14 +623,14 @@ static void DestroyObject(uint netId)
}
}
internal static void OnLocalClientObjectDestroy(NetworkConnection _, ObjectDestroyMessage msg)
internal static void OnLocalClientObjectDestroy(ObjectDestroyMessage msg)
{
if (LogFilter.Debug) Debug.Log("ClientScene.OnLocalObjectObjDestroy netId:" + msg.netId);
NetworkIdentity.spawned.Remove(msg.netId);
}
internal static void OnLocalClientObjectHide(NetworkConnection _, ObjectHideMessage msg)
internal static void OnLocalClientObjectHide(ObjectHideMessage msg)
{
if (LogFilter.Debug) Debug.Log("ClientScene::OnLocalObjectObjHide netId:" + msg.netId);
@ -640,7 +640,7 @@ internal static void OnLocalClientObjectHide(NetworkConnection _, ObjectHideMess
}
}
internal static void OnLocalClientSpawn(NetworkConnection _, SpawnMessage msg)
internal static void OnLocalClientSpawn(SpawnMessage msg)
{
if (NetworkIdentity.spawned.TryGetValue(msg.netId, out NetworkIdentity localObject) && localObject != null)
{
@ -648,7 +648,7 @@ internal static void OnLocalClientSpawn(NetworkConnection _, SpawnMessage msg)
}
}
internal static void OnUpdateVarsMessage(NetworkConnection _, UpdateVarsMessage msg)
internal static void OnUpdateVarsMessage(UpdateVarsMessage msg)
{
if (LogFilter.Debug) Debug.Log("ClientScene.OnUpdateVarsMessage " + msg.netId);
@ -662,7 +662,7 @@ internal static void OnUpdateVarsMessage(NetworkConnection _, UpdateVarsMessage
}
}
internal static void OnRPCMessage(NetworkConnection _, RpcMessage msg)
internal static void OnRPCMessage(RpcMessage msg)
{
if (LogFilter.Debug) Debug.Log("ClientScene.OnRPCMessage hash:" + msg.functionHash + " netId:" + msg.netId);
@ -672,7 +672,7 @@ internal static void OnRPCMessage(NetworkConnection _, RpcMessage msg)
}
}
internal static void OnSyncEventMessage(NetworkConnection _, SyncEventMessage msg)
internal static void OnSyncEventMessage(SyncEventMessage msg)
{
if (LogFilter.Debug) Debug.Log("ClientScene.OnSyncEventMessage " + msg.netId);
@ -686,7 +686,7 @@ internal static void OnSyncEventMessage(NetworkConnection _, SyncEventMessage ms
}
}
internal static void OnClientAuthority(NetworkConnection _, ClientAuthorityMessage msg)
internal static void OnClientAuthority(ClientAuthorityMessage msg)
{
if (LogFilter.Debug) Debug.Log("ClientScene.OnClientAuthority for netId: " + msg.netId);

View File

@ -403,6 +403,18 @@ public static void RegisterHandler(MsgType msgType, NetworkMessageDelegate handl
handlers[msgType] = MessagePacker.MessageHandler<T>(handler, requireAuthentication);
}
/// <summary>
/// Register a handler for a particular message type.
/// <para>There are several system message types which you can add handlers for. You can also add your own message types.</para>
/// </summary>
/// <typeparam name="T">The message type to unregister.</typeparam>
/// <param name="handler"></param>
/// <param name="requireAuthentication">true if the message requires an authenticated connection</param>
public static void RegisterHandler<T>(Action<T> handler, bool requireAuthentication = true) where T : IMessageBase, new()
{
RegisterHandler( (NetworkConnection _, T value) => { handler(value); }, requireAuthentication) ;
}
/// <summary>
/// Obsolete: Use <see cref="UnregisterHandler{T}"/> instead
/// </summary>

View File

@ -595,6 +595,18 @@ public static void RegisterHandler(MsgType msgType, NetworkMessageDelegate handl
handlers[msgType] = MessagePacker.MessageHandler(handler, requireAuthentication);
}
/// <summary>
/// Register a handler for a particular message type.
/// <para>There are several system message types which you can add handlers for. You can also add your own message types.</para>
/// </summary>
/// <typeparam name="T">Message type</typeparam>
/// <param name="handler">Function handler which will be invoked for when this message type is received.</param>
/// <param name="requireAuthentication">True if the message requires an authenticated connection</param>
public static void RegisterHandler<T>(Action<T> handler, bool requireAuthentication = true) where T : IMessageBase, new()
{
RegisterHandler<T>((_, value) => { handler(value); }, requireAuthentication);
}
/// <summary>
/// Obsolete: Use <see cref="UnregisterHandler{T}"/> instead.
/// </summary>

View File

@ -81,7 +81,7 @@ internal static void OnServerPing(NetworkConnection conn, NetworkPingMessage msg
// Executed at the client when we receive a Pong message
// find out how long it took since we sent the Ping
// and update time offset
internal static void OnClientPong(NetworkConnection _, NetworkPongMessage msg)
internal static void OnClientPong(NetworkPongMessage msg)
{
double now = LocalTime();