1. 程式人生 > >mysql報文通訊:報文的接收和傳送函式

mysql報文通訊:報文的接收和傳送函式

文章中的原始碼來自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: 封裝了對各種套接字進行操作的介面,底層操作套接字進行實際的報文資料的收發