MarcFletcher / NetworkComms.Net

NetworkComms.Net is a high performance cross-platform network library written in C#.
http://networkcomms.net
Apache License 2.0
542 stars 242 forks source link

Possible Bug when trying to reconnect with unmanaged connection #16

Open jpvdmerwe opened 8 years ago

jpvdmerwe commented 8 years ago

Hi,

I might have found a possible bug when trying to reconnect with an unmanaged connection.

First I create my unmanaged connection:

SendReceiveOptions optionsToUse = new SendReceiveOptions<NullSerializer>();
ConnectionInfo connectionInfo = new ConnectionInfo("192.168.0.2", 10001, ApplicationLayerProtocolStatus.Disabled);

var conn = TCPConnection.GetConnection(connectionInfo, optionsToUse);

After this I check the conn.ConnectionInfo.ConnectionState to see if it is ConnectionState.Established.

Here is my testing scenario:

  1. Start application
  2. Ethernet cable plugged in - ConnectionState is Established
  3. Ethernet cable is removed, a timer will create a new connection after 1 minute - ConnectionState is still Established.

Am I using the ConnectionState incorrectly?

Update: If you do not use the CloseConnection method before creating the new connection it will use the last ConnectionInfo for the IP / Port. Not sure if this is still a bug, for now everything seems fine.

Kind Regards, JP

MarcFletcher commented 8 years ago

Hi JP, AFAIK the value of ConnectionState cannot be relied upon for unmanaged connections. This state is primarily intended for use with managed connections. That being the case the bug is probably the connection state for unmanaged connections should always be ConnectionState.Undefined. I'll raise this as a bug for now. Marc

jpvdmerwe commented 8 years ago

Hi Marc,

Thanks for the response. I'm using the LastTrafficTime from the ConnectionInfo to determine whether to close the connection and attempt to reconnect.

Do you perhaps have a better approach?

JP

MarcFletcher commented 8 years ago

JP, in 95% of cases the managed connection is what you want to be using. How come you are using unmanaged connections?

jpvdmerwe commented 8 years ago

Marc,

I do use the managed connections when communicating with other NetworkComms.Net applications, however I use the unmanaged connections when communicating with 3rd party devices like GPS Devices, Serial Comms over TCP/IP devices, etc.

cimyong commented 4 years ago

I did too