shapito: use msg type enums as protocol msg names

This commit is contained in:
Dmitry Simonenko 2018-03-01 15:39:44 +03:00
parent 7444e3de46
commit 64ff4221e0
5 changed files with 34 additions and 39 deletions

View File

@ -110,7 +110,7 @@ shapito_be_read_password(shapito_password_t *pw, char *data, uint32_t size)
int rc = shapito_read(&len, &data, &size);
if (shapito_unlikely(rc != 0))
return -1;
if (shapito_unlikely(header->type != 'p'))
if (shapito_unlikely(header->type != SHAPITO_FE_PASSWORD_MESSAGE))
return -1;
pw->password_len = len;
pw->password = malloc(len);
@ -128,7 +128,7 @@ shapito_be_read_query(char **query, uint32_t *query_len, char *data, uint32_t si
int rc = shapito_read(&len, &data, &size);
if (shapito_unlikely(rc != 0))
return -1;
if (shapito_unlikely(header->type != 'Q'))
if (shapito_unlikely(header->type != SHAPITO_FE_QUERY))
return -1;
*query = header->data;
*query_len = len;

View File

@ -24,7 +24,7 @@ shapito_be_write_error_as(shapito_stream_t *stream, char *severity, char *code,
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t) + size);
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'E');
shapito_stream_write8(stream, SHAPITO_BE_ERROR_RESPONSE);
shapito_stream_write32(stream, sizeof(uint32_t) + size);
shapito_stream_write8(stream, 'S');
shapito_stream_write(stream, severity, 6);
@ -71,7 +71,7 @@ shapito_be_write_notice(shapito_stream_t *stream, char *message, int len)
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t) + len + 1);
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'N');
shapito_stream_write8(stream, SHAPITO_BE_NOTICE_RESPONSE);
shapito_stream_write32(stream, sizeof(uint32_t) + len);
shapito_stream_write(stream, message, len);
shapito_stream_write8(stream, 0);
@ -84,7 +84,7 @@ shapito_be_write_authentication_ok(shapito_stream_t *stream)
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t) + sizeof(uint32_t));
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'R');
shapito_stream_write8(stream, SHAPITO_BE_AUTHENTICATION);
shapito_stream_write32(stream, sizeof(uint32_t) + sizeof(uint32_t));
shapito_stream_write32(stream, 0);
return 0;
@ -96,7 +96,7 @@ shapito_be_write_authentication_clear_text(shapito_stream_t *stream)
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t) + sizeof(uint32_t));
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'R');
shapito_stream_write8(stream, SHAPITO_BE_AUTHENTICATION);
shapito_stream_write32(stream, sizeof(uint32_t) + sizeof(uint32_t));
shapito_stream_write32(stream, 3);
return 0;
@ -108,7 +108,7 @@ shapito_be_write_authentication_md5(shapito_stream_t *stream, char salt[4])
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t) + sizeof(uint32_t) + 4);
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'R');
shapito_stream_write8(stream, SHAPITO_BE_AUTHENTICATION);
shapito_stream_write32(stream, sizeof(uint32_t) + sizeof(uint32_t) + 4);
shapito_stream_write32(stream, 5);
shapito_stream_write(stream, salt, 4);
@ -123,7 +123,7 @@ shapito_be_write_backend_key_data(shapito_stream_t *stream, uint32_t pid, uint32
sizeof(uint32_t));
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'K');
shapito_stream_write8(stream, SHAPITO_BE_BACKEND_KEY_DATA);
shapito_stream_write32(stream, sizeof(uint32_t) + sizeof(uint32_t) +
sizeof(uint32_t));
shapito_stream_write32(stream, pid);
@ -138,7 +138,7 @@ shapito_be_write_parameter_status(shapito_stream_t *stream, char *key, int key_l
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t) + key_len + value_len);
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'S');
shapito_stream_write8(stream, SHAPITO_BE_PARAMETER_STATUS);
shapito_stream_write32(stream, sizeof(uint32_t) + key_len + value_len);
shapito_stream_write(stream, key, key_len);
shapito_stream_write(stream, value, value_len);
@ -151,7 +151,7 @@ shapito_be_write_ready(shapito_stream_t *stream, uint8_t status)
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t) + sizeof(uint8_t));
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'Z');
shapito_stream_write8(stream, SHAPITO_BE_READY_FOR_QUERY);
shapito_stream_write32(stream, sizeof(uint32_t) + sizeof(uint8_t));
shapito_stream_write8(stream, status);
return 0;
@ -164,7 +164,7 @@ shapito_be_write_row_description(shapito_stream_t *stream)
if (shapito_unlikely(rc == -1))
return -1;
int position = shapito_stream_used(stream);
shapito_stream_write8(stream, 'T');
shapito_stream_write8(stream, SHAPITO_BE_ROW_DESCRIPTION);
shapito_stream_write32(stream, sizeof(uint32_t) + sizeof(uint16_t));
shapito_stream_write16(stream, 0);
return position;
@ -260,7 +260,7 @@ shapito_be_write_data_row(shapito_stream_t *stream)
if (shapito_unlikely(rc == -1))
return -1;
int position = shapito_stream_used(stream);
shapito_stream_write8(stream, 'D');
shapito_stream_write8(stream, SHAPITO_BE_DATA_ROW);
shapito_stream_write32(stream, sizeof(uint32_t) + sizeof(uint16_t));
shapito_stream_write16(stream, 0);
return position;
@ -299,7 +299,7 @@ shapito_be_write_complete(shapito_stream_t *stream, char *message, int len)
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t) + len);
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'C');
shapito_stream_write8(stream, SHAPITO_BE_COMMAND_COMPLETE);
shapito_stream_write32(stream, sizeof(uint32_t) + len);
shapito_stream_write(stream, message, len);
return 0;
@ -311,7 +311,7 @@ shapito_be_write_empty_query(shapito_stream_t *stream)
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t));
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'I');
shapito_stream_write8(stream, SHAPITO_BE_EMPTY_QUERY_RESPONSE);
shapito_stream_write32(stream, sizeof(uint32_t));
return 0;
}
@ -322,7 +322,7 @@ shapito_be_write_parse_complete(shapito_stream_t *stream)
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t));
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, '1');
shapito_stream_write8(stream, SHAPITO_BE_PARSE_COMPLETE);
shapito_stream_write32(stream, sizeof(uint32_t));
return 0;
}
@ -333,7 +333,7 @@ shapito_be_write_bind_complete(shapito_stream_t *stream)
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t));
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, '2');
shapito_stream_write8(stream, SHAPITO_BE_BIND_COMPLETE);
shapito_stream_write32(stream, sizeof(uint32_t));
return 0;
}
@ -344,7 +344,7 @@ shapito_be_write_portal_suspended(shapito_stream_t *stream)
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t));
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 's');
shapito_stream_write8(stream, SHAPITO_BE_PORTAL_SUSPENDED);
shapito_stream_write32(stream, sizeof(uint32_t));
return 0;
}
@ -355,7 +355,7 @@ shapito_be_write_no_data(shapito_stream_t *stream)
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t));
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'n');
shapito_stream_write8(stream, SHAPITO_BE_NO_DATA);
shapito_stream_write32(stream, sizeof(uint32_t));
return 0;
}

