MultiverseSocial.com

Calling Channels from any language

The Channels shared-memory link (JavaShm, version 3) as a language-neutral spec. Any language that can map a file and read big-endian integers can join a running Channels process, send it commands and read its replies. It is the same link the Java side (CChannel) uses today, so this page describes the working code in channels7.

Draft · Sources: defines.h (JSHM_*, JCMD_*, CD_OFF_*), channels.h (struct JavaShmHeader, struct JavaShmSlot), JavaChannel.c (JAVASHM CORE, CODEC, HOOK), JavaShmLink.java, JavaShmCommands.java. Example client: channels_client.py.

1 · Roles

Owner
JavaChannel inside channels.exe. It creates the segment, publishes the header, keeps its own heartbeat, runs every command that comes up and answers down.
Client
Any other process: Java (CChannel), Python, Rust, Go, C#, Node and so on. It claims one slot, sends requests up and reads replies and commands down.
Directions
up (DIR_UP = 0) is client → owner; dn (DIR_DN = 1) is owner → client. Each direction of each slot holds one payload, newest wins.

2 · The file

The segment is a file-backed mapping that both sides map read-write and shared:

<dir>/JavaChannel_SBI.shm      8,256 bytes = 64 (header) + 16 × 512 (slots)
dir = $JAVACHANNEL_DIR, else <the channels.exe folder>/javachannel   (Java: -Djavachannel.dir, else JAVACHANNEL_DIR)

