QNX相關歷史文章:
Handling Read and Write Messages
這篇文章主要描述讀寫消息的處理。
1. Handling the _IO_READ message
io_read
處理函數負責處理_IO_READ
消息,將收到的數據返回給客戶端,比如當客戶端調用read()/readdir()/fread()/fgetc()
等接口時。
消息的定義如下:
struct _io_read {
uint16_t type;
uint16_t combine_len;
int32_t nbytes;
uint32_t xtype;
};
typedef union {
struct _io_read i;
/* unsigned char data[nbytes]; */
/* nbytes is returned with MsgReply */
} io_read_t;
-
combine_len
,用于消息組合; -
nbytes
,用于客戶端期望讀取的字節數; -
xtype
,這個字段下文會介紹到,主要用于擴展;
下邊是一個完整的消息讀取示例代碼:
#include <errno.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/iofunc.h>
#include <sys/dispatch.h>
int io_read (resmgr_context_t *ctp, io_read_t *msg, RESMGR_OCB_T *ocb);
static char *buffer = "Hello world\n";
static resmgr_connect_funcs_t connect_funcs;
static resmgr_io_funcs_t io_funcs;
static iofunc_attr_t attr;
main(int argc, char **argv)
{
/* declare variables we'll be using */
resmgr_attr_t resmgr_attr;
dispatch_t *dpp;
dispatch_context_t *ctp;
int id;
/* initialize dispatch interface */
if((dpp = dispatch_create()) == NULL) {
fprintf(stderr, "%s: Unable to allocate dispatch handle.\n",
argv[0]);
return EXIT_FAILURE;
}
/* initialize resource manager attributes */
memset(&resmgr_attr, 0, sizeof resmgr_attr);
resmgr_attr.nparts_max = 1;
resmgr_attr.msg_max_size = 2048;
/* initialize functions for handling messages */
iofunc_func_init(_RESMGR_CONNECT_NFUNCS, &connect_funcs,
_RESMGR_IO_NFUNCS, &io_funcs);
io_funcs.read = io_read;
/* initialize attribute structure used by the device */
iofunc_attr_init(&attr, S_IFNAM | 0666, 0, 0);
attr.nbytes = strlen(buffer)+1;
/* attach our device name */
if((id = resmgr_attach(dpp, &resmgr_attr, "/dev/sample", _FTYPE_ANY, 0,
&connect_funcs, &io_funcs, &attr)) == -1) {
fprintf(stderr, "%s: Unable to attach name.\n", argv[0]);
return EXIT_FAILURE;
}
/* allocate a context structure */
ctp = dispatch_context_alloc(dpp);
/* start the resource manager message loop */
while(1) {
if((ctp = dispatch_block(ctp)) == NULL) {
fprintf(stderr, "block error\n");
return EXIT_FAILURE;
}
dispatch_handler(ctp);
}
}
int
io_read (resmgr_context_t *ctp, io_read_t *msg, RESMGR_OCB_T *ocb)
{
int nleft;
int nbytes;
int nparts;
int status;
if ((status = iofunc_read_verify (ctp, msg, ocb, NULL)) != EOK)
return (status);
if ((msg->i.xtype & _IO_XTYPE_MASK) != _IO_XTYPE_NONE)
return (ENOSYS);
/*
* On all reads (first and subsequent), calculate
* how many bytes we can return to the client,
* based upon the number of bytes available (nleft)
* and the client's buffer size
*/
nleft = ocb->attr->nbytes - ocb->offset;
nbytes = min (msg->i.nbytes, nleft);
if (nbytes > 0) {
/* set up the return data IOV */
SETIOV (ctp->iov, buffer + ocb->offset, nbytes);
/* set up the number of bytes (returned by client's read()) */
_IO_SET_READ_NBYTES (ctp, nbytes);
/*
* advance the offset by the number of bytes
* returned to the client.
*/
ocb->offset += nbytes;
nparts = 1;
} else {
/*
* they've asked for zero bytes or they've already previously
* read everything
*/
_IO_SET_READ_NBYTES (ctp, 0);
nparts = 0;
}
/* mark the access time as invalid (we just accessed it) */
if (msg->i.nbytes > 0)
ocb->attr->flags |= IOFUNC_ATTR_ATIME;
return (_RESMGR_NPARTS (nparts));
}
上述代碼中,ocb
結構存放了緩存的偏移位置,此外指向的屬性結構體attr
中,存放了緩存的實際大小。而attr
中緩沖的大小是通過iofunc_attr_init()
接口設置進去的。
此外,在代碼中需要注意用到了ctp->iov
,使用SETIOV()
宏,將ctp->iov
指向了需要回復的數據。
當沒有讀取到數據時,返回(_RESMGR_NPARTS(0))
,當讀取到數據后,怎么來告知客戶端讀取了多少呢?代碼中_IO_SET_READ_NBYTES()
宏將讀取的信息保存到了ctp
中,當返回到庫的時候,會將讀取的字節數當成MsgReplyv()
函數的參數,并通知內核MsgSend()
該發送什么,最終上層便能獲取到字節數。
2. Handling the _IO_WRITE message
io_write
處理函數負責處理_IO_WRITE
消息,將數據寫入到設備。當客戶端調用write()/fflush()
等操作時執行。
消息定義如下:
struct _io_write {
uint16_t type;
uint16_t combine_len;
int32_t nbytes;
uint32_t xtype;
/* unsigned char data[nbytes]; */
};
typedef union {
struct _io_write i;
/* nbytes is returned with MsgReply */
} io_write_t;
這個結構中的字段與io_read_t
中一致,下邊看一個寫操作的代碼:
int
io_write (resmgr_context_t *ctp, io_write_t *msg, RESMGR_OCB_T *ocb)
{
int status;
char *buf;
if ((status = iofunc_write_verify(ctp, msg, ocb, NULL)) != EOK)
return (status);
if ((msg->i.xtype & _IO_XTYPE_MASK) != _IO_XTYPE_NONE)
return(ENOSYS);
/* set up the number of bytes (returned by client's write()) */
_IO_SET_WRITE_NBYTES (ctp, msg->i.nbytes);
buf = (char *) malloc(msg->i.nbytes + 1);
if (buf == NULL)
return(ENOMEM);
/*
* Reread the data from the sender's message buffer.
* We're not assuming that all of the data fit into the
* resource manager library's receive buffer.
*/
resmgr_msgread(ctp, buf, msg->i.nbytes, sizeof(msg->i));
buf [msg->i.nbytes] = '\0'; /* just in case the text is not NULL terminated */
printf ("Received %d bytes = '%s'\n", msg -> i.nbytes, buf);
free(buf);
if (msg->i.nbytes > 0)
ocb->attr->flags |= IOFUNC_ATTR_MTIME | IOFUNC_ATTR_CTIME;
return (_RESMGR_NPARTS (0));
}
當在寫入的時候,如果提供的buffer大小不夠,而需要寫入的數據又很大,此時就需要使用一個循環機制來多次寫入了。
3. Methods of return and replying
可以使用各種方式從處理程序中返回到資源管理器庫:
返回錯誤
比如申請內存不夠時:return (ENOMEM)
返回指向數據的
IOV
數組
可以使用IOV
數組來指向多片數據,比如如下代碼:
my_header_t header;
a_buffer_t buffers[N];
...
SETIOV(&ctp->iov[0], &header, sizeof(header));
SETIOV(&ctp->iov[1], &buffers[i], sizeof(buffers[i]));
return (_RESMGR_NPARTS(2));
- 返回一個包含數據的緩存
比如在響應read()
請求時,所有的數據都在一個緩存中,那么可以以下兩種方式:
return (_RESMGR_PTR(ctp, buffer, nbytes));
和:
SETIOV (ctp->iov, buffer, nbytes);
return (_RESMGR_NPARTS(1));
成功返回,但是不攜帶數據
比如:return (EOK)
,不過更常見的方式是:return (_RESMGR_NPARTS(0))
。讓資源管理器庫去返回
在服務器端執行回復
上邊講到的返回情況,都是通過資源管理器庫調用MsgReply*()/MsgError()
來解除客戶端的阻塞狀態,在有些case中,你可能不需要這些庫來回復,可以使用return (_RESMGR_NOREPLY)
推遲回復,讓客戶端保持阻塞
一個例子是管道資源管理器,當一個客戶端讀取管道時,而此時管道中沒有數據,可以有兩種選擇:1)返回錯誤,2)保持客戶端阻塞,等有數據時再回復。
另外一個例子是往一個設備寫數據時,希望數據全部寫入后再回復。返回并告訴庫執行默認處理
可以執行return (_RESMGR_DEFAULT)
。
4. Handling other read/write details
4.1 處理
xtype
成員
從上文中可知,在io_read_t/io_write_t
等消息結構中,有一個xtype
成員,這個成員包含了可用于調整標準I/O函數行為的一些擴展信息,如下:
-
_IO_XTYPE_NONE
,沒有提供擴展類型信息; -
_IO_XTYPE_OFFSET
,客戶端調用pread()/pread64()/pwrite()/pwrite64()
時,不希望用到OCB中的偏移,而是提供了一個一次性的偏移量,該偏移量存放在消息緩沖的開頭,緊跟在struct _io_read
或struct _io_write
后,比如:
struct myread_offset {
struct _io_read read;
struct _xtype_offset offset;
}
-
_IO_XTYPE_READCOND
,如果客戶端調用readcond()
,通常會對時間和緩沖區的大小增加一些限制。這個限制放置在消息緩沖區的開頭,緊跟在struct _io_read
或struct _io_write
后,比如:
struct myreadcond {
struct _io_read read;
struct _xtype_readcond cond;
}
-
_IO_XFLAG_DIR_EXTRA_HINT
,只有在讀取目錄時才有效。
當你不想使用擴展功能時,可以參考以下例子:
int
io_read (resmgr_context_t *ctp, io_read_t *msg,
RESMGR_OCB_T *ocb)
{
int status;
if ((status = iofunc_read_verify(ctp, msg, ocb, NULL))
!= EOK) {
return (status);
}
/* No special xtypes */
if ((msg->i.xtype & _IO_XTYPE_MASK) != _IO_XTYPE_NONE)
return (ENOSYS);
...
}
4.2 處理
pread*()
和pwrite*()
客戶端調用pread*()
函數時,處理_IO_READ
消息的示例代碼如下:
/* we are defining io_pread_t here to make the code below
simple */
typedef struct {
struct _io_read read;
struct _xtype_offset offset;
} io_pread_t;
int
io_read (resmgr_context_t *ctp, io_read_t *msg,
RESMGR_OCB_T *ocb)
{
off64_t offset; /* where to read from */
int status;
if ((status = iofunc_read_verify(ctp, msg, ocb, NULL))
!= EOK) {
return(status);
}
switch(msg->i.xtype & _IO_XTYPE_MASK) {
case _IO_XTYPE_NONE:
offset = ocb->offset;
break;
case _IO_XTYPE_OFFSET:
/*
* io_pread_t is defined above.
* Client is doing a one-shot read to this offset by
* calling one of the pread*() functions
*/
offset = ((io_pread_t *) msg)->offset.offset;
break;
default:
return(ENOSYS);
}
...
}
客戶端調用pwrite*()
時,處理_IO_WRITE
消息的示例代碼如下:
/* we are defining io_pwrite_t here to make the code below
simple */
typedef struct {
struct _io_write write;
struct _xtype_offset offset;
} io_pwrite_t;
int
io_write (resmgr_context_t *ctp, io_write_t *msg,
RESMGR_OCB_T *ocb)
{
off64_t offset; /* where to write */
int status;
size_t skip; /* offset into msg to where the data
resides */
if ((status = iofunc_write_verify(ctp, msg, ocb, NULL))
!= EOK) {
return(status);
}
switch(msg->i.xtype & _IO_XTYPE_MASK) {
case _IO_XTYPE_NONE:
offset = ocb->offset;
skip = sizeof(io_write_t);
break;
case _IO_XTYPE_OFFSET:
/*
* io_pwrite_t is defined above
* client is doing a one-shot write to this offset by
* calling one of the pwrite*() functions
*/
offset = ((io_pwrite_t *) msg)->offset.offset;
skip = sizeof(io_pwrite_t);
break;
default:
return(ENOSYS);
}
...
/*
* get the data from the sender's message buffer,
* skipping all possible header information
*/
resmgr_msgreadv(ctp, iovs, niovs, skip);
...
}
需要注意的是,通常情況下在發送消息緩沖中,數據會緊跟著struct _io_write
,而在上邊這種情況下,數據緊跟在struct _xtype_offset
后邊,這里邊會存在一個偏移。
4.3 處理
readcond()
與處理pread()/_IO_XTYPE_OFFSET
類似,如下:
typedef struct {
struct _io_read read;
struct _xtype_readcond cond;
} io_readcond_t
struct _xtype_readcond *cond
...
CASE _IO_XTYPE_READCOND:
cond = &((io_readcond_t *)msg)->cond
break;
}
5. Updating the time for reads and writes
在讀操作的那個示例代碼中,有如下代碼:
if (msg->i.nbytes > 0)
ocb->attr->flags |= IOFUNC_ATTR_ATIME;
根據POSIX標準,當讀取大于0字節的數據并且成功后,需要去更新訪問時間,但是POSIX標準中并沒有說需要立刻去更新,因此當有多次讀的時候,可能并不想每次都從內核中獲取時間,可以在需要更新的時候再統一更新,這樣做的缺點是時間記錄的不是每一次讀操作的時間。如果想記錄每次讀的時間,那么可以調用iofunc_time_update()
接口,代碼如下:
if (msg->i.nbytes > 0) {
ocb->attr->flags |= IOFUNC_ATTR_ATIME;
iofunc_time_update(ocb->attr);
}
寫操作也是一樣的原理。