2010-01-25 140 views
2

我對Erlang非常陌生,我正在轉換一些PHP的東西,我無法弄清楚這一點。這裏是PHP函數:Erlang二進制包

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比較的東西試過,包( 「VV」,1,3)在PHP應該= < < 1/unsigned-little,3/unsigned-little >>,no?

而且,我想要做的規格:http://developer.valvesoftware.com/wiki/Source_RCON_Protocol

HALP!

謝謝

+0

你的嘗試有什麼問題? – Zed 2010-01-25 11:22:39

回答

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). 

希望能幫助別人!