Erlang二进制包

时间:2010-01-25 09:41:14

标签: php erlang binary packet

我是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中包装(“VV”,1,3)应该=&lt;&lt; 1 / unsigned-little,3 / unsigned-little&gt;&gt;,不是吗?

此外,我正在尝试做的规范:http://developer.valvesoftware.com/wiki/Source_RCON_Protocol

HALP!

由于

1 个答案:

答案 0 :(得分:1)

知道了,没有使用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).

希望能帮助别人!