Transports#
A mod adds a transport, such as TCP/IP or HTTP, as a protocol. This in-process transport takes its calls from other threads of the game process. Its protocol names it, gives its settings, and creates its server:
[KRPCProtocol]
public sealed class QueueProtocol : ProtocolDescriptor
{
readonly IList<Setting> settings = new List<Setting> ();
public override string Id {
get { return "QueueExample"; }
}
public override string DisplayName {
get { return "In-process queue"; }
}
public override string Name {
get { return "Queue Server"; }
}
public override IList<Setting> Settings {
get { return settings; }
}
public override IMessageServer Create (
IDictionary<string, string> values)
{
return new QueueServer ();
KRPC.Server.KRPCProtocol marks the class, which needs a public parameterless constructor. The game creates
and registers it before it loads its settings, so a server of the protocol starts with the others.
Each setting is a KRPC.Server.Setting, and the server window draws a field for it by its kind.
The id names the protocol in the settings file.
Calls#
Each call the transport takes is a KRPC.Server.Call. It decodes its request on the game
thread, and receives its response there:
sealed class QueuedCall : Call
{
readonly ProcedureSignature procedure;
readonly object[] arguments;
readonly TaskCompletionSource<ProcedureResult> result =
new TaskCompletionSource<ProcedureResult> ();
public QueuedCall (ProcedureSignature procedure, object[] arguments)
: base (Stopwatch.GetTimestamp ())
{
this.procedure = procedure;
this.arguments = arguments;
}
public Task<ProcedureResult> Result {
get { return result.Task; }
}
// Runs on the game thread
public override Request Decode ()
{
var call = new ProcedureCall (procedure);
for (var i = 0; i < arguments.Length; i++)
call.Arguments.Add (new Argument ((uint)i, arguments [i]));
var request = new Request ();
request.Calls.Add (call);
return request;
}
public override void Complete (Response response)
{
if (response.HasError)
result.TrySetResult (
new ProcedureResult { Error = response.Error });
else
result.TrySetResult (response.Results [0]);
}
public override void Abort ()
{
result.TrySetCanceled ();
Abort fails a call whose client ended before it completed. A request names its procedure by a
ProcedureSignature, from KRPC.Server.Transport.ServerCore.Services.
A transport with its own encoding reaches the object store and the structures through
ServerCore. AddObject gives the id of a class instance and GetObject the instance of an
id, both on the game thread. Struct gives the structure with a type, with its fields.
Clients#
Each client queues its calls on a KRPC.Server.CallQueue, which the server core reads. The
queue completes each call with the response to its request:
public Task<ProcedureResult> Call (
string service, string procedure, params object[] arguments)
{
var signature = ServerCore.Services
.First (x => x.Name == service).Procedures [procedure];
var call = new QueuedCall (signature, arguments);
var current = client;
if (current == null)
call.Abort ();
else
current.Calls.Enqueue (call);
return call.Result;
Enqueue may be called from any thread, and wakes the server’s update. End aborts the calls
queued and in flight. The server raises OnClientRequestingConnection for each new client, as
the network servers do, so the player approves it in the same way:
public void Update ()
{
if (client == null)
return;
if (!client.Connected)
Disconnected (client);
if (requested)
return;
var args =
new ClientRequestingConnectionEventArgs<IMessageStream> (client);
if (OnClientRequestingConnection != null)
OnClientRequestingConnection (this, args);
if (args.Request.ShouldAllow) {
requested = true;
connected = true;
client.Connected = true;
if (OnClientConnected != null)
OnClientConnected (this,
new ClientConnectedEventArgs<IMessageStream> (client));
} else if (args.Request.ShouldDeny) {
requested = true;
client.Close ();
A client the server core ends has Connected false, and the next update raises
OnClientDisconnected for it.
Note
A breaking change to the transport API is a major change of the version of KRPC.Core,
which a mod checks through the assembly version.