/* close down the connection to a node and mark it as down */ void node_disconnect(merlin_node *node, const char *fmt, ...) { va_list ap; char *reason = NULL; if (node->state == STATE_CONNECTED) node_log_event_count(node, 1); if (fmt) { va_start(ap, fmt); vasprintf(&reason, fmt, ap); va_end(ap); } node_set_state(node, STATE_NONE, reason); if (reason) free(reason); node->last_recv = 0; /* avoid spurious close() errors while strace/valgrind debugging */ if (node->sock >= 0) close(node->sock); node->sock = -1; iocache_reset(node->ioc); }
unsigned long iocache_available(iocache *ioc) { if (!ioc || !ioc->ioc_buf || !ioc->ioc_bufsize || !ioc->ioc_buflen) return 0; if (ioc->ioc_buflen <= ioc->ioc_offset) { iocache_reset(ioc); return 0; } return ioc->ioc_buflen - ioc->ioc_offset; }
/** * Attempts to move data from the end to the beginning * of the ioc_buf, expelling old data to make more room * for new reads. */ static inline void iocache_move_data(iocache *ioc) { unsigned long available; if (!ioc->ioc_offset) return; /* nothing to do */ /* if we're fully read, we only have to reset the counters */ if (ioc->ioc_buflen <= ioc->ioc_offset) { iocache_reset(ioc); return; } available = ioc->ioc_buflen - ioc->ioc_offset; memmove(ioc->ioc_buf, ioc->ioc_buf + ioc->ioc_offset, available); ioc->ioc_offset = 0; ioc->ioc_buflen = available; }