#HTTP Call + Jobs
1 messages · Page 1 of 1 (latest)
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
can i integrate someshow HttpClient with pure ecs world? use await, etc?
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?
no
I am afraid to think what happens under the hood for you 😅
{
while (saveQueue.TryDequeue(out Entity playerToSave))
{
Debug.Log("MainThread: " + System.Threading.Thread.CurrentThread.ManagedThreadId);
SavePlayerAsync(client, new CharacterModel() { });
}
}).Run();
entityCommandBuffer.Playback(EntityManager);```
jobs and async don't work together in any way
you can use job as async operation
but that's just useless
{
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
as it should
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
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
because you have job overhead for nothing
also using deprecated entities for each
use SystemAPI.Query
How?
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?
this one is fine with job
but yeah, you better switch it into IJobEntity
Can i use IJobEntity with SystemBase?
yes
there is no Schedule on it
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
Have you looked at my save system
It can handle 100k entities in a few ms entirely threaded
it is part of .core repo?
No
Does require core though
i released my full save library, except migration, for free
what migration is about?
I don't see a link
of paid asset
I guess it's not released yet?
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
yeah, I just don't have time/effort to write documentation/demos at the moment
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
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
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
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
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 🗒️
The save system outputs as NativeList<byte> for performance reasons, so no
You know that could work, I don't even need to change anything in the library to make that work