rdavisau / sockets-for-pcl

Cross-platform socket API for Xamarin iOS/Android/Forms, Xamarin.Mac/MonoMac, Windows Phone 8/8.1, Windows Store and Windows Desktop.
MIT License
224 stars 72 forks source link

Method with "async void" signature #115

Closed ThomasZeman closed 7 years ago

ThomasZeman commented 7 years ago

In e.g. UpdSocketBase (Android) the method RunMessageReceiver has an "async void" signature which can lead to program/app crashes which are hard to understand and analyse. The signature should be changed to "async Task" and awaited in e.g. StartListeningAsync.

rdavisau commented 7 years ago

Howdy!

Thanks for this and the other issue with good observations. For this one, I think changing the signature is a good move; but I don't think it can be awaited as it would halt at the call site until the socket failed or disconnected.

One approach I've tried in the past is like this, exposing the errors through an event. Thoughts?

ThomasZeman commented 7 years ago

Looks better without the async void signature. Two other things: I would avoid copying arrays whenever possible especially in high frequent call scenarios. Also maybe consider instead of deriving from MemoryStream derive from Stream directly and aggregate MemoryStream if needed plus override the Async methods.