Skip to content

Raw RPC and Packets

Алексей . edited this page Mar 30, 2026 · 5 revisions

Working with Raw RPC and Packets

When to use the raw API

Use this layer when:

  • a packet or RPC is not described in the parser catalog yet
  • you want to read BitStreamReader manually
  • you need transport details such as ERpcId, EPacketId, DataBitLength, raw payload, or offsets

If a parser already exists and you only need a typed payload, SF.Events is usually the better choice.

Raw RPC API

Source:

  • https://github.com/AllahPower/SF-Arizona/blob/master/src/SF/Networking/SFRpc.cs

Subscribe to an incoming RPC by enum

using SFSharp;

using IDisposable sub = SF.Rpc.Subscribe(ERpcId.ShowDialog, args =>
{
    BitStreamReader reader = args.CreateReader();
    ushort dialogId = reader.ReadUInt16();
    DialogStyle style = (DialogStyle)reader.ReadUInt8();
    string title = reader.ReadStringUInt8Length();

    SF.Chat.Add($"dialog={dialogId} title={title}");
});

Subscribe with a parser function

using IDisposable sub = SF.Rpc.Bind(
    ERpcId.ShowDialog,
    SampRpc.ParseShowDialog,
    (rpc, raw) =>
    {
        SF.Chat.Add($"dialog={rpc.DialogId} title={rpc.Title}");
    },
    name: "Wiki.ShowDialog");

Stream incoming RPCs

await foreach (IncomingRpcPayload payload in SF.Rpc.Stream(ERpcId.ClientMessage, cancellationToken))
{
    ClientMessageRpc rpc = payload.Parse(SampRpc.ParseClientMessage);
    SF.Chat.Add(rpc.Text);
}

Raw packet API

Subscribe to a standard packet

using IDisposable sub = SF.Packets.SubscribeIncoming(EPacketId.PlayerSync, args =>
{
    BitStreamReader reader = args.CreateReader();
    byte packetId = reader.ReadUInt8();
    byte leftRight = reader.ReadUInt8();

    SF.Chat.Add($"player sync packet={packetId} lr={leftRight}");
});

Subscribe to an outgoing packet

using IDisposable sub = SF.Packets.SubscribeOutgoing(EPacketId.StatsSync, args =>
{
    BitStreamReader reader = args.CreateReader();
    byte packetId = reader.ReadUInt8();
    SF.Chat.Add($"outgoing packet={packetId}");
});

Arizona 220 and 221

Arizona has two custom transport channels:

  • EPacketId.ArizonaCef (220) with u8 subId
  • EPacketId.ArizonaCefEx (221) with u16 subId

Read subId manually

using IDisposable sub = SF.Packets.SubscribeIncoming(EPacketId.ArizonaCef, args =>
{
    BitStreamReader reader = args.CreateReader();
    reader.SkipBytes(1);
    int subId = ArizonaPacket.ReadSubId220(ref reader);

    SF.Chat.Add($"220:{subId}");
});

Use SF.Arizona

using IDisposable sub220 = SF.Arizona.SubscribeIncoming(EArizona.TextDrawPositionUpdate, args =>
{
    BitStreamReader reader = args.CreateReader();
    ushort textdrawId = reader.ReadUInt16();
    float x = reader.ReadFloat();
    float y = reader.ReadFloat();

    SF.Chat.Add($"220:{args.SubId} td={textdrawId} pos=({x}, {y})");
});

Enum or custom parser

Enum-only hook

using IDisposable sub = SF.Rpc.Subscribe(ERpcId.ServerJoin, args =>
{
    SF.Chat.Add($"ServerJoin bits={args.DataBitLength}");
});

Custom parser

static (ushort Id, string Text) ParseMyDialog(IncomingRpcArgs args)
{
    BitStreamReader reader = args.CreateReader();
    ushort id = reader.ReadUInt16();
    reader.ReadUInt8();
    string title = reader.ReadStringUInt8Length();
    reader.ReadStringUInt8Length();
    reader.ReadStringUInt8Length();
    string text = reader.ReadStringUInt16Length();
    return (id, title + " | " + text);
}

using IDisposable sub = SF.Rpc.Bind(
    ERpcId.ShowDialog,
    ParseMyDialog,
    (rpc, _) => SF.Chat.Add($"dialog={rpc.Id} {rpc.Text}"),
    name: "Wiki.CustomDialog");

Why use the raw API

Pros:

  • full control over the bitstream
  • works for unknown packets
  • easy to inspect payload size and offsets
  • good for reverse engineering

Cons:

  • more boilerplate
  • you maintain the parser yourself
  • easier to make a parsing mistake

Clone this wiki locally