Talk:Io.c

From Organic Design wiki
Revision as of 07:53, 21 August 2006 by Nad (talk | contribs) (try again now...)

Testing

Compiling... for darwin
In file included from peerd.c:55:
io.c: In function `streamOpen':
io.c:184: warning: initialization makes pointer from integer without a cast
io.c:190: error: dereferencing pointer to incomplete type
--Rob 19:35, 21 Aug 2006 (NZST)
Try now - I think that missing struct is in netdb.h for *ux --Nad 19:53, 21 Aug 2006 (NZST)

General structure

I'm incorporating the file IO into this script now so its more in line with the final Storage and Distribution concept. I've renamed it to io.c to reflect this more general nature of it (and its less of a mouthfull than storage and distribution). --Nad 16:44, 15 Aug 2006 (NZST)

  • This change requires the PAKSIZE to be dynamic, so it can be specific to media specifications
  • It may require &read()/&recv() to be in the streamInfo structs to be called depening on whether the stream is a file or socket.
  • streamOpen() has been changed to allow a textual representation of the remote resource/peer to be passed with fd<0 so that it will try and resolve the text to a connected fd.

Server Iteration - network()

Called by nodal reduction from ROOT loop. Each iteration must do a generic select-update, and process a single packet for one of the streams in each direction.

Select

Multiplexing with select.

Socket

Constants to setsockopts for buffers. This may be used for the iterative non-forking non-blocking socket select processing.

SO_SNDBUF  

Sets send buffer size information. This option takes an int value in the optval argument.

SO_RCVBUF  

Sets receive buffer size information. This option takes an int value in the optval argument.

--Rob 11:30, 5 Jul 2006 (NZST)


  • Find docs on simple socket interative no buffer no fork multiplexing (ask Rob b)
  • Maybe let OS buffer stream until end of message detected, then chunk message into structure
    • peek

  • added O_NONBLOCK so that the accept() returns even if no incomming requests
  • made a struct-type called streamInfo and an array of them called streams so that all the corrently connected clients have their own accumulating input buffer.
  • MAXCLIENTS is the size of the streams array
  • BUFSIZE is the size of each streams input buffer which determines the largest message it can read
  • PAKSIZE is the number of bytes read on each iteration of the server loop

Example snipits

Example select() from http://publib.boulder.ibm.com

int main()
{
    int sockfd, cnt, i = 1;
    struct sockaddr_in  serv_addr;

    bzero((char *)&serv_addr, sizeof (serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr("172.16.55.25");
    serv_addr.sin_port = htons(102);

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        exit(1);
    if (fcntl(sockfd, F_SETFL, FNONBLOCK) < 0)
        exit(1);
    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof
            (serv_addr)) < 0 && errno != EINPROGRESS)
        exit(1);
    for (cnt=0; cnt<2; cnt++) {
        fd_set  readfds, writefds;

        FD_ZERO(&readfds);
        FD_SET(sockfd, &readfds);
        FD_ZERO(&writefds);
        FD_SET(sockfd, &writefds);

        if (select(sockfd + 1, &readfds, &writefds, NULL,
                NULL) < 0)
            exit(1);
        printf("Iteration %d ==============\n", i);
        printf("FD_ISSET(sockfd, &readfds) == %d\n",
            FD_ISSET(sockfd, &readfds));
        printf("FD_ISSET(sockfd, &writefds) == %d\n",
            FD_ISSET(sockfd, &writefds));
        i++;
    }
    return 0;
}

and another example from http://docs.sun.com/app/docs/doc/817-4415/6mjum5sgc?a=view

main() {
    int sock, length;
    struct sockaddr_in6 server;
    int msgsock;
    char buf[1024];
    int rval;
    fd_set ready;
    struct timeval to;
    /* Open a socket and bind it as in previous examples. */
    /* Start accepting connections. */
    listen(sock, MAXCLIENTS); 
    do {
        FD_ZERO(&ready);
        FD_SET(sock, &ready);
        to.tv_sec = 5;
        to.tv_usec = 0;
        if (select(sock + 1, &ready, (fd_set *)0, 
                   (fd_set *)0, &to) == -1) {
            perror("select");
            continue;
        }
        if (FD_ISSET(sock, &ready)) {
            msgsock = accept(sock, (struct sockaddr *)0, (int *)0);
            if (msgsock == -1)
                perror("accept");
            else do {
                memset(buf, 0, sizeof buf);
                if ((rval = read(msgsock, buf, sizeof(buf))) == -1)
                    perror("reading stream message");
                else if (rval == 0)
                    printf("Ending connection\n");
                else
                    printf("-->%s\n", buf);
            } while (rval > 0);
            close(msgsock);
        } else
            printf("Do something else\n");
        } while (TRUE);
    exit(0);
}