Erlang 二进制数据包

发布于 2024-08-19 13:36:53 字数 1048 浏览 6 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

治碍 2024-08-26 13:36:53

明白了,没有使用 32 位整数! (感谢 ndim @ freenode)

raw_send(Sock, String1, String2, Type) ->
    RequestId = random:uniform(10),
    String1Bin = list_to_binary(String1),
    String2Bin = list_to_binary(String2),
    PacketData = <<RequestId:32/little, Type:32/little, String1Bin/binary, 0, String2Bin/binary, 0>>,
    DataLength = byte_size(PacketData),
    Packet = <<DataLength:32/little, PacketData/binary>>,
    ok = gen_tcp:send(Sock, Packet).

希望对某人有所帮助!

Got it, wasn't using 32 bit integers! (Thanks to ndim @ freenode)

raw_send(Sock, String1, String2, Type) ->
    RequestId = random:uniform(10),
    String1Bin = list_to_binary(String1),
    String2Bin = list_to_binary(String2),
    PacketData = <<RequestId:32/little, Type:32/little, String1Bin/binary, 0, String2Bin/binary, 0>>,
    DataLength = byte_size(PacketData),
    Packet = <<DataLength:32/little, PacketData/binary>>,
    ok = gen_tcp:send(Sock, Packet).

Hope that helps someone!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文