#include <my_global.h>#include <mysql.h>#include <mysql_embed.h>#include <mysql_com.h>#include <mysqld_error.h>#include <my_sys.h>#include <m_string.h>#include <my_net.h>#include <violite.h>#include <signal.h>#include <errno.h>#include "thr_alarm.h"Include dependency graph for net.c:

Go to the source code of this file.
Defines | |
| #define | NO_ALARM |
| #define | DONT_USE_THR_ALARM |
| #define | update_statistics(A) |
| #define | thd_increment_bytes_sent(N) |
| #define | TEST_BLOCKING 8 |
| #define | MAX_PACKET_LENGTH (256L*256L*256L-1) |
| #define | NET_DATA_IS_READY_CAN_RETURN_MINUS_ONE |
Functions | |
| static my_bool | net_write_buff (NET *net, const char *packet, ulong len) |
| my_bool | my_net_init (NET *net, Vio *vio) |
| void | net_end (NET *net) |
| my_bool | net_realloc (NET *net, ulong length) |
| static int | net_data_is_ready (my_socket sd) |
| void | net_clear (NET *net) |
| my_bool | net_flush (NET *net) |
| my_bool | my_net_write (NET *net, const char *packet, ulong len) |
| my_bool | net_write_command (NET *net, uchar command, const char *header, ulong head_len, const char *packet, ulong len) |
| int | net_real_write (NET *net, const char *packet, ulong len) |
| static ulong | my_real_read (NET *net, ulong *complen) |
| ulong | my_net_read (NET *net) |
| #define MAX_PACKET_LENGTH (256L*256L*256L-1) |
Definition at line 110 of file net.c.
Referenced by my_net_read(), my_net_write(), net_write_buff(), and net_write_command().
| #define NET_DATA_IS_READY_CAN_RETURN_MINUS_ONE |
| #define thd_increment_bytes_sent | ( | N | ) |
| #define update_statistics | ( | A | ) |
Definition at line 117 of file net.c.
References st_net::buff, st_net::buff_end, COMP_HEADER_SIZE, st_net::compress, st_net::compress_pkt_nr, DBUG_ENTER, DBUG_RETURN, st_net::error, FALSE, st_net::fd, st_net::last_errno, st_net::last_error, st_net::max_packet, my_malloc(), my_net_local_init(), MY_WME, MYF, NET_HEADER_SIZE, st_net::no_send_eof, st_net::no_send_error, st_net::no_send_ok, st_net::pkt_nr, st_net::query_cache_query, st_net::read_pos, st_net::reading_or_writing, st_net::remain_in_buf, st_net::report_error, st_net::return_errno, st_net::return_status, TEST_BLOCKING, test_flags, st_net::vio, vio_blocking, vio_fastsend, vio_fd(), st_net::where_b, and st_net::write_pos.
00118 { 00119 DBUG_ENTER("my_net_init"); 00120 my_net_local_init(net); /* Set some limits */ 00121 if (!(net->buff=(uchar*) my_malloc((uint32) net->max_packet+ 00122 NET_HEADER_SIZE + COMP_HEADER_SIZE, 00123 MYF(MY_WME)))) 00124 DBUG_RETURN(1); 00125 net->buff_end=net->buff+net->max_packet; 00126 net->vio = vio; 00127 net->no_send_ok= net->no_send_eof= net->no_send_error= 0; 00128 net->error=0; net->return_errno=0; net->return_status=0; 00129 net->pkt_nr=net->compress_pkt_nr=0; 00130 net->write_pos=net->read_pos = net->buff; 00131 net->last_error[0]=0; 00132 net->compress=0; net->reading_or_writing=0; 00133 net->where_b = net->remain_in_buf=0; 00134 net->last_errno=0; 00135 net->query_cache_query=0; 00136 net->report_error= 0; 00137 00138 if (vio != 0) /* If real connection */ 00139 { 00140 net->fd = vio_fd(vio); /* For perl DBI/DBD */ 00141 #if defined(MYSQL_SERVER) && !defined(__WIN__) 00142 if (!(test_flags & TEST_BLOCKING)) 00143 { 00144 my_bool old_mode; 00145 vio_blocking(vio, FALSE, &old_mode); 00146 } 00147 #endif 00148 vio_fastsend(vio); 00149 } 00150 DBUG_RETURN(0); 00151 }
Here is the call graph for this function:

Definition at line 969 of file net.c.
References st_net::buf_length, st_net::buff, st_net::compress, ER_NET_UNCOMPRESS_ERROR, st_net::error, st_net::last_errno, MAX_PACKET_LENGTH, memmove, my_real_read(), my_uncompress(), NET_HEADER_SIZE, packet_error, st_net::read_pos, st_net::remain_in_buf, st_net::report_error, st_net::save_char, uint3korr, and st_net::where_b.
00970 { 00971 ulong len,complen; 00972 00973 #ifdef HAVE_COMPRESS 00974 if (!net->compress) 00975 { 00976 #endif 00977 len = my_real_read(net,&complen); 00978 if (len == MAX_PACKET_LENGTH) 00979 { 00980 /* First packet of a multi-packet. Concatenate the packets */ 00981 ulong save_pos = net->where_b; 00982 ulong total_length=0; 00983 do 00984 { 00985 net->where_b += len; 00986 total_length += len; 00987 len = my_real_read(net,&complen); 00988 } while (len == MAX_PACKET_LENGTH); 00989 if (len != packet_error) 00990 len+= total_length; 00991 net->where_b = save_pos; 00992 } 00993 net->read_pos = net->buff + net->where_b; 00994 if (len != packet_error) 00995 net->read_pos[len]=0; /* Safeguard for mysql_use_result */ 00996 return len; 00997 #ifdef HAVE_COMPRESS 00998 } 00999 else 01000 { 01001 /* We are using the compressed protocol */ 01002 01003 ulong buf_length; 01004 ulong start_of_packet; 01005 ulong first_packet_offset; 01006 uint read_length, multi_byte_packet=0; 01007 01008 if (net->remain_in_buf) 01009 { 01010 buf_length= net->buf_length; /* Data left in old packet */ 01011 first_packet_offset= start_of_packet= (net->buf_length - 01012 net->remain_in_buf); 01013 /* Restore the character that was overwritten by the end 0 */ 01014 net->buff[start_of_packet]= net->save_char; 01015 } 01016 else 01017 { 01018 /* reuse buffer, as there is nothing in it that we need */ 01019 buf_length= start_of_packet= first_packet_offset= 0; 01020 } 01021 for (;;) 01022 { 01023 ulong packet_len; 01024 01025 if (buf_length - start_of_packet >= NET_HEADER_SIZE) 01026 { 01027 read_length = uint3korr(net->buff+start_of_packet); 01028 if (!read_length) 01029 { 01030 /* End of multi-byte packet */ 01031 start_of_packet += NET_HEADER_SIZE; 01032 break; 01033 } 01034 if (read_length + NET_HEADER_SIZE <= buf_length - start_of_packet) 01035 { 01036 if (multi_byte_packet) 01037 { 01038 /* Remove packet header for second packet */ 01039 memmove(net->buff + first_packet_offset + start_of_packet, 01040 net->buff + first_packet_offset + start_of_packet + 01041 NET_HEADER_SIZE, 01042 buf_length - start_of_packet); 01043 start_of_packet += read_length; 01044 buf_length -= NET_HEADER_SIZE; 01045 } 01046 else 01047 start_of_packet+= read_length + NET_HEADER_SIZE; 01048 01049 if (read_length != MAX_PACKET_LENGTH) /* last package */ 01050 { 01051 multi_byte_packet= 0; /* No last zero len packet */ 01052 break; 01053 } 01054 multi_byte_packet= NET_HEADER_SIZE; 01055 /* Move data down to read next data packet after current one */ 01056 if (first_packet_offset) 01057 { 01058 memmove(net->buff,net->buff+first_packet_offset, 01059 buf_length-first_packet_offset); 01060 buf_length-=first_packet_offset; 01061 start_of_packet -= first_packet_offset; 01062 first_packet_offset=0; 01063 } 01064 continue; 01065 } 01066 } 01067 /* Move data down to read next data packet after current one */ 01068 if (first_packet_offset) 01069 { 01070 memmove(net->buff,net->buff+first_packet_offset, 01071 buf_length-first_packet_offset); 01072 buf_length-=first_packet_offset; 01073 start_of_packet -= first_packet_offset; 01074 first_packet_offset=0; 01075 } 01076 01077 net->where_b=buf_length; 01078 if ((packet_len = my_real_read(net,&complen)) == packet_error) 01079 return packet_error; 01080 if (my_uncompress((byte*) net->buff + net->where_b, &packet_len, 01081 &complen)) 01082 { 01083 net->error= 2; /* caller will close socket */ 01084 net->report_error= 1; 01085 #ifdef MYSQL_SERVER 01086 net->last_errno=ER_NET_UNCOMPRESS_ERROR; 01087 #endif 01088 return packet_error; 01089 } 01090 buf_length+=packet_len; 01091 } 01092 01093 net->read_pos= net->buff+ first_packet_offset + NET_HEADER_SIZE; 01094 net->buf_length= buf_length; 01095 net->remain_in_buf= (ulong) (buf_length - start_of_packet); 01096 len = ((ulong) (start_of_packet - first_packet_offset) - NET_HEADER_SIZE - 01097 multi_byte_packet); 01098 net->save_char= net->read_pos[len]; /* Must be saved */ 01099 net->read_pos[len]=0; /* Safeguard for mysql_use_result */ 01100 } 01101 #endif /* HAVE_COMPRESS */ 01102 return len; 01103 }
Here is the call graph for this function:

Definition at line 357 of file net.c.
References DBUG_DUMP, int3store, MAX_PACKET_LENGTH, NET_HEADER_SIZE, net_write_buff(), st_net::pkt_nr, test, unlikely, and st_net::vio.
00358 { 00359 uchar buff[NET_HEADER_SIZE]; 00360 if (unlikely(!net->vio)) /* nowhere to write */ 00361 return 0; 00362 /* 00363 Big packets are handled by splitting them in packets of MAX_PACKET_LENGTH 00364 length. The last packet is always a packet that is < MAX_PACKET_LENGTH. 00365 (The last packet may even have a length of 0) 00366 */ 00367 while (len >= MAX_PACKET_LENGTH) 00368 { 00369 const ulong z_size = MAX_PACKET_LENGTH; 00370 int3store(buff, z_size); 00371 buff[3]= (uchar) net->pkt_nr++; 00372 if (net_write_buff(net, (char*) buff, NET_HEADER_SIZE) || 00373 net_write_buff(net, packet, z_size)) 00374 return 1; 00375 packet += z_size; 00376 len-= z_size; 00377 } 00378 /* Write last packet */ 00379 int3store(buff,len); 00380 buff[3]= (uchar) net->pkt_nr++; 00381 if (net_write_buff(net,(char*) buff,NET_HEADER_SIZE)) 00382 return 1; 00383 #ifndef DEBUG_DATA_PACKETS 00384 DBUG_DUMP("packet_header",(char*) buff,NET_HEADER_SIZE); 00385 #endif 00386 return test(net_write_buff(net,packet,len)); 00387 }
Here is the call graph for this function:

Definition at line 773 of file net.c.
References __WIN__, st_net::buff, COMP_HEADER_SIZE, st_net::compress, DBUG_PRINT, ER_NET_FCNTL_ERROR, st_net::error, EXTRA_DEBUG, st_net::last_errno, my_progname, NET_HEADER_SIZE, packet_error, pos(), st_net::read_timeout, st_net::reading_or_writing, st_net::report_error, st_net::retry_count, thr_alarm, thr_alarm_in_use, thr_alarm_init, thr_got_alarm, TRUE, st_net::vio, vio_blocking, vio_errno, vio_is_blocking, vio_read, vio_should_retry, vio_timeout, and st_net::where_b.
Referenced by my_net_read().
00774 { 00775 uchar *pos; 00776 long length; 00777 uint i,retry_count=0; 00778 ulong len=packet_error; 00779 thr_alarm_t alarmed; 00780 #ifndef NO_ALARM 00781 ALARM alarm_buff; 00782 #endif 00783 my_bool net_blocking=vio_is_blocking(net->vio); 00784 uint32 remain= (net->compress ? NET_HEADER_SIZE+COMP_HEADER_SIZE : 00785 NET_HEADER_SIZE); 00786 *complen = 0; 00787 00788 net->reading_or_writing=1; 00789 thr_alarm_init(&alarmed); 00790 #ifndef NO_ALARM 00791 if (net_blocking) 00792 thr_alarm(&alarmed,net->read_timeout,&alarm_buff); 00793 #else 00794 vio_timeout(net->vio, 0, net->read_timeout); 00795 #endif /* NO_ALARM */ 00796 00797 pos = net->buff + net->where_b; /* net->packet -4 */ 00798 for (i=0 ; i < 2 ; i++) 00799 { 00800 while (remain > 0) 00801 { 00802 /* First read is done with non blocking mode */ 00803 if ((int) (length=vio_read(net->vio,(char*) pos,remain)) <= 0L) 00804 { 00805 my_bool interrupted = vio_should_retry(net->vio); 00806 00807 DBUG_PRINT("info",("vio_read returned %d, errno: %d", 00808 length, vio_errno(net->vio))); 00809 #if !defined(__WIN__) || defined(MYSQL_SERVER) 00810 /* 00811 We got an error that there was no data on the socket. We now set up 00812 an alarm to not 'read forever', change the socket to non blocking 00813 mode and try again 00814 */ 00815 if ((interrupted || length == 0) && !thr_alarm_in_use(&alarmed)) 00816 { 00817 if (!thr_alarm(&alarmed,net->read_timeout,&alarm_buff)) /* Don't wait too long */ 00818 { 00819 my_bool old_mode; 00820 while (vio_blocking(net->vio, TRUE, &old_mode) < 0) 00821 { 00822 if (vio_should_retry(net->vio) && 00823 retry_count++ < net->retry_count) 00824 continue; 00825 DBUG_PRINT("error", 00826 ("fcntl returned error %d, aborting thread", 00827 vio_errno(net->vio))); 00828 #ifdef EXTRA_DEBUG 00829 fprintf(stderr, 00830 "%s: read: fcntl returned error %d, aborting thread\n", 00831 my_progname,vio_errno(net->vio)); 00832 #endif /* EXTRA_DEBUG */ 00833 len= packet_error; 00834 net->error= 2; /* Close socket */ 00835 net->report_error= 1; 00836 #ifdef MYSQL_SERVER 00837 net->last_errno= ER_NET_FCNTL_ERROR; 00838 #endif 00839 goto end; 00840 } 00841 retry_count=0; 00842 continue; 00843 } 00844 } 00845 #endif /* (!defined(__WIN__) || defined(MYSQL_SERVER) */ 00846 if (thr_alarm_in_use(&alarmed) && !thr_got_alarm(&alarmed) && 00847 interrupted) 00848 { /* Probably in MIT threads */ 00849 if (retry_count++ < net->retry_count) 00850 continue; 00851 #ifdef EXTRA_DEBUG 00852 fprintf(stderr, "%s: read looped with error %d, aborting thread\n", 00853 my_progname,vio_errno(net->vio)); 00854 #endif /* EXTRA_DEBUG */ 00855 } 00856 #if defined(THREAD_SAFE_CLIENT) && !defined(MYSQL_SERVER) 00857 if (vio_should_retry(net->vio)) 00858 { 00859 DBUG_PRINT("warning",("Interrupted read. Retrying...")); 00860 continue; 00861 } 00862 #endif 00863 DBUG_PRINT("error",("Couldn't read packet: remain: %u errno: %d length: %ld", 00864 remain, vio_errno(net->vio), length)); 00865 len= packet_error; 00866 net->error= 2; /* Close socket */ 00867 net->report_error= 1; 00868 #ifdef MYSQL_SERVER 00869 net->last_errno= (vio_was_interrupted(net->vio) ? ER_NET_READ_INTERRUPTED : 00870 ER_NET_READ_ERROR); 00871 #endif 00872 goto end; 00873 } 00874 remain -= (uint32) length; 00875 pos+= (ulong) length; 00876 update_statistics(thd_increment_bytes_received(length)); 00877 } 00878 if (i == 0) 00879 { /* First parts is packet length */ 00880 ulong helping; 00881 DBUG_DUMP("packet_header",(char*) net->buff+net->where_b, 00882 NET_HEADER_SIZE); 00883 if (net->buff[net->where_b + 3] != (uchar) net->pkt_nr) 00884 { 00885 if (net->buff[net->where_b] != (uchar) 255) 00886 { 00887 DBUG_PRINT("error", 00888 ("Packets out of order (Found: %d, expected %u)", 00889 (int) net->buff[net->where_b + 3], 00890 net->pkt_nr)); 00891 #ifdef EXTRA_DEBUG 00892 fprintf(stderr,"Packets out of order (Found: %d, expected %d)\n", 00893 (int) net->buff[net->where_b + 3], 00894 (uint) (uchar) net->pkt_nr); 00895 #endif 00896 } 00897 len= packet_error; 00898 net->report_error= 1; 00899 #ifdef MYSQL_SERVER 00900 net->last_errno=ER_NET_PACKETS_OUT_OF_ORDER; 00901 #endif 00902 goto end; 00903 } 00904 net->compress_pkt_nr= ++net->pkt_nr; 00905 #ifdef HAVE_COMPRESS 00906 if (net->compress) 00907 { 00908 /* 00909 If the packet is compressed then complen > 0 and contains the 00910 number of bytes in the uncompressed packet 00911 */ 00912 *complen=uint3korr(&(net->buff[net->where_b + NET_HEADER_SIZE])); 00913 } 00914 #endif 00915 00916 len=uint3korr(net->buff+net->where_b); 00917 if (!len) /* End of big multi-packet */ 00918 goto end; 00919 helping = max(len,*complen) + net->where_b; 00920 /* The necessary size of net->buff */ 00921 if (helping >= net->max_packet) 00922 { 00923 if (net_realloc(net,helping)) 00924 { 00925 #if defined(MYSQL_SERVER) && !defined(NO_ALARM) 00926 if (!net->compress && 00927 !my_net_skip_rest(net, (uint32) len, &alarmed, &alarm_buff)) 00928 net->error= 3; /* Successfully skiped packet */ 00929 #endif 00930 len= packet_error; /* Return error and close connection */ 00931 goto end; 00932 } 00933 } 00934 pos=net->buff + net->where_b; 00935 remain = (uint32) len; 00936 } 00937 } 00938 00939 end: 00940 if (thr_alarm_in_use(&alarmed)) 00941 { 00942 my_bool old_mode; 00943 thr_end_alarm(&alarmed); 00944 vio_blocking(net->vio, net_blocking, &old_mode); 00945 } 00946 net->reading_or_writing=0; 00947 #ifdef DEBUG_DATA_PACKETS 00948 if (len != packet_error) 00949 DBUG_DUMP("data",(char*) net->buff+net->where_b, len); 00950 #endif 00951 return(len); 00952 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void net_clear | ( | NET * | net | ) |
Definition at line 276 of file net.c.
References st_net::buff, st_net::compress_pkt_nr, count, DBUG_ENTER, DBUG_PRINT, DBUG_VOID_RETURN, st_net::error, FALSE, st_net::max_packet, net_data_is_ready(), st_net::pkt_nr, st_vio::sd, TRUE, st_net::vio, vio_blocking, vio_description(), vio_read, and st_net::write_pos.
00277 { 00278 int count, ready; 00279 DBUG_ENTER("net_clear"); 00280 #if !defined(EMBEDDED_LIBRARY) 00281 while((ready= net_data_is_ready(net->vio->sd)) > 0) 00282 { 00283 /* The socket is ready */ 00284 if ((count= vio_read(net->vio, (char*) (net->buff), 00285 (uint32) net->max_packet)) > 0) 00286 { 00287 DBUG_PRINT("info",("skipped %d bytes from file: %s", 00288 count, vio_description(net->vio))); 00289 #ifdef EXTRA_DEBUG 00290 fprintf(stderr,"skipped %d bytes from file: %s\n", 00291 count, vio_description(net->vio)); 00292 #endif 00293 } 00294 else 00295 { 00296 DBUG_PRINT("info",("socket ready but only EOF to read - disconnected")); 00297 net->error= 2; 00298 break; 00299 } 00300 } 00301 #ifdef NET_DATA_IS_READY_CAN_RETURN_MINUS_ONE 00302 /* 'net_data_is_ready' returned "don't know" */ 00303 if (ready == -1) 00304 { 00305 /* Read unblocking to clear net */ 00306 my_bool old_mode; 00307 if (!vio_blocking(net->vio, FALSE, &old_mode)) 00308 { 00309 while ((count= vio_read(net->vio, (char*) (net->buff), 00310 (uint32) net->max_packet)) > 0) 00311 DBUG_PRINT("info",("skipped %d bytes from file: %s", 00312 count, vio_description(net->vio))); 00313 vio_blocking(net->vio, TRUE, &old_mode); 00314 } 00315 } 00316 #endif 00317 #endif 00318 net->pkt_nr=net->compress_pkt_nr=0; /* Ready for new command */ 00319 net->write_pos=net->buff; 00320 DBUG_VOID_RETURN; 00321 }
Here is the call graph for this function:

| static int net_data_is_ready | ( | my_socket | sd | ) | [static] |
Definition at line 217 of file net.c.
Referenced by net_clear().
00218 { 00219 #ifdef HAVE_POLL 00220 struct pollfd ufds; 00221 int res; 00222 00223 ufds.fd= sd; 00224 ufds.events= POLLIN | POLLPRI; 00225 if (!(res= poll(&ufds, 1, 0))) 00226 return 0; 00227 if (res < 0 || !(ufds.revents & (POLLIN | POLLPRI))) 00228 return 0; 00229 return 1; 00230 #else 00231 fd_set sfds; 00232 struct timeval tv; 00233 int res; 00234 00235 #ifndef __WIN__ 00236 /* Windows uses an _array_ of 64 fd's as default, so it's safe */ 00237 if (sd >= FD_SETSIZE) 00238 return -1; 00239 #define NET_DATA_IS_READY_CAN_RETURN_MINUS_ONE 00240 #endif 00241 00242 FD_ZERO(&sfds); 00243 FD_SET(sd, &sfds); 00244 00245 tv.tv_sec= tv.tv_usec= 0; 00246 00247 if ((res= select(sd+1, &sfds, NULL, NULL, &tv)) < 0) 00248 return 0; 00249 else 00250 return test(res ? FD_ISSET(sd, &sfds) : 0); 00251 #endif 00252 }
Here is the caller graph for this function:

| void net_end | ( | NET * | net | ) |
Definition at line 154 of file net.c.
References st_net::buff, DBUG_ENTER, DBUG_VOID_RETURN, MY_ALLOW_ZERO_PTR, my_free, and MYF.
00155 { 00156 DBUG_ENTER("net_end"); 00157 my_free((gptr) net->buff,MYF(MY_ALLOW_ZERO_PTR)); 00158 net->buff=0; 00159 DBUG_VOID_RETURN; 00160 }
Definition at line 326 of file net.c.
References st_net::buff, st_net::compress, st_net::compress_pkt_nr, DBUG_ENTER, DBUG_RETURN, error, net_real_write(), st_net::pkt_nr, test, and st_net::write_pos.
00327 { 00328 my_bool error= 0; 00329 DBUG_ENTER("net_flush"); 00330 if (net->buff != net->write_pos) 00331 { 00332 error=test(net_real_write(net,(char*) net->buff, 00333 (ulong) (net->write_pos - net->buff))); 00334 net->write_pos=net->buff; 00335 } 00336 /* Sync packet number if using compression */ 00337 if (net->compress) 00338 net->pkt_nr=net->compress_pkt_nr; 00339 DBUG_RETURN(error); 00340 }
Here is the call graph for this function:

Definition at line 542 of file net.c.
References COMP_HEADER_SIZE, st_net::compress, st_net::compress_pkt_nr, DBUG_ENTER, DBUG_PRINT, DBUG_RETURN, ER_NET_ERROR_ON_WRITE, ER_NET_WRITE_INTERRUPTED, ER_OUT_OF_RESOURCES, st_net::error, int3store, interrupted, st_net::last_errno, memcpy, my_compress(), my_free, my_malloc(), my_progname, MY_WME, MYF, NET_HEADER_SIZE, pos(), query_cache_insert(), st_net::query_cache_query, st_net::reading_or_writing, st_net::report_error, st_net::retry_count, SOCKET_EINTR, thd_increment_bytes_sent, thr_alarm, thr_alarm_in_use, thr_alarm_init, thr_end_alarm, thr_got_alarm, TRUE, update_statistics, st_net::vio, vio_blocking, vio_errno, vio_is_blocking, vio_should_retry, vio_timeout, vio_write, and st_net::write_timeout.
00543 { 00544 long int length; 00545 char *pos,*end; 00546 thr_alarm_t alarmed; 00547 #ifndef NO_ALARM 00548 ALARM alarm_buff; 00549 #endif 00550 uint retry_count=0; 00551 my_bool net_blocking = vio_is_blocking(net->vio); 00552 DBUG_ENTER("net_real_write"); 00553 00554 #if defined(MYSQL_SERVER) && defined(HAVE_QUERY_CACHE) \ 00555 && !defined(MYSQL_INSTANCE_MANAGER) 00556 if (net->query_cache_query != 0) 00557 query_cache_insert(net, packet, len); 00558 #endif 00559 00560 if (net->error == 2) 00561 DBUG_RETURN(-1); /* socket can't be used */ 00562 00563 net->reading_or_writing=2; 00564 #ifdef HAVE_COMPRESS 00565 if (net->compress) 00566 { 00567 ulong complen; 00568 uchar *b; 00569 uint header_length=NET_HEADER_SIZE+COMP_HEADER_SIZE; 00570 if (!(b=(uchar*) my_malloc((uint32) len + NET_HEADER_SIZE + 00571 COMP_HEADER_SIZE, MYF(MY_WME)))) 00572 { 00573 #ifdef MYSQL_SERVER 00574 net->last_errno= ER_OUT_OF_RESOURCES; 00575 net->error= 2; 00576 /* TODO is it needed to set this variable if we have no socket */ 00577 net->report_error= 1; 00578 #endif 00579 net->reading_or_writing= 0; 00580 DBUG_RETURN(1); 00581 } 00582 memcpy(b+header_length,packet,len); 00583 00584 if (my_compress((byte*) b+header_length,&len,&complen)) 00585 complen=0; 00586 int3store(&b[NET_HEADER_SIZE],complen); 00587 int3store(b,len); 00588 b[3]=(uchar) (net->compress_pkt_nr++); 00589 len+= header_length; 00590 packet= (char*) b; 00591 } 00592 #endif /* HAVE_COMPRESS */ 00593 00594 /* DBUG_DUMP("net",packet,len); */ 00595 #ifndef NO_ALARM 00596 thr_alarm_init(&alarmed); 00597 if (net_blocking) 00598 thr_alarm(&alarmed,(uint) net->write_timeout,&alarm_buff); 00599 #else 00600 alarmed=0; 00601 vio_timeout(net->vio, 1, net->write_timeout); 00602 #endif /* NO_ALARM */ 00603 00604 pos=(char*) packet; end=pos+len; 00605 while (pos != end) 00606 { 00607 if ((long) (length=vio_write(net->vio,pos,(uint32) (end-pos))) <= 0) 00608 { 00609 my_bool interrupted = vio_should_retry(net->vio); 00610 #if !defined(__WIN__) 00611 if ((interrupted || length==0) && !thr_alarm_in_use(&alarmed)) 00612 { 00613 if (!thr_alarm(&alarmed,(uint) net->write_timeout,&alarm_buff)) 00614 { /* Always true for client */ 00615 my_bool old_mode; 00616 while (vio_blocking(net->vio, TRUE, &old_mode) < 0) 00617 { 00618 if (vio_should_retry(net->vio) && retry_count++ < net->retry_count) 00619 continue; 00620 #ifdef EXTRA_DEBUG 00621 fprintf(stderr, 00622 "%s: my_net_write: fcntl returned error %d, aborting thread\n", 00623 my_progname,vio_errno(net->vio)); 00624 #endif /* EXTRA_DEBUG */ 00625 #ifdef MYSQL_SERVER 00626 net->last_errno= ER_NET_ERROR_ON_WRITE; 00627 #endif 00628 net->error= 2; /* Close socket */ 00629 net->report_error= 1; 00630 goto end; 00631 } 00632 retry_count=0; 00633 continue; 00634 } 00635 } 00636 else 00637 #endif /* !defined(__WIN__) */ 00638 if (thr_alarm_in_use(&alarmed) && !thr_got_alarm(&alarmed) && 00639 interrupted) 00640 { 00641 if (retry_count++ < net->retry_count) 00642 continue; 00643 #ifdef EXTRA_DEBUG 00644 fprintf(stderr, "%s: write looped, aborting thread\n", 00645 my_progname); 00646 #endif /* EXTRA_DEBUG */ 00647 } 00648 #if defined(THREAD_SAFE_CLIENT) && !defined(MYSQL_SERVER) 00649 if (vio_errno(net->vio) == SOCKET_EINTR) 00650 { 00651 DBUG_PRINT("warning",("Interrupted write. Retrying...")); 00652 continue; 00653 } 00654 #endif /* defined(THREAD_SAFE_CLIENT) && !defined(MYSQL_SERVER) */ 00655 net->error= 2; /* Close socket */ 00656 net->report_error= 1; 00657 #ifdef MYSQL_SERVER 00658 net->last_errno= (interrupted ? ER_NET_WRITE_INTERRUPTED : 00659 ER_NET_ERROR_ON_WRITE); 00660 #endif /* MYSQL_SERVER */ 00661 break; 00662 } 00663 pos+=length; 00664 update_statistics(thd_increment_bytes_sent(length)); 00665 } 00666 #ifndef __WIN__ 00667 end: 00668 #endif 00669 #ifdef HAVE_COMPRESS 00670 if (net->compress) 00671 my_free((char*) packet,MYF(0)); 00672 #endif 00673 if (thr_alarm_in_use(&alarmed)) 00674 { 00675 my_bool old_mode; 00676 thr_end_alarm(&alarmed); 00677 vio_blocking(net->vio, net_blocking, &old_mode); 00678 } 00679 net->reading_or_writing=0; 00680 DBUG_RETURN(((int) (pos != end))); 00681 }
Here is the call graph for this function:

Definition at line 165 of file net.c.
References st_net::buff, st_net::buff_end, COMP_HEADER_SIZE, DBUG_ENTER, DBUG_PRINT, DBUG_RETURN, ER_NET_PACKET_TOO_LARGE, ER_OUT_OF_RESOURCES, st_net::error, IO_SIZE, st_net::last_errno, st_net::max_packet, st_net::max_packet_size, my_realloc(), MY_WME, MYF, NET_HEADER_SIZE, st_net::report_error, and st_net::write_pos.
00166 { 00167 uchar *buff; 00168 ulong pkt_length; 00169 DBUG_ENTER("net_realloc"); 00170 DBUG_PRINT("enter",("length: %lu", length)); 00171 00172 if (length >= net->max_packet_size) 00173 { 00174 DBUG_PRINT("error", ("Packet too large. Max size: %lu", 00175 net->max_packet_size)); 00176 net->error= 1; 00177 net->report_error= 1; 00178 net->last_errno= ER_NET_PACKET_TOO_LARGE; 00179 DBUG_RETURN(1); 00180 } 00181 pkt_length = (length+IO_SIZE-1) & ~(IO_SIZE-1); 00182 /* 00183 We must allocate some extra bytes for the end 0 and to be able to 00184 read big compressed blocks 00185 */ 00186 if (!(buff=(uchar*) my_realloc((char*) net->buff, (uint32) pkt_length + 00187 NET_HEADER_SIZE + COMP_HEADER_SIZE, 00188 MYF(MY_WME)))) 00189 { 00190 net->error= 1; 00191 net->report_error= 1; 00192 net->last_errno= ER_OUT_OF_RESOURCES; 00193 DBUG_RETURN(1); 00194 } 00195 net->buff=net->write_pos=buff; 00196 net->buff_end=buff+(net->max_packet=pkt_length); 00197 DBUG_RETURN(0); 00198 }
Here is the call graph for this function:

Definition at line 487 of file net.c.
References st_net::buff, st_net::buff_end, st_net::compress, DBUG_DUMP, st_net::max_packet, MAX_PACKET_LENGTH, memcpy, net_real_write(), and st_net::write_pos.
Referenced by my_net_write(), and net_write_command().
00488 { 00489 ulong left_length; 00490 if (net->compress && net->max_packet > MAX_PACKET_LENGTH) 00491 left_length= MAX_PACKET_LENGTH - (net->write_pos - net->buff); 00492 else 00493 left_length= (ulong) (net->buff_end - net->write_pos); 00494 00495 #ifdef DEBUG_DATA_PACKETS 00496 DBUG_DUMP("data", packet, len); 00497 #endif 00498 if (len > left_length) 00499 { 00500 if (net->write_pos != net->buff) 00501 { 00502 /* Fill up already used packet and write it */ 00503 memcpy((char*) net->write_pos,packet,left_length); 00504 if (net_real_write(net,(char*) net->buff, 00505 (ulong) (net->write_pos - net->buff) + left_length)) 00506 return 1; 00507 net->write_pos= net->buff; 00508 packet+= left_length; 00509 len-= left_length; 00510 } 00511 if (net->compress) 00512 { 00513 /* 00514 We can't have bigger packets than 16M with compression 00515 Because the uncompressed length is stored in 3 bytes 00516 */ 00517 left_length= MAX_PACKET_LENGTH; 00518 while (len > left_length) 00519 { 00520 if (net_real_write(net, packet, left_length)) 00521 return 1; 00522 packet+= left_length; 00523 len-= left_length; 00524 } 00525 } 00526 if (len > net->max_packet) 00527 return net_real_write(net, packet, len) ? 1 : 0; 00528 /* Send out rest of the blocks as full sized blocks */ 00529 } 00530 memcpy((char*) net->write_pos,packet,len); 00531 net->write_pos+= len; 00532 return 0; 00533 }
Here is the call graph for this function:

Here is the caller graph for this function:

| my_bool net_write_command | ( | NET * | net, | |
| uchar | command, | |||
| const char * | header, | |||
| ulong | head_len, | |||
| const char * | packet, | |||
| ulong | len | |||
| ) |
Definition at line 418 of file net.c.
References DBUG_ENTER, DBUG_PRINT, DBUG_RETURN, int3store, MAX_PACKET_LENGTH, net_flush(), NET_HEADER_SIZE, net_write_buff(), st_net::pkt_nr, and test.
00421 { 00422 ulong length=len+1+head_len; /* 1 extra byte for command */ 00423 uchar buff[NET_HEADER_SIZE+1]; 00424 uint header_size=NET_HEADER_SIZE+1; 00425 DBUG_ENTER("net_write_command"); 00426 DBUG_PRINT("enter",("length: %lu", len)); 00427 00428 buff[4]=command; /* For first packet */ 00429 00430 if (length >= MAX_PACKET_LENGTH) 00431 { 00432 /* Take into account that we have the command in the first header */ 00433 len= MAX_PACKET_LENGTH - 1 - head_len; 00434 do 00435 { 00436 int3store(buff, MAX_PACKET_LENGTH); 00437 buff[3]= (uchar) net->pkt_nr++; 00438 if (net_write_buff(net,(char*) buff, header_size) || 00439 net_write_buff(net, header, head_len) || 00440 net_write_buff(net, packet, len)) 00441 DBUG_RETURN(1); 00442 packet+= len; 00443 length-= MAX_PACKET_LENGTH; 00444 len= MAX_PACKET_LENGTH; 00445 head_len= 0; 00446 header_size= NET_HEADER_SIZE; 00447 } while (length >= MAX_PACKET_LENGTH); 00448 len=length; /* Data left to be written */ 00449 } 00450 int3store(buff,length); 00451 buff[3]= (uchar) net->pkt_nr++; 00452 DBUG_RETURN(test(net_write_buff(net, (char*) buff, header_size) || 00453 (head_len && net_write_buff(net, (char*) header, head_len)) || 00454 net_write_buff(net, packet, len) || net_flush(net))); 00455 }
Here is the call graph for this function:

1.4.7

