C и Erlang: пример порта Erlang

Disclaimer: The author of the question has an average knowledge of Erlang and a basic knowledge of C.

Я читаюРуководство по взаимодействию сейчас. Я успешно скомпилировалcomplex.c пример, и он работает с портом Erlang без проблем.

Тем не менее, я хотел бы понять, как работает фактический C-код. Я понимаю это в целом: в примере он читает 2 байта из стандартного ввода и проверяет первый байт. В зависимости от первого байта он вызываетfoo или жеbar функция. Это предел моего понимания этого сейчас.

Итак, если мы возьмем обаerl_comm.c:

<code>/* erl_comm.c */

typedef unsigned char byte;

read_cmd(byte *buf)
{
  int len;

  if (read_exact(buf, 2) != 2)
    return(-1);
  len = (buf[0] << 8) | buf[1];
  return read_exact(buf, len);
}

write_cmd(byte *buf, int len)
{
  byte li;

  li = (len >> 8) & 0xff;
  write_exact(&li, 1);

  li = len & 0xff;
  write_exact(&li, 1);

  return write_exact(buf, len);
}

read_exact(byte *buf, int len)
{
  int i, got=0;

  do {
    if ((i = read(0, buf+got, len-got)) <= 0)
      return(i);
    got += i;
  } while (got<len);

  return(len);
}

write_exact(byte *buf, int len)
{
  int i, wrote = 0;

  do {
    if ((i = write(1, buf+wrote, len-wrote)) <= 0)
      return (i);
    wrote += i;
  } while (wrote<len);

  return (len);
}
</code>

а такжеport.c:

<code>/* port.c */

typedef unsigned char byte;

int main() {
  int fn, arg, res;
  byte buf[100];

  while (read_cmd(buf) > 0) {
    fn = buf[0];
    arg = buf[1];

    if (fn == 1) {
      res = foo(arg);
    } else if (fn == 2) {
      res = bar(arg);
    }

    buf[0] = res;
    write_cmd(buf, 1);
  }
}
</code>

What does each function actually do there? What purpose do li, len, i, wrote, got variables actually serve?

Еще несколько небольших вопросов:

Why do not the functions have any return types, even voids? When Erlang port sends data to C, the first byte determines a function to be called. If the byte holds the decimal 1, then foo() is called, if the byte holds the decimal 2, then bar() is called. If not changed anyhow this protocol can be used to call up to 255 different C functions with only 1 parameter each. Is that right? "Adding the length indicator will be done automatically by the Erlang port, but must be done explicitly in the external C program". What does that mean? On which line of code is it done? From the Tutorial: "By default, the C program should read from standard input (file descriptor 0) and write to standard output (file descriptor 1)." Then: "Note that stdin and stdout are for buffered input/output and should not be used for the communication with Erlang!" What is the catch here? why buf is initialized to [100]?

Ответы на вопрос(1)

Ваш ответ на вопрос