In this PR I've switched the asynchronous pattern from the old BeginX/EndX to the more modern Task-based API. The PR consists of a number of small commits so that it's easier to follow along. The PR consists of the following logical steps:
Upgrade the .NET framework to allow using the Task-based API for sockets.
Refactor the StartListening method. The method was doing multiple things, these have been separated out into their own, smaller methods. The overall flow is still the same, but it's easier to get an overview of that the code is doing, due to the smaller method sizes.
Refactor from using the BeginReceive/EndReceive pattern to using a Task for the purpose. This change is contained in its own commit to make it clear what parts I changed to implement this.
The logic behind how packets are processed is simpler as the async parts are now in a central place, instead of being spread across multiple methods. This allows us to use a simpler mental model of what is happening: Loop to receive packets, handle them as they arrive, shut down when the socket is closed.
I've tested that the app still runs, but async code is tricky; there could easily be something I've overlooked. Do test thouroughly before merging anyhing from the latest commit in this PR. Please let me know if you can spot any behaviour that changes due to this PR. :-)
Fixes #3
In this PR I've switched the asynchronous pattern from the old BeginX/EndX to the more modern Task-based API. The PR consists of a number of small commits so that it's easier to follow along. The PR consists of the following logical steps:
The logic behind how packets are processed is simpler as the async parts are now in a central place, instead of being spread across multiple methods. This allows us to use a simpler mental model of what is happening: Loop to receive packets, handle them as they arrive, shut down when the socket is closed.
I've tested that the app still runs, but async code is tricky; there could easily be something I've overlooked. Do test thouroughly before merging anyhing from the latest commit in this PR. Please let me know if you can spot any behaviour that changes due to this PR. :-)