View File

@ -15,7 +15,7 @@ shapito_fe_read_ready(int *status, char *data, uint32_t size)
int rc = shapito_read(&len, &data, &size);
if (shapito_unlikely(rc != 0))
return -1;
if (shapito_unlikely(header->type != 'Z' || len != 1))
if (shapito_unlikely(header->type != SHAPITO_BE_READY_FOR_QUERY || len != 1))
return -1;
*status = header->data[0];
return 0;
@ -29,7 +29,7 @@ shapito_fe_read_key(shapito_key_t *key, char *data, uint32_t size)
int rc = shapito_read(&len, &data, &size);
if (shapito_unlikely(rc != 0))
return -1;
if (shapito_unlikely(header->type != 'K' || len != 8))
if (shapito_unlikely(header->type != SHAPITO_BE_BACKEND_KEY_DATA || len != 8))
return -1;
uint32_t pos_size = len;
char *pos = header->data;
@ -50,7 +50,7 @@ shapito_fe_read_auth(uint32_t *type, char salt[4], char *data, uint32_t size)
int rc = shapito_read(&len, &data, &size);
if (shapito_unlikely(rc != 0))
return -1;
if (shapito_unlikely(header->type != 'R'))
if (shapito_unlikely(header->type != SHAPITO_BE_AUTHENTICATION))
return -1;
uint32_t pos_size = len;
char *pos = header->data;
@ -85,7 +85,7 @@ shapito_fe_read_parameter(char *data, uint32_t size,
int rc = shapito_read(&len, &data, &size);
if (shapito_unlikely(rc != 0))
return -1;
if (shapito_unlikely(header->type != 'S'))
if (shapito_unlikely(header->type != SHAPITO_BE_PARAMETER_STATUS))
return -1;
uint32_t pos_size = len;
char *pos = header->data;
@ -112,7 +112,7 @@ shapito_fe_read_error(shapito_fe_error_t *error, char *data, uint32_t size)
int rc = shapito_read(&len, &data, &size);
if (shapito_unlikely(rc != 0))
return -1;
if (shapito_unlikely(header->type != 'E'))
if (shapito_unlikely(header->type != SHAPITO_BE_ERROR_RESPONSE))
return -1;
memset(error, 0, sizeof(*error));
uint32_t pos_size = len;

View File

@ -81,7 +81,7 @@ shapito_fe_write_terminate(shapito_stream_t *stream)
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t));
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'X');
shapito_stream_write8(stream, SHAPITO_FE_TERMINATE);
shapito_stream_write32(stream, sizeof(uint32_t));
return 0;
}
@ -92,7 +92,7 @@ shapito_fe_write_password(shapito_stream_t *stream, char *password, int len)
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t) + len);
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'p');
shapito_stream_write8(stream, SHAPITO_FE_PASSWORD_MESSAGE);
shapito_stream_write32(stream, sizeof(uint32_t) + len);
shapito_stream_write(stream, password, len);
return 0;
@ -104,7 +104,7 @@ shapito_fe_write_query(shapito_stream_t *stream, char *query, int len)
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t) + len);
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'Q');
shapito_stream_write8(stream, SHAPITO_FE_QUERY);
shapito_stream_write32(stream, sizeof(uint32_t) + len);
shapito_stream_write(stream, query, len);
return 0;
@ -121,7 +121,7 @@ shapito_fe_write_parse(shapito_stream_t *stream,
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t) + size);
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'P');
shapito_stream_write8(stream, SHAPITO_FE_PARSE);
shapito_stream_write32(stream, sizeof(uint32_t) + size);
shapito_stream_write(stream, operator_name, operator_len);
shapito_stream_write(stream, query, query_len);
@ -156,7 +156,7 @@ shapito_fe_write_bind(shapito_stream_t *stream,
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t) + size);
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'B');
shapito_stream_write8(stream, SHAPITO_FE_BIND);
shapito_stream_write32(stream, sizeof(uint32_t) + size);
shapito_stream_write(stream, portal_name, portal_len);
shapito_stream_write(stream, operator_name, operator_len);
@ -186,7 +186,7 @@ shapito_fe_write_describe(shapito_stream_t *stream, uint8_t type, char *name, in
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t) + size);
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'D');
shapito_stream_write8(stream, SHAPITO_FE_DESCRIBE);
shapito_stream_write32(stream, sizeof(uint32_t) + size);
shapito_stream_write8(stream, type);
shapito_stream_write(stream, name, name_len);
@ -200,7 +200,7 @@ shapito_fe_write_execute(shapito_stream_t *stream, char *portal, int portal_len,
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t) + size);
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'E');
shapito_stream_write8(stream, SHAPITO_FE_EXECUTE);
shapito_stream_write32(stream, sizeof(uint32_t) + size);
shapito_stream_write(stream, portal, portal_len);
shapito_stream_write32(stream, limit);
@ -213,7 +213,7 @@ shapito_fe_write_sync(shapito_stream_t *stream)
int rc = shapito_stream_ensure(stream, sizeof(shapito_header_t));
if (shapito_unlikely(rc == -1))
return -1;
shapito_stream_write8(stream, 'S');
shapito_stream_write8(stream, SHAPITO_FE_SYNC);
shapito_stream_write32(stream, sizeof(uint32_t));
return 0;
}

View File

@ -9,14 +9,9 @@
#define SHAPITO_API
#define shapito_likely(expr) \
__builtin_expect(!! (expr), 1)
#define shapito_unlikely(expr) \
__builtin_expect(!! (expr), 0)
#define shapito_packed \
__attribute__((packed))
#define shapito_likely(expr) __builtin_expect(!! (expr), 1)
#define shapito_unlikely(expr) __builtin_expect(!! (expr), 0)
#define shapito_packed __attribute__((packed))
#define shapito_container_of(ptr, type, field) \
((type*)((char*)(ptr) - __builtin_offsetof(type, field)))