private static void WriteNetObject(NetObject netObject, ref DataStreamWriter streamWriter) { streamWriter.WriteInt(netObject.ID); DataStreamWriter sizeWriter = streamWriter; streamWriter.WriteInt(0); int length = streamWriter.Length; netObject.Serialize(ref streamWriter, false); sizeWriter.WriteInt(streamWriter.Length - length); }
private static void WriteNetObject(NetObject netObject, ref DataStreamWriter streamWriter) { if (!netObject.IsMine) { return; } streamWriter.WriteInt(netObject.ID); DataStreamWriter sizeWriter = streamWriter; streamWriter.WriteInt(0); int length = streamWriter.Length; netObject.Serialize(ref streamWriter, false, b => b.HasAuthority); sizeWriter.WriteInt(streamWriter.Length - length); }
private void SendSpawnMessage(NetObject[] netObjects, NativeList <NetworkConnection> connections) { if (connections.Length == 0) { return; } AssertActive(); const int headerSizeInBytes = 8; var streamWriter = new DataStreamWriter(MaxBytesPerMessage, Allocator.Temp); var objectWriter = new DataStreamWriter(MaxBytesPerMessage - headerSizeInBytes, Allocator.Temp); var objectIndex = 0; // compose new message if objects left to send or copy to message stream while (objectIndex < netObjects.Length || objectWriter.Length > 0) { streamWriter.Clear(); // write header streamWriter.WriteInt(Commands.SpawnNetObjects); DataStreamWriter objectCountWriter = streamWriter; streamWriter.WriteInt(0); // copy data over to message stream and write to object stream var objectsInMessage = 0; while (streamWriter.Length + objectWriter.Length <= MaxBytesPerMessage) { if (objectWriter.Length > 0) { streamWriter.WriteBytes(objectWriter.AsNativeArray()); objectWriter.Clear(); objectsInMessage++; } if (objectIndex < netObjects.Length) { NetObject netObject = netObjects[objectIndex++]; objectWriter.WriteInt(netObject.ID); objectWriter.WriteUShort(netObject.PrefabIndex); objectWriter.WriteInt(netObject.OwnerActorNumber); objectWriter.WriteVector3(netObject.transform.position); objectWriter.WriteQuaternion(netObject.transform.rotation); objectWriter.WriteInt(netObject.gameObject.scene.buildIndex); DataStreamWriter objectSizeWriter = objectWriter; objectWriter.WriteInt(0); int length = objectWriter.Length; netObject.Serialize(ref objectWriter, true); objectSizeWriter.WriteInt(objectWriter.Length - length); } else { break; } } objectCountWriter.WriteInt(objectsInMessage); // message complete. Send if payload present if (objectsInMessage == 0) { return; } for (var connectionIndex = 0; connectionIndex < connections.Length; connectionIndex++) { DataStreamWriter writer = _serverDriver.BeginSend(_reliablePipeline, connections[connectionIndex]); writer.WriteBytes(streamWriter.AsNativeArray()); _serverDriver.EndSend(writer); } } }