590
553
uint i,retry_count=0;
591
554
ulong len=packet_error;
593
my_bool net_blocking=vio_is_blocking(net->vio);
594
555
uint32 remain= (net->compress ? NET_HEADER_SIZE+COMP_HEADER_SIZE :
557
/* Backup of the original SO_RCVTIMEO timeout */
558
struct timeval backtime;
598
net->reading_or_writing=1;
599
thr_alarm_init(&alarmed);
563
net->reading_or_writing= 1;
600
564
/* Read timeout is set in my_net_set_read_timeout */
602
pos = net->buff + net->where_b; /* net->packet -4 */
603
for (i=0 ; i < 2 ; i++)
566
pos = net->buff + net->where_b; /* net->packet -4 */
569
/* Check for error, currently assert */
571
struct timeval waittime;
574
waittime.tv_sec= net->read_timeout;
577
memset(&backtime, 0, sizeof(struct timeval));
578
error= getsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
581
error= setsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
582
&waittime, (socklen_t)sizeof(struct timeval));
586
for (i= 0; i < 2 ; i++)
607
/* First read is done with non blocking mode */
608
if ((long) (length= vio_read(net->vio, pos, remain)) <= 0L)
610
my_bool interrupted = vio_should_retry(net->vio);
612
DBUG_PRINT("info",("vio_read returned %ld errno: %d",
613
(long) length, vio_errno(net->vio)));
614
if (thr_alarm_in_use(&alarmed) && !thr_got_alarm(&alarmed) &&
616
{ /* Probably in MIT threads */
617
if (retry_count++ < net->retry_count)
620
fprintf(stderr, "%s: read looped with error %d, aborting thread\n",
621
my_progname,vio_errno(net->vio));
622
#endif /* EXTRA_DEBUG */
624
if (vio_errno(net->vio) == SOCKET_EINTR)
626
DBUG_PRINT("warning",("Interrupted read. Retrying..."));
629
DBUG_PRINT("error",("Couldn't read packet: remain: %u errno: %d length: %ld",
630
remain, vio_errno(net->vio), (long) length));
632
net->error= 2; /* Close socket */
633
net->last_errno= (vio_was_interrupted(net->vio) ?
634
ER_NET_READ_INTERRUPTED :
638
remain -= (uint32) length;
640
update_statistics(thd_increment_bytes_received(length));
643
{ /* First parts is packet length */
645
DBUG_DUMP("packet_header", net->buff+net->where_b,
647
if (net->buff[net->where_b + 3] != (uchar) net->pkt_nr)
649
if (net->buff[net->where_b] != (uchar) 255)
652
("Packets out of order (Found: %d, expected %u)",
653
(int) net->buff[net->where_b + 3],
657
fprintf(stderr,"Error: Packets out of order (Found: %d, expected %d)\n",
658
(int) net->buff[net->where_b + 3],
659
(uint) (uchar) net->pkt_nr);
665
/* Not a NET error on the client. XXX: why? */
668
net->compress_pkt_nr= ++net->pkt_nr;
672
If the packet is compressed then complen > 0 and contains the
673
number of bytes in the uncompressed packet
675
*complen=uint3korr(&(net->buff[net->where_b + NET_HEADER_SIZE]));
678
len=uint3korr(net->buff+net->where_b);
679
if (!len) /* End of big multi-packet */
681
helping = max(len,*complen) + net->where_b;
682
/* The necessary size of net->buff */
683
if (helping >= net->max_packet)
685
if (net_realloc(net,helping))
687
len= packet_error; /* Return error and close connection */
691
pos=net->buff + net->where_b;
692
remain = (uint32) len;
590
/* First read is done with non blocking mode */
591
if ((long) (length= vio_read(net->vio, pos, remain)) <= 0L)
593
bool interrupted = vio_should_retry(net->vio);
595
DBUG_PRINT("info",("vio_read returned %ld errno: %d",
596
(long) length, vio_errno(net->vio)));
598
{ /* Probably in MIT threads */
599
if (retry_count++ < net->retry_count)
602
if (vio_errno(net->vio) == SOCKET_EINTR)
607
net->error= 2; /* Close socket */
608
net->last_errno= (vio_was_interrupted(net->vio) ?
609
ER_NET_READ_INTERRUPTED :
613
remain -= (uint32) length;
615
update_statistics(thd_increment_bytes_received(length));
618
{ /* First parts is packet length */
621
if (net->buff[net->where_b + 3] != (uchar) net->pkt_nr)
624
/* Not a NET error on the client. XXX: why? */
627
net->compress_pkt_nr= ++net->pkt_nr;
631
If the packet is compressed then complen > 0 and contains the
632
number of bytes in the uncompressed packet
634
*complen=uint3korr(&(net->buff[net->where_b + NET_HEADER_SIZE]));
637
len=uint3korr(net->buff+net->where_b);
638
if (!len) /* End of big multi-packet */
640
helping = max(len,*complen) + net->where_b;
641
/* The necessary size of net->buff */
642
if (helping >= net->max_packet)
644
if (net_realloc(net,helping))
646
len= packet_error; /* Return error and close connection */
650
pos=net->buff + net->where_b;
651
remain = (uint32) len;
697
if (thr_alarm_in_use(&alarmed))
700
thr_end_alarm(&alarmed);
701
vio_blocking(net->vio, net_blocking, &old_mode);
703
net->reading_or_writing=0;
704
#ifdef DEBUG_DATA_PACKETS
705
if (len != packet_error)
706
DBUG_DUMP("data", net->buff+net->where_b, len);
656
error= setsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
657
&backtime, (socklen_t)sizeof(struct timeval));
659
net->reading_or_writing= 0;