Streamline modutil_get_complete_packets code. No external impact.
This commit is contained in:
@ -25,6 +25,7 @@
|
||||
* Date Who Description
|
||||
* 04/06/14 Mark Riddoch Initial implementation
|
||||
* 24/10/14 Massimiliano Pinto Added modutil_send_mysql_err_packet, modutil_create_mysql_err_msg
|
||||
* 04/01/16 Martin Brampton Streamline code in modutil_get_complete_packets
|
||||
*
|
||||
* @endverbatim
|
||||
*/
|
||||
@ -555,65 +556,54 @@ return_packetbuf:
|
||||
* @param p_readbuf Buffer to split, set to NULL if no partial packets are left
|
||||
* @return Head of the chain of complete packets, all in a single, contiguous buffer
|
||||
*/
|
||||
GWBUF* modutil_get_complete_packets(GWBUF** p_readbuf)
|
||||
GWBUF* modutil_get_complete_packets(GWBUF **p_readbuf)
|
||||
{
|
||||
GWBUF *buff = NULL, *packet;
|
||||
GWBUF *complete_part = NULL;
|
||||
uint8_t *ptr;
|
||||
uint32_t len,blen,total = 0;
|
||||
uint32_t len, blen, total = 0;
|
||||
|
||||
if (p_readbuf == NULL || (*p_readbuf) == NULL ||
|
||||
gwbuf_length(*p_readbuf) < 3)
|
||||
/** Give up if the parameter is not a pointer to a pointer or
|
||||
* the total buffer length is less than the 3 bytes needed to
|
||||
* hold a packet length. */
|
||||
if(p_readbuf == NULL || (*p_readbuf) == NULL ||
|
||||
(blen = gwbuf_length(*p_readbuf)) < 3)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
packet = gwbuf_make_contiguous(*p_readbuf);
|
||||
packet->next = NULL;
|
||||
*p_readbuf = packet;
|
||||
ptr = (uint8_t*)packet->start;
|
||||
len = gw_mysql_get_byte3(ptr) + 4;
|
||||
blen = gwbuf_length(packet);
|
||||
*p_readbuf = gwbuf_make_contiguous(*p_readbuf);
|
||||
ptr = (uint8_t*)(*p_readbuf)->start;
|
||||
|
||||
if (len == blen)
|
||||
{
|
||||
*p_readbuf = NULL;
|
||||
return packet;
|
||||
}
|
||||
else if (len > blen)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (total + len < blen)
|
||||
/** We need at least 3 bytes of the packet header to know how long the next
|
||||
* packet is going to be, if we are going to cycle round again. */
|
||||
while(total + (len = gw_mysql_get_byte3(ptr) + 4) < (blen - 3))
|
||||
{
|
||||
ptr += len;
|
||||
total += len;
|
||||
|
||||
/** We need at least 3 bytes of the packet header to know how long the whole
|
||||
* packet is going to be. */
|
||||
if (total + 3 >= blen)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
len = gw_mysql_get_byte3(ptr) + 4;
|
||||
}
|
||||
|
||||
/** Full packets only, return original */
|
||||
if (total + len == blen)
|
||||
total += len;
|
||||
/* If total has overshot the buffer(s) return null, this is an error. */
|
||||
if (total > blen)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
/** Full packets only, return original and null the passed buffer */
|
||||
if(total == blen)
|
||||
{
|
||||
GWBUF *packet = *p_readbuf;
|
||||
*p_readbuf = NULL;
|
||||
return packet;
|
||||
}
|
||||
|
||||
/** The next packet is a partial, split into complete and partial packets */
|
||||
if ((buff = gwbuf_clone_portion(packet, 0, total)) == NULL)
|
||||
if((complete_part = gwbuf_clone_portion(*p_readbuf,0,total)) == NULL)
|
||||
{
|
||||
MXS_ERROR("Failed to partially clone buffer.");
|
||||
MXS_ERROR("Failed to partially clone buffer while extracting complete packets.");
|
||||
return NULL;
|
||||
}
|
||||
gwbuf_consume(packet,total);
|
||||
return buff;
|
||||
*p_readbuf = gwbuf_consume(*p_readbuf,total);
|
||||
return complete_part;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -623,7 +613,7 @@ GWBUF* modutil_get_complete_packets(GWBUF** p_readbuf)
|
||||
* detection of partial packets in buffers.
|
||||
* @param reply Buffer to use
|
||||
* @param use_ok Whether the DEPRECATE_EOF flag is set
|
||||
* @param n_found If there were previous packets found
|
||||
* @param n_found If there were previous packets found
|
||||
* @return Number of EOF packets
|
||||
*/
|
||||
int
|
||||
|
||||
Reference in New Issue
Block a user