mysql报文通信:报文的接收和发送函数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/spb3732/article/details/82889165

文章中的源码来自mysql 5.7.10版本。

1. NET结构

             该结构定义了网络连接描述符,本结构是实现接收和发送协议数据的核心。该结构的定义在include\mysql_com.h

                 定义如下:

typedef struct st_net {

  Vio *vio;

  unsigned char *buff,*buff_end,*write_pos,*read_pos;

  my_socket fd;                    /* For Perl DBI/dbd */

  /*

    The following variable is set if we are doing several queries in one

    command ( as in LOAD TABLE ... FROM MASTER ),

    and do not want to confuse the client with OK at the wrong time

  */

  unsigned long remain_in_buf,length, buf_length, where_b;

  unsigned long max_packet,max_packet_size;

  unsigned int pkt_nr,compress_pkt_nr;

  unsigned int write_timeout, read_timeout, retry_count;

  int fcntl;

  unsigned int *return_status;

  unsigned char reading_or_writing;

  char save_char;

  my_bool unused1; /* Please remove with the next incompatible ABI change */

  my_bool unused2; /* Please remove with the next incompatible ABI change */

  my_bool compress;

  my_bool unused3; /* Please remove with the next incompatible ABI change. */

  /*

    Pointer to query object in query cache, do not equal NULL (0) for

    queries in cache that have not stored its results yet

  */

  /*

    Unused, please remove with the next incompatible ABI change.

  */

  unsigned char *unused;

  unsigned int last_errno;

  unsigned char error; 

  my_bool unused4; /* Please remove with the next incompatible ABI change. */

  my_bool unused5; /* Please remove with the next incompatible ABI change. */

  /** Client library error message buffer. Actually belongs to struct MYSQL. */

  char last_error[MYSQL_ERRMSG_SIZE];

  /** Client library sqlstate buffer. Set along with the error message. */

  char sqlstate[SQLSTATE_LENGTH+1];

  /**

    Extension pointer, for the caller private use.

    Any program linking with the networking library can use this pointer,

    which is handy when private connection specific data needs to be

    maintained.

    The mysqld server process uses this pointer internally,

    to maintain the server internal instrumentation for the connection.

  */

  void *extension;

} NET;

该变量维护了一个buffer缓存,该缓存既用于读,也用于写。发送报文数据时先将数据包存入该buffer中,当该buffer填满后再发送。读包时。。。。。。

主要数据成员:

                    compress: 1表示启动压缩协议

                    read_or_writing:  0:没有进行I/O操作   1:读 2:写    

                    pkt_nr: 协议的包序号,防止包错乱

                    compress_pkt_nr:压缩包的包序号

                    return_status:指向当前连接的THD的server_status变量

                   max_packet_size: 值为数据库参数max_allowed_packet的值

                  max-packet: 最初设置为数据库参数net_buffer_length变量的值(缺省为16K),可以增大,最大值为数据库参数max-allowed-packet(缺省为4M)

                    retry_count:认定网络I/O操作失败前应该重试的次数,缺省为10。见函数(sql\net_serv.cc : net_should_retry)

                    remain_buffer:使用压缩协议时,可能会读取超过压缩长度之外的内容,该变量缓存这部分内容。不适用压缩协议时该值无效. 见函数(sql\net_serv.cc: my_net_read)

             buff_end:当前分配buffer的结尾处。初始化分配max_packet,该指针指向buffer + net->max_packet,见函数(sql\net_serv.cc: my_net_init)

                    length:当前包的长度,不包含包头

                    write_pos:目前缓冲区写入的内容。eg:写一个4字节的整数,该值+4

                    read_pos:目前从缓冲区读取的内容  

                   buffer_length:包缓冲区的长度。如果压缩,缓冲区包含下一个包的前移内容

                   where_b:缓冲区当前读位置的偏移量,值等于read_pos-buffer

                  save_char:缓冲区末尾为0能提高运算效率。给值用于保存原有的值,后续恢复。

                  vio: 封装了对各种套接字进行操作的接口,底层操作套接字进行实际的报文数据的收发

猜你喜欢

转载自blog.csdn.net/spb3732/article/details/82889165