| | |
| | | |
| | | template <class T> T* ptrT(const size_t l=1){ return (T*)calloc(l, sizeof(T)); } |
| | | |
| | | static struct cstr cstr_clone(const struct cstr old){ |
| | | return cstr_new(old.str, old.size); |
| | | } |
| | | static struct cstr cstr_ref(char* str, const size_t len){ |
| | | static inline struct cstr null_cstr(){ |
| | | struct cstr cs; |
| | | memset(&cs, 0, sizeof(cs)); |
| | | return cs; |
| | | } |
| | | static inline struct cstr cstr_ref(char* str, const size_t len){ |
| | | struct cstr cs = null_cstr(); |
| | | cs.size = len; |
| | | cs.str = str; |
| | | return cs; |
| | | } |
| | | static inline struct cstr cstr_clone(const struct cstr old){ |
| | | return cstr_new(old.str, old.size); |
| | | } |
| | | struct cstr cstr_new(const char* str, const size_t len){ |
| | | struct cstr cs; |
| | | memset(&cs, 0, sizeof(cs)); |
| | | struct cstr cs = null_cstr(); |
| | | cs.size = len; |
| | | cs.str = ptrT<char>(len); |
| | | memcpy(cs.str, str, len); |
| | |
| | | void cstr_free(struct cstr str){ |
| | | if (str.str && str.size) free(str.str); |
| | | } |
| | | struct cstrarr cstr_arr_new(const size_t count){ |
| | | static inline struct cstrarr null_cstr_arr(){ |
| | | struct cstrarr arr; |
| | | memset(&arr, 0, sizeof(arr)); |
| | | return arr; |
| | | } |
| | | struct cstrarr cstr_arr_new(const size_t count){ |
| | | struct cstrarr arr = null_cstr_arr(); |
| | | arr.arr = (struct cstr*)calloc(count, sizeof(struct cstr)); |
| | | arr.count = count; |
| | | return arr; |
| | |
| | | free(arr.arr); |
| | | } |
| | | |
| | | void free_creg(struct creg* reg){ |
| | | if (reg){ |
| | | if (reg->pinfo) free_proc_info(reg->pinfo); |
| | | cstr_arr_free(reg->channel); |
| | | cstr_arr_free(reg->topic_pub); |
| | | cstr_arr_free(reg->topic_sub); |
| | | cstr_arr_free(reg->topic_sub_net); |
| | | free(reg); |
| | | struct cproc* make_proc_info(const struct cstr name, const struct cstr id, const struct cstr info){ |
| | | auto proc = ptrT<struct cproc>(); |
| | | proc->name = cstr_clone(name); |
| | | proc->id = cstr_clone(id); |
| | | // proc->info = cstr_clone(info); |
| | | return proc; |
| | | } |
| | | } |
| | | |
| | | struct cproc* clone_proc_info(struct cproc* pi){ |
| | | struct cproc* clone_proc_info(const struct cproc* pi){ |
| | | if (!pi) return NULL; |
| | | auto newpi = ptrT<struct cproc>(); |
| | | *newpi = *pi; |
| | | newpi->name = cstr_clone(newpi->name); |
| | | newpi->id = cstr_clone(newpi->id); |
| | | // todo: ignore info |
| | |
| | | cstr_free(pi->id); |
| | | cstr_free(pi->info); |
| | | free(pi); |
| | | } |
| | | } |
| | | |
| | | void free_creg(struct creg* reg){ |
| | | if (reg){ |
| | | if (reg->pinfo) free_proc_info(reg->pinfo); |
| | | cstr_arr_free(reg->channel); |
| | | cstr_arr_free(reg->topic_pub); |
| | | cstr_arr_free(reg->topic_sub); |
| | | cstr_arr_free(reg->topic_sub_net); |
| | | free(reg); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | auto assign_arr = [&obj](yyjson_val* v){ |
| | | const size_t count = yyjson_arr_size(v); |
| | | struct cstrarr arr; |
| | | memset(&arr, 0, sizeof(arr)); |
| | | arr.count = count; |
| | | struct cstrarr arr = cstr_arr_new(count); |
| | | for(size_t i = 0; i < count; i++){ |
| | | auto sv = yyjson_arr_get(v, i); |
| | | char* entry = NULL; |
| | |
| | | const size_t size = msg->msg.size; |
| | | |
| | | auto pl = ptrT<struct cproclist>(); |
| | | pl->cli = json2cclients(data, size, &pl->clientsize); |
| | | pl->cli = json2cclients(data, size, &pl->count); |
| | | return pl; |
| | | } |
| | | void free_submsg_proclist(struct cproclist* pl){ |
| | | if (pl) if (pl->cli) free_cclients(pl->cli, pl->clientsize); |
| | | if (pl) if (pl->cli) free_cclients(pl->cli, pl->count); |
| | | } |
| | | |
| | | ////////////////////////////////////////////////// |
| | |
| | | return msg; |
| | | } |
| | | |
| | | struct creqmsg* make_req_msg(const char* topic, const size_t topics, |
| | | struct creqmsg* make_req_msg(const char* topic, const size_t topicl, |
| | | const char* data, const size_t datal) |
| | | { |
| | | auto msg = ptrT<struct creqmsg>(); |
| | | MsgRequestTopic msgRT; |
| | | msgRT.set_topic(topic, topics); |
| | | msgRT.set_topic(topic, topicl); |
| | | msgRT.set_data(data, datal); |
| | | |
| | | auto pbstr = msgRT.SerializeAsString(); |
| | |
| | | } |
| | | |
| | | struct cstackmsgerr* get_reqmsg_stackerr(struct creqmsg* msg){ |
| | | struct cstr path, body; |
| | | memset(&path, 0, sizeof(path)); |
| | | memset(&body, 0, sizeof(body)); |
| | | struct cstr path = null_cstr(); |
| | | struct cstr body = null_cstr(); |
| | | tie(path, body) = json2reqmsg(msg->msg); |
| | | if (body.size == 0) return NULL; |
| | | |
| | |
| | | } |
| | | // decode success msg |
| | | struct cstackmsg* get_reqmsg_stack(struct creqmsg* msg){ |
| | | struct cstr path, body; |
| | | memset(&path, 0, sizeof(path)); |
| | | memset(&body, 0, sizeof(body)); |
| | | struct cstr path = null_cstr(); |
| | | struct cstr body = null_cstr(); |
| | | tie(path, body) = json2reqmsg(msg->msg); |
| | | if (body.size == 0) return NULL; |
| | | |