using FishNet.Managing; using FishNet.Managing.Logging; using System; using UnityEngine; namespace FishNet.Transporting { /// /// Processes connection states, and data sent to and from a socket. /// public abstract class Transport : MonoBehaviour { #region Private. /// /// NetworkManager for this transport. /// public NetworkManager NetworkManager { get; private set; } /// /// Index this transport belongs to when using multiple transports at once. /// public int Index { get; private set; } #endregion #region Initialization and unity. /// /// Initializes the transport. Use this instead of Awake. /// Index this transport belongs to when using multiple transports at once. /// public virtual void Initialize(NetworkManager networkManager, int transportIndex) { NetworkManager = networkManager; Index = transportIndex; } #endregion #region ConnectionStates. /// /// Gets the address of a remote connection Id. /// /// Connectionid to get the address for. /// public abstract string GetConnectionAddress(int connectionId); /// /// Called when a connection state changes for the local client. /// public abstract event Action OnClientConnectionState; /// /// Called when a connection state changes for the local server. /// public abstract event Action OnServerConnectionState; /// /// Called when a connection state changes for a remote client. /// public abstract event Action OnRemoteConnectionState; /// /// Handles a ConnectionStateArgs for the local client. /// /// Data being handled. public abstract void HandleClientConnectionState(ClientConnectionStateArgs connectionStateArgs); /// /// Handles a ConnectionStateArgs for the local server. /// /// Data being handled. public abstract void HandleServerConnectionState(ServerConnectionStateArgs connectionStateArgs); /// /// Handles a ConnectionStateArgs for a remote client. /// /// Data being handled. public abstract void HandleRemoteConnectionState(RemoteConnectionStateArgs connectionStateArgs); /// /// Gets the current local ConnectionState. /// /// True if getting ConnectionState for the server. public abstract LocalConnectionState GetConnectionState(bool server); /// /// Gets the current ConnectionState of a client connected to the server. Can only be called on the server. /// /// ConnectionId to get ConnectionState for. public abstract RemoteConnectionState GetConnectionState(int connectionId); #endregion #region Sending. /// /// Sends to the server. /// /// Channel to use. /// Data to send. public abstract void SendToServer(byte channelId, ArraySegment segment); /// /// Sends to a client. /// /// Channel to use. /// Data to send. /// ConnectionId to send to. When sending to clients can be used to specify which connection to send to. public abstract void SendToClient(byte channelId, ArraySegment segment, int connectionId); #endregion #region Receiving /// /// Called when the client receives data. /// public abstract event Action OnClientReceivedData; /// /// Handles a ClientReceivedDataArgs. /// /// Data being handled. public abstract void HandleClientReceivedDataArgs(ClientReceivedDataArgs receivedDataArgs); /// /// Called when the server receives data. /// public abstract event Action OnServerReceivedData; /// /// Handles a ServerReceivedDataArgs. /// /// Data being handled. public abstract void HandleServerReceivedDataArgs(ServerReceivedDataArgs receivedDataArgs); #endregion #region Iterating. /// /// Processes data received by the socket. /// /// True to process data received on the server. public abstract void IterateIncoming(bool server); /// /// Processes data to be sent by the socket. /// /// True to process data received on the server. public abstract void IterateOutgoing(bool server); #endregion #region Configuration. /// /// Returns if the transport is only run locally, offline. /// While true several security checks are disabled. /// /// public virtual bool IsLocalTransport(int connectionid) => false; /// /// Gets how long in seconds until either the server or client socket must go without data before being timed out. /// /// True to get the timeout for the server socket, false for the client socket. /// public virtual float GetTimeout(bool asServer) => -1f; /// /// Sets how long in seconds until either the server or client socket must go without data before being timed out. /// /// True to set the timeout for the server socket, false for the client socket. public virtual void SetTimeout(float value, bool asServer) { } /// /// Returns the maximum number of clients allowed to connect to the server. If the transport does not support this method the value -1 is returned. /// /// Maximum clients transport allows. public virtual int GetMaximumClients() { string message = $"The current transport does not support this feature."; if (NetworkManager == null) NetworkManager.StaticLogWarning(message); else NetworkManager.LogWarning(message); return -1; } /// /// Sets the maximum number of clients allowed to connect to the server. If applied at runtime and clients exceed this value existing clients will stay connected but new clients may not connect. /// /// Maximum clients to allow. public virtual void SetMaximumClients(int value) { string message = $"The current transport does not support this feature."; if (NetworkManager == null) NetworkManager.StaticLogWarning(message); else NetworkManager.LogWarning(message); } /// /// Sets which address the client will connect to. /// /// Address client will connect to. public virtual void SetClientAddress(string address) { } /// /// Returns which address the client will connect to. /// public virtual string GetClientAddress() => string.Empty; /// /// Sets which address the server will bind to. /// /// Address server will bind to. /// Address type to set. public virtual void SetServerBindAddress(string address, IPAddressType addressType) { } /// /// Gets which address the server will bind to. /// /// Address type to return. public virtual string GetServerBindAddress(IPAddressType addressType) => string.Empty; /// /// Sets which port to use. /// /// Port to use. public virtual void SetPort(ushort port) { } /// /// Gets which port to use. /// public virtual ushort GetPort() => 0; #endregion #region Start and stop. /// /// Starts the local server or client using configured settings. /// /// True to start server. public abstract bool StartConnection(bool server); /// /// Stops the local server or client. /// /// True to stop server. public abstract bool StopConnection(bool server); /// /// Stops a remote client from the server, disconnecting the client. /// /// ConnectionId of the client to disconnect. /// True to abrutly stop the client socket. The technique used to accomplish immediate disconnects may vary depending on the transport. /// When not using immediate disconnects it's recommended to perform disconnects using the ServerManager rather than accessing the transport directly. /// public abstract bool StopConnection(int connectionId, bool immediately); /// /// Stops both client and server. /// public abstract void Shutdown(); #endregion #region Channels. /// /// Gets the MTU for a channel. /// /// Channel to get MTU for. /// MTU of channel. public abstract int GetMTU(byte channel); #endregion } }