public void Send(TransportConnection connection, int resource, ref Wav wav)
        {
            using var writer = new DataBufferWriter(0);
            writer.WriteInt((int)EAudioSendType.SendReplyResourceData);
            writer.WriteInt(resource);
            writer.WriteValue((double)wav.getLength());

            foreach (var(_, feature) in Features)
            {
                feature.Driver.Send(default, connection, writer.Span);
Example #2
0
        protected override void OnUpdate()
        {
            base.OnUpdate();

            var maxId = 0;

            foreach (var entity in resourceSet.GetEntities())
            {
                maxId = Math.Max(maxId, entity.Get <AudioResource>().Id);
            }

            // for memory usage, don't put this call into the foreach since stackalloc is only freed when this method itself is finished
            Span <Entity> clientUpdated = stackalloc Entity[resourceSet.Count];

            foreach (var(featureEntity, feature) in Features)
            {
                var update     = false;
                var previousId = 0;
                if (!clientLastMaxId.TryGetValue(feature, out var clientMaxId) || clientMaxId < maxId)
                {
                    previousId = clientMaxId;
                    clientLastMaxId[feature] = maxId;
                    update = true;
                }

                if (update)
                {
                    var updatedCount = 0;
                    foreach (var entity in resourceSet.GetEntities())
                    {
                        if (entity.Get <AudioResource>().Id > previousId)
                        {
                            clientUpdated[updatedCount++] = entity;
                        }
                    }

                    using var writer = new DataBufferWriter(updatedCount);
                    writer.WriteInt((int)EAudioSendType.RegisterResource);
                    writer.WriteInt(updatedCount);
                    foreach (var entity in clientUpdated.Slice(0, updatedCount))
                    {
                        writer.WriteInt(entity.Get <AudioResource>().Id);
                        var typeMarker = writer.WriteInt(0);
                        if (entity.TryGet(out AudioBytesData bytesData))
                        {
                            writer.WriteInt((int)EAudioRegisterResourceType.Bytes, typeMarker);
                            writer.WriteInt(bytesData.Value.Length);
                            writer.WriteDataSafe(bytesData.Value.AsSpan(), default);
                        }
                    }

                    if (feature.Driver.Broadcast(feature.PreferredChannel, writer.Span) < 0)
                    {
                        throw new InvalidOperationException("Couldn't send data!");
                    }
                }
            }
        }
Example #3
0
        protected override void OnUpdate()
        {
            base.OnUpdate();

            if (withoutIdSet.Count > 0)
            {
                Span <Entity> entities = stackalloc Entity[withoutIdSet.Count];
                withoutIdSet.GetEntities().CopyTo(entities);
                foreach (ref readonly var entity in entities)
                {
                    entity.Set(new AudioPlayerId(selfLastMaxId++));
                }
            }

            var maxId = 0;

            foreach (var entity in playerSet.GetEntities())
            {
                maxId = Math.Max(maxId, entity.Get <AudioPlayerId>().Id);
            }

            // for memory usage, don't put this call into the foreach since stackalloc is only freed when this method itself is finished
            Span <Entity> clientUpdated = stackalloc Entity[playerSet.Count];

            foreach (var(featureEntity, feature) in Features)
            {
                var update     = false;
                var previousId = 0;
                if (!clientLastMaxId.TryGetValue(feature, out var clientMaxId) || clientMaxId < maxId)
                {
                    previousId = clientMaxId;
                    clientLastMaxId[feature] = maxId;
                    update = true;
                }

                if (update)
                {
                    var updatedCount = 0;
                    foreach (var entity in playerSet.GetEntities())
                    {
                        if (entity.Get <AudioPlayerId>().Id > previousId)
                        {
                            clientUpdated[updatedCount++] = entity;
                        }
                    }

                    using var writer = new DataBufferWriter(updatedCount);
                    writer.WriteInt((int)EAudioSendType.RegisterPlayer);
                    writer.WriteInt(updatedCount);
                    foreach (var entity in clientUpdated.Slice(0, updatedCount))
                    {
                        writer.WriteInt(entity.Get <AudioPlayerId>().Id);
                        writer.WriteStaticString(TypeExt.GetFriendlyName(entity.Get <AudioPlayerType>().Type));
                    }

                    if (feature.Driver.Broadcast(feature.PreferredChannel, writer.Span) < 0)
                    {
                        throw new InvalidOperationException("Couldn't send data!");
                    }
                }
            }
        }