void __WriteCallback(ev::io &watcher) { if (writebuf.empty()) { char szSeq[20]; snprintf(szSeq, 20, "%d", total); writebuf = testdata + szSeq; } int nWritten = write(watcher.fd, writebuf.c_str()+writelen, writebuf.size()-writelen); if (nWritten <= 0) { if (errno != EAGAIN) { __Stats(); } return; } writebytes += nWritten; writelen += nWritten; if (writelen >= (int)writebuf.size()) { watcher.set(ev::READ); } }
/* Callback umum */ void callback(ev::io &watcher, int revents) { if (EV_ERROR & revents) { perror("got invalid event"); return; } if (revents & EV_READ) read_cb(watcher); if (revents & EV_WRITE) write_cb(watcher); if (write_queue.empty()) { io.set(ev::READ); } else { io.set(ev::READ|ev::WRITE); } }
/* Socket dapat ditulis */ void write_cb(ev::io &watcher) { if (write_queue.empty()) { io.set(ev::READ); return; } Buffer* buffer = write_queue.front(); ssize_t written = write(watcher.fd, buffer->dpos(), buffer->nbytes()); if (written < 0) { perror("read error"); return; } buffer->pos += written; if (buffer->nbytes() == 0) { write_queue.pop_front(); delete buffer; } /* Tutup dan bebaskan watcher saat selesai mengirim response (stateless) */ io.stop(); close(sfd); }