Beispiel #1
0
static void _server(drizzle_con_st *con, server_state_st *state)
{
  drizzle_return_t ret;
  const drizzle_field_t fields[2]=
  {
    (drizzle_field_t)"test_field_1",
    (drizzle_field_t)"test_field_2"
  };
  const size_t field_sizes[2]= { 12, 12 };

  switch(state->state)
  {
  case SERVER_STATE_START:
    drizzle_con_set_protocol_version(con, 10);
    drizzle_con_set_server_version(con, "test_version");
    drizzle_con_set_thread_id(con, 1);
    drizzle_con_set_scramble(con, (const uint8_t *)"ABCDEFGHIJKLMNOPQRST");
    drizzle_con_set_capabilities(con, DRIZZLE_CAPABILITIES_NONE);
    drizzle_con_set_charset(con, 8);
    drizzle_con_set_status(con, DRIZZLE_CON_STATUS_NONE);
    drizzle_con_set_max_packet_size(con, DRIZZLE_MAX_PACKET_SIZE);

  case SERVER_STATE_HANDSHAKE_WRITE:
    drizzle_test("drizzle_handshake_server_write");
    ret= drizzle_handshake_server_write(con);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_HANDSHAKE_WRITE;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
    {
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
    }

  case SERVER_STATE_HANDSHAKE_READ:
    drizzle_test("drizzle_handshake_client_read");
    ret= drizzle_handshake_client_read(con);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_HANDSHAKE_READ;
      return;
    }
    else if (ret == DRIZZLE_RETURN_LOST_CONNECTION)
    {
      return;
    }
    else if (ret == DRIZZLE_RETURN_COULD_NOT_CONNECT)
    {
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
    {
      drizzle_test_error("returned %s (%s)", drizzle_con_error(con), drizzle_strerror(ret));
    }

    drizzle_test("drizzle_result_create");
    if (drizzle_result_create(con, &state->result) == NULL)
      drizzle_test_error("returned %s", drizzle_con_error(con));

  case SERVER_STATE_HANDSHAKE_RESULT:
    drizzle_test("drizzle_handshake_result_write");
    ret= drizzle_result_write(con, &state->result, true);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_HANDSHAKE_RESULT;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);

    drizzle_result_free(&state->result);

  case SERVER_STATE_COMMAND_BUFFER:
    drizzle_test("drizzle_con_command_buffer");
    state->data= drizzle_con_command_buffer(con, &state->command, &state->total,
                                            &ret);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_COMMAND_BUFFER;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);

    if (state->command != DRIZZLE_COMMAND_QUERY)
      drizzle_test_error("command was not a query");

    if (state->total != 6 && !memcmp(state->data, "SELECT", 6))
      drizzle_test_error("query doesn't match");

    if (state->data != NULL)
    {
      free(state->data);
      state->data= NULL;
    }

    drizzle_test("drizzle_result_create");
    if (drizzle_result_create(con, &state->result) == NULL)
      drizzle_test_error("returned %s", drizzle_con_error(con));

    drizzle_result_set_column_count(&state->result, 2);

  case SERVER_STATE_RESULT_HEADER:
    drizzle_test("drizzle_handshake_result_write");
    ret= drizzle_result_write(con, &state->result, false);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_RESULT_HEADER;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);

    drizzle_test("drizzle_column_create");
    if (drizzle_column_create(&state->result, &state->column) == NULL)
      drizzle_test_error("returned %s", drizzle_con_error(con));

    drizzle_column_set_catalog(&state->column, "test_catalog");
    drizzle_column_set_db(&state->column, "test_db");
    drizzle_column_set_table(&state->column, "test_table");
    drizzle_column_set_orig_table(&state->column, "test_orig_table");
    drizzle_column_set_name(&state->column, "test_column_1");
    drizzle_column_set_orig_name(&state->column, "test_orig_column_1");
    drizzle_column_set_charset(&state->column, 8);
    drizzle_column_set_size(&state->column, 32);
    drizzle_column_set_type(&state->column, DRIZZLE_COLUMN_TYPE_VARCHAR);
    drizzle_column_set_flags(&state->column, DRIZZLE_COLUMN_FLAGS_NONE);

  case SERVER_STATE_RESULT_COLUMN_1:
    drizzle_test("drizzle_column_write");
    ret= drizzle_column_write(&state->result, &state->column);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_RESULT_COLUMN_1;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);

    drizzle_column_set_name(&state->column, "test_column_2");
    drizzle_column_set_orig_name(&state->column, "test_orig_column_2");

  case SERVER_STATE_RESULT_COLUMN_2:
    drizzle_test("drizzle_column_write");
    ret= drizzle_column_write(&state->result, &state->column);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_RESULT_COLUMN_2;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);

    drizzle_test("drizzle_column_free");
    drizzle_column_free(&state->column);

    drizzle_result_set_eof(&state->result, true);

  case SERVER_STATE_RESULT_COLUMN_EOF:
    drizzle_test("drizzle_handshake_result_write");
    ret= drizzle_result_write(con, &state->result, false);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_RESULT_COLUMN_EOF;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);

    drizzle_result_calc_row_size(&state->result, fields, field_sizes);

  case SERVER_STATE_RESULT_ROW_1:
    ret= drizzle_row_write(&state->result);

  case SERVER_STATE_RESULT_ROW_1_FIELD_1:
    ret= drizzle_field_write(&state->result, fields[0], field_sizes[0],
                             field_sizes[0]);

  case SERVER_STATE_RESULT_ROW_1_FIELD_2:
    ret= drizzle_field_write(&state->result, fields[1], field_sizes[1],
                             field_sizes[1]);

  case SERVER_STATE_RESULT_ROW_EOF:
    drizzle_test("drizzle_handshake_result_write");
    ret= drizzle_result_write(con, &state->result, true);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_RESULT_ROW_EOF;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);

    drizzle_result_free(&state->result);

    state->state = SERVER_STATE_DONE;

  case SERVER_STATE_DONE:
    return;

  default:
    drizzle_test_error("invalid server state");
  }
}
Beispiel #2
0
void do_process(connection_t *con)
{
    drizzle_st          *drizzle;
    drizzle_return_t     ret;
    drizzle_con_st      *dc = con->dc;
    drizzle_command_t    command;
    uint8_t             *data = NULL;
    size_t               total;

    dbgin();

    drizzle = con->client->drizzle;

    /*
    ret = drizzle_con_wait(drizzle);
    if (ret != DRIZZLE_RETURN_OK) {
        printf("drizzle_con_wait failed:%s\n", drizzle_error(drizzle));
        return;
    }
    */

    while (1) {
        switch (con->state) {
            case INIT:
                /* Handshake packets */
                drizzle_con_set_protocol_version(dc, 10);
                drizzle_con_set_server_version(dc, "ev_drizzle_server demo 0.0.1");
                drizzle_con_set_thread_id(dc, 1);
                drizzle_con_set_scramble(dc, (const uint8_t *)"ABCDEFGHIJKLMNOPQRST");
                drizzle_con_set_capabilities(dc, DRIZZLE_CAPABILITIES_NONE);
                drizzle_con_set_charset(dc, 8);
                drizzle_con_set_status(dc, DRIZZLE_CON_STATUS_NONE);
                drizzle_con_set_max_packet_size(dc, DRIZZLE_MAX_PACKET_SIZE);

                con->state = HANDSHAKE_WRITE;
                break;

            case HANDSHAKE_WRITE:
                printf("handshake write\n");

                ret = drizzle_handshake_server_write(dc);

                if (ret == DRIZZLE_RETURN_IO_WAIT) {
                    return;
                }

                if (ret == DRIZZLE_RETURN_OK) {
                    stop_watch_write(con);
                    con->state = HANDSHAKE_READ;
                    break;
                }
                printf("drizzle_handshake_server_write error:%s",
                        drizzle_error(drizzle));
                exit(1);
                break;
            case HANDSHAKE_READ:
                printf("handshake read\n");

                /* prepare libdrizzle internal event */
                dc->revents |= POLLIN;

                ret = drizzle_handshake_client_read(dc);

                if (ret == DRIZZLE_RETURN_IO_WAIT) {
                    return;
                }

                if (ret == DRIZZLE_RETURN_OK) {
                    con->state = HANDSHAKE_DONE;
                    break;
                }
                printf("drizzle_handshake_server_read error:%s",
                        drizzle_error(drizzle));
                exit(1);
                break;

            case HANDSHAKE_DONE:
                printf("handshake done\n");
                con->result = (drizzle_result_st *)
                    malloc(sizeof(drizzle_result_st));
                if (drizzle_result_create(dc, con->result) == NULL) {
                    printf("drizzle_result_create error:%s",
                            drizzle_error(drizzle));
                    exit(1);
                }

                ret = drizzle_result_write(dc, con->result, true);
                if (ret != DRIZZLE_RETURN_OK) {
                    printf("drizzle_result_write error:%s",
                            drizzle_error(drizzle));
                    exit(1);
                }
                con->state = PREPARE_COMMAND;
                break;
            case PREPARE_COMMAND:
                printf("prepare command\n");

                /* prepare libdrizzle internal event */
                dc->revents |= POLLIN;

                drizzle_result_free(dc->result);

                data = (uint8_t *) drizzle_con_command_buffer(dc, &command,
                        &total, &ret);


                if (ret == DRIZZLE_RETURN_LOST_CONNECTION ||
                        (ret == DRIZZLE_RETURN_OK &&
                         command == DRIZZLE_COMMAND_QUIT))
                {
                    free(data);

                    do_finalize(con);
                    return;
                }

                if (ret == DRIZZLE_RETURN_IO_WAIT) {
                    return;
                }

                printf("Command data:%s\n", data);

                if (drizzle_result_create(dc, con->result) == NULL) {
                    printf("drizzle_result_create error:%s",
                            drizzle_error(drizzle));
                    exit(1);
                }

                if (command != DRIZZLE_COMMAND_QUERY) {
                    printf("command is not QUERY command\n");
                    ret = drizzle_result_write(dc, dc->result, true);
                    if (ret != DRIZZLE_RETURN_OK) {
                        printf("drizzle_result_write error:%s",
                                drizzle_error(drizzle));
                        exit(1);
                    }
                    continue;
                }

                ret = drizzle_result_write(dc, dc->result, true);
                if (ret != DRIZZLE_RETURN_OK) {
                    printf("drizzle_result_write error:%s",
                            drizzle_error(drizzle));
                    exit(1);
                }

                return;
                break;
            default:
                printf("Unsupported state %d\n", con->state);
                //sleep(10);
        }
    }

}
Beispiel #3
0
static void server(drizzle_st *drizzle, drizzle_con_st *con,
                   drizzle_result_st *result, drizzle_column_st *column)
{
    drizzle_return_t ret;
    drizzle_command_t command;
    uint8_t *data= NULL;
    size_t total;
    char *field[2];
    char field1[DRIZZLE_FIELD_MAX];
    char field2[DRIZZLE_FIELD_MAX];
    size_t size[2];
    uint64_t x;

    field[0]= field1;
    field[1]= field2;

    /* Handshake packets. */
    drizzle_con_set_protocol_version(con, 10);
    drizzle_con_set_server_version(con, "libdrizzle example 1.2.3");
    drizzle_con_set_thread_id(con, 1);
    drizzle_con_set_scramble(con, (const uint8_t *)"ABCDEFGHIJKLMNOPQRST");
    drizzle_con_set_capabilities(con, DRIZZLE_CAPABILITIES_NONE);
    drizzle_con_set_charset(con, 8);
    drizzle_con_set_status(con, DRIZZLE_CON_STATUS_NONE);
    drizzle_con_set_max_packet_size(con, DRIZZLE_MAX_PACKET_SIZE);

    ret= drizzle_handshake_server_write(con);
    DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write", drizzle)

    ret= drizzle_handshake_client_read(con);
    DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", drizzle)

    if (drizzle_result_create(con, result) == NULL)
        DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)

        ret= drizzle_result_write(con, result, true);
    DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)

    /* Command loop. */
    while (1)
    {
        drizzle_result_free(result);
        if (data != NULL)
            free(data);

        data= drizzle_con_command_buffer(con, &command, &total, &ret);
        if (ret == DRIZZLE_RETURN_LOST_CONNECTION ||
                (ret == DRIZZLE_RETURN_OK && command == DRIZZLE_COMMAND_QUIT))
        {
            if (data != NULL)
                free(data);
            return;
        }
        DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", drizzle)

        if (drizzle_result_create(con, result) == NULL)
            DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)

            if (command != DRIZZLE_COMMAND_QUERY)
            {
                ret= drizzle_result_write(con, result, true);
                DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
                continue;
            }

        drizzle_result_set_column_count(result, 2);

        ret= drizzle_result_write(con, result, false);
        DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)

        /* Columns. */
        if (drizzle_column_create(result, column) == NULL)
            DRIZZLE_RETURN_ERROR("drizzle_column_create", drizzle)

            drizzle_column_set_catalog(column, "default");
        drizzle_column_set_db(column, "drizzle_test_db");
        drizzle_column_set_table(column, "drizzle_test_table");
        drizzle_column_set_orig_table(column, "drizzle_test_table");
        drizzle_column_set_name(column, "test_column_1");
        drizzle_column_set_orig_name(column, "test_column_1");
        drizzle_column_set_charset(column, 8);
        drizzle_column_set_size(column, DRIZZLE_FIELD_MAX);
        drizzle_column_set_type(column, DRIZZLE_COLUMN_TYPE_VARCHAR);

        ret= drizzle_column_write(result, column);
        DRIZZLE_RETURN_CHECK(ret, "drizzle_column_write", drizzle)

        drizzle_column_set_name(column, "test_column_2");
        drizzle_column_set_orig_name(column, "test_column_2");

        ret= drizzle_column_write(result, column);
        DRIZZLE_RETURN_CHECK(ret, "drizzle_column_write", drizzle)

        drizzle_column_free(column);

        drizzle_result_set_eof(result, true);

        ret= drizzle_result_write(con, result, false);
        DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)

        /* Rows. */
        for (x= 0; x < DRIZZLE_RESULT_ROWS; x++)
        {
            size[0]= (size_t)snprintf(field[0], DRIZZLE_FIELD_MAX,
                                      "field %" PRIu64 "-1", x);
            if (size[0] >= DRIZZLE_FIELD_MAX)
                size[0]= DRIZZLE_FIELD_MAX - 1;

            size[1]= (size_t)snprintf(field[1], DRIZZLE_FIELD_MAX,
                                      "field %" PRIu64 "-2", x);
            if (size[1] >= DRIZZLE_FIELD_MAX)
                size[1]= DRIZZLE_FIELD_MAX - 1;

            /* This is needed for MySQL and old Drizzle protocol. */
            drizzle_result_calc_row_size(result, (drizzle_field_t *)field, size);

            ret= drizzle_row_write(result);
            DRIZZLE_RETURN_CHECK(ret, "drizzle_row_write", drizzle)

            /* Fields. */
            ret= drizzle_field_write(result, (drizzle_field_t)field[0], size[0],
                                     size[0]);
            DRIZZLE_RETURN_CHECK(ret, "drizzle_field_write", drizzle)

            ret= drizzle_field_write(result, (drizzle_field_t)field[1], size[1],
                                     size[1]);
            DRIZZLE_RETURN_CHECK(ret, "drizzle_field_write", drizzle)
        }

        ret= drizzle_result_write(con, result, true);
        DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
    }