views:

99

answers:

1

Hey,

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

+1  A: 

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!

Twelve-60