📄 net.c
字号:
"%s: my_net_write: fcntl returned error %d, aborting thread\n", my_progname,vio_errno(net->vio));#endif /* EXTRA_DEBUG */ net->error=2; /* Close socket */ goto end; } } retry_count=0; continue; } } else#endif /* (!defined(__WIN__) && !defined(__EMX__)) */ if (thr_alarm_in_use(&alarmed) && !thr_got_alarm(&alarmed) && interrupted) { if (retry_count++ < RETRY_COUNT) continue;#ifdef EXTRA_DEBUG fprintf(stderr, "%s: write looped, aborting thread\n", my_progname);#endif /* EXTRA_DEBUG */ }#if defined(THREAD_SAFE_CLIENT) && !defined(MYSQL_SERVER) if (vio_errno(net->vio) == SOCKET_EINTR) { DBUG_PRINT("warning",("Interrupted write. Retrying...")); continue; }#endif /* defined(THREAD_SAFE_CLIENT) && !defined(MYSQL_SERVER) */ net->error=2; /* Close socket */#ifdef MYSQL_SERVER net->last_errno= (interrupted ? ER_NET_WRITE_INTERRUPTED : ER_NET_ERROR_ON_WRITE);#endif /* MYSQL_SERVER */ break; } pos+=length; statistic_add(bytes_sent,length,&LOCK_bytes_sent); }#ifndef __WIN__ end:#endif#ifdef HAVE_COMPRESS if (net->compress) my_free((char*) packet,MYF(0));#endif if (thr_alarm_in_use(&alarmed)) { thr_end_alarm(&alarmed); vio_blocking(net->vio, net_blocking); } net->reading_or_writing=0; DBUG_RETURN(((int) (pos != end)));}/******************************************************************************* Read something from server/clinet*****************************************************************************/#ifdef MYSQL_SERVER/* Help function to clear the commuication buffer when we get a too big packet*/static void my_net_skip_rest(NET *net, ulong remain, thr_alarm_t *alarmed){ ALARM alarm_buff; uint retry_count=0; if (!thr_alarm_in_use(&alarmed)) { if (!thr_alarm(alarmed,net->timeout,&alarm_buff) || (!vio_is_blocking(net->vio) && vio_blocking(net->vio,TRUE) < 0)) return; /* Can't setup, abort */ } while (remain > 0) { ulong length; if ((int) (length=vio_read(net->vio,(char*) net->buff,remain)) <= 0L) { my_bool interrupted = vio_should_retry(net->vio); if (!thr_got_alarm(alarmed) && interrupted) { /* Probably in MIT threads */ if (retry_count++ < RETRY_COUNT) continue; } return; } remain -=(ulong) length; statistic_add(bytes_received,(ulong) length,&LOCK_bytes_received); }}#endif /* MYSQL_SERVER */static uintmy_real_read(NET *net, ulong *complen){ uchar *pos; long length; uint i,retry_count=0; ulong len=packet_error; thr_alarm_t alarmed;#if (!defined(__WIN__) && !defined(__EMX__) && !defined(OS2)) || defined(MYSQL_SERVER) ALARM alarm_buff;#endif my_bool net_blocking=vio_is_blocking(net->vio); ulong remain= (net->compress ? NET_HEADER_SIZE+COMP_HEADER_SIZE : NET_HEADER_SIZE); *complen = 0; net->reading_or_writing=1; thr_alarm_init(&alarmed);#ifdef MYSQL_SERVER if (net_blocking) thr_alarm(&alarmed,net->timeout,&alarm_buff);#endif /* MYSQL_SERVER */ pos = net->buff + net->where_b; /* net->packet -4 */ for (i=0 ; i < 2 ; i++) { while (remain > 0) { /* First read is done with non blocking mode */ if ((int) (length=vio_read(net->vio,(char*) pos,remain)) <= 0L) { my_bool interrupted = vio_should_retry(net->vio); DBUG_PRINT("info",("vio_read returned %d, errno: %d", length, vio_errno(net->vio)));#if (!defined(__WIN__) && !defined(__EMX__) && !defined(OS2)) || defined(MYSQL_SERVER) /* We got an error that there was no data on the socket. We now set up an alarm to not 'read forever', change the socket to non blocking mode and try again */ if ((interrupted || length == 0) && !thr_alarm_in_use(&alarmed)) { if (!thr_alarm(&alarmed,net->timeout,&alarm_buff)) /* Don't wait too long */ { if (!vio_is_blocking(net->vio)) { while (vio_blocking(net->vio,TRUE) < 0) { if (vio_should_retry(net->vio) && retry_count++ < RETRY_COUNT) continue; DBUG_PRINT("error", ("fcntl returned error %d, aborting thread", vio_errno(net->vio)));#ifdef EXTRA_DEBUG fprintf(stderr, "%s: read: fcntl returned error %d, aborting thread\n", my_progname,vio_errno(net->vio));#endif /* EXTRA_DEBUG */ len= packet_error; net->error=2; /* Close socket */#ifdef MYSQL_SERVER net->last_errno=ER_NET_FCNTL_ERROR;#endif goto end; } } retry_count=0; continue; } }#endif /* (!defined(__WIN__) && !defined(__EMX__)) || defined(MYSQL_SERVER) */ if (thr_alarm_in_use(&alarmed) && !thr_got_alarm(&alarmed) && interrupted) { /* Probably in MIT threads */ if (retry_count++ < RETRY_COUNT) continue;#ifdef EXTRA_DEBUG fprintf(stderr, "%s: read looped with error %d, aborting thread\n", my_progname,vio_errno(net->vio));#endif /* EXTRA_DEBUG */ }#if defined(THREAD_SAFE_CLIENT) && !defined(MYSQL_SERVER) if (vio_should_retry(net->vio)) { DBUG_PRINT("warning",("Interrupted read. Retrying...")); continue; }#endif DBUG_PRINT("error",("Couldn't read packet: remain: %d errno: %d length: %d alarmed: %d", remain,vio_errno(net->vio),length,alarmed)); len= packet_error; net->error=2; /* Close socket */#ifdef MYSQL_SERVER net->last_errno= (interrupted ? ER_NET_READ_INTERRUPTED : ER_NET_READ_ERROR);#endif goto end; } remain -= (ulong) length; pos+= (ulong) length; statistic_add(bytes_received,(ulong) length,&LOCK_bytes_received); } if (i == 0) { /* First parts is packet length */ ulong helping; if (net->buff[net->where_b + 3] != (uchar) net->pkt_nr) { if (net->buff[net->where_b] != (uchar) 255) { DBUG_PRINT("error", ("Packets out of order (Found: %d, expected %d)", (int) net->buff[net->where_b + 3], (uint) (uchar) net->pkt_nr));#ifdef EXTRA_DEBUG fprintf(stderr,"Packets out of order (Found: %d, expected %d)\n", (int) net->buff[net->where_b + 3], (uint) (uchar) net->pkt_nr);#endif } len= packet_error;#ifdef MYSQL_SERVER net->last_errno=ER_NET_PACKETS_OUT_OF_ORDER;#endif goto end; } net->pkt_nr++;#ifdef HAVE_COMPRESS if (net->compress) { /* complen is > 0 if package is really compressed */ *complen=uint3korr(&(net->buff[net->where_b + NET_HEADER_SIZE])); }#endif len=uint3korr(net->buff+net->where_b); helping = max(len,*complen) + net->where_b; /* The necessary size of net->buff */ if (helping >= net->max_packet) { /* We must allocate one extra byte for the end null */ if (net_realloc(net,helping+1)) {#ifdef MYSQL_SERVER if (i == 1) my_net_skip_rest(net, len, &alarmed);#endif len= packet_error; /* Return error */ goto end; } } pos=net->buff + net->where_b; remain = len; } }end: if (thr_alarm_in_use(&alarmed)) { thr_end_alarm(&alarmed); vio_blocking(net->vio, net_blocking); } net->reading_or_writing=0; return(len);}uintmy_net_read(NET *net){ ulong len,complen;#ifdef HAVE_COMPRESS if (!net->compress) {#endif len = my_real_read (net,&complen); net->read_pos = net->buff + net->where_b; if (len != packet_error) net->read_pos[len]=0; /* Safeguard for mysql_use_result */ return len;#ifdef HAVE_COMPRESS } if (net->remain_in_buf) net->buff[net->buf_length - net->remain_in_buf]=net->save_char; for (;;) { if (net->remain_in_buf) { uchar *pos = net->buff + net->buf_length - net->remain_in_buf; if (net->remain_in_buf >= 4) { net->length = uint3korr(pos); if (net->length <= net->remain_in_buf - 4) { /* We have a full packet */ len=net->length; net->remain_in_buf -= net->length + 4; net->read_pos=pos + 4; break; /* We have a full packet */ } } /* Move data down to read next data packet after current one */ if (net->buf_length != net->remain_in_buf) { memmove(net->buff,pos,net->remain_in_buf); net->buf_length=net->remain_in_buf; } net->where_b=net->buf_length; } else { net->where_b=0; net->buf_length=0; } if ((len = my_real_read(net,&complen)) == packet_error) break; if (my_uncompress((byte*) net->buff + net->where_b, &len, &complen)) { len= packet_error; net->error=2; /* caller will close socket */#ifdef MYSQL_SERVER net->last_errno=ER_NET_UNCOMPRESS_ERROR;#endif break; } net->buf_length+=len; net->remain_in_buf+=len; } if (len != packet_error) { net->save_char= net->read_pos[len]; /* Must be saved */ net->read_pos[len]=0; /* Safeguard for mysql_use_result */ } return len;#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -