tools.anubis 4.1 KB
/*
 * 
 * User: David RENE
 * Date: 28/12/2006
 * Time: 01:35
 * (c) Otherwise Technology
 *
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
 
read system/types.anubis
read system/string.anubis

public type Byte_Net_Result:
  failure,
  time_out,
  success(Int8).

  
public define Byte_Net_Result
  read_network_byte
  (
    RAddr(Int8) conn,
    Int32        t_out
  )=
  if read(conn, 1, t_out) is
  {
    failure        then failure,
    success(byte)  then
    if nth(0, byte) is 
    {
      failure     then time_out, 
      success(c)  then success(c)
    }
  }.
  
  
public type Bytes_Net_Result:
  failure,
  time_out,
  success(ByteArray),
  truncated(ByteArray).

public define Bytes_Net_Result
  read_network_bytes
  (
    RAddr(Int8)  conn,
    Int32         how_many,
    Int32         t_out
  )=
  if read(conn, how_many, t_out) is
  {
    failure        then failure,
    success(bytes) then
      with len = length(bytes),
      //if the returned length size is 0, it means timeout
      if len = 0              then
        time_out
      else if len = how_many  then
        success(bytes)
      else
        truncated(bytes)
  }.

public define One
   put_Int32_to_network
     (
       ByteArray  dest, 
       Int32      where, 
       Int32      what
     ) =
   forget(put(dest,where  ,truncate_to_int8(what>>24) )); 
   forget(put(dest,where+1,truncate_to_int8((what>>16) & 0xFF)));
   forget(put(dest,where+2,truncate_to_int8((what>>8) & 0xFF))); 
   forget(put(dest,where+3,truncate_to_int8(what & 0xFF))).

public define Int32
  get_Int32_from_network
  (
    ByteArray  src,
    Int32      where
  ) =
  with  b0 = int8_to_int32(force_nth(where + 3, src)),
        b1 = int8_to_int32(force_nth(where + 2, src)),
        b2 = int8_to_int32(force_nth(where + 1, src)),
        b3 = int8_to_int32(force_nth(where    , src)),
   b0 | (b1 << 8) | (b2 << 16) | (b3 << 24).




   
public define Int64
  host_to_lendian_Int64
  (
    ByteArray  src,
  ) =
  with  b7 = int8_to_int32(force_nth(7, src)),
        b6 = int8_to_int32(force_nth(6, src)),
        b5 = int8_to_int32(force_nth(5, src)),
        b4 = int8_to_int32(force_nth(4, src)),
        
        b3 = int8_to_int32(force_nth(3, src)),
        b2 = int8_to_int32(force_nth(2, src)),
        b1 = int8_to_int32(force_nth(1, src)),
        b0 = int8_to_int32(force_nth(0, src)),
   int64( b0 | (b1 << 8) | (b2 << 16) | (b3 << 24),
          b4 | (b5 << 8) | (b6 << 16) | (b7 << 24)).
   
public define Int16
  lendian_to_host_Int16
  (
    ByteArray  src,
  ) =
  int16(force_nth(1, src), force_nth(0, src)).
  
public define Int32
  lendian_to_host_Int32
  (
    ByteArray  src,
  ) =
  with  b3 = int8_to_int32(force_nth(3, src)),
        b2 = int8_to_int32(force_nth(2, src)),
        b1 = int8_to_int32(force_nth(1, src)),
        b0 = int8_to_int32(force_nth(0, src)),
   b0 | (b1 << 8) | (b2 << 16) | (b3 << 24).



public define Int64
  lendian_to_host_Int64
  (
    ByteArray  src,
  ) =
  with  b7 = int8_to_int32(force_nth(7, src)),
        b6 = int8_to_int32(force_nth(6, src)),
        b5 = int8_to_int32(force_nth(5, src)),
        b4 = int8_to_int32(force_nth(4, src)),
        
        b3 = int8_to_int32(force_nth(3, src)),
        b2 = int8_to_int32(force_nth(2, src)),
        b1 = int8_to_int32(force_nth(1, src)),
        b0 = int8_to_int32(force_nth(0, src)),
   int64( b4 | (b5 << 8) | (b6 << 16) | (b7 << 24),   //high
          b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)).  //low

public define Maybe(Float)
  lendian_to_host_Float64
  (
    ByteArray  src,
  ) =
/*  print("from the network 0x" + to_hexa(src) + "\n");
  with new_val = constant_byte_array(8,0),
  
  forget(put(new_val, 0, (force_nth(7, src))));
  forget(put(new_val, 1, (force_nth(6, src))));
  forget(put(new_val, 2, (force_nth(5, src))));
  forget(put(new_val, 3, (force_nth(4, src))));
  forget(put(new_val, 4, (force_nth(3, src))));
  forget(put(new_val, 5, (force_nth(2, src))));
  forget(put(new_val, 6, (force_nth(1, src))));
  forget(put(new_val, 7, (force_nth(0, src))));
  print("After conversion 0x" + to_hexa(new_val) + "\n");
*/
  (Maybe(Float)) unserialize(src).