static bool send_chunk(Request* request) { Py_ssize_t bytes_sent; static uv_buf_t resbuf; uv_write_t * wr; wr = (uv_buf_t*) malloc(sizeof *wr); //dprint("发送chunk:\n%s",PyString_AS_STRING(request->current_chunk) + request->current_chunk_p); dprint("发送大小:%d",PyString_GET_SIZE(request->current_chunk) - request->current_chunk_p); assert(request->current_chunk != NULL); assert(!(request->current_chunk_p == PyString_GET_SIZE(request->current_chunk) && PyString_GET_SIZE(request->current_chunk) != 0)); resbuf = uv_buf_init(PyString_AS_STRING(request->current_chunk) + request->current_chunk_p, PyString_GET_SIZE(request->current_chunk) - request->current_chunk_p); bytes_sent = uv_write( wr, request->ev_watcher, &resbuf, 1, after_write); if(bytes_sent == -1){ dprint("发送数据出错"); dprint("chunk:\n%s",PyString_AS_STRING(request->current_chunk) + request->current_chunk_p); return handle_nonzero_errno(request); } request->current_chunk_p += resbuf.len; if(request->current_chunk_p == PyString_GET_SIZE(request->current_chunk)) { Py_CLEAR(request->current_chunk); request->current_chunk_p = 0; return false; } //@@@@@@@@@@@@@@@@@ Py_CLEAR(request->current_chunk); //@@@@@@@@@@@@@@@@@ free(resbuf.base); return true; }
static bool send_chunk(Request* request) { Py_ssize_t bytes_sent; assert(request->current_chunk != NULL); assert(!(request->current_chunk_p == PyString_GET_SIZE(request->current_chunk) && PyString_GET_SIZE(request->current_chunk) != 0)); bytes_sent = write( request->client_fd, PyString_AS_STRING(request->current_chunk) + request->current_chunk_p, PyString_GET_SIZE(request->current_chunk) - request->current_chunk_p ); if(bytes_sent == -1) return handle_nonzero_errno(request); request->current_chunk_p += bytes_sent; if(request->current_chunk_p == PyString_GET_SIZE(request->current_chunk)) { Py_CLEAR(request->current_chunk); request->current_chunk_p = 0; return false; } return true; }
static bool do_sendfile(Request* request) { Py_ssize_t bytes_sent = portable_sendfile( request->client_fd, request->current_chunk_p /* current_chunk_p stores the file fd */ ); if(bytes_sent == -1) return handle_nonzero_errno(request); return bytes_sent != 0; }
static bool do_sendfile(Request* request) { Py_ssize_t bytes_sent = sendfile( request->client_fd, request->current_chunk_p, /* current_chunk_p stores the file fd */ NULL, SENDFILE_CHUNK_SIZE ); if(bytes_sent == -1) return handle_nonzero_errno(request); return bytes_sent != 0; }