static int erl_json_ei_number(void* ctx, const char * val, unsigned int len) { State* pState = (State*) ctx; flog(stderr, "number", 0, val, len); list_header_for_value(pState); switch(numbers_as(pState)) { case EEP0018_PARSE_NUMBERS_AS_NUMBER: { if(memchr(val, '.', len) || memchr(val, 'e', len) || memchr(val, 'E', len)) ei_x_encode_double(&pState->ei_buf, strtod(val, 0)); else ei_x_encode_long(&pState->ei_buf, strtol(val, 0, 10)); break; } case EEP0018_PARSE_NUMBERS_AS_FLOAT: { ei_x_encode_double(&pState->ei_buf, strtod(val, 0)); break; } case EEP0018_PARSE_NUMBERS_AS_TUPLE: { /* While "1e1" is a valid JSON number, it is not a valid parameter to list_to_float/1 We fix that by inserting ".0" before the exponent e. */ const char* exp = memchr(val, 'e', len); if(exp && exp > val) { const char* dot = memchr(val, '.', exp - val); if(!dot) { char* tmp = alloca(len + 5); memcpy(tmp, val, exp - val); memcpy(tmp + (exp - val), ".0", 2); memcpy(tmp + (exp - val) + 2, exp, len - (exp - val)); len += 2; val = tmp; tmp[len] = 0; } } ei_x_encode_tuple_header(&pState->ei_buf, 3); ei_x_encode_atom_len(&pState->ei_buf, "number", 6); ei_x_encode_string_len(&pState->ei_buf, val, len); ei_x_encode_atom_len(&pState->ei_buf, "a", 1); // a dummy break; } } return 1; }
/* Check if integer or float */ static int pdigit(const char** fmt, ei_x_buff* x) { const char* start = *fmt; char c; int dotp=0; double d; long l; if (**fmt == '-' || **fmt == '+') (*fmt)++; for (;;) { c = *(*fmt)++; if (isdigit((int)c)) continue; else if (!dotp && (c == '.')) { dotp = 1; continue; } else break; } --(*fmt); if (dotp) { sscanf(start, "%lf", &d); return ei_x_encode_double(x, d); } else { sscanf(start, "%ld", &l); return ei_x_encode_long(x, l); } }
int ei_x_encode_kf_val(ei_x_buff* values, double f) { EI_X_ENCODE_NULL_OR_INF(f, nf, wf); if(f == nf || q2e_isnan(f)) { EI(ei_x_encode_atom(values, "null")); return 0; } else if(f == wf) { EI(ei_x_encode_atom(values, "infinity")); return 0; } EI(ei_x_encode_double(values, f)); return 0; }
/* * The format letters are: * a - An atom * c - A character * s - A string * i - An integer * l - A long integer * u - An unsigned long integer * f - A float * d - A double float * p - An Erlang PID */ static int pformat(const char** fmt, union arg** args, ei_x_buff* x) { int res = 0; ++(*fmt); /* skip tilde */ switch (*(*fmt)++) { case 'a': res = ei_x_encode_atom(x, (*args)->s); (*args)++; break; case 'c': res = ei_x_encode_char(x, (*args)->c); (*args)++; break; case 's': res = ei_x_encode_string(x, (*args)->s); (*args)++; break; case 'i': res = ei_x_encode_long(x, (*args)->l); (*args)++; break; case 'l': res = ei_x_encode_long(x, (*args)->l); (*args)++; break; case 'u': res = ei_x_encode_ulong(x, (*args)->u); (*args)++; break; case 'f': /* float is expanded to double (C calling conventions) */ case 'd': res = ei_x_encode_double(x, (*args)->d); (*args)++; break; case 'p': res = ei_x_encode_pid(x, (*args)->pid); (*args)++; break; default: res = -1; break; } return res; }
int main(void) #endif { ErlConnect conp; Erl_IpAddr thisipaddr = (Erl_IpAddr)0; FILE *fp = (FILE *)0; char* charp = "foo"; double *doublep = NULL; double doublex = 0.0; ei_cnode xec; ei_reg *ei_regp = NULL; ei_term eterm; ei_x_buff eix; erlang_big *bigp = NULL; erlang_fun efun; erlang_msg *msgp = NULL; erlang_msg emsg; erlang_pid *pidp = NULL; erlang_pid epid; erlang_port eport; erlang_ref eref; erlang_trace etrace; int *intp = NULL; int intx = 0; long *longp = NULL; long longx = 0; short creation = 0; struct ei_reg_stat *ei_reg_statp = NULL; struct ei_reg_tabstat *ei_reg_tabstatp = NULL; struct hostent *hostp = NULL; unsigned char * ucharp = (unsigned char *)"foo"; unsigned long *ulongp = NULL; unsigned long ulongx = 0; void *voidp = NULL; #ifndef VXWORKS EI_LONGLONG *longlongp = (EI_LONGLONG*)NULL; EI_LONGLONG longlongx = 0; EI_ULONGLONG *ulonglongp = (EI_ULONGLONG*)NULL; EI_ULONGLONG ulonglongx = 0; #endif enum erlang_char_encoding enc; intx = erl_errno; ei_connect_init(&xec, charp, charp, creation); ei_connect_xinit (&xec, charp, charp, charp, thisipaddr, charp, creation); ei_connect(&xec, charp); ei_xconnect (&xec, thisipaddr, charp); ei_receive(intx, ucharp, intx); ei_receive_msg(intx, &emsg, &eix); ei_xreceive_msg(intx, &emsg, &eix); ei_send(intx, &epid, charp, intx); ei_reg_send(&xec, intx, charp, charp, intx); ei_rpc(&xec, intx, charp, charp, charp, intx, &eix); ei_rpc_to(&xec, intx, charp, charp, charp, intx); ei_rpc_from(&xec, intx, intx, &emsg, &eix); ei_publish(&xec, intx); ei_accept(&xec, intx, &conp); ei_unpublish(&xec); ei_thisnodename(&xec); ei_thishostname(&xec); ei_thisalivename(&xec); ei_self(&xec); ei_gethostbyname(charp); ei_gethostbyaddr(charp, intx, intx); ei_gethostbyname_r(charp, hostp, charp, intx, intp); ei_gethostbyaddr_r(charp, intx, intx, hostp, charp, intx, intp); ei_encode_version(charp, intp); ei_x_encode_version(&eix); ei_encode_long(charp, intp, longx); ei_x_encode_long(&eix, longx); ei_encode_ulong(charp, intp, ulongx); ei_x_encode_ulong(&eix, ulongx); ei_encode_double(charp, intp, doublex); ei_x_encode_double(&eix, doublex); ei_encode_boolean(charp, intp, intx); ei_x_encode_boolean(&eix, intx); ei_encode_char(charp, intp, 'a'); ei_x_encode_char(&eix, 'a'); ei_encode_string(charp, intp, charp); ei_encode_string_len(charp, intp, charp, intx); ei_x_encode_string(&eix, charp); ei_x_encode_string_len(&eix, charp, intx); ei_encode_atom(charp, intp, charp); ei_encode_atom_as(charp, intp, charp, ERLANG_LATIN1, ERLANG_UTF8); ei_encode_atom_len(charp, intp, charp, intx); ei_encode_atom_len_as(charp, intp, charp, intx, ERLANG_ASCII, ERLANG_LATIN1); ei_x_encode_atom(&eix, charp); ei_x_encode_atom_as(&eix, charp, ERLANG_LATIN1, ERLANG_UTF8); ei_x_encode_atom_len(&eix, charp, intx); ei_x_encode_atom_len_as(&eix, charp, intx, ERLANG_LATIN1, ERLANG_UTF8); ei_encode_binary(charp, intp, (void *)0, longx); ei_x_encode_binary(&eix, (void*)0, intx); ei_encode_pid(charp, intp, &epid); ei_x_encode_pid(&eix, &epid); ei_encode_fun(charp, intp, &efun); ei_x_encode_fun(&eix, &efun); ei_encode_port(charp, intp, &eport); ei_x_encode_port(&eix, &eport); ei_encode_ref(charp, intp, &eref); ei_x_encode_ref(&eix, &eref); ei_encode_trace(charp, intp, &etrace); ei_x_encode_trace(&eix, &etrace); ei_encode_tuple_header(charp, intp, intx); ei_x_encode_tuple_header(&eix, longx); ei_encode_list_header(charp, intp, intx); ei_x_encode_list_header(&eix, longx); /* #define ei_encode_empty_list(buf,i) ei_encode_list_header(buf,i,0) */ ei_x_encode_empty_list(&eix); ei_get_type(charp, intp, intp, intp); ei_get_type_internal(charp, intp, intp, intp); ei_decode_version(charp, intp, intp); ei_decode_long(charp, intp, longp); ei_decode_ulong(charp, intp, ulongp); ei_decode_double(charp, intp, doublep); ei_decode_boolean(charp, intp, intp); ei_decode_char(charp, intp, charp); ei_decode_string(charp, intp, charp); ei_decode_atom(charp, intp, charp); ei_decode_atom_as(charp, intp, charp, MAXATOMLEN_UTF8, ERLANG_WHATEVER, &enc, &enc); ei_decode_binary(charp, intp, (void *)0, longp); ei_decode_fun(charp, intp, &efun); free_fun(&efun); ei_decode_pid(charp, intp, &epid); ei_decode_port(charp, intp, &eport); ei_decode_ref(charp, intp, &eref); ei_decode_trace(charp, intp, &etrace); ei_decode_tuple_header(charp, intp, intp); ei_decode_list_header(charp, intp, intp); ei_decode_ei_term(charp, intp, &eterm); ei_print_term(fp, charp, intp); ei_s_print_term(&charp, charp, intp); ei_x_format(&eix, charp); ei_x_format_wo_ver(&eix, charp); ei_x_new(&eix); ei_x_new_with_version(&eix); ei_x_free(&eix); ei_x_append(&eix, &eix); ei_x_append_buf(&eix, charp, intx); ei_skip_term(charp, intp); ei_reg_open(intx); ei_reg_resize(ei_regp, intx); ei_reg_close(ei_regp); ei_reg_setival(ei_regp, charp, longx); ei_reg_setfval(ei_regp, charp, doublex); ei_reg_setsval(ei_regp, charp, charp); ei_reg_setpval(ei_regp, charp, voidp, intx); ei_reg_setval(ei_regp, charp, intx); ei_reg_getival(ei_regp, charp); ei_reg_getfval(ei_regp, charp); ei_reg_getsval(ei_regp, charp); ei_reg_getpval(ei_regp, charp, intp); ei_reg_getval(ei_regp, charp, intx); ei_reg_markdirty(ei_regp, charp); ei_reg_delete(ei_regp, charp); ei_reg_stat(ei_regp, charp, ei_reg_statp); ei_reg_tabstat(ei_regp, ei_reg_tabstatp); ei_reg_dump(intx, ei_regp, charp, intx); ei_reg_restore(intx, ei_regp, charp); ei_reg_purge(ei_regp); #if defined(HAVE_GMP_H) && defined(HAVE_LIBGMP) { mpz_t obj; ei_decode_bignum(charp, intp, obj); ei_encode_bignum(charp, intp, obj); ei_x_encode_bignum(&eix, obj); } #endif /* HAVE_GMP_H && HAVE_LIBGMP */ #ifndef VXWORKS ei_decode_longlong(charp, intp, longlongp); ei_decode_ulonglong(charp, intp, ulonglongp); ei_encode_longlong(charp, intp, longlongx); ei_encode_ulonglong(charp, intp, ulonglongx); ei_x_encode_longlong(&eix, longlongx); ei_x_encode_ulonglong(&eix, ulonglongx); #endif #ifdef USE_EI_UNDOCUMENTED ei_decode_intlist(charp, intp, longp, intp); ei_receive_encoded(intx, &charp, intp, msgp, intp); ei_send_encoded(intx, pidp, charp, intx); ei_send_reg_encoded(intx, pidp, charp, charp, intx); ei_decode_big(charp, intp, bigp); ei_big_comp(bigp, bigp); ei_big_to_double(bigp, doublep); ei_small_to_big(intx, bigp); ei_alloc_big(intx); ei_free_big(bigp); #endif /* USE_EI_UNDOCUMENTED */ return BUFSIZ + EAGAIN + EHOSTUNREACH + EIO + EI_BIN + EI_DELET + EI_DIRTY + EI_FLT + EI_FORCE + EI_INT + EI_NOPURGE + EI_STR + EMSGSIZE + ENOMEM + ERL_ERROR + ERL_EXIT + ERL_LINK + ERL_MSG + ERL_NO_TIMEOUT + ERL_REG_SEND + ERL_SEND + ERL_TICK + ERL_TIMEOUT + ERL_UNLINK + ETIMEDOUT + MAXATOMLEN; }
/* * Function returns always success - we uses EPMD for transport */ int erl_rpc_send(erl_rpc_ctx_t *ctx, int depth) { if (ctx->response_sent) return 0; ctx->response_sent = 1; erl_rpc_ctx_t *handler; erl_rpc_param_t *fault = *(ctx->fault_p); if (fault) { LM_ERR("fault: %d %.*s\n",fault->type, STR_FMT(&fault->value.S)); /* restore clear point */ ctx->response->index = ctx->response_index; /* {error,{struct,[ {"code", 400}, {"error","Error message"}]}}*/ if (ei_x_encode_tuple_header(ctx->response,1)) goto error; /* {error,{_,_}} */ if (rpc_reply_with_struct && ei_x_encode_atom(ctx->response,"struct")) goto error; /* {error,{struct,_}} */ if (ei_x_encode_list_header(ctx->response,2)) goto error; /* {error,{struct,[_,_]}} */ if (ei_x_encode_tuple_header(ctx->response,2)) goto error; /* {error,{struct,[{_,_},_]}} */ if (ei_x_encode_atom(ctx->response,"code")) goto error; /* {error,{struct,[{code,_},_]}} */ if (ei_x_encode_long(ctx->response,fault->type)) goto error;/* {error,{struct,[{code,400},_]}} */ if (ei_x_encode_tuple_header(ctx->response,2)) goto error; /* {error,{struct,[{code,400},{_,_}]}} */ if (ei_x_encode_binary(ctx->response,"error",sizeof("error")-1)) goto error; /* {error,{struct,[{code,400},{<<"error">>,_}]}} */ if (ei_x_encode_binary(ctx->response,(void*)fault->value.S.s,fault->value.S.len)) /* {error,{struct,[{code,400},{<<"error">>,<<Msg>>}]}} */ goto error; if (ei_x_encode_empty_list(ctx->response)) goto error; } else if (ctx->reply_params) { while(ctx->reply_params) { if (ctx->reply_params->member_name) { /* {"member_name", _} */ if (ei_x_encode_tuple_header(ctx->response,2)) goto error; if (ei_x_encode_binary(ctx->response,ctx->reply_params->member_name, strlen(ctx->reply_params->member_name))) goto error; } /* {"member_name", MemberValue} */ switch (ctx->reply_params->type) { case ERL_INTEGER_EXT: if(ei_x_encode_long(ctx->response,ctx->reply_params->value.n)) goto error; break; case ERL_FLOAT_EXT: if(ei_x_encode_double(ctx->response,ctx->reply_params->value.d)) goto error; break; case ERL_STRING_EXT: if(ei_x_encode_binary(ctx->response,ctx->reply_params->value.S.s,ctx->reply_params->value.S.len)) goto error; break; case ERL_SMALL_TUPLE_EXT: /* add as {struct,list(no_params)} */ handler = (erl_rpc_ctx_t*)ctx->reply_params->value.handler; if (ei_x_encode_tuple_header(ctx->response,1)) goto error; if (rpc_reply_with_struct && ei_x_encode_atom(ctx->response,"struct")) goto error; if (ei_x_encode_list_header(ctx->response,handler->no_params)) goto error; if (erl_rpc_send(handler, depth++)) goto error; if (ei_x_encode_empty_list(ctx->response)) goto error; break; case ERL_LIST_EXT: /* add as [list(no_params)] */ handler = (erl_rpc_ctx_t*)ctx->reply_params->value.handler; if (ei_x_encode_list_header(ctx->response,handler->no_params)) goto error; if (erl_rpc_send(handler, depth++)) goto error; if (handler->no_params) if (ei_x_encode_empty_list(ctx->response)) goto error; break; default: LM_ERR("Unknown type '%c' for encoding RPC reply\n",ctx->reply_params->type); break; } ctx->reply_params=ctx->reply_params->next; } } else if (!depth) { /* restore start point */ LM_WARN("encode empty response -> ok"); ctx->response->index = ctx->response_index; if (ei_x_encode_atom(ctx->response,"ok")) goto error; } return 0; error: LM_ERR("error while encoding response\n"); return -1; }
int add_event_data(ei_x_buff* sendbuf, int topic, const void* data) { switch (topic) { case EVENT_COMMAND_FINISHED: { ei_x_encode_atom(sendbuf, "event_command_finished"); struct SCommandFinishedEvent* event_data = (struct SCommandFinishedEvent*)data; ei_x_encode_tuple_header(sendbuf, 6); { if (ei_x_encode_atom(sendbuf, "unitid") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unitid"); return -1; } if (ei_x_encode_long(sendbuf, event_data->unitId) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unitId); return -1; } } { if (ei_x_encode_atom(sendbuf, "commandid") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "commandid"); return -1; } if (ei_x_encode_long(sendbuf, event_data->commandId) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->commandId); return -1; } } { if (ei_x_encode_atom(sendbuf, "commandtopicid") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "commandtopicid"); return -1; } if (ei_x_encode_long(sendbuf, event_data->commandTopicId) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->commandTopicId); return -1; } } break; } case EVENT_ENEMY_CREATED: { ei_x_encode_atom(sendbuf, "event_enemy_created"); struct SEnemyCreatedEvent* event_data = (struct SEnemyCreatedEvent*)data; ei_x_encode_tuple_header(sendbuf, 2); { if (ei_x_encode_atom(sendbuf, "enemy") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy"); return -1; } if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy); return -1; } } break; } case EVENT_ENEMY_DAMAGED: { ei_x_encode_atom(sendbuf, "event_enemy_damaged"); struct SEnemyDamagedEvent* event_data = (struct SEnemyDamagedEvent*)data; ei_x_encode_tuple_header(sendbuf, 12); { if (ei_x_encode_atom(sendbuf, "enemy") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy"); return -1; } if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy); return -1; } } { if (ei_x_encode_atom(sendbuf, "attacker") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "attacker"); return -1; } if (ei_x_encode_long(sendbuf, event_data->attacker) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->attacker); return -1; } } { if (ei_x_encode_atom(sendbuf, "damage") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "damage"); return -1; } if (ei_x_encode_double(sendbuf, event_data->damage) < 0) { fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->damage); return -1; } } { if (ei_x_encode_atom(sendbuf, "dir_posf3") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "dir_posf3"); return -1; } if (ei_x_encode_tuple_header(sendbuf, 3) < 0) { fprintf(stderr, "cannot encode '%i' as 'tuple_header'\n", 3); return -1; } if (ei_x_encode_double(sendbuf, event_data->dir_posF3[0]) < 0) { fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[0]); return -1; } if (ei_x_encode_double(sendbuf, event_data->dir_posF3[1]) < 0) { fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[1]); return -1; } if (ei_x_encode_double(sendbuf, event_data->dir_posF3[2]) < 0) { fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[2]); return -1; } } { if (ei_x_encode_atom(sendbuf, "weapondefid") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "weapondefid"); return -1; } if (ei_x_encode_long(sendbuf, event_data->weaponDefId) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->weaponDefId); return -1; } } { if (ei_x_encode_atom(sendbuf, "paralyzer") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "paralyzer"); return -1; } if (ei_x_encode_atom(sendbuf, event_data->paralyzer ? "true" : "false") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", event_data->paralyzer ? "true" : "false"); return -1; } } break; } case EVENT_ENEMY_DESTROYED: { ei_x_encode_atom(sendbuf, "event_enemy_destroyed"); struct SEnemyDestroyedEvent* event_data = (struct SEnemyDestroyedEvent*)data; ei_x_encode_tuple_header(sendbuf, 4); { if (ei_x_encode_atom(sendbuf, "enemy") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy"); return -1; } if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy); return -1; } } { if (ei_x_encode_atom(sendbuf, "attacker") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "attacker"); return -1; } if (ei_x_encode_long(sendbuf, event_data->attacker) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->attacker); return -1; } } break; } case EVENT_ENEMY_ENTER_LOS: { ei_x_encode_atom(sendbuf, "event_enemy_enter_los"); struct SEnemyEnterLOSEvent* event_data = (struct SEnemyEnterLOSEvent*)data; ei_x_encode_tuple_header(sendbuf, 2); { if (ei_x_encode_atom(sendbuf, "enemy") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy"); return -1; } if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy); return -1; } } break; } case EVENT_ENEMY_ENTER_RADAR: { ei_x_encode_atom(sendbuf, "event_enemy_enter_radar"); struct SEnemyEnterRadarEvent* event_data = (struct SEnemyEnterRadarEvent*)data; ei_x_encode_tuple_header(sendbuf, 2); { if (ei_x_encode_atom(sendbuf, "enemy") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy"); return -1; } if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy); return -1; } } break; } case EVENT_ENEMY_FINISHED: { ei_x_encode_atom(sendbuf, "event_enemy_finished"); struct SEnemyFinishedEvent* event_data = (struct SEnemyFinishedEvent*)data; ei_x_encode_tuple_header(sendbuf, 2); { if (ei_x_encode_atom(sendbuf, "enemy") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy"); return -1; } if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy); return -1; } } break; } case EVENT_ENEMY_LEAVE_LOS: { ei_x_encode_atom(sendbuf, "event_enemy_leave_los"); struct SEnemyLeaveLOSEvent* event_data = (struct SEnemyLeaveLOSEvent*)data; ei_x_encode_tuple_header(sendbuf, 2); { if (ei_x_encode_atom(sendbuf, "enemy") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy"); return -1; } if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy); return -1; } } break; } case EVENT_ENEMY_LEAVE_RADAR: { ei_x_encode_atom(sendbuf, "event_enemy_leave_radar"); struct SEnemyLeaveRadarEvent* event_data = (struct SEnemyLeaveRadarEvent*)data; ei_x_encode_tuple_header(sendbuf, 2); { if (ei_x_encode_atom(sendbuf, "enemy") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "enemy"); return -1; } if (ei_x_encode_long(sendbuf, event_data->enemy) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->enemy); return -1; } } break; } case EVENT_LOAD: { ei_x_encode_atom(sendbuf, "event_load"); struct SLoadEvent* event_data = (struct SLoadEvent*)data; ei_x_encode_tuple_header(sendbuf, 2); { if (ei_x_encode_atom(sendbuf, "file") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "file"); return -1; } if (ei_x_encode_string(sendbuf, event_data->file) < 0) { fprintf(stderr, "cannot encode '%s' as 'string'\n", event_data->file); return -1; } } break; } case EVENT_LUA_MESSAGE: { ei_x_encode_atom(sendbuf, "event_lua_message"); struct SLuaMessageEvent* event_data = (struct SLuaMessageEvent*)data; ei_x_encode_tuple_header(sendbuf, 2); { if (ei_x_encode_atom(sendbuf, "indata") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "indata"); return -1; } if (ei_x_encode_string(sendbuf, event_data->inData) < 0) { fprintf(stderr, "cannot encode '%s' as 'string'\n", event_data->inData); return -1; } } break; } case EVENT_MESSAGE: { ei_x_encode_atom(sendbuf, "event_message"); struct SMessageEvent* event_data = (struct SMessageEvent*)data; ei_x_encode_tuple_header(sendbuf, 4); { if (ei_x_encode_atom(sendbuf, "player") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "player"); return -1; } if (ei_x_encode_long(sendbuf, event_data->player) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->player); return -1; } } { if (ei_x_encode_atom(sendbuf, "message") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "message"); return -1; } if (ei_x_encode_string(sendbuf, event_data->message) < 0) { fprintf(stderr, "cannot encode '%s' as 'string'\n", event_data->message); return -1; } } break; } case EVENT_RELEASE: { ei_x_encode_atom(sendbuf, "event_release"); struct SReleaseEvent* event_data = (struct SReleaseEvent*)data; ei_x_encode_tuple_header(sendbuf, 2); { if (ei_x_encode_atom(sendbuf, "reason") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "reason"); return -1; } if (ei_x_encode_long(sendbuf, event_data->reason) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->reason); return -1; } } break; } case EVENT_SAVE: { ei_x_encode_atom(sendbuf, "event_save"); struct SSaveEvent* event_data = (struct SSaveEvent*)data; ei_x_encode_tuple_header(sendbuf, 2); { if (ei_x_encode_atom(sendbuf, "file") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "file"); return -1; } if (ei_x_encode_string(sendbuf, event_data->file) < 0) { fprintf(stderr, "cannot encode '%s' as 'string'\n", event_data->file); return -1; } } break; } case EVENT_SEISMIC_PING: { ei_x_encode_atom(sendbuf, "event_seismic_ping"); struct SSeismicPingEvent* event_data = (struct SSeismicPingEvent*)data; ei_x_encode_tuple_header(sendbuf, 4); { if (ei_x_encode_atom(sendbuf, "pos_posf3") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "pos_posf3"); return -1; } if (ei_x_encode_tuple_header(sendbuf, 3) < 0) { fprintf(stderr, "cannot encode '%i' as 'tuple_header'\n", 3); return -1; } if (ei_x_encode_double(sendbuf, event_data->pos_posF3[0]) < 0) { fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->pos_posF3[0]); return -1; } if (ei_x_encode_double(sendbuf, event_data->pos_posF3[1]) < 0) { fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->pos_posF3[1]); return -1; } if (ei_x_encode_double(sendbuf, event_data->pos_posF3[2]) < 0) { fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->pos_posF3[2]); return -1; } } { if (ei_x_encode_atom(sendbuf, "strength") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "strength"); return -1; } if (ei_x_encode_double(sendbuf, event_data->strength) < 0) { fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->strength); return -1; } } break; } case EVENT_UNIT_CAPTURED: { ei_x_encode_atom(sendbuf, "event_unit_captured"); struct SUnitCapturedEvent* event_data = (struct SUnitCapturedEvent*)data; ei_x_encode_tuple_header(sendbuf, 6); { if (ei_x_encode_atom(sendbuf, "unitid") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unitid"); return -1; } if (ei_x_encode_long(sendbuf, event_data->unitId) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unitId); return -1; } } { if (ei_x_encode_atom(sendbuf, "oldteamid") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "oldteamid"); return -1; } if (ei_x_encode_long(sendbuf, event_data->oldTeamId) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->oldTeamId); return -1; } } { if (ei_x_encode_atom(sendbuf, "newteamid") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "newteamid"); return -1; } if (ei_x_encode_long(sendbuf, event_data->newTeamId) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->newTeamId); return -1; } } break; } case EVENT_UNIT_CREATED: { ei_x_encode_atom(sendbuf, "event_unit_created"); struct SUnitCreatedEvent* event_data = (struct SUnitCreatedEvent*)data; ei_x_encode_tuple_header(sendbuf, 4); { if (ei_x_encode_atom(sendbuf, "unit") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit"); return -1; } if (ei_x_encode_long(sendbuf, event_data->unit) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit); return -1; } } { if (ei_x_encode_atom(sendbuf, "builder") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "builder"); return -1; } if (ei_x_encode_long(sendbuf, event_data->builder) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->builder); return -1; } } break; } case EVENT_UNIT_DAMAGED: { ei_x_encode_atom(sendbuf, "event_unit_damaged"); struct SUnitDamagedEvent* event_data = (struct SUnitDamagedEvent*)data; ei_x_encode_tuple_header(sendbuf, 12); { if (ei_x_encode_atom(sendbuf, "unit") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit"); return -1; } if (ei_x_encode_long(sendbuf, event_data->unit) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit); return -1; } } { if (ei_x_encode_atom(sendbuf, "attacker") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "attacker"); return -1; } if (ei_x_encode_long(sendbuf, event_data->attacker) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->attacker); return -1; } } { if (ei_x_encode_atom(sendbuf, "damage") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "damage"); return -1; } if (ei_x_encode_double(sendbuf, event_data->damage) < 0) { fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->damage); return -1; } } { if (ei_x_encode_atom(sendbuf, "dir_posf3") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "dir_posf3"); return -1; } if (ei_x_encode_tuple_header(sendbuf, 3) < 0) { fprintf(stderr, "cannot encode '%i' as 'tuple_header'\n", 3); return -1; } if (ei_x_encode_double(sendbuf, event_data->dir_posF3[0]) < 0) { fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[0]); return -1; } if (ei_x_encode_double(sendbuf, event_data->dir_posF3[1]) < 0) { fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[1]); return -1; } if (ei_x_encode_double(sendbuf, event_data->dir_posF3[2]) < 0) { fprintf(stderr, "cannot encode '%f' as 'double'\n", event_data->dir_posF3[2]); return -1; } } { if (ei_x_encode_atom(sendbuf, "weapondefid") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "weapondefid"); return -1; } if (ei_x_encode_long(sendbuf, event_data->weaponDefId) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->weaponDefId); return -1; } } { if (ei_x_encode_atom(sendbuf, "paralyzer") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "paralyzer"); return -1; } if (ei_x_encode_atom(sendbuf, event_data->paralyzer ? "true" : "false") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", event_data->paralyzer ? "true" : "false"); return -1; } } break; } case EVENT_UNIT_DESTROYED: { ei_x_encode_atom(sendbuf, "event_unit_destroyed"); struct SUnitDestroyedEvent* event_data = (struct SUnitDestroyedEvent*)data; ei_x_encode_tuple_header(sendbuf, 4); { if (ei_x_encode_atom(sendbuf, "unit") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit"); return -1; } if (ei_x_encode_long(sendbuf, event_data->unit) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit); return -1; } } { if (ei_x_encode_atom(sendbuf, "attacker") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "attacker"); return -1; } if (ei_x_encode_long(sendbuf, event_data->attacker) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->attacker); return -1; } } break; } case EVENT_UNIT_FINISHED: { ei_x_encode_atom(sendbuf, "event_unit_finished"); struct SUnitFinishedEvent* event_data = (struct SUnitFinishedEvent*)data; ei_x_encode_tuple_header(sendbuf, 2); { if (ei_x_encode_atom(sendbuf, "unit") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit"); return -1; } if (ei_x_encode_long(sendbuf, event_data->unit) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit); return -1; } } break; } case EVENT_UNIT_GIVEN: { ei_x_encode_atom(sendbuf, "event_unit_given"); struct SUnitGivenEvent* event_data = (struct SUnitGivenEvent*)data; ei_x_encode_tuple_header(sendbuf, 6); { if (ei_x_encode_atom(sendbuf, "unitid") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unitid"); return -1; } if (ei_x_encode_long(sendbuf, event_data->unitId) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unitId); return -1; } } { if (ei_x_encode_atom(sendbuf, "oldteamid") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "oldteamid"); return -1; } if (ei_x_encode_long(sendbuf, event_data->oldTeamId) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->oldTeamId); return -1; } } { if (ei_x_encode_atom(sendbuf, "newteamid") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "newteamid"); return -1; } if (ei_x_encode_long(sendbuf, event_data->newTeamId) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->newTeamId); return -1; } } break; } case EVENT_UNIT_IDLE: { ei_x_encode_atom(sendbuf, "event_unit_idle"); struct SUnitIdleEvent* event_data = (struct SUnitIdleEvent*)data; ei_x_encode_tuple_header(sendbuf, 2); { if (ei_x_encode_atom(sendbuf, "unit") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit"); return -1; } if (ei_x_encode_long(sendbuf, event_data->unit) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit); return -1; } } break; } case EVENT_UNIT_MOVE_FAILED: { ei_x_encode_atom(sendbuf, "event_unit_move_failed"); struct SUnitMoveFailedEvent* event_data = (struct SUnitMoveFailedEvent*)data; ei_x_encode_tuple_header(sendbuf, 2); { if (ei_x_encode_atom(sendbuf, "unit") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unit"); return -1; } if (ei_x_encode_long(sendbuf, event_data->unit) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unit); return -1; } } break; } case EVENT_UPDATE: { ei_x_encode_atom(sendbuf, "event_update"); struct SUpdateEvent* event_data = (struct SUpdateEvent*)data; ei_x_encode_tuple_header(sendbuf, 2); { if (ei_x_encode_atom(sendbuf, "frame") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "frame"); return -1; } if (ei_x_encode_long(sendbuf, event_data->frame) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->frame); return -1; } } break; } case EVENT_WEAPON_FIRED: { ei_x_encode_atom(sendbuf, "event_weapon_fired"); struct SWeaponFiredEvent* event_data = (struct SWeaponFiredEvent*)data; ei_x_encode_tuple_header(sendbuf, 4); { if (ei_x_encode_atom(sendbuf, "unitid") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "unitid"); return -1; } if (ei_x_encode_long(sendbuf, event_data->unitId) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->unitId); return -1; } } { if (ei_x_encode_atom(sendbuf, "weapondefid") < 0) { fprintf(stderr, "cannot encode '%s' as 'atom'\n", "weapondefid"); return -1; } if (ei_x_encode_long(sendbuf, event_data->weaponDefId) < 0) { fprintf(stderr, "cannot encode '%i' as 'long'\n", event_data->weaponDefId); return -1; } } break; } default: { ei_x_encode_long(sendbuf, topic); ei_x_encode_binary(sendbuf, data, sizeof(data)); } } return 0; }
static inline void write_double(State *p, double d) { ei_x_encode_double(&p->ei_buf, d); }
void * record(void *args) { int error; pa_sample_spec ss; char ss_a[PA_SAMPLE_SPEC_SNPRINT_MAX]; memset(pa_buff, 0, ABUFF_SIZE); ss.format = PA_SAMPLE_FLOAT32LE; ss.channels = CHANNELS; ss.rate = frequency; pa_s = pa_simple_new(NULL, /* PulseAudio server. */ "Recorder", /* Application's name. */ PA_STREAM_RECORD, /* Stream direction. */ NULL, /* Sink Device. */ "PulseAudio-read", /* Stream description. */ &ss, /* Sample format. */ NULL, /* Channel map */ NULL, /* Buffering attributes. */ &error /* Error code. */ ); if (NULL == pa_s) { fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); exit(1); } pa_sample_spec_snprint(ss_a, sizeof(ss_a), &ss); D("Opening the recording stream with sample specification '%s'", ss_a); D("%s", "Start recording"); while (recording) { int n; int error; n = pa_simple_read(pa_s, (void *)pa_buff, ABUFF_SIZE, &error); if (-1 != n) { int i; ei_x_buff result; /* Prepare the output buffer that will hold the result */ check(ei_x_new_with_version(&result)); /* List size */ check(ei_x_encode_list_header(&result, INSIZE)); /* List elements */ for (i = 0; i < NSAMPLES; i++) check(ei_x_encode_double(&result, pa_buff[i])); /* Make a proper list */ check(ei_x_encode_empty_list(&result)); // D("%s", "Sending data"); write_cmd(&result); ei_x_free(&result); } } pa_simple_free(pa_s); pthread_exit(NULL); }
int ei_x_encode_ke(ei_x_buff* types, ei_x_buff* values, K r, QOpts* opts) { EI(ei_x_encode_atom(types, "real")); EI(ei_x_encode_double(values, (double)r->e)); return 0; }
int srdb1_encode_kv(int tupsize,const db_key_t* _k, const db_op_t* _op, const db_val_t* _v, const int _n, ei_x_buff *argbuf) { int i; struct tm* tt; time_t t_t; if(_k) { ei_x_encode_list_header(argbuf, _n); for(i = 0; i < _n; i++) { db_val_t *vv; ei_x_encode_tuple_header(argbuf, tupsize); ei_x_encode_atom_len(argbuf,_k[i]->s,_k[i]->len); if(tupsize == 3 ) { if (_op) { ei_x_encode_atom(argbuf,_op[i]); } else { ei_x_encode_atom(argbuf,"="); } } vv=&(_v[i]); if (VAL_NULL(vv)) { ei_x_encode_atom(argbuf,"undefined"); } else { switch(VAL_TYPE(vv)) { case DB1_INT: ei_x_encode_ulong(argbuf, VAL_INT(vv)); break; case DB1_BIGINT: ei_x_encode_longlong(argbuf, VAL_BIGINT(vv)); break; case DB1_DOUBLE: ei_x_encode_double(argbuf, VAL_DOUBLE(vv)); break; case DB1_STRING: ei_x_encode_string(argbuf,VAL_STRING(vv)); break; case DB1_STR: ei_x_encode_string_len(argbuf,VAL_STR(vv).s,VAL_STR(vv).len); break; case DB1_DATETIME: t_t=VAL_TIME(vv); tt= localtime(&t_t); ei_x_encode_tuple_header(argbuf, 2); ei_x_encode_tuple_header(argbuf, 3); ei_x_encode_long(argbuf, tt->tm_year + 1900); ei_x_encode_long(argbuf, tt->tm_mon +1); ei_x_encode_long(argbuf, tt->tm_mday); ei_x_encode_tuple_header(argbuf, 3); ei_x_encode_long(argbuf, tt->tm_hour); ei_x_encode_long(argbuf, tt->tm_min); ei_x_encode_long(argbuf, tt->tm_sec); break; case DB1_BLOB: ei_x_encode_binary(argbuf,VAL_BLOB(vv).s,VAL_BLOB(vv).len); break; case DB1_BITMAP: ei_x_encode_ulong(argbuf,VAL_BITMAP(vv)); break; } } } ei_x_encode_empty_list(argbuf); } else { ei_x_encode_list_header(argbuf, 0); } return 0; }