Byte order: big-endian for every field (network order; Tony's ruling of 2026-11-21: objects are stored big-endian so each one goes out in one write). That covers the header, the slot state, heartbeat, sequence and length words, the ChannelData records, every command and ACK, and the ODL objects. The endian mark 0x01020304 is stored as the bytes 01 02 03 04, so it reads back exactly when your reader is big-endian. Version 2 was little-endian; a version 2 reader sees version 0x03000000 and the mark 0x04030201 and stays out. The file stays on disk between runs; the owner re-initialises it on start.

4 · Slot (512 bytes each, slot i at 64 + i × 512)

OffsetSizeFieldMeaning
08state(seconds << 32) | pid of the holder; 0 = free
88hb_msthe holder’s last heartbeat in milliseconds
164seq_upup sequence (odd while writing, even when stable)
204len_upup payload length, 0–192
244seq_dndn sequence
284len_dndn payload length, 0–192
3280recthe ChannelData record for this slot (§5)
11216reservedzero
128192upclient → owner payload
320192dnowner → client payload

5 · ChannelData record (80 bytes at slot + 32)

The same fixed-size record every channel uses, here describing the slot’s up stream. The up writer keeps it current inside the up seqlock.

OffsetSizeFieldSet by the client
01type0
11ownerthe slot number, at claim
21ref_kind0
31content_dir0
44sched0
88count0+1 on every up write
168count10
248count20
328byteslength of the latest up payload
408offset128, at claim
488ref_id0
568created_atclaim time, ms
648last_accesstime of the latest up write, ms
728flags0

6 · Handshake

  1. Owner publishes. It stores magic = 0, zeroes all 16 slots, writes every header field, then stores the magic 0x4A435348 with release ordering. A reader that sees the magic sees the whole header.
  2. Client opens. Map the file (8,256 bytes). Accept it when magic, version 3, endian mark, slot_count 16 and slot_size 512 all match, and the owner heartbeat is fresh (the Java client allows 10,000 ms).
  3. Client claims a slot. Scan slots 0–15. A slot is free when its state is 0, or stale when now_sec − (state >> 32) > 3. Compare-and-swap the state from the value you read to (now_sec << 32) | pid. The first success is your slot.
  4. Client resets its record. Under the up seqlock (§7): set len_up = 0, zero the 80-byte record, then owner = slot, offset = 128, created_at = now. Write hb_ms = now.
  5. Client records seq_dn. Remember the dn sequence at claim time; payloads with that sequence belong to an earlier holder and are skipped.
  6. Owner greets. On its next tick the owner sees a new pid in the state word and sends HELLO down: proto 0x0201, caps, its pid, name "JavaChannel". The client ACKs it (§8).
  7. Client introduces itself. Send HELLO up: proto 0x0201, caps, pid, a name of up to 127 bytes. The owner ACKs with rc 0 when the major protocol byte (0x02) matches, and with E_VERSION (−7) for another major.
  8. Heartbeat. Once a second, compare-and-swap the state word to the new (now_sec << 32) | pid and write hb_ms. When the low 32 bits hold another pid, the slot has been reclaimed: claim again.
  9. Leave. Compare-and-swap the state from your value to 0. The owner also reclaims slots whose heartbeat is stale or whose pid has exited, every 500 ms.

7 · The torn-frame check (seqlock)

Each direction carries a sequence word that does the same job as the seq/trailer pair on the DMA frames: a reader accepts a payload only when the sequence is even and reads the same before and after the copy. One writer per direction, so writers never contend.

Write (one writer)

q = seq
seq = q + 1            // odd: writing
release fence
copy payload into the buffer
len = n
(up only) rec.bytes = n; rec.count0 += 1; rec.last_access = now
seq = q + 2            // even: stable, release store

Read (any time)

repeat up to 10,000 times:
  a = seq  (acquire)
  if a is odd: try again
  n = min(len, 192, your buffer)
  copy n bytes
  acquire fence
  if seq == a: return the n bytes
return -2   // the writer is mid-write: read on the next poll

A reader learns of new data when the sequence changes to a new even value; the Java poller checks seq_dn every 5 ms and reads only on a change.

8 · Request and response frames

A payload (up to 192 bytes) holds one or more commands back to back. Each command is:

BytesFieldMeaning
0–1op (u16)direction in the high byte, verb in the low byte
2–3arglen (u16)bytes of arguments that follow
4…argsin signature order: u / i = 4-byte unsigned / signed; s = u16 length + that many UTF-8 bytes (up to 127)

9 · Verbs (low byte of op)

#VerbArgs#VerbArgs
0NOP–15BUFFER_MAPuuus
1ACKuiu (+seq)16INPUT_EVENTuuu
2OPEN_HYPERWINDOWuiiiis17RESIZE_EVENTuii
3GOB_CREATEuuiiiiis18FRAME_SYNCuuu
4GOB_MOVEuii19PINGuu
5GOB_SHOWui20NACKuiu (+seq)
6GOB_EXPUNGEu21SHUTDOWNu
7SAYuus22WIN_MOVEuii
8HELLOuuus23WIN_SIZEuii
9CLOSE_HYPERWINDOWu24WIN_DRAWuiiiiu
10GOB_RESIZEuii25WIN_EVENTSuu
11GOB_SET_TEXTUREuus26OBJ_SYNCuuuu
12GOB_SET_SHADERuuu27VIEWFRAMEuuus
13SHADER_LOADuus28VIEWFRAME_DONEuii
14GPU_DISPATCHuuuuu29ABORT_REMOTEu

Good first verbs from a new language: HELLO, PING (nonce, ms; the ACK (op, 0, nonce) is the pong) and SAY (speaker, line, text; the owner ACKs it and prints the line).

10 · No-wait rules

11 · Limits (fixed, nothing grows)

ItemLimitSource
Segment size8,256 bytes, fixed at creationJSHM_TOTAL_SIZE
Clients at once16 slotsJSHM_SLOTS
Payload per direction192 bytes, one payloadJSHM_PAYLOAD_CAP
Arguments per command8JCMD_ARGS_MAX
String per command127 bytes (longer strings are cut to 127)JCMD_STR_MAX
Verbs30 (0–29)JCMD_V_COUNT
Owner dn queue per slot4 payloadsJSH_PEND_Q
Read retries10,000, then −2ReadJavaShm
Slot staleness3 sJSHM_STALE_SEC
Mapped buffers (BUFFER_MAP)255 buffers, sized in 4,096-byte pages, named JavaChannel_SBI_buf<id>JSHM_BUF_MAX, JSHM_BUF_PAGE

Every buffer, queue and table here has its size fixed when it is created, so memory use stays flat however long the link runs.

12 · Python client

channels_client.py (view as text; standard library only) follows this page: it opens and checks the header, claims a slot, keeps a 1-second heartbeat thread, writes up and reads down with the seqlock, encodes and decodes every verb, ACKs owner commands with the dn sequence, and leaves cleanly. For the claim it uses a true 64-bit compare-and-swap when the atomics package is installed, and a write-then-verify step for a single Python client otherwise.

python channels_client.py --dir "C:\path\to\channels7\javachannel" --say "Hello from Python"

link = ChannelsLink(dir).open()      # check magic, version, endian, slots, owner heartbeat
link.claim()                         # CAS a free or stale slot, reset its record
link.send("HELLO", (PROTO, 0, link.pid), "python")
link.send("PING", (1, now_ms))
replies = link.poll()                # no wait: new dn payload -> ACKs out, replies back
link.release()

The same steps port directly to other languages: a memory-mapped file, big-endian loads and stores, and one atomic compare-and-swap for the slot state word.

Hits--