3.8.1 VBUF, VSTRING和VSTREAM结构体

来源:互联网 发布:ubuntu caffe 安装 编辑:程序博客网 时间:2024/06/01 18:20

postfix定义了VSTRING和VSTREAM“层次结构”,用来操作字符串与流:

struct VBUF {   int     flags;                    /* status, see below */   unsigned char *data;                /*variable-length buffer */   ssize_t len;                         /*buffer length */   ssize_t cnt;                         /*bytes left to read/write */   unsigned char *ptr;                   /*read/write position */   VBUF_GET_READY_FN get_ready; /*read buffer empty action */   VBUF_PUT_READY_FN put_ready; /*write buffer full action */   VBUF_SPACE_FN space;           /*request for buffer space */};


VBUF结构体是VSTRING和VSTREAM的基石。VSTRING和VSTREAM均包含VBUF指针。

flags:错误信息标志。

data :内容指针。

len :缓冲区长度。

cnt :剩余读写数据量。

ptr:读写位置指针。

get_ready、put_ready,space:读、写、扩容回调函数接口。

 

/util/vstring.h

VSTRING封装VBUF结构体,加上了最大长度限制字段。

/*  *We can't allow bare VBUFs in the interface, because VSTRINGs have a  *specific initialization and destruction sequence.  */typedef struct VSTRING {   VBUF    vbuf;   ssize_t maxlen;} VSTRING;
/util/vstream.htypedef struct VSTREAM {   VBUF    buf;                    /* generic intelligentbuffer */   int     fd;                                  /* file handle, no 256 limit */   VSTREAM_RW_FN read_fn;             /*buffer fill action */   VSTREAM_RW_FN write_fn;            /*buffer fill action */   ssize_t req_bufsize;                  /*requested read/write buffer size */   void   *context;                       /* application context */   off_t   offset;                            /* cached seek info*/   char   *path;                            /* give it at leasttry */   int     read_fd;                       /* read channel(double-buffered) */   int     write_fd;                      /* write channel(double-buffered) */   VBUF    read_buf;                           /* read buffer(double-buffered) */   VBUF    write_buf;                          /* write buffer(double-buffered) */   pid_t   pid;                       /* vstream_popen/close()*/   VSTREAM_WAITPID_FN waitpid_fn;       /*vstream_popen/close() */   int     timeout;                       /* read/write timout */   VSTREAM_JMP_BUF *jbuf;               /*exception handling */   struct timeval iotime;               /*time of last fill/flush */   struct timeval time_limit;                 /*read/write time limit */} VSTREAM;


VSTREAM结构体共定义了3个VBUF缓冲区和两个回调函数接口。

VSTREAM封装比较特殊的是postfix根据自身需要定义的timeout和jbuf字段。

timeout字段定义流操作的超时值,比如需要为smtp会话设置超时。

postfix用sigsetjmp来做异常处理。VSTREAM_JMP_BUF即jmp_buf或sigjmp_buf:

#ifdef NO_SIGSETJMP#define VSTREAM_JMP_BUF  jmp_buf#else#define VSTREAM_JMP_BUF  sigjmp_buf#endif


0 0
原创粉丝点击