Skip to content

Commit

Permalink
update EzyDefaultController (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
vu-luong authored Aug 20, 2023
1 parent 5726342 commit d10261e
Showing 1 changed file with 75 additions and 3 deletions.
78 changes: 75 additions & 3 deletions unity/EzyDefaultController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using com.tvd12.ezyfoxserver.client.config;
using com.tvd12.ezyfoxserver.client.evt;
using com.tvd12.ezyfoxserver.client.logger;
using com.tvd12.ezyfoxserver.client.support;
using UnityEngine;
Expand All @@ -16,7 +17,8 @@ public class EzyDefaultController : MonoBehaviour
protected EzySocketProxy socketProxy;
protected EzyAppProxy appProxy;

private readonly List<Tuple<String, Object>> handlers = new();
private readonly List<Object> socketHandlers = new();
private readonly List<Tuple<String, Object>> appHandlers = new();

protected static readonly EzyLogger LOGGER = EzyUnityLoggerFactory
.getLogger<EzyDefaultController>();
Expand Down Expand Up @@ -53,17 +55,87 @@ protected void OnEnable()
);
}

protected void OnLoginSuccess<T>(EzySocketProxyDataHandler<T> handler)
{
socketHandlers.Add(
socketProxy.onLoginSuccess(handler)
);
}

protected void OnLoginError<T>(EzySocketProxyDataHandler<T> handler)
{
socketHandlers.Add(
socketProxy.onLoginError(handler)
);
}

protected void OnUdpHandshake<T>(EzySocketProxyDataHandler<T> handler)
{
socketHandlers.Add(
socketProxy.onUdpHandshake(handler)
);
}

protected void OnAppAccessed<T>(EzyAppProxyDataHandler<T> handler)
{
socketHandlers.Add(
socketProxy.onAppAccessed(handler)
);
}

protected void OnDisconnected(EzySocketProxyEventHandler<EzyDisconnectionEvent> handler)
{
socketHandlers.Add(
socketProxy.onDisconnected(handler)
);
}

protected void OnReconnecting(EzySocketProxyEventHandler<EzyDisconnectionEvent> handler)
{
socketHandlers.Add(
socketProxy.onReconnecting(handler)
);
}

protected void OnPingLost(EzySocketProxyEventHandler<EzyLostPingEvent> handler)
{
socketHandlers.Add(
socketProxy.onPingLost(handler)
);
}

protected void OnTryConnect(EzySocketProxyEventHandler<EzyTryConnectEvent> handler)
{
socketHandlers.Add(
socketProxy.onTryConnect(handler)
);
}

protected void AddHandler<T>(String cmd, EzyAppProxyDataHandler<T> handler)
{
handlers.Add(
appHandlers.Add(
new Tuple<String, Object>(cmd, appProxy.on(cmd, handler))
);
}

protected virtual void OnDestroy()
{
LOGGER.debug("OnDestroy");
foreach (Tuple<String, Object> tuple in handlers)
UnbindSocketHandlers();
UnbindAppHandlers();
}

protected virtual void UnbindSocketHandlers()
{
foreach (var socketProxyHandler in socketHandlers)
{
socketProxy.unbind(socketProxyHandler);
}
}

protected virtual void UnbindAppHandlers()
{
foreach (Tuple<String, Object> tuple in appHandlers)
{
appProxy.unbind(tuple.Item1, tuple.Item2);
}
Expand Down

0 comments on commit d10261e

Please sign in to comment.