简体   繁体   中英

Reading dynamically growing file using NSInputStream

I should use Objective-C to read some slowly growing file (under Mac OS X).

"Slowly" means that I read to EOF before it grows bigger.

In means of POSIX code in plain syncronous CI can do it as following:

while(1)
{
    res = select(fd+1,&fdset,NULL,&fdset,some_timeout);
    if(res > 0)
    {
        len = read(fd,buf,sizeof(buf));
        if (len>0)
        {
            printf("Could read %u bytes. Continue.\n", len);
        }
        else
        {
            sleep(some_timeout_in_sec);
        }
    }
}

Now I want to re-write this in some asynchronous manner, using NSInputSource or some other async Objective-C technique.

The problem with NSInputSource: If I use scheduleInRunLoop: method then once I get NSStreamEventEndEncountered event, I stop receiving any events.

Can I still use NSInputSource or should I pass to using NSFileHandle somehow or what would you recommend ?

I see a few problems.

1) some_Timeout, for select() needs to be a struct timeval *.

2) for sleep() some_timeout needs to be an integer number of seconds.

3) the value in some_timeout is decremented via select() (which is why the last parameter is a pointer to the struct timeval*. And that struct needs to be re-initialized before each call to select().

4) the parameters to select() are highest fd of interest+1, then three separate struct fd_set * objects. The first is for input files, the second is for output files, the third is for exceptions, however, the posted code is using the same struct fd_set for both the inputs and the exceptions, This probably will not be what is needed.

When the above problems are corrected, the code should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM