void _bus_terminal_update_channels(bus_terminal_t* bt) { struct bus_terminal_channel_t* btc; int32_t i, ret; assert(bt && bt->bus); // load terminal channel info bt->local_channel_version = bt->bus->channel_version; for (i = 0; i < bt->bus->channel_count; ++ i) { if (bt->bus->channels[i].from == bt->self && idtable_get(bt->send_channels, bt->bus->channels[i].to) == NULL) { btc = bus_terminal_channel_init(bt->bus->channels[i].shmkey, bt->bus->channels[i].from, bt->bus->channels[i].to, bt->bus->channels[i].channel_size, 1); assert(btc); ret = idtable_add(bt->send_channels, bt->bus->channels[i].to, btc); assert(0 == ret); } else if (bt->bus->channels[i].to == bt->self && idtable_get(bt->recv_channels, bt->bus->channels[i].from) == NULL) { btc = bus_terminal_channel_init(bt->bus->channels[i].shmkey, bt->bus->channels[i].from, bt->bus->channels[i].to, bt->bus->channels[i].channel_size, 1); assert(btc); ret = idtable_add(bt->recv_channels, bt->bus->channels[i].from, btc); assert(0 == ret); } } }
static int _accept_read(sock_t fd, void* arg) { WSCtx* ctx = (WSCtx*)MALLOC(sizeof(WSCtx)); assert(ctx); ctx->con = wsconn_create(r); assert(ctx->con); wsconn_set_build_func(ctx->con, _wscon_build, NULL); wsconn_set_read_func(ctx->con, _wscon_read, NULL); wsconn_set_close_func(ctx->con, _wscon_close, NULL); wsconn_set_fd(ctx->con, fd); int res = idtable_add(con_table, fd, ctx); assert(0 == res); res = wsconn_start(ctx->con); assert(0 == res); printf("fd[%d] connected\n", fd); return 0; }
int32_t _bus_terminal_register_channel(bus_terminal_t* bt, bus_addr_t to, size_t sz) { int i, found, ret; struct bus_channel_t* bc; struct bus_terminal_channel_t* btc; if (!bt) return bus_err_fail; // make sure same version bus_terminal_tick(bt); // add lock process_lock_lock(bt->lock); if (bt->local_channel_version != bt->bus->channel_version) { process_lock_unlock(bt->lock); return bus_err_fail; } // no left free channel if (bt->bus->channel_count >= BUS_MAX_CHANNLE_COUNT) { process_lock_unlock(bt->lock); return bus_err_channel_full; } // check terminals found = -1; for (i = 0; i < bt->bus->terminal_count; ++ i) { if (bt->bus->terminals[i] == to) { found = 0; break; } } if (found) { process_lock_unlock(bt->lock); return bus_err_peek_fail; } // check not self (no loop bus) if (to == bt->self) { process_lock_unlock(bt->lock); return bus_err_peek_fail; } // create channel bc = &bt->bus->channels[bt->bus->channel_count]; bc->from = bt->self; bc->to = to; bc->shmkey = bt->bus->key + (++ bt->bus->channel_key); bc->channel_size = sz; btc = bus_terminal_channel_init(bc->shmkey, bc->from, bc->to, bc->channel_size, 0); if (!btc) { process_lock_unlock(bt->lock); return bus_err_channel_fail; } // add terminal-channel to idtable ret = idtable_add(bt->send_channels, bc->to, btc); assert(0 == ret); // add channel version ++ bt->bus->channel_count; atom_inc(&bt->bus->channel_version); process_lock_unlock(bt->lock); bt->local_channel_version = bt->bus->channel_version; return bus_ok; }