int load_cfg_cli_svr(json_t *root, const char *key, cli_svr_cfg *cfg) { json_t *node = json_object_get(root, key); if (!node || !json_is_string(node)) return -__LINE__; ERR_RET_LN(nw_sock_cfg_parse(json_string_value(node), &cfg->addr, &cfg->sock_type)); return 0; }
int main(int argc, char *argv[]) { if (argc < 2) { printf("usage: %s bind...\n", argv[0]); exit(0); } int bind_count = argc - 1; nw_svr_bind *bind_arr = malloc(sizeof(nw_svr_bind) * bind_count); if (bind_arr == NULL) { printf("malloc fail\n"); exit(0); } memset(bind_arr, sizeof(nw_svr_bind) * bind_count, 0); for (int i = 0; i < bind_count; ++i) { int ret = nw_sock_cfg_parse(argv[i + 1], &bind_arr[i].addr, &bind_arr[i].sock_type); if (ret < 0) { printf("parse bind: %s fail: %d\n", argv[1], ret); exit(0); } } nw_svr_cfg cfg; memset(&cfg, 0, sizeof(cfg)); cfg.bind_count = bind_count; cfg.bind_arr = bind_arr; cfg.max_pkg_size = 10240; nw_svr_type type; memset(&type, 0, sizeof(type)); type.decode_pkg = decode_pkg; type.on_new_connection = on_new_connection; type.on_connection_close = on_connection_close; type.on_recv_pkg = on_recv_pkg; type.on_recv_fd = on_recv_fd; type.on_error_msg = on_error_msg; nw_svr *svr = nw_svr_create(&cfg, &type, NULL); if (svr == NULL) { printf("nw_svr_create fail\n"); exit(0); } nw_svr_start(svr); nw_timer timer; nw_timer_set(&timer, 5.0, true, on_timer, svr); nw_timer_start(&timer); printf("echo server start\n"); nw_loop_run(); printf("echo server stop\n"); return 0; }
int load_cfg_ws_svr(json_t *root, const char *key, ws_svr_cfg *cfg) { json_t *node = json_object_get(root, key); if (!node || !json_is_object(node)) return -__LINE__; json_t *bind = json_object_get(node, "bind"); if (!bind) return -__LINE__; if (json_is_string(bind)) { cfg->bind_count = 1; cfg->bind_arr = malloc(sizeof(nw_svr_bind)); if (nw_sock_cfg_parse(json_string_value(bind), &cfg->bind_arr[0].addr, &cfg->bind_arr[0].sock_type) < 0) return -__LINE__; } else if (json_is_array(bind)) { cfg->bind_count = json_array_size(bind); if (cfg->bind_count == 0) return -__LINE__; cfg->bind_arr = malloc(sizeof(nw_svr_bind) * cfg->bind_count); for (uint32_t i = 0; i < cfg->bind_count; ++i) { json_t *row = json_array_get(bind, i); if (!json_is_string(row)) return -__LINE__; if (nw_sock_cfg_parse(json_string_value(row), &cfg->bind_arr[i].addr, &cfg->bind_arr[i].sock_type) < 0) return -__LINE__; } } else { return -__LINE__; } ERR_RET(read_cfg_uint32(node, "max_pkg_size", &cfg->max_pkg_size, true, 0)); ERR_RET(read_cfg_uint32(node, "buf_limit", &cfg->buf_limit, false, 0)); ERR_RET(read_cfg_uint32(node, "read_mem", &cfg->read_mem, false, 0)); ERR_RET(read_cfg_uint32(node, "write_mem", &cfg->write_mem, false, 0)); ERR_RET(read_cfg_int(node, "keep_alive", &cfg->keep_alive, false, 3600)); ERR_RET(read_cfg_str(node, "protocol", &cfg->protocol, "chat")); ERR_RET(read_cfg_str(node, "origin", &cfg->origin, "")); return 0; }
int load_cfg_rpc_clt(json_t *root, const char *key, rpc_clt_cfg *cfg) { json_t *node = json_object_get(root, key); if (!node || !json_is_object(node)) return -__LINE__; ERR_RET(read_cfg_str(node, "name", &cfg->name, NULL)); json_t *addr = json_object_get(node, "addr"); if (json_is_string(addr)) { cfg->addr_count = 1; cfg->addr_arr = malloc(sizeof(nw_addr_t)); if (nw_sock_cfg_parse(json_string_value(addr), &cfg->addr_arr[0], &cfg->sock_type) < 0) return -__LINE__; } else if (json_is_array(addr)) { cfg->addr_count = json_array_size(addr); if (cfg->addr_count == 0) return -__LINE__; cfg->addr_arr = malloc(sizeof(nw_addr_t) * cfg->addr_count); for (uint32_t i = 0; i < cfg->addr_count; ++i) { json_t *row = json_array_get(addr, i); if (!json_is_string(row)) return -__LINE__; if (nw_sock_cfg_parse(json_string_value(row), &cfg->addr_arr[i], &cfg->sock_type) < 0) return -__LINE__; } } else { return -__LINE__; } ERR_RET(read_cfg_uint32(node, "max_pkg_size", &cfg->max_pkg_size, true, 0)); ERR_RET(read_cfg_uint32(node, "buf_limit", &cfg->buf_limit, false, 0)); ERR_RET(read_cfg_uint32(node, "read_mem", &cfg->read_mem, false, 0)); ERR_RET(read_cfg_uint32(node, "write_mem", &cfg->read_mem, false, 0)); ERR_RET(read_cfg_real(node, "reconnect_timeout", &cfg->reconnect_timeout, false, 0)); ERR_RET(read_cfg_real(node, "heartbeat_timeout", &cfg->heartbeat_timeout, false, 0)); return 0; }
int load_cfg_rpc_svr(json_t *root, const char *key, rpc_svr_cfg *cfg) { json_t *node = json_object_get(root, key); if (!node || !json_is_object(node)) return -__LINE__; json_t *bind = json_object_get(node, "bind"); if (!bind) return -__LINE__; if (json_is_string(bind)) { cfg->bind_count = 1; cfg->bind_arr = malloc(sizeof(nw_svr_bind)); if (nw_sock_cfg_parse(json_string_value(bind), &cfg->bind_arr[0].addr, &cfg->bind_arr[0].sock_type) < 0) return -__LINE__; } else if (json_is_array(bind)) { cfg->bind_count = json_array_size(bind); if (cfg->bind_count == 0) return -__LINE__; cfg->bind_arr = malloc(sizeof(nw_svr_bind) * cfg->bind_count); for (uint32_t i = 0; i < cfg->bind_count; ++i) { json_t *row = json_array_get(bind, i); if (!json_is_string(row)) return -__LINE__; if (nw_sock_cfg_parse(json_string_value(row), &cfg->bind_arr[i].addr, &cfg->bind_arr[i].sock_type) < 0) return -__LINE__; } } else { return -__LINE__; } ERR_RET(read_cfg_uint32(node, "max_pkg_size", &cfg->max_pkg_size, true, 0)); ERR_RET(read_cfg_uint32(node, "buf_limit", &cfg->buf_limit, false, 0)); ERR_RET(read_cfg_uint32(node, "read_mem", &cfg->read_mem, false, 0)); ERR_RET(read_cfg_uint32(node, "write_mem", &cfg->write_mem, false, 0)); ERR_RET(read_cfg_bool(node, "heartbeat_check", &cfg->heartbeat_check, false, true)); return 0; }
int load_cfg_clt(json_t *root, const char *key, nw_clt_cfg *cfg) { json_t *node = json_object_get(root, key); if (!node || !json_is_object(node)) return -__LINE__; json_t *addr = json_object_get(node, "addr"); if (!addr || !json_is_string(addr)) return -__LINE__; if (nw_sock_cfg_parse(json_string_value(addr), &cfg->addr, &cfg->sock_type) < 0) return -__LINE__; ERR_RET(read_cfg_uint32(node, "max_pkg_size", &cfg->max_pkg_size, true, 0)); ERR_RET(read_cfg_uint32(node, "buf_limit", &cfg->buf_limit, false, 0)); ERR_RET(read_cfg_uint32(node, "read_mem", &cfg->read_mem, false, 0)); ERR_RET(read_cfg_uint32(node, "write_mem", &cfg->read_mem, false, 0)); ERR_RET(read_cfg_real(node, "reconnect_timeout", &cfg->reconnect_timeout, false, 0)); return 0; }
int main(int argc, char *argv[]) { if (argc < 2) { printf("usage: %s bind...\n", argv[0]); exit(0); } int bind_count = argc - 1; nw_svr_bind *bind_arr = malloc(sizeof(nw_svr_bind) * bind_count); assert(bind_arr != NULL); memset(bind_arr, sizeof(nw_svr_bind) * bind_count, 0); for (int i = 0; i < bind_count; ++i) { int ret = nw_sock_cfg_parse(argv[i + 1], &bind_arr[i].addr, &bind_arr[i].sock_type); if (ret < 0) { printf("parse bind: %s fail: %d\n", argv[1], ret); exit(0); } } nw_svr_cfg cfg; memset(&cfg, 0, sizeof(cfg)); cfg.bind_count = bind_count; cfg.bind_arr = bind_arr; rpc_svr_type type; memset(&type, 0, sizeof(type)); type.on_recv_pkg = on_recv_pkg; type.on_new_connection = on_new_connection; type.on_connection_close = on_connection_close; rpc_svr *svr = rpc_svr_create(&cfg, &type); if (svr == NULL) error(1, errno, "rpc_svr_create fail"); rpc_svr_start(svr); nw_loop_run(); return 0; }