// Returns the created socket, or -1 if failed. static int create_listening_socket(const char *host, int port) { int s = socket(AF_INET, SOCK_STREAM, 0); if (s == -1) { shout("cannot create the listening socket: %s\n", strerror(errno)); return -1; } int optval = 1; setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char const*)&optval, sizeof(optval)); setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char const*)&optval, sizeof(optval)); struct sockaddr_in addr; addr.sin_family = AF_INET; if (inet_aton(host, &addr.sin_addr) == 0) { shout("cannot convert the host string '%s' to a valid address\n", host); return -1; } addr.sin_port = htons(port); debug("binding %s:%d\n", host, port); if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) == -1) { shout("cannot bind the listening socket: %s\n", strerror(errno)); return -1; } if (listen(s, LISTEN_QUEUE_SIZE) == -1) { shout("failed to listen the socket: %s\n", strerror(errno)); return -1; } return s; }
int main(int argc, char **argv) { signal(SIGPIPE, SIG_IGN); if (!setup(argc, argv)) { usage(argv[0]); return EXIT_FAILURE; } srand(getpid()); while (true) { //wait_ms(1000); char value[20]; snprintf(value, sizeof(value), "%d", rand()); shout("set(%s, %s)\n", key, value); if (set(key, value, 1000)) { char *reply = get(key, 1000); if (reply) { shout("%s = %s\n", key, reply); free(reply); } } else { shout("set() failed\n"); } } return EXIT_SUCCESS; }
static bool stream_message_append(stream_t stream, size_t len, void *data) { ShubMessageHdr *msg; debug("appending %d\n", *(int*)data); if (stream->output.curmessage == NULL) { shout("cannot append, the message was not started\n"); stream->good = false; return false; } int newsize = stream->output.curmessage->size + sizeof(ShubMessageHdr) + len; if (newsize > BUFFER_SIZE) { // the flushing will not help here shout("the message cannot be bigger than the buffer size\n"); stream->good = false; return false; } if (stream->output.ready + newsize > BUFFER_SIZE) { if (!stream_flush(stream)) { shout("failed to flush before extending the message\n"); stream->good = false; return false; } } msg = stream->output.curmessage; memcpy((char*)msg + msg->size + sizeof(ShubMessageHdr), data, len); msg->size += len; return true; }
void raft_handle_message(raft_t r, raft_msg_t m) { if (m->term > r->term) { if (r->role != FOLLOWER) { shout("I have an old term, demoting myself\n"); } raft_set_term(r, m->term); r->role = FOLLOWER; } assert(m->msgtype >= 0); assert(m->msgtype < 4); switch (m->msgtype) { case RAFT_MSG_UPDATE: raft_handle_update(r, (raft_msg_update_t *)m); break; case RAFT_MSG_DONE: raft_handle_done(r, (raft_msg_done_t *)m); break; case RAFT_MSG_CLAIM: raft_handle_claim(r, (raft_msg_claim_t *)m); break; case RAFT_MSG_VOTE: raft_handle_vote(r, (raft_msg_vote_t *)m); break; default: shout("unknown message type\n"); } }
bool raft_peer_up(raft_t r, int id, char *host, int port, bool self) { if (r->peernum >= r->config.peernum_max) { shout("too many peers\n"); return false; } raft_peer_t *p = r->peers + id; raft_peer_init(p); p->up = true; p->host = host; p->port = port; if (inet_aton(p->host, &p->addr.sin_addr) == 0) { shout( "cannot convert the host string '%s'" " to a valid address\n", p->host ); return false; } p->addr.sin_family = AF_INET; p->addr.sin_port = htons(p->port); if (self) { if (r->me != NOBODY) { shout("cannot set 'self' peer multiple times\n"); return false; } r->me = id; srand(id); raft_reset_timer(r); } r->peernum++; return true; }
// Set the status of the specified global commit. Return 'true' on success, // 'false' otherwise. bool clog_write(clog_t clog, xid_t xid, int status) { clogfile_t *file = clog_xid_to_file(clog, xid); if (!file) { debug("xid %u out of range, creating the file\n", xid); clogfile_t newfile; if (!clogfile_open_by_id(&newfile, clog->datadir, XID_TO_FILEID(xid), true)) { shout( "failed to create new clogfile " "while saving transaction status\n" ); return false; } clogfile_chain_t *lastfile = new_clogfile_chain(&newfile); lastfile->prev = clog->lastfile; clog->lastfile = lastfile; } file = clog_xid_to_file(clog, xid); if (!file) { shout("the file is absent despite our efforts\n"); return false; } bool ok = clogfile_set_status(file, xid, status); return ok; }
static bool raft_config_is_ok(raft_config_t *config) { bool ok = true; if (config->peernum_max < 3) { shout("please ensure peernum_max >= 3\n"); ok = false; } if (config->heartbeat_ms >= config->election_ms_min) { shout("please ensure heartbeat_ms < election_ms_min (substantially)\n"); ok = false; } if (config->election_ms_min >= config->election_ms_max) { shout("please ensure election_ms_min < election_ms_max\n"); ok = false; } if (sizeof(raft_msg_update_t) + config->chunk_len - 1 > UDP_SAFE_SIZE) { shout( "please ensure chunk_len <= %lu, %d is too much for UDP\n", UDP_SAFE_SIZE - sizeof(raft_msg_update_t) + 1, config->chunk_len ); ok = false; } if (config->msg_len_max < sizeof(raft_msg_any_t)) { shout("please ensure msg_len_max >= %lu\n", sizeof(raft_msg_any_t)); ok = false; } return ok; }
static bool server_accept(server_t server) { debug("a new connection is queued\n"); int fd = accept(server->listener, NULL, NULL); if (fd == -1) { shout("failed to accept a connection: %s\n", strerror(errno)); return false; } debug("a new connection accepted\n"); if (server->streamsnum >= MAX_STREAMS) { shout("streams limit hit, disconnecting the accepted connection\n"); close(fd); return false; } // add new stream stream_t s = server->streams + server->streamsnum++; stream_init(s, fd); FD_SET(fd, &server->all); if (fd > server->maxfd) { server->maxfd = fd; } return true; }
bool server_tick(server_t server, int timeout_ms) { int i; int numready; bool raft_ready = false; #ifdef USE_EPOLL struct epoll_event events[MAX_EVENTS]; numready = epoll_wait(server->epollfd, events, MAX_EVENTS, timeout_ms); if (numready < 0) { shout("failed to epoll: %s\n", strerror(errno)); return false; } for (i = 0; i < numready; i++) { stream_t stream = (stream_t)events[i].data.ptr; if (stream == NULL) { server_accept(server); } else if (stream == &server->raft_stream) { raft_ready = true; } else { if (events[i].events & EPOLLERR) { stream->good = false; } else if (events[i].events & EPOLLIN) { server_stream_handle(server, stream); } } } #else fd_set readfds = server->all; struct timeval timeout = ms2tv(timeout_ms); numready = select(server->maxfd + 1, &readfds, NULL, NULL, &timeout); if (numready == -1) { shout("failed to select: %s\n", strerror(errno)); return false; } if (FD_ISSET(server->listener, &readfds)) { numready--; server_accept(server); } if ((server->raft_stream.good) && FD_ISSET(server->raft_stream.fd, &readfds)) { numready--; raft_ready = true; } stream_t s; for (s = server_used_chain; (s != NULL) && (numready > 0); s = s->next) { if (FD_ISSET(s->fd, &readfds)) { server_stream_handle(server, s); numready--; } } #endif server_close_bad_streams(server); server_flush(server); return raft_ready; }
static bool server_accept(server_t server) { int fd; stream_t s; debug("a new connection is queued\n"); fd = accept(server->listener, NULL, NULL); if (fd == -1) { shout("failed to accept a connection: %s\n", strerror(errno)); return false; } debug("a new connection fd=%d accepted\n", fd); if (!server->enabled) { shout("server disabled, disconnecting the accepted connection fd=%d\n", fd); // FIXME: redirect instead of disconnecting close(fd); return false; } s = server->free_chain; if (s == NULL) { s = malloc(sizeof(stream_data_t)); } else { server->free_chain = s->next; } /* add new stream */ s->next = server->used_chain; server->used_chain = s; stream_init(s, fd); return server_add_socket(server, fd, s); }
static bool stream_message_start(stream_t stream, unsigned int chan) { ShubMessageHdr *msg; if (!stream->good) { return false; } if (stream->output.curmessage) { shout("cannot start new message while the old one is unfinished\n"); stream->good = false; return false; } if (BUFFER_SIZE - stream->output.ready < sizeof(ShubMessageHdr)) { if (!stream_flush(stream)) { shout("failed to flush before starting new message\n"); stream->good = false; return false; } } msg = stream->output.curmessage = (ShubMessageHdr*)(stream->output.data + stream->output.ready); msg->size = 0; msg->code = 'r'; msg->chan = chan; return true; }
void server_disable(server_t server) { if (!server->enabled) return; server->enabled = false; shout("server disabled\n"); server_close_all_streams(server); shout("client connections closed\n"); }
static void select_next_server(void) { int orig_leader = leader; for (int i = 0; i < MAX_SERVERS; i++) { int idx = (orig_leader + i + 1) % MAX_SERVERS; HostPort *hp = servers + idx; if (hp->up) { leader = idx; shout("switched from server %d to server %d\n", orig_leader, leader); return; } } shout("all servers are down\n"); }
int raft_emit(raft_t r, raft_update_t update) { assert(r->leader == r->me); assert(r->role == LEADER); if (r->log.size == r->config.log_len) { int compacted = raft_compact(r); if (compacted > 1) { debug("compacted %d entries\n", compacted); } else { shout( "cannot emit new entries, the log is" " full and cannot be compacted\n" ); return -1; } } int newindex = RAFT_LOG_LAST_INDEX(r) + 1; raft_entry_t *e = &RAFT_LOG(r, newindex); e->term = r->term; assert(e->update.len == 0); assert(e->update.data == NULL); e->update.len = update.len; e->bytes = update.len; e->update.data = malloc(update.len); memcpy(e->update.data, update.data, update.len); r->log.size++; raft_beat(r, NOBODY); raft_reset_timer(r); return newindex; }
static bool stream_flush(stream_t stream) { int tosend = stream->output.ready; if (tosend == 0) { // nothing to do return true; } char *cursor = stream->output.data; while (tosend > 0) { // repeat sending until we send everything int sent = send(stream->fd, cursor, tosend, 0); if (sent == -1) { shout("failed to flush the stream\n"); stream->good = false; return false; } sent_messages += 1; sent_bytes += sent; cursor += sent; tosend -= sent; assert(tosend >= 0); } stream->output.ready = 0; ShubMessageHdr *msg = stream->output.curmessage; if (msg) { // move the unfinished message to the start of the buffer memmove(stream->output.data, msg, msg->size + sizeof(ShubMessageHdr)); stream->output.curmessage = (ShubMessageHdr*)stream->output.data; } return true; }
void listBoxItemDoubleClicked(int row, const MouseEvent& e) { //prints all the data for the shout clicked on list<Shout*>::iterator itr; itr = allShouts.begin(); advance(itr, row); time_t temp = (*itr)->getDate(); struct tm * timeinfo; char date[80]; timeinfo = localtime( &temp ); strftime(date, 80, "%c" , timeinfo); std::string message = (*itr)->getSender() + "\n"; message += date; message += "\n\n\""; if(!(*itr)->getIsPublic()) message += "@" + (*itr)->getmMention() + " "; message += (*itr)->getMessage() + "\""; String shout(message.c_str()); //popup box with the details for the shout AlertWindow::showMessageBoxAsync(AlertWindow::NoIcon, T("Shout:"),shout); }
int main(object me, string arg) { if( !wizardp(me) || !arg) return 0; shout(HIW"【传音】" + me->name() + HIW + "[" + me->query("id") + "]" + HIW + "纵声长啸:" + arg + "\n"NOR); write(HIW"你纵声长啸:" + arg + "\n"NOR); return 1; }
static bool query(Message *msg, Message *answer, int timeout_ms) { timeout_t timeout; if (timeout_ms < 0) { while (true) { timeout_start(&timeout, 100); if (try_query(msg, answer, &timeout)) { return true; } else { disconnect_leader(); } } } else { timeout_start(&timeout, timeout_ms); TIMEOUT_LOOP_START(&timeout); { if (try_query(msg, answer, &timeout)) { return true; } else { disconnect_leader(); } } TIMEOUT_LOOP_END(&timeout); } shout("query failed after %d ms\n", timeout_elapsed_ms(&timeout)); return false; }
void server_loop(server_t server) { while (1) { int i; fd_set readfds = server->all; debug("selecting\n"); int numready = select(server->maxfd + 1, &readfds, NULL, NULL, NULL); if (numready == -1) { shout("failed to select: %s\n", strerror(errno)); return; } if (FD_ISSET(server->listener, &readfds)) { numready--; server_accept(server); } for (i = 0; (i < server->streamsnum) && (numready > 0); i++) { stream_t stream = server->streams + i; if (FD_ISSET(stream->fd, &readfds)) { server_stream_handle(server, stream); numready--; } } server_close_bad_streams(server); server_flush(server); } }
static void socket_set_recv_timeout(int sock, int ms) { struct timeval tv; tv.tv_sec = ms / 1000; tv.tv_usec = ((ms % 1000) * 1000); if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) { shout("failed to set socket recv timeout: %s\n", strerror(errno)); } }
static void socket_set_reuseaddr(int sock) { int optval = 1; if (setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, (char const*)&optval, sizeof(optval) ) == -1) { shout("failed to set socket to reuseaddr: %s\n", strerror(errno)); } }
int main(object me, string arg) { if( !wizardp(me) ) return notify_fail("─┐╟░╘▌▓╗┐к╖┼═ц╝╥ shoutбг\n"); if (!arg) return notify_fail("─у╧ы╥к┤є╜╨╩▓ўс?\n"); shout( HIW+me->name() + "╫▌╔∙│д╨ег║" + arg + "\n"NOR); write(HIW"─у╫▌╔∙│д╨ег║" + arg + "\n"NOR); return 1; }
int main(object me, string arg) { if( !wizardp(me) ) return notify_fail("目前暂不开放玩家 shout。\n"); if (!arg) return notify_fail("你想要大叫什么?\n"); shout( HIW + me->name() + HIW + "纵声长啸:" + arg + "\n"NOR); write(HIW"你纵声长啸:" + arg + "\n"NOR); return 1; }
int main(object me, string arg) { if( !wizardp(me) ) return notify_fail("目前暂不开放玩家 shout。\n"); if (!arg) return notify_fail("你想要宣布什么?\n"); shout(HIG "【巫师公告】:" + arg + "\n" NOR); write(HIG "【巫师公告】:" + arg + "\n" NOR); return 1; }
void send(string str, mixed x) { #ifndef __NO_ENVIRONMENT__ if (x) say(str, x); else say(str); #else shout(str); #endif }
void OutLame::flush() { if(!enc_flags) { error("Lame encoder is not initialized"); return; } lock(); encoded = lame_encode_flush_nogap (enc_flags,(unsigned char*)buffer,ENCBUFFER_SIZE); shout(); unlock(); }
void IChatHandler::_sCmd_Shout(IWorldPlayer *_player, const char *_message) { FDASSERT(_player); FDASSERT(_message); if (*_message == ' ') ++_message; //send shout(_player, _message); }
int main(object me, string arg) { // if( !wizardp(me) && me->query("id") != "sad" && // me->query("id") != "long" ) if (!wizardp(me) && member_array(me->query("id"), SHOUT_LIST) == -1) return notify_fail("目前暂不开放玩家 shout。\n"); if (!arg) return notify_fail("你想要大叫什么?\n"); shout( me->name() + "纵声长啸:" + arg + "\n"); write("你纵声长啸:" + arg + "\n"); return 1; }
static bool raft_peers_init(raft_t raft) { raft->peers = malloc(raft->config.peernum_max * sizeof(raft_peer_t)); if (!raft->peers) { shout("failed to allocate memory for raft peers\n"); return false; } for (int i = 0; i < raft->config.peernum_max; i++) { raft_peer_init(raft->peers + i); } return true; }
void end_quest(int step) { switch(step) { case 0: tell_room(TO,"The mist slowly takes the shape of a man, as " + "a portal of light opens over the altar. The " + "shining spirit turns towards you and smiles.\n"); set_alarm(4.0,0.0,"end_quest",1); break; case 1: tell_room(TO,"Suddenly a scream echoes through the " + "valley!!\n"); (CASTLE + "Room/lev5_nirachs_study")->orb_destroyed(); set_alarm(3.0,0.0,"end_quest",2); break; case 2: tell_room(TO,"A shriek is heard, and you feel something " + "is approaching the shrine.\n"); set_alarm(3.0,0.0,"end_quest",3); break; case 3: tell_room(TO,"The shriek grows louder and louder. " + "Whatever it is, it's very close now.\n"); set_alarm(4.0,0.0,"end_quest",4); break; case 4: tell_room(TO,"Suddenly a black shape sweeps into the room! " + "You recoil in horror as two eyes filled with " + "hatred glare at you!\n"); set_alarm(4.0,0.0,"end_quest",5); break; case 5: tell_room(TO,"Alywin speaks: Nirach, it is time for us to " + "depart. Nirachs spirit obeys, walking over to " + "stand behind the radiant apparition of Alywin. " + "Alywin turns towards you.\n"); set_alarm(6.0,0.0,"end_quest",6); break; case 6: tell_room(TO,"Alywin says: I thank you, " + quest_solver + ", for freeing me and thereby banishing Nirach. " + "For years to come, the people of this valley " + "will praise your name in song, and my prayers " + "will be with you, always... Farewell.\n"); set_alarm(7.0,0.0,"end_quest",7); break; case 7: tell_room(TO,"Alywin bows to you and the two spirits walk " + "into the light. The light fades away. You look " + "around and wonder if this was all just a " + "dream...\n"); shout("The bells in Oakdale chime for freedom.\n" + C(quest_solver) + " has banished Nirach.\n" + "The people of the valley are free!!!\n"); quest_solver = 0; break; default: quest_solver = 0; // Just in case break; } }