Example #1
0
int
msg_attack_get(struct msg *msg, struct kill* *value)
{
  if (msg->attack_set != 1) {
    msg->attack_data = kill_new();
    if (msg->attack_data == NULL)
      return (-1);
    msg->attack_set = 1;
  }
  *value = msg->attack_data;
  return (0);
}
Example #2
0
int
msg_attack_assign(struct msg *msg,
    const struct kill* value)
{
   struct evbuffer *tmp = NULL;
   if (msg->attack_set) {
     kill_clear(msg->attack_data);
     msg->attack_set = 0;
   } else {
     msg->attack_data = kill_new();
     if (msg->attack_data == NULL) {
       event_warn("%s: kill_new()", __func__);
       goto error;
     }
   }
   if ((tmp = evbuffer_new()) == NULL) {
     event_warn("%s: evbuffer_new()", __func__);
     goto error;
   }
   kill_marshal(tmp, value);
   if (kill_unmarshal(msg->attack_data, tmp) == -1) {
     event_warnx("%s: kill_unmarshal", __func__);
     goto error;
   }
   msg->attack_set = 1;
   evbuffer_free(tmp);
   return (0);
 error:
   if (tmp != NULL)
     evbuffer_free(tmp);
   if (msg->attack_data != NULL) {
     kill_free(msg->attack_data);
     msg->attack_data = NULL;
   }
   return (-1);
}
Example #3
0
int
msg_unmarshal(struct msg *tmp,  struct evbuffer *evbuf)
{
  ev_uint32_t tag;
  while (evbuffer_get_length(evbuf) > 0) {
    if (evtag_peek(evbuf, &tag) == -1)
      return (-1);
    switch (tag) {

      case MSG_FROM_NAME:

        if (tmp->from_name_set)
          return (-1);
        if (evtag_unmarshal_string(evbuf, MSG_FROM_NAME, &tmp->from_name_data) == -1) {
          event_warnx("%s: failed to unmarshal from_name", __func__);
          return (-1);
        }
        tmp->from_name_set = 1;
        break;

      case MSG_TO_NAME:

        if (tmp->to_name_set)
          return (-1);
        if (evtag_unmarshal_string(evbuf, MSG_TO_NAME, &tmp->to_name_data) == -1) {
          event_warnx("%s: failed to unmarshal to_name", __func__);
          return (-1);
        }
        tmp->to_name_set = 1;
        break;

      case MSG_ATTACK:

        if (tmp->attack_set)
          return (-1);
        tmp->attack_data = kill_new();
        if (tmp->attack_data == NULL)
          return (-1);
        if (evtag_unmarshal_kill(evbuf, MSG_ATTACK, tmp->attack_data) == -1) {
          event_warnx("%s: failed to unmarshal attack", __func__);
          return (-1);
        }
        tmp->attack_set = 1;
        break;

      case MSG_RUN:

        if (tmp->run_length >= tmp->run_num_allocated &&
            msg_run_expand_to_hold_more(tmp) < 0) {
          puts("HEY NOW");
          return (-1);
        }
        tmp->run_data[tmp->run_length] = run_new();
        if (tmp->run_data[tmp->run_length] == NULL)
          return (-1);
        if (evtag_unmarshal_run(evbuf, MSG_RUN, tmp->run_data[tmp->run_length]) == -1) {
          event_warnx("%s: failed to unmarshal run", __func__);
          return (-1);
        }
        ++tmp->run_length;
        tmp->run_set = 1;
        break;

      default:
        return -1;
    }
  }

  if (msg_complete(tmp) == -1)
    return (-1);
  return (0);
}
int
msg_unmarshal(struct msg *tmp,  struct evbuffer *evbuf)
{
  ev_uint32_t tag;
  while (EVBUFFER_LENGTH(evbuf) > 0) {
    if (evtag_peek(evbuf, &tag) == -1)
      return (-1);
    switch (tag) {

      case MSG_FROM_NAME:

        if (tmp->from_name_set)
          return (-1);
        if (evtag_unmarshal_string(evbuf, MSG_FROM_NAME, &tmp->from_name_data) == -1) {
          event_warnx("%s: failed to unmarshal from_name", __func__);
          return (-1);
        }
        tmp->from_name_set = 1;
        break;

      case MSG_TO_NAME:

        if (tmp->to_name_set)
          return (-1);
        if (evtag_unmarshal_string(evbuf, MSG_TO_NAME, &tmp->to_name_data) == -1) {
          event_warnx("%s: failed to unmarshal to_name", __func__);
          return (-1);
        }
        tmp->to_name_set = 1;
        break;

      case MSG_ATTACK:

        if (tmp->attack_set)
          return (-1);
        tmp->attack_data = kill_new();
        if (tmp->attack_data == NULL)
          return (-1);
        if (evtag_unmarshal_kill(evbuf, MSG_ATTACK, tmp->attack_data) == -1) {
          event_warnx("%s: failed to unmarshal attack", __func__);
          return (-1);
        }
        tmp->attack_set = 1;
        break;

      case MSG_RUN:

        if (msg_run_add(tmp) == NULL)
          return (-1);
        if (evtag_unmarshal_run(evbuf, MSG_RUN,
          tmp->run_data[tmp->run_length - 1]) == -1) {
          --tmp->run_length;
          event_warnx("%s: failed to unmarshal run", __func__);
          return (-1);
        }
        tmp->run_set = 1;
        break;

      default:
        return -1;
    }
  }

  if (msg_complete(tmp) == -1)
    return (-1);
  return (0);
}