256 lines
11 KiB
C#
256 lines
11 KiB
C#
using UnityEngine;
|
|
using Unity.Netcode;
|
|
using System;
|
|
|
|
namespace Northbound
|
|
{
|
|
public class NetworkDebugger : MonoBehaviour
|
|
{
|
|
[Header("Debug Settings")]
|
|
[SerializeField] private bool enableVerboseLogging = true;
|
|
[SerializeField] private bool logTransportEvents = true;
|
|
|
|
private Unity.Netcode.Transports.UTP.UnityTransport _transport;
|
|
private NetworkManager _networkManager;
|
|
|
|
private void Start()
|
|
{
|
|
_networkManager = NetworkManager.Singleton;
|
|
if (_networkManager == null)
|
|
{
|
|
Debug.LogError("[NetworkDebugger] NetworkManager not found!");
|
|
return;
|
|
}
|
|
|
|
_transport = _networkManager.GetComponent<Unity.Netcode.Transports.UTP.UnityTransport>();
|
|
if (_transport == null)
|
|
{
|
|
Debug.LogError("[NetworkDebugger] UnityTransport not found!");
|
|
return;
|
|
}
|
|
|
|
SubscribeToEvents();
|
|
LogNetworkConfiguration();
|
|
}
|
|
|
|
private void SubscribeToEvents()
|
|
{
|
|
if (_networkManager != null)
|
|
{
|
|
_networkManager.OnServerStarted += OnServerStarted;
|
|
_networkManager.OnClientConnectedCallback += OnClientConnected;
|
|
_networkManager.OnClientDisconnectCallback += OnClientDisconnected;
|
|
_networkManager.ConnectionApprovalCallback += OnConnectionApproval;
|
|
_networkManager.OnTransportFailure += OnTransportFailure;
|
|
}
|
|
|
|
if (logTransportEvents && _transport != null)
|
|
{
|
|
LogTransportConfiguration();
|
|
}
|
|
}
|
|
|
|
private void UnsubscribeFromEvents()
|
|
{
|
|
if (_networkManager != null)
|
|
{
|
|
_networkManager.OnServerStarted -= OnServerStarted;
|
|
_networkManager.OnClientConnectedCallback -= OnClientConnected;
|
|
_networkManager.OnClientDisconnectCallback -= OnClientDisconnected;
|
|
_networkManager.ConnectionApprovalCallback -= OnConnectionApproval;
|
|
_networkManager.OnTransportFailure -= OnTransportFailure;
|
|
}
|
|
}
|
|
|
|
private void LogNetworkConfiguration()
|
|
{
|
|
Debug.Log("=== NETWORK CONFIGURATION ===");
|
|
Debug.Log("NetworkManager Present: " + (_networkManager != null));
|
|
string transportType = _transport != null ? "Unity Transport" : "None";
|
|
Debug.Log("Transport Type: " + transportType);
|
|
LogTransportConfiguration();
|
|
LogNetworkSettings();
|
|
Debug.Log("============================");
|
|
}
|
|
|
|
private void LogTransportConfiguration()
|
|
{
|
|
if (_transport == null) return;
|
|
|
|
Debug.Log("=== TRANSPORT CONFIGURATION ===");
|
|
Debug.Log("Connection Data Address: " + _transport.ConnectionData.Address);
|
|
Debug.Log("Connection Data Port: " + _transport.ConnectionData.Port);
|
|
Debug.Log("Connection Data Server Listen Address: " + _transport.ConnectionData.ServerListenAddress);
|
|
Debug.Log("Max Connect Attempts: " + _transport.MaxConnectAttempts);
|
|
Debug.Log("Connect Timeout (ms): " + _transport.ConnectTimeoutMS);
|
|
Debug.Log("Max Packet Queue Size: " + _transport.MaxPacketQueueSize);
|
|
Debug.Log("================================");
|
|
}
|
|
|
|
private void LogNetworkSettings()
|
|
{
|
|
if (_networkManager == null) return;
|
|
|
|
Debug.Log("=== NETWORK MANAGER SETTINGS ===");
|
|
Debug.Log("NetworkConfig: " + (_networkManager.NetworkConfig != null));
|
|
if (_networkManager.NetworkConfig != null)
|
|
{
|
|
Debug.Log("Player Prefab: " + (_networkManager.NetworkConfig.PlayerPrefab != null));
|
|
Debug.Log("Connection Approval: " + _networkManager.NetworkConfig.ConnectionApproval);
|
|
Debug.Log("Client Connection Buffer: " + _networkManager.NetworkConfig.ClientConnectionBufferTimeout);
|
|
Debug.Log("Enable Scene Management: " + _networkManager.NetworkConfig.EnableSceneManagement);
|
|
}
|
|
Debug.Log("================================");
|
|
}
|
|
|
|
private void OnServerStarted()
|
|
{
|
|
Debug.Log("<color=green>[NetworkDebugger] Server Started Successfully</color>");
|
|
Debug.Log("Listening on: " + _transport.ConnectionData.Address + ":" + _transport.ConnectionData.Port);
|
|
Debug.Log("IsHost: " + _networkManager.IsHost);
|
|
Debug.Log("IsServer: " + _networkManager.IsServer);
|
|
}
|
|
|
|
private void OnClientConnected(ulong clientId)
|
|
{
|
|
if (enableVerboseLogging)
|
|
{
|
|
Debug.Log("<color=cyan>[NetworkDebugger] Client Connected: " + clientId + "</color>");
|
|
Debug.Log("Local Client ID: " + _networkManager.LocalClientId);
|
|
Debug.Log("Connected Clients Count: " + _networkManager.ConnectedClients.Count);
|
|
}
|
|
|
|
foreach (var client in _networkManager.ConnectedClients)
|
|
{
|
|
string playerStatus = client.Value.PlayerObject != null ? "Exists" : "NULL";
|
|
Debug.Log(" Client ID: " + client.Key + ", Player Object: " + playerStatus);
|
|
}
|
|
}
|
|
|
|
private void OnClientDisconnected(ulong clientId)
|
|
{
|
|
Debug.Log("<color=yellow>[NetworkDebugger] Client Disconnected: " + clientId + "</color>");
|
|
Debug.Log("Reason: Check logs above for details");
|
|
}
|
|
|
|
private void OnConnectionApproval(NetworkManager.ConnectionApprovalRequest request,
|
|
NetworkManager.ConnectionApprovalResponse response)
|
|
{
|
|
Debug.Log("<color=magenta>[NetworkDebugger] Connection Approval Request from Client " + request.ClientNetworkId + "</color>");
|
|
Debug.Log(" Payload Length: " + request.Payload.Length);
|
|
Debug.Log(" Client ID: " + request.ClientNetworkId);
|
|
|
|
LogApprovalResponse(response);
|
|
}
|
|
|
|
private void LogApprovalResponse(NetworkManager.ConnectionApprovalResponse response)
|
|
{
|
|
Debug.Log("<color=magenta>[NetworkDebugger] Approval Response:</color>");
|
|
Debug.Log(" Approved: " + response.Approved);
|
|
Debug.Log(" Create Player Object: " + response.CreatePlayerObject);
|
|
Debug.Log(" Position: " + response.Position);
|
|
Debug.Log(" Rotation: " + response.Rotation);
|
|
Debug.Log(" Player Prefab Hash: " + response.PlayerPrefabHash);
|
|
}
|
|
|
|
private void OnTransportFailure()
|
|
{
|
|
Debug.LogError("<color=red>[NetworkDebugger] TRANSPORT FAILURE!</color>");
|
|
Debug.LogError("This usually indicates:");
|
|
Debug.LogError("1. Port not forwarded correctly");
|
|
Debug.LogError("2. Firewall blocking the connection");
|
|
Debug.LogError("3. Wrong IP address");
|
|
Debug.LogError("4. Server not listening on the correct address");
|
|
Debug.LogError("5. Network timeout");
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (enableVerboseLogging && _networkManager != null)
|
|
{
|
|
MonitorConnectionState();
|
|
}
|
|
}
|
|
|
|
private void MonitorConnectionState()
|
|
{
|
|
if (_networkManager.IsServer && _networkManager.IsClient)
|
|
{
|
|
if (Time.frameCount % 300 == 0)
|
|
{
|
|
Debug.Log("<color=green>[NetworkDebugger] Host Mode - " + Time.time.ToString("F2") + "s</color>");
|
|
Debug.Log(" Connected Clients: " + _networkManager.ConnectedClients.Count);
|
|
Debug.Log(" Server Listening: " + _transport.ConnectionData.Address + ":" + _transport.ConnectionData.Port);
|
|
}
|
|
}
|
|
else if (_networkManager.IsClient)
|
|
{
|
|
if (Time.frameCount % 300 == 0 && !_networkManager.IsConnectedClient)
|
|
{
|
|
Debug.LogWarning("<color=yellow>[NetworkDebugger] Client Not Connected - " + Time.time.ToString("F2") + "s</color>");
|
|
Debug.LogWarning(" Connecting to: " + _transport.ConnectionData.Address + ":" + _transport.ConnectionData.Port);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
UnsubscribeFromEvents();
|
|
}
|
|
|
|
[ContextMenu("Log Current Network State")]
|
|
public void LogCurrentState()
|
|
{
|
|
Debug.Log("=== CURRENT NETWORK STATE ===");
|
|
|
|
if (_networkManager == null)
|
|
{
|
|
Debug.Log("NetworkManager is NULL");
|
|
return;
|
|
}
|
|
|
|
Debug.Log("Is Server: " + _networkManager.IsServer);
|
|
Debug.Log("Is Client: " + _networkManager.IsClient);
|
|
Debug.Log("Is Host: " + _networkManager.IsHost);
|
|
Debug.Log("Is Connected Client: " + _networkManager.IsConnectedClient);
|
|
Debug.Log("Local Client ID: " + _networkManager.LocalClientId);
|
|
Debug.Log("Connected Clients Count: " + _networkManager.ConnectedClients.Count);
|
|
|
|
if (_transport != null)
|
|
{
|
|
Debug.Log("Transport Address: " + _transport.ConnectionData.Address);
|
|
Debug.Log("Transport Port: " + _transport.ConnectionData.Port);
|
|
Debug.Log("Server Listen Address: " + _transport.ConnectionData.ServerListenAddress);
|
|
}
|
|
|
|
Debug.Log("==========================");
|
|
}
|
|
|
|
[ContextMenu("Test Port Binding")]
|
|
public void TestPortBinding()
|
|
{
|
|
if (_transport != null)
|
|
{
|
|
Debug.Log("<color=yellow>[NetworkDebugger] Port Binding Test:</color>");
|
|
Debug.Log(" Address: " + _transport.ConnectionData.Address);
|
|
Debug.Log(" Port: " + _transport.ConnectionData.Port);
|
|
Debug.Log(" Server Listen Address: " + _transport.ConnectionData.ServerListenAddress);
|
|
Debug.LogWarning("Note: Actual binding happens when StartHost/StartServer is called");
|
|
}
|
|
}
|
|
|
|
[ContextMenu("Check Firewall Issues")]
|
|
public void CheckPotentialFirewallIssues()
|
|
{
|
|
Debug.Log("=== POTENTIAL FIREWALL ISSUES ===");
|
|
Debug.LogWarning("If you see 'Failed to connect to server' errors:");
|
|
Debug.LogWarning("1. Check Windows Firewall settings");
|
|
Debug.LogWarning("2. Allow Unity Editor and your build through firewall");
|
|
Debug.LogWarning("3. Ensure port is properly forwarded in router");
|
|
Debug.LogWarning("4. Test port with online port checker (e.g., canyouseeme.org)");
|
|
Debug.LogWarning("5. Check antivirus/security software");
|
|
Debug.Log("================================");
|
|
}
|
|
}
|
|
}
|