void handle_client_write(void *arg) { client_t *cli = (client_t*) arg; buf_t *buf = cli->buf_send; apr_socket_t *sock = cli->pjob->pfd.desc.s; LOG_TRACE("handle client write"); apr_thread_mutex_lock(cli->comm->mx); apr_status_t status = buf_to_sock(buf, sock); SAFE_ASSERT(status == APR_SUCCESS || status == APR_EAGAIN); if (status == APR_SUCCESS || status == APR_EAGAIN) { int mode = (buf_sz_cnt(buf) > 0) ? (APR_POLLIN | APR_POLLOUT) : APR_POLLIN; if (buf_sz_cnt(buf) == 0) { poll_mgr_update_job(cli->pjob->mgr, cli->pjob, mode); } if (status == APR_EAGAIN) { LOG_DEBUG("cli poll on write, socket busy, resource " "temporarily unavailable. mode: %s, size in send buf: %d", mode == APR_POLLIN ? "only in" : "in and out", buf_sz_cnt(buf)); } else { LOG_DEBUG("cli write something out."); } } else if (status == APR_ECONNRESET) { LOG_ERROR("connection reset on write, is this a mac os?"); poll_mgr_remove_job(cli->pjob->mgr, cli->pjob); return; } else if (status == APR_EAGAIN) { SAFE_ASSERT(0); } else if (status == APR_EPIPE) { LOG_ERROR("on write, broken pipe, epipe error, is this a mac os?"); poll_mgr_remove_job(cli->pjob->mgr, cli->pjob); return; } else { LOG_ERROR("error code: %d, error message: %s", (int)status, apr_strerror(status, (char*)malloc(100), 100)); SAFE_ASSERT(status == APR_SUCCESS); } // poll_mgr_update_job(cli->pjob->mgr, cli->pjob, mode); apr_thread_mutex_unlock(cli->comm->mx); }
void handle_sconn_write(void* arg) { sconn_t *sconn = arg; // LOG_TRACE("write message on socket %x", pfd->desc.s); apr_thread_mutex_lock(sconn->comm->mx); buf_t *buf = sconn->buf_send; apr_socket_t *sock = sconn->pjob->pfd.desc.s; apr_status_t status = APR_SUCCESS; status = buf_to_sock(buf, sock); if (status == APR_SUCCESS || status == APR_EAGAIN) { int mode = (buf_sz_cnt(buf) > 0) ? (APR_POLLIN | APR_POLLOUT) : APR_POLLIN; // if (buf_sz_cnt(buf) == 0) { poll_mgr_update_job(sconn->pjob->mgr, sconn->pjob, mode); //} if (status == APR_EAGAIN) { LOG_DEBUG("sconn poll on write, socket busy, resource " "temporarily unavailable. mode: %s, size in send buf: %d", mode == APR_POLLIN ? "only in" : "in and out", buf_sz_cnt(buf)); } else { LOG_DEBUG("sconn write something out."); } } else if (status == APR_ECONNRESET) { LOG_INFO("connection reset on write, is this a mac os?"); poll_mgr_remove_job(sconn->pjob->mgr, sconn->pjob); } else if (status == APR_EAGAIN) { SAFE_ASSERT(0); } else if (status == APR_EPIPE) { LOG_INFO("on write, broken pipe, epipe error. remove poll job."); poll_mgr_remove_job(sconn->pjob->mgr, sconn->pjob); } else { LOG_ERROR("error code: %d, error message: %s", (int)status, apr_strerror(status, malloc(100), 100)); SAFE_ASSERT(status == APR_SUCCESS); } apr_thread_mutex_unlock(sconn->comm->mx); }
void handle_sconn_read(void* arg) { LOG_TRACE("sconn read handle read."); sconn_t *sconn = (sconn_t *) arg; buf_t *buf = sconn->buf_recv; apr_socket_t *sock = sconn->pjob->pfd.desc.s; apr_status_t status = APR_SUCCESS; //status = apr_socket_recv(pfd->desc.s, (char *)buf, &n); status = buf_from_sock(buf, sock); // invoke msg handling. size_t sz_c = 0; while ((sz_c = buf_sz_content(buf)) > SZ_SZMSG) { uint32_t sz_msg = 0; SAFE_ASSERT(buf_peek(buf, (uint8_t*)&sz_msg, SZ_SZMSG) == SZ_SZMSG); SAFE_ASSERT(sz_msg > 0); if (sz_c >= sz_msg + SZ_SZMSG + SZ_MSGID) { SAFE_ASSERT(buf_read(buf, (uint8_t*)&sz_msg, SZ_SZMSG) == SZ_SZMSG); msgid_t msgid = 0; SAFE_ASSERT(buf_read(buf, (uint8_t*)&msgid, SZ_MSGID) == SZ_MSGID); LOG_TRACE("next message size: %d, type: %x", (int32_t)sz_msg, (int32_t)msgid); rpc_state *state = malloc(sizeof(rpc_state)); state->sz_input = sz_msg; state->raw_input = malloc(sz_msg); state->raw_output = NULL; state->sz_output = 0; state->sconn = sconn; state->msgid = msgid; SAFE_ASSERT(buf_read(buf, (uint8_t*)state->raw_input, sz_msg) == sz_msg); // apr_thread_pool_push(tp_on_read_, (*(ctx->on_recv)), (void*)state, 0, NULL); // mpr_thread_pool_push(tp_read_, (void*)state); // apr_atomic_inc32(&n_data_recv_); //(*(ctx->on_recv))(NULL, state); // FIXME call if (msgid & NEW_THREAD_CALL) { apr_thread_pool_t *tp = sconn->tp; SAFE_ASSERT(tp != NULL); apr_thread_pool_push(tp, msg_call, state, 0, NULL); } else { msg_call(NULL, state); } } else { break; } } if (status == APR_SUCCESS) { } else if (status == APR_EOF) { LOG_INFO("sconn poll on read, received eof, close socket"); poll_mgr_remove_job(sconn->pjob->mgr, sconn->pjob); } else if (status == APR_ECONNRESET) { LOG_INFO("on read. connection reset, close socket"); poll_mgr_remove_job(sconn->pjob->mgr, sconn->pjob); // TODO [improve] you may retry connect } else if (status == APR_EAGAIN) { LOG_TRACE("sconn poll on read, socket busy, resource temporarily unavailable."); // do nothing. } else { LOG_ERROR("unkown error on poll reading. %s\n", apr_strerror(status, malloc(100), 100)); SAFE_ASSERT(0); } }
void handle_client_read(void *arg) { client_t *cli = (client_t*) arg; buf_t *buf = cli->buf_recv; apr_socket_t *sock = cli->pjob->pfd.desc.s; apr_status_t status = buf_from_sock(buf, sock); // invoke msg handling. size_t sz_c = 0; while ((sz_c = buf_sz_content(buf)) > SZ_SZMSG) { uint32_t sz_msg = 0; buf_peek(buf, (uint8_t*)&sz_msg, sizeof(sz_msg)); if (sz_c >= sz_msg + SZ_SZMSG + SZ_MSGID) { buf_read(buf, (uint8_t*)&sz_msg, SZ_SZMSG); msgid_t msgid = 0; buf_read(buf, (uint8_t*)&msgid, SZ_MSGID); rpc_state *state = (rpc_state*)malloc(sizeof(rpc_state)); state->sz_input = sz_msg; state->raw_input = (uint8_t *)malloc(sz_msg); // state->ctx = ctx; buf_read(buf, state->raw_input, sz_msg); // state->ctx = ctx; /* apr_thread_pool_push(tp_on_read_, (*(ctx->on_recv)), (void*)state, 0, NULL); // mpr_thread_pool_push(tp_read_, (void*)state); */ // apr_atomic_inc32(&n_data_recv_); //(*(ctx->on_recv))(NULL, state); // FIXME call rpc_state* (**fun)(void*) = NULL; size_t sz; mpr_hash_get(cli->comm->ht, &msgid, SZ_MSGID, (void**)&fun, &sz); SAFE_ASSERT(fun != NULL); LOG_TRACE("going to call function %x", *fun); // ctx->n_rpc++; // ctx->sz_recv += n; (**fun)(state); free(state->raw_input); free(state); } else { break; } } if (status == APR_SUCCESS) { } else if (status == APR_EOF) { LOG_DEBUG("cli poll on read, received eof, close socket"); poll_mgr_remove_job(cli->pjob->mgr, cli->pjob); } else if (status == APR_ECONNRESET) { LOG_ERROR("cli poll on read. connection reset."); poll_mgr_remove_job(cli->pjob->mgr, cli->pjob); // TODO [improve] you may retry connect } else if (status == APR_EAGAIN) { LOG_DEBUG("cli poll on read. read socket busy, resource temporarily unavailable."); // do nothing. } else { LOG_ERROR("unkown error on poll reading. %s\n", apr_strerror(status, (char*)malloc(100), 100)); SAFE_ASSERT(0); } }