using System;
using UnityEngine;
namespace Unity.Netcode
{
///
/// The generic transport class all Netcode for GameObjects network transport implementations
/// derive from. Use this class to add a custom transport.
/// for an example of how a transport is integrated
///
public abstract class NetworkTransport : MonoBehaviour
{
///
/// A constant `clientId` that represents the server
/// When this value is found in methods such as `Send`, it should be treated as a placeholder that means "the server"
///
public abstract ulong ServerClientId { get; }
///
/// Gets a value indicating whether this is supported in the current runtime context
/// This is used by multiplex adapters
///
/// true if is supported; otherwise, false.
public virtual bool IsSupported => true;
internal INetworkMetrics NetworkMetrics;
///
/// Delegate for transport network events
///
public delegate void TransportEventDelegate(NetworkEvent eventType, ulong clientId, ArraySegment payload, float receiveTime);
///
/// Occurs when the transport has a new transport network event.
/// Can be used to make an event based transport instead of a poll based.
/// Invocation has to occur on the Unity thread in the Update loop.
///
public event TransportEventDelegate OnTransportEvent;
///
/// Invokes the . Invokation has to occur on the Unity thread in the Update loop.
///
/// The event type
/// The clientId this event is for
/// The incoming data payload
/// The time the event was received, as reported by Time.realtimeSinceStartup.
protected void InvokeOnTransportEvent(NetworkEvent eventType, ulong clientId, ArraySegment payload, float receiveTime)
{
OnTransportEvent?.Invoke(eventType, clientId, payload, receiveTime);
}
///
/// Send a payload to the specified clientId, data and networkDelivery.
///
/// The clientId to send to
/// The data to send
/// The delivery type (QoS) to send data with
public abstract void Send(ulong clientId, ArraySegment payload, NetworkDelivery networkDelivery);
///
/// Polls for incoming events, with an extra output parameter to report the precise time the event was received.
///
/// The clientId this event is for
/// The incoming data payload
/// The time the event was received, as reported by Time.realtimeSinceStartup.
/// Returns the event type
public abstract NetworkEvent PollEvent(out ulong clientId, out ArraySegment payload, out float receiveTime);
///
/// Connects client to the server
///
/// Returns success or failure
public abstract bool StartClient();
///
/// Starts to listening for incoming clients
///
/// Returns success or failure
public abstract bool StartServer();
///
/// Disconnects a client from the server
///
/// The clientId to disconnect
public abstract void DisconnectRemoteClient(ulong clientId);
///
/// Disconnects the local client from the server
///
public abstract void DisconnectLocalClient();
///
/// Gets the round trip time for a specific client. This method is optional
///
/// The clientId to get the RTT from
/// Returns the round trip time in milliseconds
public abstract ulong GetCurrentRtt(ulong clientId);
///
/// Shuts down the transport
///
public abstract void Shutdown();
///
/// Initializes the transport
///
/// /// optionally pass in NetworkManager
public abstract void Initialize(NetworkManager networkManager = null);
}
#if UNITY_INCLUDE_TESTS
public abstract class TestingNetworkTransport : NetworkTransport
{
}
#endif
}