Lep Posted December 7, 2022 Posted December 7, 2022 This is the first time I've finally experienced this issue. So wanted to ask: is this one of the cases where I should be using Async operations or does it have something to do with my code?I was working on a Map Reloader so I don't have to restart the server every time that I change an NPC and realized exactly how the server was handling things.My code looks as such:private static void Process_ReloadMap(Player client, string[] command) { foreach (var player in PlayerManager.Players.Values) if (player.MapID == client.MapID) player.SendMessage($"{client.Name}: Map will be reloaded in: 5"); Thread.Sleep(1000); foreach (var player in PlayerManager.Players.Values) if (player.MapID == client.MapID) player.SendMessage($"{client.Name}: Map will be reloaded in: 4"); Thread.Sleep(1000); foreach (var player in PlayerManager.Players.Values) if (player.MapID == client.MapID) player.SendMessage($"{client.Name}: Map will be reloaded in: 3"); Thread.Sleep(1000); foreach (var player in PlayerManager.Players.Values) if (player.MapID == client.MapID) player.SendMessage($"{client.Name}: Map will be reloaded in: 2"); Thread.Sleep(1000); foreach (var player in PlayerManager.Players.Values) if (player.MapID == client.MapID) player.SendMessage($"{client.Name}: Map will be reloaded in: 1"); MapManager.RemoveMap(client.MapID); foreach (var player in PlayerManager.Players.Values) { if (player.MapID == client.MapID) { player.ChangeMap(player.MapID, player.X, player.Y); player.SendMessage($"{client.Name}: Map successfully reloaded."); } } }I was using the following code, but then I realized that the server will do a countdown for each player individually before the map got reloaded, which means I would of had to wait 50 seconds for 10 players.private static void Process_ReloadMap(Player client, string[] command) { foreach (var player in PlayerManager.Players.Values) { if (player.MapID == client.MapID) { player.SendMessage($"{client.Name}: Map will be reloaded in: 5"); Thread.Sleep(1000); player.SendMessage($"{client.Name}: Map will be reloaded in: 4"); Thread.Sleep(1000); player.SendMessage($"{client.Name}: Map will be reloaded in: 3"); Thread.Sleep(1000); player.SendMessage($"{client.Name}: Map will be reloaded in: 2"); Thread.Sleep(1000); player.SendMessage($"{client.Name}: Map will be reloaded in: 1"); } } MapManager.RemoveMap(client.MapID); foreach (var player in PlayerManager.Players.Values) { if (player.MapID == client.MapID) { player.ChangeMap(player.MapID, player.X, player.Y); player.SendMessage($"{client.Name}: Map successfully reloaded."); } } } Quote
Spirited Posted December 8, 2022 Posted December 8, 2022 So, if you're following the multi-threaded game server model I mention on my blog where you have a single channel per map, then you're blocking your only thread. Those thread sleeps are what's killing you. Generally, you should never use thread sleeps in any main execution thread. What you can do instead is spawn a new task that performs the reload. Like maybe have a component on your server for managing the task (and making sure not more than one can be spawned at a time).Edit: Also, if you reload a map while players are on it... they could potentially perform actions on the map unless you block traffic during the reload. Quote
Lep Posted December 8, 2022 Author Posted December 8, 2022 So, if you're following the multi-threaded game server model I mention on my blog where you have a single channel per map, then you're blocking your only thread. Those thread sleeps are what's killing you. Generally, you should never use thread sleeps in any main execution thread. What you can do instead is spawn a new task that performs the reload. Like maybe have a component on your server for managing the task (and making sure not more than one can be spawned at a time).Edit: Also, if you reload a map while players are on it... they could potentially perform actions on the map unless you block traffic during the reload.I haven't really looked into threading before; I might start now. I was thinking it might have been an issue due to how the server reads one thing at a time, then on to the next. I'm currently working on Redux since I've already put some work into it, I know one of your projects has async tasks in it and I've been thinking of using it as reference, if not transferring some of the work over.The question towards the Async was more towards when the map is reloading and you can't see what other characters are doing, but once it finishes, they perform the actions. I'm guessing that's where the threading comes in and I thought I could just bypass that by making the server read the jump packets and the map reload at the same time with an asynchronous task. Quote
Spirited Posted December 8, 2022 Posted December 8, 2022 Ah! Well, it's time you learn about threading then, 'cause it's VERY important for making a game server. Rule of thumb though, never have a thread sleep. If you have your command just reload NPCs in-place without a countdown, then it'd probably be fine. It's just for development purposes anyways, right? Quote
Lep Posted December 8, 2022 Author Posted December 8, 2022 Yeah, sometimes I work on both the local and the server hosted on the VPS to see the difference and as I edit the database I'd rather just use a command to reload the NPCs at once but just in case, I wanted to have a countdown in case someone else was doing something. I have to work on a Update method for the NPCs at some point anyways, so in the meantime I might just do that while I get to learn threads. Redux is kind of weird when it comes to updating things on the map, some things have it, some don't. I've been just removing and readding everything lately, but I think it's time the code gets a small upgrade. A lot of calculations are not in the source or are messed up, so I've been mostly working on that instead of taking the time to work on infrastructure.Edit: I guess I could also just make a clock that counts down instead of stopping threads ngl -.0Edit: I keep clicking "Quote" instead of reply... Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.