#HTTP Call + Jobs

1 messages · Page 1 of 1 (latest)

frozen marsh
#

How do i use UnityWebRequest with Jobs?

torn moon
#

UnityWebRequest is a managed type, so you cannot pass it to a job.

#

The request itself can be done asynchronously though (yielding until the operating system returns the data from the socket), so there isn't really a reason to use jobs with it

frozen marsh
#

        Entities
            .WithoutBurst()
            .ForEach(async (PlayerAuthenticateTag tag, Entity entity) =>
            {
                entityCommandBuffer.RemoveComponent<PlayerAuthenticateTag>(entity);

                HttpContent content = new StringContent(JsonConvert.SerializeObject(new
                {
                    character_id = tag.CharacterId
                }), System.Text.Encoding.UTF8);

                content.Headers.Add("Authorization", $"Bearer {tag.AccessToken}");

                var response = await client.PostAsync(AUTHENTICATE_URL, content);

                CharacterModel characterModel = JsonConvert.DeserializeObject<CharacterModel>(await response.Content.ReadAsStringAsync());
            });```
is this fine?
frozen marsh
#

why not? it is working for me

#

But i extracted the async call outside the foreach

mystic drift
#

I am afraid to think what happens under the hood for you 😅

frozen marsh
#
        {
            while (saveQueue.TryDequeue(out Entity playerToSave))
            {
                Debug.Log("MainThread: " + System.Threading.Thread.CurrentThread.ManagedThreadId);

                SavePlayerAsync(client, new CharacterModel() { });
            }
        }).Run();

        entityCommandBuffer.Playback(EntityManager);```
mystic drift
#

jobs and async don't work together in any way

#

you can use job as async operation

#

but that's just useless

frozen marsh
#
    {
        try
        {
            HttpContent content = new StringContent(JsonConvert.SerializeObject(model), System.Text.Encoding.UTF8);

           
            await client.PostAsync($"", content);


        }
        finally
        {
            Debug.Log("AuthenticateThread: " + System.Threading.Thread.CurrentThread.ManagedThreadId);
        }
    }```
#

it works for me

#

but i`m not using jobs

#

I`m using but it running on main thread

mystic drift
frozen marsh
#

Yeah, it works. Another job with burst create the save queue

#

and the main thread consume from save queue to save

#

it is for a online game. The server needs to save the players to database every once

mystic drift
#

you don't need jobs for that...

#

it's way slower than just running it as method

frozen marsh
#

Why is it slower?

#

For what? saving or building the save queue?

#
            .WithoutBurst()
            .ForEach((PlayerAuthenticateTag tag, Entity entity) =>
            {
                entityCommandBuffer.RemoveComponent<PlayerAuthenticateTag>(entity);

                AuthenticatePlayerAsync(client, tag.CharacterId, tag.AccessToken, entity);
            })
            .Run();``` this is how i`m authenticating the player
mystic drift
#

also using deprecated entities for each

#

use SystemAPI.Query

frozen marsh
#

How?

mystic drift
#

look up SystemAPI.Query in manual

#

it has example

frozen marsh
#

i need the RefRW<L?

#

with foreach i don`t need it

#

@mystic drift can i replace this with SystemAPI.Query?

            .WithBurst()
            .ForEach((ref PlayerSaveData saveData, in Entity entity) =>
            {
                if (time.ElapsedTime >= saveData.SaveAt)
                {
                    saveData.SaveAt = time.ElapsedTime + 30;

                    saveQueue.Enqueue(entity);
                }
            })
            .Schedule();```
#

It has with burst and query

#

and schedule

#

i need an IJobEntity for this?

mystic drift
#

but yeah, you better switch it into IJobEntity

frozen marsh
#

Can i use IJobEntity with SystemBase?

mystic drift
#

yes

frozen marsh
#

there is no Schedule on it

red rock
#

Did you make it partial?

#

It won't code gen the schedule unless it's marked partial

frozen marsh
#

Yeah, i forgot the partial

#
    {
        TimeData time = SystemAPI.Time;

        NativeQueue<Entity> saveQueue = SaveQueue;
        HttpClient client = Client;

        var entityCommandBuffer = new EntityCommandBuffer(Allocator.Temp);

        ProduceSaveQueueJob produceJob = new ProduceSaveQueueJob()
        {
            SaveQueue = saveQueue,
            Time = time
        };

        produceJob.Schedule();

        foreach(var (tag, entity) in SystemAPI.Query<PlayerAuthenticateTag>().WithEntityAccess())
        {
            entityCommandBuffer.RemoveComponent<PlayerAuthenticateTag>(entity);

            AuthenticatePlayerAsync(client, tag.CharacterId, tag.AccessToken, entity);
        }

        CompleteDependency();

        while (saveQueue.TryDequeue(out Entity playerToSave))
        {
            Debug.Log("MainThread: " + System.Threading.Thread.CurrentThread.ManagedThreadId);

            SavePlayerAsync(client, new CharacterModel() { });
        }

        entityCommandBuffer.Playback(EntityManager);
    }```
#

this is the final code

#

It is working

#

But the player save system is full main thread.

I have benchmarked my networking and i can have 3000 players (all moving and with 300 players per area of interest) using 800 megabits/second and all my 12 cores

#

I`m afraid the save system can bottleneck the server

red rock
#

Have you looked at my save system

#

It can handle 100k entities in a few ms entirely threaded

mystic drift
red rock
#

No

#

Does require core though

#

i released my full save library, except migration, for free

mystic drift
#

what migration is about?

#

I don't see a link

#

of paid asset

#

I guess it's not released yet?

red rock
#

migration is for maintaining save files post launch

#

when you change you data and you need to migrate from 1 format to anohter

#

imagine, you have
struct MyData : IComponentData {int Value; }

and you decide to change it to
struct MyData : IComponentData {float Value; ushort Value2 }

You need to migrate it

red rock
#

so i decided to just release 95% of it for free as i've had a few people ask for it

#

basically all the stuff you need before you launch your game, completely full featured

#

migration is a post launch problem as it's often too much effort to care about while rapidly developing

frozen marsh
#

I`m saving to database as it is a mmo game, a persistent world like tibia for example

#

Not really viable to save as binary data

#

But it does not save all players at same time

red rock
#

oh yeah that makes sense

#

something to add to my roadmap

#

i actualy think my implementation would be fine for this to serialize individual players while still allowing you serialize the rest of the world state

#

but i'll need to throw together a sample

#

do you actually need to serialize individual fields

#

or just individual players?

#

with maybe a bit of human readable meta

frozen marsh
#

individual players

#

i`m using JSON (that is what the api accepts)

#

But i always save a full player snapshot

#

every 30s or when players do a important action (like die, pickup an item, etc) i save a snapshot of this player

red rock
#

yeah in that case my library could do this, but i think if i had a filter that could do a set of entities instead of using shared filters it'd be a lot nicer 🗒️

frozen marsh
#

why not enableable components?

#

are they good?

#

can your library output JSON?

torn moon
red rock