Erlang 二进制数据包
我对 Erlang 很陌生,我正在转换一些 PHP 内容,但我无法弄清楚这一点。这是 PHP 中的函数:
public function raw_send($string1, $string2 = NULL, $type = SERVERDATA_EXECCOMMAND) {
$data = pack('VV', $this->get_request_id(), $type) . $string1 . chr(0) . $string2 . chr(0); // build data
$packet = pack('V', strlen($data)) . $data;
fwrite($this->fp, $packet, strlen($packet));
}
这是我的尝试:
raw_send(Sock, String1, String2, Type) ->
RequestId = random:uniform(10),
PacketData = list_to_binary([<<RequestId, Type>>, String1, 0, String2, 0]),
DataLength = byte_size(PacketData),
Packet = list_to_binary([<<DataLength>>, PacketData]),
ok = gen_tcp:send(Sock, Packet).
我尝试使用 crc32 来比较事物,php 中的 pack("VV", 1, 3) 应该 = <<1/unsigned-little, 3/unsigned-小>>,不是吗?
另外,我想要做的事情的规范: http://developer.valvesoftware.com/wiki/ Source_RCON_Protocol
停下来!
谢谢
I'm very new to Erlang, and I am converting some of my PHP stuff, and I can't figure this one out. Here is the function in PHP:
public function raw_send($string1, $string2 = NULL, $type = SERVERDATA_EXECCOMMAND) {
$data = pack('VV', $this->get_request_id(), $type) . $string1 . chr(0) . $string2 . chr(0); // build data
$packet = pack('V', strlen($data)) . $data;
fwrite($this->fp, $packet, strlen($packet));
}
This is my attempt:
raw_send(Sock, String1, String2, Type) ->
RequestId = random:uniform(10),
PacketData = list_to_binary([<<RequestId, Type>>, String1, 0, String2, 0]),
DataLength = byte_size(PacketData),
Packet = list_to_binary([<<DataLength>>, PacketData]),
ok = gen_tcp:send(Sock, Packet).
I've tried using crc32 to compare things, pack("VV", 1, 3) in php should = <<1/unsigned-little, 3/unsigned-little>>, no?
Also, specs of what I'm trying to do: http://developer.valvesoftware.com/wiki/Source_RCON_Protocol
Halp!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
明白了,没有使用 32 位整数! (感谢 ndim @ freenode)
希望对某人有所帮助!
Got it, wasn't using 32 bit integers! (Thanks to ndim @ freenode)
Hope that helps someone!