InSim Relay

Note: this is a work in progress

InSim Relay is a service that allows you to connect to any subscribed LFS host and share data. You connect to the relay server and select a specific host, with which you are able to send and receive a subset of InSim packets. For the most part it is exactly like connecting to a normal host, with a few small differences. In order to send any instructions to a relay host you must specify an admin password. Some hosts may also require a spectator password to connect at all.

Connecting to InSim Relay

Connecting to InSim Relay is simple, are requires only that the InSimSettings.IsRelayHost property is set to true when initializing InSim.

// Create new InSim connection.
InSim insim = new InSim();

// Initiaize InSim relay.
insim.Initialize(new InSimSettings {
    IsRelayHost = true
});

// Stop program from exiting.
Console.ReadLine();

When IsRelayHost is set to true, all other InSim settings are ignored.

Requesting the host list

Once connected to InSim relay we can request a list of hosts connected to the relay server.

static void Main() {
    InSim insim = new InSim();

    // Bind handler for HOS (host list) packet.
    insim.Bind<IR_HOS>(HostList);

    insim.Initialize(new InSimSettings {
        IsRelayHost = true
    });

    // Request host list.
    insim.Send(new IR_HLR { ReqI = 1 });

    Console.ReadLine();
}

static void HostList(InSim insim, IR_HOS hos) {
    // Loop through each host connected to the server and print out some details
    foreach (HInfo info in hos.Info) {
        Console.WriteLine(
            "{0} ({1} / {2})",
            StringHelper.StripColors(info.HName),
            info.Track,
            info.NumConns);
    }
}

Up to six hosts are included in the IS_HOS packet, if more than six hosts are connected to InSim Relay then more than one IS_HOS packet is sent. The flags property of the HInfo sub-packet can be used to determine whether a host is the first or last in the list, as well as other options such as whether the host requires a password.

Selecting a host

To select a specific relay host, simply send a IR_SEL packet, specifying the host name. InSim Relay will then begin sending packets for that host. In this example we select a host, then request its connection list.

static void Main() {
    InSim insim = new InSim();

    // Bind event handler for NCN (new connection) packet
    insim.Bind<IS_NCN>(NewConnection);

    // Initialize InSim relay
    insim.Initialize(new InSimSettings {
        IsRelayHost = true
    });

    // Select a host
    insim.Send(new IR_SEL {
        HName = "[CLC] CityLifeCrew Cruise", // Host to select
        Admin = String.Empty, // Optional admin pass
        Spec = String.Empty // Optional spectator pass
    });

    // Request connection list.
    insim.Send(new IS_TINY {
        ReqI = 1,
        SubT = TinyType.TINY_NCN,
    });

    Console.ReadLine();
}

static void NewConnection(InSim insim, IS_NCN ncn) {
    // Output name of each player connected to the host
    Console.WriteLine("{0}", StringHelper.StripColors(ncn.PName));
}

Once selected you can send and receive packets as normal, presuming you have set the correct admin password.

Handling Relay Errors

...

Last edited Mar 11, 2012 at 6:09 PM by AlexMcBride, version 8

Comments

No comments yet.