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.
3 · Header (64 bytes at offset 0)
| Offset | Size | Field | Value |
|---|---|---|---|
| 0 | 4 | magic | 0x4A435348 (the bytes 4A 43 53 48, "JCSH"), written last by the owner (release store) |
| 4 | 4 | version | 3 (bytes 00 00 00 03) |
| 8 | 4 | slot_count | 16 |
| 12 | 4 | slot_size | 512 |
| 16 | 4 | header_size | 64 |
| 20 | 4 | payload_off | 128 (the up buffer inside a slot) |
| 24 | 4 | owner_pid | the owner’s process id |
| 28 | 4 | endian | 0x01020304 (bytes 01 02 03 04) |
| 32 | 8 | owner_hb_ms | the owner’s heartbeat, wall-clock milliseconds since 1970 |
| 40 | 4 | stale_sec | 3 |
| 44 | 4 | payload_cap | 192 |
| 48 | 16 | reserved | zero |
4 · Slot (512 bytes each, slot i at 64 + i × 512)
| Offset | Size | Field | Meaning |
|---|---|---|---|
| 0 | 8 | state | (seconds << 32) | pid of the holder; 0 = free |
| 8 | 8 | hb_ms | the holder’s last heartbeat in milliseconds |
| 16 | 4 | seq_up | up sequence (odd while writing, even when stable) |
| 20 | 4 | len_up | up payload length, 0–192 |
| 24 | 4 | seq_dn | dn sequence |
| 28 | 4 | len_dn | dn payload length, 0–192 |
| 32 | 80 | rec | the ChannelData record for this slot (§5) |
| 112 | 16 | reserved | zero |
| 128 | 192 | up | client → owner payload |
| 320 | 192 | dn | owner → 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.
| Offset | Size | Field | Set by the client |
|---|---|---|---|
| 0 | 1 | type | 0 |
| 1 | 1 | owner | the slot number, at claim |
| 2 | 1 | ref_kind | 0 |
| 3 | 1 | content_dir | 0 |
| 4 | 4 | sched | 0 |
| 8 | 8 | count0 | +1 on every up write |
| 16 | 8 | count1 | 0 |
| 24 | 8 | count2 | 0 |
| 32 | 8 | bytes | length of the latest up payload |
| 40 | 8 | offset | 128, at claim |
| 48 | 8 | ref_id | 0 |
| 56 | 8 | created_at | claim time, ms |
| 64 | 8 | last_access | time of the latest up write, ms |
| 72 | 8 | flags | 0 |
6 · Handshake
- Owner publishes. It stores magic = 0, zeroes all 16 slots, writes every header field, then stores the magic
0x4A435348with release ordering. A reader that sees the magic sees the whole header. - 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).
- 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. - 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.
- Client records seq_dn. Remember the dn sequence at claim time; payloads with that sequence belong to an earlier holder and are skipped.
- Owner greets. On its next tick the owner sees a new pid in the state word and sends
HELLOdown: proto0x0201, caps, its pid, name"JavaChannel". The client ACKs it (§8). - Client introduces itself. Send
HELLOup: proto0x0201, caps, pid, a name of up to 127 bytes. The owner ACKs with rc 0 when the major protocol byte (0x02) matches, and withE_VERSION(−7) for another major. - Heartbeat. Once a second, compare-and-swap the state word to the new
(now_sec << 32) | pidand write hb_ms. When the low 32 bits hold another pid, the slot has been reclaimed: claim again. - 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:
| Bytes | Field | Meaning |
|---|---|---|
| 0–1 | op (u16) | direction in the high byte, verb in the low byte |
| 2–3 | arglen (u16) | bytes of arguments that follow |
| 4… | args | in signature order: u / i = 4-byte unsigned / signed; s = u16 length + that many UTF-8 bytes (up to 127) |
- Direction:
0x0200TO_C for client requests,0x0100TO_JAVA for owner commands to the client,0x0000ANY for NOP, ACK and NACK (and only those). - Request: one up write of TO_C commands, for example
PING=02 13 00 08 | nonce | ms(op0x0213, arglen 8, each big-endian). - Response: the owner runs every command in the up payload and answers all of them in one dn write:
ACK(op, rc, arg0) for success or a known result,NACK(op, rc, arg0) for a command it declines. Each ACK/NACK carries a fourth u32, the sequence of the payload it answers. - Commands down: when the owner sends TO_JAVA commands (HELLO first), the client runs them and answers with one up payload of ACKs (op, rc, arg0, seq_dn). The owner keeps that dn payload in place until the ACK arrives and queues anything newer behind it.
- Padding: a NOP with arglen 0 is four bytes of padding and is skipped.
- Forward growth: a reader takes the arguments its signature lists and skips any extra bytes inside arglen, so a newer peer can append fields.
- Result codes: 0 ok, −1 E_BADLEN, −2 E_BADOP, −3 E_NOSPACE, −4 E_UNBOUND, −5 E_NOTFOUND, −6 E_FAILED, −7 E_VERSION, −8 E_NOGPU, −9 E_BUSY.
9 · Verbs (low byte of op)
| # | Verb | Args | # | Verb | Args |
|---|---|---|---|---|---|
| 0 | NOP | – | 15 | BUFFER_MAP | uuus |
| 1 | ACK | uiu (+seq) | 16 | INPUT_EVENT | uuu |
| 2 | OPEN_HYPERWINDOW | uiiiis | 17 | RESIZE_EVENT | uii |
| 3 | GOB_CREATE | uuiiiiis | 18 | FRAME_SYNC | uuu |
| 4 | GOB_MOVE | uii | 19 | PING | uu |
| 5 | GOB_SHOW | ui | 20 | NACK | uiu (+seq) |
| 6 | GOB_EXPUNGE | u | 21 | SHUTDOWN | u |
| 7 | SAY | uus | 22 | WIN_MOVE | uii |
| 8 | HELLO | uuus | 23 | WIN_SIZE | uii |
| 9 | CLOSE_HYPERWINDOW | u | 24 | WIN_DRAW | uiiiiu |
| 10 | GOB_RESIZE | uii | 25 | WIN_EVENTS | uu |
| 11 | GOB_SET_TEXTURE | uus | 26 | OBJ_SYNC | uuuu |
| 12 | GOB_SET_SHADER | uuu | 27 | VIEWFRAME | uuus |
| 13 | SHADER_LOAD | uus | 28 | VIEWFRAME_DONE | uii |
| 14 | GPU_DISPATCH | uuuuu | 29 | ABORT_REMOTE | u |
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
- Lock-free. The link uses only atomic loads, stores and compare-and-swap on the mapped bytes. There are no mutexes, semaphores or OS events, so a slow or exited peer never holds the other side.
- Big-endian atomics. The sequence and state words are big-endian like everything else. An aligned 4- or 8-byte atomic moves the word as one unit in any byte order, so load or store the raw word atomically and swap it in a register (C:
__atomic_load_nthen__builtin_bswap32). For compare-and-swap, swap both the expected and the new value first, so the CAS compares the exact bytes on the wire. Java's big-endianbyteBufferViewVarHandledoes this for you. - Bounded reads. A read retries at most 10,000 times and then returns −2 (busy); the caller simply reads again on its next poll.
- Writes always complete. A writer owns its direction, so a write is a fixed number of steps.
- Polling. Check the sequence word on your own timer (the Java client uses 5 ms) and read only when it changes to a new even value.
- Pacing up. One payload per direction, newest wins: leave a gap between up writes so the owner reads each one (the Java client uses 25 ms,
cchannel.gap), and put several commands in one payload when they go together. Inchannels.exe livethe JavaChannel thread wakes every 250 ms, so a client there should leave more than 250 ms between up writes; the step 46 round-trip test used 600 ms. - Owner flow control. The owner holds each command payload down until its ACK arrives, queues newer ones (up to 4) and answers
E_BUSYwhen the queue is full, so callers learn immediately and choose when to retry. - Liveness by timestamps. Heartbeats are numbers in the segment: the client refreshes its state word every second, the owner refreshes
owner_hb_ms, and either side judges the other by age.
11 · Limits (fixed, nothing grows)
| Item | Limit | Source |
|---|---|---|
| Segment size | 8,256 bytes, fixed at creation | JSHM_TOTAL_SIZE |
| Clients at once | 16 slots | JSHM_SLOTS |
| Payload per direction | 192 bytes, one payload | JSHM_PAYLOAD_CAP |
| Arguments per command | 8 | JCMD_ARGS_MAX |
| String per command | 127 bytes (longer strings are cut to 127) | JCMD_STR_MAX |
| Verbs | 30 (0–29) | JCMD_V_COUNT |
| Owner dn queue per slot | 4 payloads | JSH_PEND_Q |
| Read retries | 10,000, then −2 | ReadJavaShm |
| Slot staleness | 3 s | JSHM_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.