Use size_t in all functions

Some of the protocol modules use ssize_t instead of size_t.

Split the function that counts the number of response packets a session
command will receive into two parts. This allows it to be reused
elsewhere.
This commit is contained in:
Markus Mäkelä
2017-03-26 10:50:46 +03:00
parent d8abefff5f
commit 1ba399a62a
3 changed files with 35 additions and 37 deletions

View File

@ -277,7 +277,7 @@ typedef struct server_command_st
{ {
mysql_server_cmd_t scom_cmd; mysql_server_cmd_t scom_cmd;
int scom_nresponse_packets; /*< packets in response */ int scom_nresponse_packets; /*< packets in response */
ssize_t scom_nbytes_to_read; /*< bytes left to read in current packet */ size_t scom_nbytes_to_read; /*< bytes left to read in current packet */
struct server_command_st* scom_next; struct server_command_st* scom_next;
} server_command_t; } server_command_t;
@ -403,17 +403,13 @@ void protocol_remove_srv_command(MySQLProtocol* p);
bool protocol_waits_response(MySQLProtocol* p); bool protocol_waits_response(MySQLProtocol* p);
mysql_server_cmd_t protocol_get_srv_command(MySQLProtocol* p, bool removep); mysql_server_cmd_t protocol_get_srv_command(MySQLProtocol* p, bool removep);
int get_stmt_nresponse_packets(GWBUF* buf, mysql_server_cmd_t cmd); int get_stmt_nresponse_packets(GWBUF* buf, mysql_server_cmd_t cmd);
bool protocol_get_response_status (MySQLProtocol* p, int* npackets, ssize_t* nbytes); bool protocol_get_response_status (MySQLProtocol* p, int* npackets, size_t* nbytes);
void protocol_set_response_status (MySQLProtocol* p, int npackets, ssize_t nbytes); void protocol_set_response_status (MySQLProtocol* p, int npackets, size_t nbytes);
void protocol_archive_srv_command(MySQLProtocol* p); void protocol_archive_srv_command(MySQLProtocol* p);
char* create_auth_fail_str(char *username, char *hostaddr, bool password, char *db, int); char* create_auth_fail_str(char *username, char *hostaddr, bool password, char *db, int);
void init_response_status ( void init_response_status(GWBUF* buf, uint8_t cmd, int* npackets, size_t* nbytes);
GWBUF* buf,
mysql_server_cmd_t cmd,
int* npackets,
ssize_t* nbytes);
bool read_complete_packet(DCB *dcb, GWBUF **readbuf); bool read_complete_packet(DCB *dcb, GWBUF **readbuf);
bool gw_get_shared_session_auth_info(DCB* dcb, MYSQL_session* session); bool gw_get_shared_session_auth_info(DCB* dcb, MYSQL_session* session);
@ -432,4 +428,15 @@ bool mxs_mysql_is_ok_packet(GWBUF *buffer);
/** Check for result set */ /** Check for result set */
bool mxs_mysql_is_result_set(GWBUF *buffer); bool mxs_mysql_is_result_set(GWBUF *buffer);
/**
* @brief Calculate how many packets a session command will receive
*
* @param buf Buffer containing the response
* @param cmd Command that was executed
* @param npackets Pointer where the number of packets is stored
* @param nbytes Pointer where number of bytes is stored
*/
void mysql_num_response_packets(GWBUF *buf, uint8_t cmd,
int* npackets, size_t *nbytes);
MXS_END_DECLS MXS_END_DECLS

View File

