Toemsel / Network

C# Network Library
https://push-force.dev
GNU Lesser General Public License v3.0
353 stars 65 forks source link

A Task was cancelled #84

Open rizwan1239 opened 2 years ago

rizwan1239 commented 2 years ago

Hi. The server randomly throws this exception anytime. The last time I found it was when no client was connected. What should be done?

image

Toemsel commented 2 years ago

Stop trying to send packets over a dead connection.

rizwan1239 commented 2 years ago

Stop trying to send packets over a dead connection.

Doesn't it automatically stop on calling connection.Close() on both sides?

Toemsel commented 2 years ago

It closes, but packets in the send-queue still try to make it through. The lib embraces a pretty slim state machine: ONLINE, OFFLINE. That's it. Everything before and after has to be considered in your code.

In your case, I assume the connection dies while sending a paket. (e.g. the sent packet is faulty)

If you think this behavior isn't intended or even a bug, please set up a small example project for further analysis.

rizwan1239 commented 2 years ago

It closes, but packets in the send-queue still try to make it through. The lib embraces a pretty slim state machine: ONLINE, OFFLINE. That's it. Everything before and after has to be considered in your code.

In your case, I assume the connection dies while sending a paket. (e.g. the sent packet is faulty)

If you think this behavior isn't intended or even a bug, please set up a small example project for further analysis.

Yeah, technically connection dies when the server is sending client a packet because connection might have closed on client side. Possibly comes from this part of code. image

I use it to check if the client is still available on the network. If an exception happens, I quickly close the connection. Do you think there still should be an issue?

Toemsel commented 2 years ago

I see. You might want to check whether the connection is still alive before you call connection.Close() Moreover, there is no need to perform pings manually. That is already a part of the lib itself. (It checks when the client/server is not reachable anymore) All you have todo is to subscribe to an event: connection.ConnectionClosed

You can configure the TIMEOUT (default 2.5 sec) or disable the pinging completely.

rizwan1239 commented 2 years ago

Are you talking about connection.SendPing()? It's probably not awaited call and doesn't have a response either. So will it cause an exception in try catch for detection of ping fail?

And yes I already have subscribed to the ConnectionClosed event. image

The initial exception randomly happened long after server was left without any clients. I tried to regenerate the exception by leaving it with 0 clients. Also tried to rapidly connect and disconnect connections but couldn't generate it. It randomly happens and sadly ends up crashing the application since its self contained build.

Toemsel commented 2 years ago

Strange. I have to test this case on my machine. So, in order to reproduce the issue, one has to:

  1. Connect a client
  2. Disconnect the client (force or via .close())
  3. Server experiences exception

// flaggy issue - doesn't occur all the time