2using System.Runtime.CompilerServices;
15 public const int HeaderSize =
sizeof(ushort);
21 public static int MaxContentSize
23 [MethodImpl(MethodImplOptions.AggressiveInlining)]
32 [MethodImpl(MethodImplOptions.AggressiveInlining)]
33 public static ushort GetId<T>() where T :
struct,
NetworkMessage =>
34 (ushort)(typeof(T).FullName.GetStableHashCode() & 0xFFFF);
39 [MethodImpl(MethodImplOptions.AggressiveInlining)]
43 ushort msgType = GetId<T>();
44 writer.WriteUShort(msgType);
47 writer.Write(message);
54 [MethodImpl(MethodImplOptions.AggressiveInlining)]
55 public static bool Unpack(
NetworkReader messageReader, out ushort msgType)
60 msgType = messageReader.ReadUShort();
63 catch (System.IO.EndOfStreamException)
72 [MethodImpl(MethodImplOptions.AggressiveInlining)]
73 internal static NetworkMessageDelegate WrapHandler<T, C>(Action<C, T, int> handler,
bool requireAuthentication)
76 => (conn, reader, channelId) =>
92 int startPos = reader.Position;
95 if (requireAuthentication && !conn.isAuthenticated)
98 Debug.LogWarning($
"Closing connection: {conn}. Received message {typeof(T)} that required authentication, but the user has not authenticated yet");
107 message = reader.Read<T>();
109 catch (Exception exception)
111 Debug.LogError($
"Closed connection: {conn}. This can happen if the other side accidentally (or an attacker intentionally) sent invalid data. Reason: {exception}");
117 int endPos = reader.Position;
126 handler((C)conn, message, channelId);
130 Debug.LogError($
"Disconnecting connId={conn.connectionId} to prevent exploits from an Exception in MessageHandler: {e.GetType().Name} {e.Message}\n{e.StackTrace}");
138 [MethodImpl(MethodImplOptions.AggressiveInlining)]
139 internal static NetworkMessageDelegate WrapHandler<T, C>(Action<C, T> handler,
bool requireAuthentication)
144 void Wrapped(C conn, T msg,
int _) => handler(conn, msg);
145 return WrapHandler((Action<C, T, int>) Wrapped, requireAuthentication);
Base NetworkConnection class for server-to-client and client-to-server connection.
abstract void Disconnect()
Disconnects this connection.
Profiling statistics for tool to subscribe to (profiler etc.)
Network Reader for most simple types like floats, ints, buffers, structs, etc. Use NetworkReaderPool....
Network Writer for most simple types like floats, ints, buffers, structs, etc. Use NetworkWriterPool....
Abstract transport layer component
static Transport activeTransport
The current transport used by Mirror.
abstract int GetMaxPacketSize(int channelId=Channels.Reliable)
Maximum message size for the given channel.