编程接口指南

异步无连接模式服务

通过在传送数据时针对非阻塞服务配置端点,以及针对异步通知轮询或接收异步通知来执行异步无连接模式网络。如果使用异步通知,则通常在信号处理程序中执行数据的实际接收。

使端点异步

使用 t_open(3NSL) 建立端点,并使用 t_bind(3NSL) 建立其标识之后,可以针对异步服务配置该端点。使用 fcntl(2) 接口在端点上设置 O_NONBLOCK 标志。这样,无立即可用缓冲区的 t_sndudata(3NSL) 调用将返回 -1,且 t_errno 设置为 TFLOW。同样,无可用数据的 t_rcvudata(3NSL) 调用将返回 -1,且 t_errno 设置为 TNODATA

异步网络传送

虽然应用程序可以使用 poll(2) 来定期检查数据是否到达或等待在端点上接收数据,但是可能需要在数据到达时接收异步通知。结合使用 ioctl(2)I_SETSIG 命令可请求在端点收到数据时将 SIGPOLL 信号发送到该进程。应用程序应检查多条消息导致单个信号的可能性。

在以下示例中,protocol 为应用程序选择的传输协议的名称。

#include <sys/types.h>

#include <tiuser.h>

#include <signal.h>

#include <stropts.h>



int				fd;

struct t_bind				*bind;

void				sigpoll(int);



	fd = t_open(protocol, O_RDWR, (struct t_info *) NULL);



	bind = (struct t_bind *) t_alloc(fd, T_BIND, T_ADDR);

	...     /* set up binding address */

	t_bind(fd, bind, bin



	/* make endpoint non-blocking */

	fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);



	/* establish signal handler for SIGPOLL */

	signal(SIGPOLL, sigpoll);



	/* request SIGPOLL signal when receive data is available */

	ioctl(fd, I_SETSIG, S_INPUT | S_HIPRI);



	...



void sigpoll(int sig)

{

	int					flags;

	struct t_unitdata					ud;



	for (;;) {

		... /* initialize ud */

		if (t_rcvudata(fd, &ud, &flags) < 0) {

			if (t_errno == TNODATA)

				break;  /* no more messages */

			... /* process other error conditions */

	}

	... /* process message in ud */

}