void Connection_execute_start(Connection *connection, Executor *executor, Batch *batch, int ordinal) { DEBUG(("Connection exec\n")); connection->current_batch = batch; connection->current_executor = executor; if(CS_ABORTED == connection->state) { connection->state = CS_CLOSED; } ReplyParser_reset(connection->parser); Buffer_flip(Batch_write_buffer(batch)); DEBUG(("Connection exec write buff:\n")); #ifndef NDEBUG Buffer_dump(Batch_write_buffer(batch), 128); #endif //kick off writing: Connection_write_data(connection, ordinal); //kick off reading when socket becomes readable if(CS_CONNECTED == connection->state) { Executor_notify_event(connection->current_executor, connection, EVENT_READ, ordinal); } }
void Connection_read_data(Connection *connection, int ordinal) { if(CS_ABORTED == connection->state) { return; } DEBUG(("connection read data fd: %d\n", connection->sockfd)); assert(connection->current_batch != NULL); assert(connection->current_executor != NULL); assert(CS_CONNECTED == connection->state); Buffer *buffer = Batch_read_buffer(connection->current_batch); assert(buffer != NULL); while(Batch_has_command(connection->current_batch)) { DEBUG(("exec rp\n")); Reply *reply = NULL; ReplyParserResult rp_res = ReplyParser_execute(connection->parser, Buffer_data(buffer), Buffer_position(buffer), &reply); switch(rp_res) { case RPR_ERROR: { Connection_abort(connection, "result parse error"); return; } case RPR_MORE: { DEBUG(("read data RPR_MORE buf recv\n")); size_t res = Buffer_recv(buffer, connection->sockfd); DEBUG(("read data RPR_MORE res: %d\n", res)); #ifndef NDEBUG Buffer_dump(buffer, 128); #endif if(res == -1) { if(errno == EAGAIN) { DEBUG(("read data expecting more data in future, adding event\n")); Executor_notify_event(connection->current_executor, connection, EVENT_READ, ordinal); return; } else { Connection_abort(connection, "read error, errno: [%d] %s", errno, strerror(errno)); return; } } else if(res == 0) { Connection_abort(connection, "read eof"); return; } break; } case RPR_REPLY: { DEBUG(("read data RPR_REPLY batch add reply\n")); Batch_add_reply(connection->current_batch, reply); break; } default: Connection_abort(connection, "unexpected result parser result, rpres: %d", rp_res); return; } } }