static void tcp_handle_write(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */, grpc_error *error) { grpc_tcp *tcp = (grpc_tcp *)arg; grpc_closure *cb; if (error != GRPC_ERROR_NONE) { cb = tcp->write_cb; tcp->write_cb = NULL; cb->cb(exec_ctx, cb->cb_arg, error); TCP_UNREF(exec_ctx, tcp, "write"); return; } if (!tcp_flush(tcp, &error)) { if (grpc_tcp_trace) { gpr_log(GPR_DEBUG, "write: delayed"); } grpc_fd_notify_on_write(exec_ctx, tcp->em_fd, &tcp->write_closure); } else { cb = tcp->write_cb; tcp->write_cb = NULL; if (grpc_tcp_trace) { const char *str = grpc_error_string(error); gpr_log(GPR_DEBUG, "write: %s", str); grpc_error_free_string(str); } grpc_closure_run(exec_ctx, cb, error); TCP_UNREF(exec_ctx, tcp, "write"); } }
static void tcp_handle_write(grpc_exec_ctx *exec_ctx, void *arg /* grpc_tcp */, bool success) { grpc_tcp *tcp = (grpc_tcp *)arg; flush_result status; grpc_closure *cb; if (!success) { cb = tcp->write_cb; tcp->write_cb = NULL; cb->cb(exec_ctx, cb->cb_arg, 0); TCP_UNREF(exec_ctx, tcp, "write"); return; } status = tcp_flush(tcp); if (status == FLUSH_PENDING) { grpc_fd_notify_on_write(exec_ctx, tcp->em_fd, &tcp->write_closure); } else { cb = tcp->write_cb; tcp->write_cb = NULL; GPR_TIMER_BEGIN("tcp_handle_write.cb", 0); cb->cb(exec_ctx, cb->cb_arg, status == FLUSH_DONE); GPR_TIMER_END("tcp_handle_write.cb", 0); TCP_UNREF(exec_ctx, tcp, "write"); } }
static void tcp_handle_write(void *arg /* grpc_tcp */, int success) { grpc_tcp *tcp = (grpc_tcp *)arg; grpc_endpoint_op_status status; grpc_iomgr_closure *cb; if (!success) { cb = tcp->write_cb; tcp->write_cb = NULL; cb->cb(cb->cb_arg, 0); TCP_UNREF(tcp, "write"); return; } GRPC_TIMER_BEGIN(GRPC_PTAG_TCP_CB_WRITE, 0); status = tcp_flush(tcp); if (status == GRPC_ENDPOINT_PENDING) { grpc_fd_notify_on_write(tcp->em_fd, &tcp->write_closure); } else { cb = tcp->write_cb; tcp->write_cb = NULL; cb->cb(cb->cb_arg, status == GRPC_ENDPOINT_DONE); TCP_UNREF(tcp, "write"); } GRPC_TIMER_END(GRPC_PTAG_TCP_CB_WRITE, 0); }
static void tcp_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, grpc_slice_buffer *buf, grpc_closure *cb) { grpc_tcp *tcp = (grpc_tcp *)ep; grpc_error *error = GRPC_ERROR_NONE; if (grpc_tcp_trace) { size_t i; for (i = 0; i < buf->count; i++) { char *data = grpc_dump_slice(buf->slices[i], GPR_DUMP_HEX | GPR_DUMP_ASCII); gpr_log(GPR_DEBUG, "WRITE %p (peer=%s): %s", tcp, tcp->peer_string, data); gpr_free(data); } } GPR_TIMER_BEGIN("tcp_write", 0); GPR_ASSERT(tcp->write_cb == NULL); if (buf->length == 0) { GPR_TIMER_END("tcp_write", 0); grpc_exec_ctx_sched(exec_ctx, cb, grpc_fd_is_shutdown(tcp->em_fd) ? tcp_annotate_error(GRPC_ERROR_CREATE("EOF"), tcp) : GRPC_ERROR_NONE, NULL); return; } tcp->outgoing_buffer = buf; tcp->outgoing_slice_idx = 0; tcp->outgoing_byte_idx = 0; if (!tcp_flush(tcp, &error)) { TCP_REF(tcp, "write"); tcp->write_cb = cb; if (grpc_tcp_trace) { gpr_log(GPR_DEBUG, "write: delayed"); } grpc_fd_notify_on_write(exec_ctx, tcp->em_fd, &tcp->write_closure); } else { if (grpc_tcp_trace) { const char *str = grpc_error_string(error); gpr_log(GPR_DEBUG, "write: %s", str); grpc_error_free_string(str); } grpc_exec_ctx_sched(exec_ctx, cb, error, NULL); } GPR_TIMER_END("tcp_write", 0); }
/* write data, escape any IACs */ void tcp_write_escape_iac __P3 (int,fd, const char *,data, int,len) { tcp_flush(); for (;;) { const char *iac = memchr(data, IAC, len); size_t l = iac ? (iac - data) + 1 : len; internal_tcp_raw_write(fd, data, l); if (iac == NULL) return; internal_tcp_raw_write(fd, iac, 1); len -= l; data = iac + 1; } }
static void tcp_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, gpr_slice_buffer *buf, grpc_closure *cb) { grpc_tcp *tcp = (grpc_tcp *)ep; flush_result status; if (grpc_tcp_trace) { size_t i; for (i = 0; i < buf->count; i++) { char *data = gpr_dump_slice(buf->slices[i], GPR_DUMP_HEX | GPR_DUMP_ASCII); gpr_log(GPR_DEBUG, "WRITE %p: %s", tcp, data); gpr_free(data); } } GPR_TIMER_BEGIN("tcp_write", 0); GPR_ASSERT(tcp->write_cb == NULL); if (buf->length == 0) { GPR_TIMER_END("tcp_write", 0); grpc_exec_ctx_enqueue(exec_ctx, cb, true, NULL); return; } tcp->outgoing_buffer = buf; tcp->outgoing_slice_idx = 0; tcp->outgoing_byte_idx = 0; status = tcp_flush(tcp); if (status == FLUSH_PENDING) { TCP_REF(tcp, "write"); tcp->write_cb = cb; grpc_fd_notify_on_write(exec_ctx, tcp->em_fd, &tcp->write_closure); } else { grpc_exec_ctx_enqueue(exec_ctx, cb, status == FLUSH_DONE, NULL); } GPR_TIMER_END("tcp_write", 0); }
static grpc_endpoint_op_status tcp_write(grpc_endpoint *ep, gpr_slice_buffer *buf, grpc_iomgr_closure *cb) { grpc_tcp *tcp = (grpc_tcp *)ep; grpc_endpoint_op_status status; if (grpc_tcp_trace) { size_t i; for (i = 0; i < buf->count; i++) { char *data = gpr_dump_slice(buf->slices[i], GPR_DUMP_HEX | GPR_DUMP_ASCII); gpr_log(GPR_DEBUG, "WRITE %p: %s", tcp, data); gpr_free(data); } } GRPC_TIMER_BEGIN(GRPC_PTAG_TCP_WRITE, 0); GPR_ASSERT(tcp->write_cb == NULL); if (buf->length == 0) { GRPC_TIMER_END(GRPC_PTAG_TCP_WRITE, 0); return GRPC_ENDPOINT_DONE; } tcp->outgoing_buffer = buf; tcp->outgoing_slice_idx = 0; tcp->outgoing_byte_idx = 0; status = tcp_flush(tcp); if (status == GRPC_ENDPOINT_PENDING) { TCP_REF(tcp, "write"); tcp->write_cb = cb; grpc_fd_notify_on_write(tcp->em_fd, &tcp->write_closure); } GRPC_TIMER_END(GRPC_PTAG_TCP_WRITE, 0); return status; }
/* * put data in the output buffer for transmission to the remote host */ void tcp_write __P2 (int,fd, char *,data) { char *iacs, *out; int len, space, iacp; len = strlen(data); if (tcp_main_fd != -1 && tcp_main_fd == fd) { if (linemode & LM_NOECHO) log_write("", 0, 1); /* log a newline only */ else log_write(data, len, 1); reprint_writeline(data); } /* must be AFTER reprint_writeline() */ if (CONN_LIST(tcp_fd).flags & SPAWN) status(1); else status(-1); if (fd != output_socket) { /* is there data to another socket? */ tcp_flush(); /* then flush it */ output_socket = fd; } out = output_buffer + output_len; space = BUFSIZE - output_len; while (len) { iacs = memchr(data, IAC, len); iacp = iacs ? iacs - data : len; if (iacp == 0) { /* we're at the IAC, send it */ if (space < 2) { tcp_flush(); out = output_buffer; space = BUFSIZE; } *out++ = (char)IAC; *out++ = (char)IAC; output_len += 2; space -= 2; data++; len--; continue; } while (space < iacp) { memcpy(out, data, space); data += space; len -= space; iacp -= space; output_len = BUFSIZE; tcp_flush(); out = output_buffer; space = BUFSIZE; } if (iacp /* && space >= iacp */ ) { memcpy(out, data, iacp); out += iacp; output_len += iacp; space -= iacp; data += iacp; len -= iacp; } } if (!space) { tcp_flush(); out = output_buffer; } *out++ = '\n'; output_len++; }
void tcp_raw_write __P3 (int,fd, const char *,data, int,len) { tcp_flush(); internal_tcp_raw_write(fd, data, len); }