The following example illustrates these steps. This
example is for illustration purposes only. It is not a complete program.
/*********************************************************************/
/* Issue a rapi_session() call to initialize RAPI. */
/*********************************************************************/
rapi_sid = rapi_session(&destination,
protocol,
0,
rapi_async, /* upcall function pointer */
0, /* no upcall argument */
&rc);
...
/*********************************************************************/
/* Create a pthread to handle RAPI upcalls. */
/*********************************************************************/
pthread_create(&thread_d,
NULL,
&rapi_th,
NULL);
...
/*********************************************************************/
/* Function: rapi_th() */
/*********************************************************************/
void *rapi_th(void *arg)
{
fd_set fds;
int fd;
struct timeval tv;
int rc = SUCCESSFUL;
/*******************************************************************/
/* This is the pthread created to handle RAPI upcalls. First, get */
/* the rapi socket descriptor to use on select(). */
/*******************************************************************/
pthread_mutex_lock(&rapi_lock);
fd = rapi_getfd(rapi_sid);
pthread_mutex_unlock(&rapi_lock);
if (fd > 0) {
/*****************************************************************/
/* Loop as long as all is well, waiting via select() for an */
/* asynchronous RAPI packet to arrive. */
/*****************************************************************/
while (rc == SUCCESSFUL) {
tv.tv_sec = 1;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(fd, &fds);
switch(select(FD_SETSIZE, &fds, (fd_set *) NULL,
(fd_set *) NULL, &tv)) {
/*************************************************************/
/* Bad return from select(). Get out. */
/*************************************************************/
case -1:
rc = UNSUCCESSFUL;
break;
/*************************************************************/
/* Time out on select(). Ignore. */
/*************************************************************/
case 0:
break;
/*************************************************************/
/* Dispatch data have arrived. Call the upcall function via */
/* rapi_dispatch(). */
/*************************************************************/
default:
pthread_mutex_lock(&rapi_lock);
rc = rapi_dispatch();
pthread_mutex_unlock(&rapi_lock);
break;
}
}
}
/*******************************************************************/
/* Error on rapi_getfd(). */
/*******************************************************************/
else {
rc = UNSUCCESSFUL;
}
pthread_exit(NULL);
}