Example #1
0
        /// <param name="value">string or null</param>
        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.WriteUInt16(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 >= MaxStringLength)
            {
                throw new DataMisalignedException($"NetworkWriter.Write(string) too long: {size}. Limit: {MaxStringLength}");
            }

            // write size and bytes
            writer.WriteUInt16(checked ((ushort)(size + 1)));
            writer.WriteBytes(stringBuffer, 0, size);
        }
Example #2
0
        // 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)
        {
            // if it is a value type,  just use typeof(T) to avoid boxing
            // this works because value types cannot be derived
            // if it is a reference type (for example IMessageBase),
            // ask the message for the real type
            Type mstType = default(T) == null && message != null?message.GetType() : typeof(T);

            int msgType = GetId(mstType);

            writer.WriteUInt16((ushort)msgType);

            writer.Write(message);
        }
Example #3
0
 public static void WriteUInt16Extension(this NetworkWriter writer, ushort value)
 {
     writer.WriteUInt16(value);
 }
Example #4
0
 public static void WriteChar(this NetworkWriter writer, char value) => writer.WriteUInt16(value);