static ssize_t plain_stream_file(IOBuf *iob, int fd, off_t len) { off_t sent = 0; off_t total = 0; off_t offset = 0; off_t block_size = MAX_SEND_BUFFER; int conn_fd = IOBuf_fd(iob); for(total = 0; fdwait(conn_fd, 'w') == 0 && total < len; total += sent) { block_size = (len - total) > block_size ? block_size : (len - total); sent = IOBuf_sendfile(conn_fd, fd, &offset, block_size); check(Register_write(iob->fd, sent) != -1, "Socket seems to be closed."); check_debug(sent > 0, "Client closed probably during sendfile on socket: %d from " "file %d", conn_fd, fd); } check(total <= len, "Wrote way too much, wrote %d but size was %zd", (int)total, len); check(total == len, "Sent other than expected, sent: %d, but expected: %zd", (int)total, len); return total; error: return -1; }
void Ram_write(Ram * ram,size_t iter,int val) { if(iter>=ram->memsize) { // avoid SIGSEGV iter=ram->memsize-1; } if(iter < ram->regs_count) { Register_write(ram->regs[iter],val); return; } iter-=ram->regs_count; ram->mem[iter]=val; }
/** * Wraps the usual send, so not much to it other than it'll avoid doing * any calls if the socket is already closed. */ int IOBuf_send(IOBuf *buf, char *data, int len) { int rc = 0; rc = buf->send(buf, data, len); if(rc >= 0) { check(Register_write(buf->fd, rc) != -1, "Failed to record write, must have died."); } else { buf->closed = 1; } return rc; error: return -1; }
static ssize_t ssl_stream_file(IOBuf *iob, int fd, off_t len) { ssize_t sent = 0; off_t total = 0; ssize_t amt = 0; ssize_t tosend = 0; int conn_fd = IOBuf_fd(iob); char buff[1024]; for(total = 0; fdwait(conn_fd, 'w') == 0 && total < len; total += tosend) { tosend = pread(fd, buff, sizeof(buff), total); check_debug(tosend > 0, "Came up short in reading file %d\n", fd); // We do this in case the file somehow lengthened on us. In general, // it shouldn't happen. if(tosend + total > len) tosend = len - total; sent = 0; while(sent < tosend) { amt = ssl_send(iob, buff, tosend); check_debug(amt > 0, "ssl_send failed in ssl_stream_file with " "return code %zd", amt); sent += amt; } check(Register_write(iob->fd, sent) != -1, "Failed to record write, must have died."); } check(total <= len, "Wrote way too much, wrote %d but size was %zd", (int)total, len); check(total == len, "Sent other than expected, sent: %d, but expected: %zd", (int)total, len); return total; error: return -1; }