// abstract transport layer component // note: not all transports need a port, so add it to yours if needed. using System; using System.ComponentModel; using UnityEngine; using UnityEngine.Events; namespace Mirror { // UnityEvent definitions [Serializable] public class UnityEventArraySegment : UnityEvent> {} [Serializable] public class UnityEventException : UnityEvent {} [Serializable] public class UnityEventInt : UnityEvent {} [Serializable] public class UnityEventIntArraySegment : UnityEvent> {} [Serializable] public class UnityEventIntException : UnityEvent {} public abstract class Transport : MonoBehaviour { /// /// The current transport used by Mirror. /// public static Transport activeTransport; /// /// Is this transport available in the current platform? /// Some transports might only be available in mobile /// Many will not work in webgl /// /// True if this transport works in the current platform public virtual bool Available() { return Application.platform != RuntimePlatform.WebGLPlayer; } #region Client /// /// Notify subscribers when when this client establish a successful connection to the server /// [HideInInspector] public UnityEvent OnClientConnected = new UnityEvent(); /// /// Notify subscribers when this client receive data from the server /// [HideInInspector] public UnityEventArraySegment OnClientDataReceived = new UnityEventArraySegment(); /// /// Notify subscribers when this clianet encounters an error communicating with the server /// [HideInInspector] public UnityEventException OnClientError = new UnityEventException(); /// /// Notify subscribers when this client disconnects from the server /// [HideInInspector] public UnityEvent OnClientDisconnected = new UnityEvent(); /// /// Determines if we are currently connected to the server /// /// True if a connection has been established to the server public abstract bool ClientConnected(); /// /// Establish a connecion to a server /// /// The IP address or FQDN of the server we are trying to connect to public abstract void ClientConnect(string address); /// /// Send data to the server /// /// The channel to use. 0 is the default channel, /// but some transports might want to provide unreliable, encrypted, compressed, or any other feature /// as new channels /// The data to send to the server /// true if the send was successful public abstract bool ClientSend(int channelId, byte[] data); /// /// Disconnect this client from the server /// public abstract void ClientDisconnect(); #endregion #region Server /// /// Notify subscribers when a client connects to this server /// [HideInInspector] public UnityEventInt OnServerConnected = new UnityEventInt(); /// /// Notify subscribers when this server receives data from the client /// [HideInInspector] public UnityEventIntArraySegment OnServerDataReceived = new UnityEventIntArraySegment(); /// /// Notify subscribers when this server has some problem communicating with the client /// [HideInInspector] public UnityEventIntException OnServerError = new UnityEventIntException(); /// /// Notify subscribers when a client disconnects from this server /// [HideInInspector] public UnityEventInt OnServerDisconnected = new UnityEventInt(); /// /// Determines if the server is up and running /// /// true if the transport is ready for connections from clients public abstract bool ServerActive(); /// /// Start listening for clients /// public abstract void ServerStart(); /// /// Send data to a client /// /// The id of the client to send the data to /// The channel to be used. Transports can use channels to implement /// other features such as unreliable, encryption, compression, etc... /// /// true if the data was sent public abstract bool ServerSend(int connectionId, int channelId, byte[] data); /// /// Disconnect a client from this server. Useful to kick people out. /// /// the id of the client to disconnect /// true if the client was kicked public abstract bool ServerDisconnect(int connectionId); /// /// Deprecated: Use ServerGetClientAddress(int connectionId) instead /// [EditorBrowsable(EditorBrowsableState.Never), Obsolete("Use ServerGetClientAddress(int connectionId) instead")] public virtual bool GetConnectionInfo(int connectionId, out string address) { address = ServerGetClientAddress(connectionId); return true; } /// /// Get the client address /// /// id of the client /// address of the client public abstract string ServerGetClientAddress(int connectionId); /// /// Stop listening for clients and disconnect all existing clients /// public abstract void ServerStop(); #endregion /// /// Shut down the transport, both as client and server /// public abstract void Shutdown(); /// /// The maximum packet size for a given channel. Unreliable transports /// usually can only deliver small packets. Reliable fragmented channels /// can usually deliver large ones. /// /// channel id /// the size in bytes that can be sent via the provided channel public abstract int GetMaxPacketSize(int channelId = Channels.DefaultReliable); // block Update() to force Transports to use LateUpdate to avoid race // conditions. messages should be processed after all the game state // was processed in Update. // -> in other words: use LateUpdate! // -> uMMORPG 480 CCU stress test: when bot machine stops, it causes // 'Observer not ready for ...' log messages when using Update // -> occupying a public Update() function will cause Warnings if a // transport uses Update. // // IMPORTANT: set script execution order to >1000 to call Transport's // LateUpdate after all others. Fixes race condition where // e.g. in uSurvival Transport would apply Cmds before // ShoulderRotation.LateUpdate, resulting in projectile // spawns at the point before shoulder rotation. public void Update() {} } }