I'll take a crack at this one ;)

I'll come right out and say I don't agree with your design. When you're
doing network I/O, it's better to put the select in your main event loop,
and then deal with reads in the form event handler. Essentially, you get a
NIL event from the event loop, and in the form handler for the NIL event you
do one read on the socket with a very short timeout. Once you read EOF
you're done. Assuming the user does something else (like tap a cancel
button, or exit the form, or the app) you close the network connection &
otherwise clean up. That way, all the other interface elements remain active
and the user stays in control.


That said, let's look at your code (my comments and changes inserted):

 int Read(void *buf, Word len) {
      while (len) {
            // Create the descriptor sets
            netFDZero(&readFDs);
            netFDZero(&writeFDs);
            netFDZero(&exceptFDs);

// I'm assuming readFDs, writeFDs, exceptFDs are declared somewhere as
NetFDSetType
            netFDSet(sysFileDescStdIn, &readFDs);
// You want any READABLE events on stdin
            netFDSet(peer, &readFDs);
// You want any READABLE events on "peer", which I assume is the open socket

            width = sysFileDescStdIn;
            width = 0;
// Make up your mind ;), Actually, sysFileDescStdIn == 0
            if (peer > width) width = peer;
// fine, but this is the same as:
// width = peer;
            numFDs = NetLibSelect(AppNetRefnum, width+1, &readFDs,
&writeFDs,
                         &exceptFDs, sysTicksPerSecond, &netErr);

                // what is numFDs here? what is netErr? 
                // if netErr == netErrTimeout then the readFD flags probably
are unchanged
                // see my rework below

                if (netErr == netErrTimeout) {
                        // just check for timeout here, nothing was readable
in the last second
                 timeCount++;
                        // update progress bar, eventually give up when
timeCount > some value...
                }
                else if (netFDIsSet(sysFileDescStdIn, &readFDs)) {
                        // clean up and cancel
                        // I'd expect a return from the Read() fn in here,
right?
                        // return ERROR_USER_INPUT? something like that?
                } else {
                        // not a timeout, and stdin not ready, so the socket
must be ready
                if ( (cnt = NetLibReceive(AppNetRefnum, peer, buf, len, 0,
NULL, 
                                0, NETTIMEOUT, &netErr)) == -1) {
                        return ERROR_GENERAL;
                }
                len -= cnt;
                (unsigned char *) buf += cnt;           
                }
   }
 }

 ==-
John Schettino author of
Palm OS Programming For Dummies, http://schettino.tripod.com

-----Original Message-----
From: Ariel Barreiro [mailto:[EMAIL PROTECTED]]
Sent: Thursday, January 06, 2000 11:28 AM
To: [EMAIL PROTECTED]
Subject: RE: NetSelect


but It doeesn't work in my palm either. Did you give a look to my code and
find everything was ok????
----- Original Message -----
From: William F. Weiher III <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, January 05, 2000 8:01 PM
Subject: RE: NetSelect


> Select does not support stdin on the emulator, I have had no trouble with
it
> on the actual device (symbol 1740)
>
>  -----Original Message-----
> From: Ariel Barreiro [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, January 05, 2000 13:46
> To: Palm Forum List
> Subject: NetSelect
>
> Dear all,
>
> I know that recently somebody asked a bout netselect but I read the
messages
> and I couldnt get the idea.
>
> I have a read and write functions that performs some stuff inside the net
> layer.The problem is that I dont handle the data reception as an event. I
> just entered in a loop while I wait for the data to get into the palm. Now
> if the data lasts 2 or 3 minutes (that are time that might last and are
> under my timeout) the palm stay hanged until the data is ready. Now I
tried
> to implement a select as I implemented for the conduit (which has the same
> read write functions). I wanted to do this to show the a countdown of the
> time out and a progress bar. That's works fine (I havent done the progress
> bar but the timeout of the select works fine).
>
> Now my problem is with the stdin File Descriptor. If a tested in the palm
it
> turns out that the stdin is always set, otherwise in the Emulator it is
> never set. What I want is the stdin let me know when the user taps
something
> on the screen, knows where and if for example the user taps the
application
> button the reception of the data should be cancelled. Can somebody help me
> do that without changing to much the style of my net layer. Thanks a lot.
>
> Here is my read function.
>
> int Read(void *buf, Word len) {
>      while (len) {
>            // Create the descriptor sets
>            netFDZero(&readFDs);
>            netFDZero(&writeFDs);
>            netFDZero(&exceptFDs);
>            netFDSet(sysFileDescStdIn, &readFDs);
>            netFDSet(peer, &readFDs);
>
>            width = sysFileDescStdIn;
>            width = 0;
>            if (peer > width) width = peer;
>
>            numFDs = NetLibSelect(AppNetRefnum, width+1, &readFDs,
&writeFDs,
> &exceptFDs, sysTicksPerSecond, &netErr);
>
>           if ( netFDIsSet(sysFileDescStdIn, &readFDs) ) {
>                 // Do some cancel routine
>         }
>           if (netFDIsSet(peer, &readFDs) == 0) {
>                 timeCount++;
>            } else {
>             if ( (cnt = NetLibReceive(AppNetRefnum, peer, buf, len, 0,
NULL,
> 0, NETTIMEOUT, &netErr)) == -1) {
>                  err = ERROR_GENERAL;
>                  return err;
>             }
>             len -= cnt;
>             (unsigned char *) buf += cnt;
>         }
>   }
> }
>
>
>
>
>
>

Reply via email to