The online racing simulator
InSimDotNet, problems reconnecting to a LFS client.
I am writing a very basic InSim program, connecting to a local LFS client. I am polling the connection periodically, by calling insim.Initialize() if IsConnected is false, to connect when the LFS client is started, which works fine. When LFS is closed however, the next attempt to connect finds my InSim object to have Disposed of itself, and an exception is thrown.
Questions:

Is catching the ObjectDisposed event the correct way to handle this?

Is it ok to just create a new instance at that point & continue with that?

Will all my event binds be ok bound to the new instance? The old instance still appears to exist, even though it 'isDisposed' and still appears to be bound to all my event handlers.

Is there a better way I could handle it, say on the Disconnected event, and somehow keep my existing InSim object alive?
The InSim object doesn't appear to be reusable. I guess you're supposed to create a new instance of InSim each time you connect to LFS. This works for me:

--- Program.cs ---

using System;

namespace ISN_Reconnect
{
class MainClass
{
public static void Main (string[] args)
{
InSimConnector isc = new InSimConnector("127.0.0.1", 29999, '!');
isc.InitAndConnect();

Console.ReadLine();
isc.Close();
}
}
}

-- InSimConnector.cs---

using System;
using System.Threading;
using InSimDotNet;
using InSimDotNet.Packets;

namespace ISN_Reconnect
{
public class InSimConnector
{
private InSimSettings settings;
private InSim inSim;

public InSimConnector(string host, UInt16 port, char prefix)
{
settings = new InSimSettings {
Host = host,
Port = port,
Prefix = prefix,
Admin = String.Empty
};
}

public void Close()
{
inSim.Disconnected -= OnDisconnected;
inSim.Disconnect();
}

public void InitAndConnect()
{
inSim = new InSim();
inSim.Disconnected += OnDisconnected;

inSim.Bind<IS_MSO>(IS_MSO_Handler);

while (true) {
try {
inSim.Initialize(settings);
} catch (InSimException ex) {
Console.WriteLine("Waiting for connection..." + ex.Message);
}
if (inSim.IsConnected) {
Console.WriteLine("InSim connected");
return;
}
Thread.Sleep(2000);
}
}

private void OnDisconnected(object sender, DisconnectedEventArgs ea)
{
InitAndConnect();
}

private void IS_MSO_Handler(InSim inSim, IS_MSO mso)
{
Console.WriteLine("Got message: " + mso.Msg);
}
}
}

I'm not sure gow .NET manages memory in this case, in the worst case this code could be leaking the "old" InSim object.
Thank you - that is pretty much how I ended up doing it for now, and it seems to work ok.
I think the old InSim instances do still exist, because they seem to still be associated with my packet event handlers. I might try releasing those on the disconnect event & see if that helps. Still, I don't think there should be a problem as it is, unless it reconnects 1000's of times!
Sorry bit late on this. Basically yes, you need to create a new instance of the InSim class each time you want to connect to LFS. If you have an existing InSim object then you would need to unhook all event handlers and unbind all packets before you dispose of the reference to it, otherwise you could end up with a memory leak.

If I was going to rewrite the library now I would not do it this way as it's a lot of unnecessary hassle, but that's how it was written at the time.

FGED GREDG RDFGDR GSFDG