Open System Services Programmer's Guide

Example 46 Using $RECEIVE in an OSS Server Process
/* server1.c
*
* Simple OSS server example using Guardian file input/output
* APIs for communication between requester(s) and server.
*
* This server accepts requests on $RECEIVE. Data requests are
* handled by printing the data and then replying back to the
* requester with the original data. Open and close requests
* are handled by updating an opener count. The server exits
* when idle for 60 seconds.
*/
#include <unistd.h>
#include <fcntl.h>
#include <tal.h>
#include <cextdecs.h(FILE_OPEN_, READUPDATEX, REPLYX, FILE_CLOSE_, \
AWAITIOX, FILE_GETINFO_)>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define NOWAITDEPTH 1 /* enable nowait operations */
#define RECVDEPTH 1 /* enable replies on $RECEIVE */
#define IDLE_PERIOD 6000 /* 60 seconds = 6000 x 0.01 second */
short retcode; /* return code for APIs that return short */
_cc_status CC; /* return code for APIs that return condition code */
short fnum, bytesread, error, openers;
char *filename = "$RECEIVE"; /* file name for incoming requests */
#define BUFF_LEN 1024 /* buffer size */
union msg
{
short sys_msg[50]; /* buffer viewed as system message */
char data[BUFF_LEN+1]; /* buffer viewed as user data */
} msg_buff; /* single buffer is enough for all connections */
int main(int argc, char **argv)
{
printf("Server starting\n");
/* The server process opens its $RECEIVE file for nowait IO. */
retcode = FILE_OPEN_(
filename, /* filename */
(short)strlen(filename), /* filename length */
&fnum, /* file number returned */
0, /* access mode (r/w) */
0, /* exclusion mode (shared) */
NOWAITDEPTH, /* nowait depth (nowait enabled) */
RECVDEPTH, /* receive depth (1 msg) */
0); /* options (sys msgs enabled) */
if (retcode != 0) /* if error, exit(1) */
{
fprintf(stderr, "FILE_OPEN_() to %s failed\n", filename);
fprintf(stderr, "Return code = %d\n", retcode);
exit(1);
}
/* Read data from $RECEIVE ... handle system msgs and user msgs */
openers = 0;
while (1)
{
CC = READUPDATEX(fnum, /* file number */
msg_buff.data, /* msg buffer */
BUFF_LEN, /* length */
&bytesread); /* number of bytes read */
if (_status_ne(CC)) /* if error, exit(1) */
{
192 Interprocess Communication