// batch as many messages as possible into writer // returns true if any batch was made. public bool MakeNextBatch(NetworkWriter writer, double timeStamp) { // if we have no messages then there's nothing to do if (messages.Count == 0) { return(false); } // make sure the writer is fresh to avoid uncertain situations if (writer.Position != 0) { throw new ArgumentException($"MakeNextBatch needs a fresh writer!"); } // write timestamp first // -> double precision for accuracy over long periods of time writer.WriteDouble(timeStamp); // do start no matter what do { // add next message no matter what. even if > threshold. // (we do allow > threshold sized messages as single batch) NetworkWriterPooled message = messages.Dequeue(); ArraySegment <byte> segment = message.ToArraySegment(); writer.WriteBytes(segment.Array, segment.Offset, segment.Count); // return the writer to pool NetworkWriterPool.Return(message); } // keep going as long as we have more messages, // AND the next one would fit into threshold. while (messages.Count > 0 && writer.Position + messages.Peek().Position <= threshold); // we had messages, so a batch was made return(true); }
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 WriteGuid(this NetworkWriter writer, Guid value) { byte[] data = value.ToByteArray(); writer.WriteBytes(data, 0, data.Length); }