编程接口指南

异步网络传送

虽然应用程序可以使用 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 */

}