using System; #pragma warning disable 618 namespace UnityEngine.Networking { // Handles network messages on client and server public delegate void NetworkMessageDelegate(NetworkMessage netMsg); // Handles requests to spawn objects on the client public delegate GameObject SpawnDelegate(Vector3 position, NetworkHash128 assetId); // Handles requests to unspawn objects on the client public delegate void UnSpawnDelegate(GameObject spawned); /// /// Container class for networking system built-in message types. /// // built-in system network messages [Obsolete("The high level API classes are deprecated and will be removed in the future.")] public class MsgType { // internal system messages - cannot be replaced by user code /// /// Internal networking system message for destroying objects. /// public const short ObjectDestroy = 1; /// /// Internal networking system message for sending a ClientRPC from server to client. /// public const short Rpc = 2; /// /// Internal networking system message for spawning objects. /// public const short ObjectSpawn = 3; /// /// Internal networking system message for telling clients they own a player object. /// public const short Owner = 4; /// /// Internal networking system message for sending a command from client to server. /// public const short Command = 5; /// /// Internal networking system message for sending tranforms from client to server. /// public const short LocalPlayerTransform = 6; /// /// Internal networking system message for sending a SyncEvent from server to client. /// public const short SyncEvent = 7; /// /// Internal networking system message for updating SyncVars on a client from a server. /// public const short UpdateVars = 8; /// /// Internal networking system message for sending a USyncList generic list. /// public const short SyncList = 9; /// /// Internal networking system message for spawning scene objects. /// public const short ObjectSpawnScene = 10; /// /// Internal networking system message for sending information about network peers to clients. /// public const short NetworkInfo = 11; /// /// Internal networking system messages used to tell when the initial contents of a scene is being spawned. /// public const short SpawnFinished = 12; /// /// Internal networking system message for hiding objects. /// public const short ObjectHide = 13; /// /// Internal networking system message for HLAPI CRC checking. /// public const short CRC = 14; /// /// Internal networking system message for setting authority to a client for an object. /// public const short LocalClientAuthority = 15; /// /// Internal networking system message for sending tranforms for client object from client to server. /// public const short LocalChildTransform = 16; /// /// Internal networking system message for identifying fragmented packets. /// public const short Fragment = 17; /// /// Internal networking system message for sending information about changes in authority for non-player objects to clients. /// public const short PeerClientAuthority = 18; // used for profiling internal const short UserMessage = 0; internal const short HLAPIMsg = 28; internal const short LLAPIMsg = 29; internal const short HLAPIResend = 30; internal const short HLAPIPending = 31; /// /// The highest value of internal networking system message ids. User messages must be above this value. User code cannot replace these handlers. /// public const short InternalHighest = 31; // public system messages - can be replaced by user code /// /// Internal networking system message for communicating a connection has occurred. /// Ensure you use RegisterHandler on the client or server. Insert MsgType.Connect as a parameter to listen for connections. /// public const short Connect = 32; /// /// Internal networking system message for communicating a disconnect has occurred. /// To help understand the reason for a disconnect, an IntegerMessage number is written to the message body, which can be read and converted to the error enum. /// public const short Disconnect = 33; /// /// Internal networking system message for communicating an error. /// public const short Error = 34; /// /// Internal networking system message for clients to tell server they are ready. /// public const short Ready = 35; /// /// Internal networking system message for server to tell clients they are no longer ready. /// Can be used when switching scenes, to stop receiving network traffic during the switch. /// public const short NotReady = 36; /// /// Internal networking system message for adding player objects to client instances. /// This is sent to the server when a client calls NetworkClient.AddPlayer(). The server should have a handler for this message type to add the player object to the game and notify the client with NetworkServer.AddPlayer(). /// public const short AddPlayer = 37; /// /// Internal networking system message for removing a player object which was spawned for a client. /// public const short RemovePlayer = 38; /// /// Internal networking system message that tells clients which scene to load when they connect to a server. /// public const short Scene = 39; /// /// Internal networking system message for sending synchronizing animation state. /// Used by the NetworkAnimation component. /// public const short Animation = 40; /// /// Internal networking system message for sending synchronizing animation parameter state. /// Used by the NetworkAnimation component. /// public const short AnimationParameters = 41; /// /// Internal networking system message for sending animation triggers. /// Used by the NetworkAnimation component. /// public const short AnimationTrigger = 42; /// /// Internal networking system message for communicating a player is ready in the lobby. /// public const short LobbyReadyToBegin = 43; /// /// Internal networking system message for communicating a lobby player has loaded the game scene. /// public const short LobbySceneLoaded = 44; /// /// Internal networking system message for communicating failing to add lobby player. /// public const short LobbyAddPlayerFailed = 45; /// /// Internal networking system messages used to return the game to the lobby scene. /// public const short LobbyReturnToLobby = 46; /// /// Internal networking system message used when a client connects to the new host of a game. /// public const short ReconnectPlayer = 47; /// /// The highest value of built-in networking system message ids. User messages must be above this value. /// //NOTE: update msgLabels below if this is changed. public const short Highest = 47; static internal string[] msgLabels = { "none", "ObjectDestroy", "Rpc", "ObjectSpawn", "Owner", "Command", "LocalPlayerTransform", "SyncEvent", "UpdateVars", "SyncList", "ObjectSpawnScene", // 10 "NetworkInfo", "SpawnFinished", "ObjectHide", "CRC", "LocalClientAuthority", "LocalChildTransform", "Fragment", "PeerClientAuthority", "", "", // 20 "", "", "", "", "", "", "", "", "", "", // 30 "", // - SystemInternalHighest "Connect", // 32, "Disconnect", "Error", "Ready", "NotReady", "AddPlayer", "RemovePlayer", "Scene", "Animation", // 40 "AnimationParams", "AnimationTrigger", "LobbyReadyToBegin", "LobbySceneLoaded", "LobbyAddPlayerFailed", // 45 "LobbyReturnToLobby", // 46 "ReconnectPlayer", // 47 }; /// /// Returns the name of internal message types by their id. /// /// A internal message id value. /// The name of the internal message. static public string MsgTypeToString(short value) { if (value < 0 || value > Highest) { return String.Empty; } string result = msgLabels[value]; if (string.IsNullOrEmpty(result)) { result = "[" + value + "]"; } return result; } } /// /// The details of a network message received by a client or server on a network connection. /// [Obsolete("The high level API classes are deprecated and will be removed in the future.")] public class NetworkMessage { /// /// The size of the largest message in bytes that can be sent on a NetworkConnection. /// Note that channels that are not Fragmented cannot send messages larger than the Maximum Transmission Unity (MTU) size, which is about 1400 bytes by default. /// public const int MaxMessageSize = (64 * 1024) - 1; /// /// The id of the message type of the message. /// public short msgType; /// /// The connection the message was recieved on. /// public NetworkConnection conn; /// /// A NetworkReader object that contains the contents of the message. /// For some built-in message types with no body, this can be null. /// public NetworkReader reader; /// /// The transport layer channel the message was sent on. /// public int channelId; /// /// Returns a string with the numeric representation of each byte in the payload. /// /// Network message payload to dump. /// Length of payload in bytes. /// Dumped info from payload. public static string Dump(byte[] payload, int sz) { string outStr = "["; for (int i = 0; i < sz; i++) { outStr += (payload[i] + " "); } outStr += "]"; return outStr; } /// /// ReadMessage is used to extract a typed network message from the NetworkReader of a NetworkMessage object. /// For example in a handler for the AddPlayer message: /// /// using UnityEngine; /// using UnityEngine.Networking; /// using UnityEngine.Networking.NetworkSystem; /// /// public class MyManager : NetworkManager /// { /// void OnServerAddPlayerMessageInternal(NetworkMessage netMsg) /// { /// var msg = netMsg.ReadMessage<AddPlayerMessage>(); /// OnServerAddPlayer(netMsg.conn, msg.playerControllerId); /// } /// } /// /// The AddPlayerMessage that is created will be populated by calling DeSerialize(). So when it is returned form ReadMessage it is ready to use. /// /// The type of the Network Message, must be derived from MessageBase. /// public TMsg ReadMessage() where TMsg : MessageBase, new() { var msg = new TMsg(); msg.Deserialize(reader); return msg; } public void ReadMessage(TMsg msg) where TMsg : MessageBase { msg.Deserialize(reader); } } /// /// Enumeration of Networking versions. /// public enum Version { /// /// The current UNET version. /// Current = 1 } /// /// Class containing constants for default network channels. /// [Obsolete("The high level API classes are deprecated and will be removed in the future.")] public class Channels { /// /// The id of the default reliable channel used by the UNet HLAPI, This channel is used for state updates and spawning. /// public const int DefaultReliable = 0; /// /// The id of the default unreliable channel used for the UNet HLAPI. This channel is used for movement updates. /// public const int DefaultUnreliable = 1; } /// /// An enumeration of the options that can be set on a network channel. /// public enum ChannelOption { /// /// The option to set the number of pending buffers for a channel. /// These buffers are allocated dynamically as required when writes to the transport layer fail. Each buffer will be the size of maxPacketSize for the channel - usually around 1400 bytes. The default is 16 buffers. /// This only applies to reliable channels. If a reliable channel runs out of pnding buffers, data will be lost. /// MaxPendingBuffers = 1, AllowFragmentation = 2, MaxPacketSize = 3 // maybe add an InitialCapacity for Pending Buffers list if needed in the future } #if UNITY_EDITOR class Profiler { internal static void IncrementStatOutgoing(short msgType) { IncrementStatOutgoing(msgType, "msg"); } internal static void IncrementStatOutgoing(short msgType, string name) { UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, msgType, name, 1); } internal static void IncrementStatIncoming(short msgType) { IncrementStatIncoming(msgType, "msg"); } internal static void IncrementStatIncoming(short msgType, string name) { UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Incoming, msgType, name, 1); } internal static void SetStatOutgoing(short msgType, int value) { UnityEditor.NetworkDetailStats.SetStat( UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing, msgType, "msg", value); } internal static void ResetAll() { UnityEditor.NetworkDetailStats.ResetAll(); } internal static void NewProfilerTick() { UnityEditor.NetworkDetailStats.NewProfilerTick(Time.time); } } #endif } #pragma warning disable 618