Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 22 Feb 1997 21:37:46 -0500
From:      Matt Thomas <matt@lkg.dec.com>
To:        Terry Lambert <terry@lambert.org>
Cc:        hackers@FreeBSD.ORG
Subject:   Re: Alternatives to SYSV IPC?
Message-ID:  <3.0.32.19970222213738.006a1cb0@netrix.lkg.dec.com>

next in thread | raw e-mail | index | archive | help

>The length field would not be necessary if the framing were enforced
>by the transport: a receive into a buffer larger than the maximum
>allowable message type that could be framed for any given transmitter
>state would be sufficient.  You do a recv into the buffer, then you
>dispatch based on a field dereference from the newly acquired data
>(probably a type field), and dispatch the data for processing from
>a big switch statement.  No additional recv's necessary.

This can be done today with the BSD4.4 socket framework.  With AF_LOCAL
(AF_UNIX) sockets and the use of MSG_EOR with send() and MSG_WAITALL
with recvmsg() it does work.  I've attached a small example program
(which works under FreeBSD 2.1.0 and Digital UNIX 4.0).

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/un.h>
#include <sys/fcntl.h>

int
main(
    int argc,
    void **argv)
{
    int fds[2];
    struct msghdr msg;
    char buf[20];
    struct iovec iov[1];
    int cc;

    if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0)
	perror("socketpair");

    if (send(fds[1], "Hi ", 3, MSG_EOR) < 0)
	perror("send");
    if (send(fds[1], "There", 5, MSG_EOR) < 0)
	perror("send");
    close(fds[1]);

    bzero(&msg, sizeof(msg));
    iov[0].iov_base = buf;
    iov[0].iov_len = sizeof(buf);
    msg.msg_iov = iov;
    msg.msg_iovlen = 1;
    for (;;) {
	cc = recvmsg(fds[0], &msg, 0);
	if (cc <= 0)
	    break;
	buf[cc] = '\0';

	printf("%d: %s%s", cc, buf, msg.msg_flags & MSG_EOR ? " (EOR)\n" : "");
    }
    if (cc < 0)
	perror("recvmsg");
    putchar('\n');
}


-- 
Matt Thomas               Internet:   matt@3am-software.com
3am Software Foundry      WWW URL:
http://www.3am-software.com/bio/matt.html
Westford, MA              Disclaimer: I disavow all knowledge of this message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3.0.32.19970222213738.006a1cb0>