@ -1425,11 +1425,11 @@ static GWBUF* process_response_data(DCB* dcb,
int nbytes_to_process) int nbytes_to_process)
{ {
int npackets_left = 0; /*< response's packet count */ int npackets_left = 0; /*< response's packet count */
ssize_t nbytes_left = 0; /*< nbytes to be read for the packet */ size_t nbytes_left = 0; /*< nbytes to be read for the packet */
MySQLProtocol* p; MySQLProtocol* p;
GWBUF* outbuf = NULL; GWBUF* outbuf = NULL;
int initial_packets = npackets_left; int initial_packets = npackets_left;
ssize_t initial_bytes = nbytes_left; size_t initial_bytes = nbytes_left;
/** Get command which was stored in gw_MySQLWrite_backend */ /** Get command which was stored in gw_MySQLWrite_backend */
p = DCB_PROTOCOL(dcb, MySQLProtocol); p = DCB_PROTOCOL(dcb, MySQLProtocol);
@ -1582,7 +1582,7 @@ static GWBUF* process_response_data(DCB* dcb,
static bool sescmd_response_complete(DCB* dcb) static bool sescmd_response_complete(DCB* dcb)
{ {
int npackets_left; int npackets_left;
ssize_t nbytes_left; size_t nbytes_left;
MySQLProtocol* p; MySQLProtocol* p;
bool succp; bool succp;

View File

@ -797,28 +797,14 @@ mysql_server_cmd_t protocol_get_srv_command(MySQLProtocol* p,
return cmd; return cmd;
} }
void mysql_num_response_packets(GWBUF *buf, uint8_t cmd, int *npackets, size_t *nbytes)
/**
* Examine command type and the readbuf. Conclude response
* packet count from the command type or from the first packet
* content.
* Fails if read buffer doesn't include enough data to read the
* packet length.
*/
void init_response_status(GWBUF* buf,
mysql_server_cmd_t cmd,
int* npackets,
ssize_t* nbytes_left)
{ {
uint8_t readbuf[3]; uint8_t readbuf[3];
int nparam = 0; int nparam = 0;
int nattr = 0; int nattr = 0;
uint8_t* data;
ss_dassert(gwbuf_length(buf) >= 3);
/** Read command byte */ /** Read command byte */
gwbuf_copy_data(buf, 4, 1, readbuf); gwbuf_copy_data(buf, MYSQL_HEADER_LEN, 1, readbuf);
if (readbuf[0] == 0xff) /*< error */ if (readbuf[0] == 0xff) /*< error */
{ {
@ -853,31 +839,36 @@ void init_response_status(GWBUF* buf,
} }
gwbuf_copy_data(buf, 0, 3, readbuf); gwbuf_copy_data(buf, 0, 3, readbuf);
*nbytes_left = gw_mysql_get_byte3(readbuf) + MYSQL_HEADER_LEN; *nbytes = gw_mysql_get_byte3(readbuf) + MYSQL_HEADER_LEN;
/** }
* There is at least one complete packet in the buffer so buffer is bigger
* than packet /**
*/ * Examine command type and the readbuf. Conclude response packet count
* from the command type or from the first packet content. Fails if read
* buffer doesn't include enough data to read the packet length.
*/
void init_response_status(GWBUF* buf, uint8_t cmd, int *npackets, size_t *nbytes_left)
{
ss_dassert(gwbuf_length(buf) >= 3);
mysql_num_response_packets(buf, cmd, npackets, nbytes_left);
ss_dassert(*nbytes_left > 0); ss_dassert(*nbytes_left > 0);
ss_dassert(*npackets > 0); ss_dassert(*npackets > 0);
} }
/** /**
* Read how many packets are left from current response and how many bytes there * Read how many packets are left from current response and how many bytes there
* is still to be read from the current packet. * is still to be read from the current packet.
*/ */
bool protocol_get_response_status(MySQLProtocol* p, bool protocol_get_response_status(MySQLProtocol* p,
int* npackets, int* npackets,
ssize_t* nbytes) size_t* nbytes)
{ {
bool succp; bool succp;
CHK_PROTOCOL(p); CHK_PROTOCOL(p);
*npackets = p->protocol_command.scom_nresponse_packets; *npackets = p->protocol_command.scom_nresponse_packets;
*nbytes = (ssize_t)p->protocol_command.scom_nbytes_to_read; *nbytes = (size_t)p->protocol_command.scom_nbytes_to_read;
if (*npackets < 0 && *nbytes == 0) if (*npackets < 0 && *nbytes == 0)
{ {
@ -893,7 +884,7 @@ bool protocol_get_response_status(MySQLProtocol* p,
void protocol_set_response_status(MySQLProtocol* p, void protocol_set_response_status(MySQLProtocol* p,
int npackets_left, int npackets_left,
ssize_t nbytes) size_t nbytes)
{ {
CHK_PROTOCOL(p); CHK_PROTOCOL(p);