// ===== epmd_port:epmd 端口发现注入位(纯核零依赖守恒)=====
// EpmdPort trait(TCP 4369 传输口注入——真实现住 adapter 位,epmd_adapter/);
// port2 组解真形自持(moonbit-epmd 同源铁证——epmd_srv.c &buf[1]/873-897;
// 双实现差分对拍=异构 oracle,双实现安放律"第二实现"裁形)。
// 真网门形:epmd -names 外证——表名=短名(srv.c symname 对拍)。
///|
/// epmd 传输口(TCP 4369 注入位——send=请求帧(含 u16 len 头);recv_all=应答体)
pub(open) trait EpmdPort {
async fn send(self : Self, frame : Bytes) -> Unit raise DistError
async fn recv_all(self : Self) -> Bytes raise DistError // Err 面 raise 点在实现(FIN=正常止)
}
///|
/// PORT2 请求组帧(端口发现——连接远程节点前查分发入口;epmd.h:27 'z'):
/// u16 len BE | 'z' | name(name 到包尾,无独立 nlen——srv.c &buf[1] 铁证;
/// 查询名=epmd -names 表名(短名——外证真形)
pub fn epmd_port2_req(name : String) -> Bytes {
let name_b = latin_bytes(name)
let body_len = 1 + name_b.length()
let out : Array[Byte] = []
out.push(((body_len >> 8) & 255).to_byte())
out.push((body_len & 255).to_byte())
out.push(122) // 'z' PORT2_REQ
for b in name_b {
let _ = out.push(b)
}
Bytes::from_array(out)
}
///|
/// 分发口信息(端口发现结果)
pub(all) struct EpmdPortInfo {
port : Int32 // 分发端口(beam-dist 连接入口)
}
///|
/// PORT2 应答解(裸写形——epmd_srv.c:1516 reply() 无 len 头;ok 形=
/// 'w'|result|port u16|nodetype|protocol|highvsn|lowvsn|nlen|name;
/// 解到 port 即用;result≠0 = NotFound(节点未注册——epmd -names 对拍))
pub fn epmd_port2_resp(body : Bytes) -> Int32 raise DistError {
if body.length() < 2 {
raise DistError::Truncated("port2 resp <2B")
}
if body[0].to_int() != 119 {
// 'w'(119)——epmd_srv.c:873 PORT2_RESP 首字节
raise DistError::Malformed("port2 首字节 \{body[0].to_int()} 非 'w'")
}
let result = body[1].to_int()
if result != 0 {
raise DistError::Malformed(
"节点未注册(port2 result=\{result}——epmd -names 对拍)",
)
}
let need = 2 + 2 + 1 + 1 + 2 + 2 + 2
if body.length() < need {
raise DistError::Truncated("port2 ok 形 <\{need}B")
}
(body[2].to_int() << 8) | body[3].to_int()
}
///|
/// 端口发现封装(连接远程节点前入口——beam-dist 复用位):
/// req 组帧→send→recv_all(裸形全收——PORT2_RESP 无 len 头)→resp 解
/// 查询名=epmd 表名(短名——外证 epmd -names 对拍形)
pub async fn dist_port_of(
name : String,
epmd : &EpmdPort,
) -> Int32 raise DistError {
epmd.send(epmd_port2_req(name))
epmd_port2_resp(epmd.recv_all())
}
///|
/// ALIVE2 请求组帧(节点注册——真客户端形:creation 由 epmd 分配,编造野值
/// = 对端 dist 处理 error→shutdown,坑账条 8 铁律;moonactor epmd.mbt 底本
/// 移植,终形=epmd -d 真帧对拍):len | 0x78 | port(BE) | 0x4d | proto=0 |
/// hver=6(BE) | lver=5(BE) | nlen | name | creation=0(BE 请求位,分配值回 resp)
pub fn epmd_alive2_req(port : Int32, name : String) -> Bytes {
let name_b = latin_bytes(name)
let nlen = name_b.length()
let body_len = 1 + 2 + 1 + 1 + 2 + 2 + 2 + nlen + 2
let out : Array[Byte] = []
out.push(((body_len >> 8) & 255).to_byte())
out.push((body_len & 255).to_byte())
out.push(0x78) // cmd = ALIVE2_REQ = 120('x')
out.push(((port >> 8) & 255).to_byte())
out.push((port & 255).to_byte())
out.push(0x4d) // type = 77(dist protocol 版)
out.push(0x00) // proto = 0
out.push(0x00) // high version = 6(BE)
out.push(0x06)
out.push(0x00) // low version = 5(BE)
out.push(0x05)
out.push(((nlen >> 8) & 255).to_byte())
out.push((nlen & 255).to_byte())
for b in name_b {
let _ = out.push(b)
}
out.push(0) // creation 请求位 = 0(BE)
out.push(0)
Bytes::from_array(out)
}
///|
/// ALIVE2 应答解(4 字节裸,无 len 头——epmd_srv.c:824 真源):
/// 'v'(0x76) | result | creation u16 BE——result≠0 拒(口占/名撞)
pub fn epmd_alive2_resp(body : Bytes) -> Int32 raise DistError {
if body.length() < 4 {
raise DistError::Truncated("alive2 resp <4B")
}
if body[0].to_int() != 118 {
raise DistError::Malformed("alive2 首字节 \{body[0].to_int()} 非 'v'")
}
let result = body[1].to_int()
if result != 0 {
raise DistError::Malformed(
"alive2 拒(result=\{result}——口占/名撞)",
)
}
(body[2].to_int() << 8) | body[3].to_int()
}
///|
// latin_bytes 复用 handshake.mbt(同包互见——零双声明)