public static void WriteString(this NetworkWriter writer, string value) { // write 0 for null support, increment real size by 1 // (note: original HLAPI would write "" for null strings, but if a // string is null on the server then it should also be null // on the client) if (value == null) { writer.WriteUShort(0); return; } // write string with same method as NetworkReader // convert to byte[] int size = encoding.GetBytes(value, 0, value.Length, stringBuffer, 0); // check if within max size if (size >= NetworkWriter.MaxStringLength) { throw new IndexOutOfRangeException($"NetworkWriter.Write(string) too long: {size}. Limit: {NetworkWriter.MaxStringLength}"); } // write size and bytes writer.WriteUShort(checked ((ushort)(size + 1))); writer.WriteBytes(stringBuffer, 0, size); }
public static void WriteUShortNullable(this NetworkWriter writer, ushort?value) { writer.WriteBool(value.HasValue); if (value.HasValue) { writer.WriteUShort(value.Value); } }
// pack message before sending // -> NetworkWriter passed as arg so that we can use .ToArraySegment // and do an allocation free send before recycling it. public static void Pack<T>(T message, NetworkWriter writer) where T : struct, NetworkMessage { ushort msgType = GetId<T>(); writer.WriteUShort(msgType); // serialize message into writer writer.Write(message); }
public static void WriteString(this NetworkWriter writer, string value) { // write 0 for null support, increment real size by 1 // (note: original HLAPI would write "" for null strings, but if a // string is null on the server then it should also be null // on the client) if (value == null) { writer.WriteUShort(0); return; } // write string with same method as NetworkReader // convert to byte[] int size = 0; try { size = encoding.GetBytes(value, 0, value.Length, stringBuffer, 0); } catch (ArgumentException exception) { Debug.LogError("Caught ArguementException in Mirror::NetworkWriter.WriteString() " + exception.Message); return; } // check if within max size if (size >= NetworkWriter.MaxStringLength) { throw new IndexOutOfRangeException("NetworkWriter.Write(string) too long: " + size + ". Limit: " + NetworkWriter.MaxStringLength); } // write size and bytes writer.WriteUShort(checked ((ushort)(size + 1))); writer.WriteBytes(stringBuffer, 0, size); }
public static void WriteShort(this NetworkWriter writer, short value) => writer.WriteUShort((ushort)value);
public static void WriteUInt16(this NetworkWriter writer, ushort value) => writer.WriteUShort(value);
public static void WriteChar(this NetworkWriter writer, char value) => writer.WriteUShort(value);