Skip to content

Commit

Permalink
dispatch_io_create() is supposed to be the correct way to read from a…
Browse files Browse the repository at this point in the history
… stream. However, it doesn't seem to work and I believe it's due to poll() not working with pseudo terminals in OS X.

Leaving the code in just for fun.

Underlying issue - when the pty closes, the block doesn't always get notified so the fd doesn't close until the window closes.
  • Loading branch information
ksherlock committed Jan 27, 2018
1 parent a6408fc commit 38dad13
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion TermWindowController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,33 @@ -(void)monitor {
dispatch_resume(_wait_source);
}

#if 0
dispatch_io_t io = dispatch_io_create(DISPATCH_IO_STREAM, fd, queue, ^(int error){
close(fd);
NSLog(@"dispatch_io_create: %d", error);
});

dispatch_io_read(io, 0, SIZE_MAX, queue, ^(bool done, dispatch_data_t data, int error){
if (error) {
NSLog(@"dispatch_io_read: %d", error);
dispatch_io_close(io, DISPATCH_IO_STOP);
return;
}

dispatch_data_apply(data, ^(dispatch_data_t, size_t, const void *buffer, size_t size){
[_emulatorView processData: (uint8_t *)buffer size: size];
return true;
} );

if (done) {
NSLog(@"closing fd");
dispatch_io_close(io, DISPATCH_IO_STOP);
return;
}

});

#else
_read_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ,
fd, 0, queue);
if (_read_source)
Expand All @@ -245,6 +271,7 @@ -(void)monitor {

for (;;) {
actual = read(fd, buffer, estimated);
//fprintf(stderr, "read: %ld\n", actual);
if (actual < 0) {
if (errno == EINTR) continue;

Expand All @@ -266,7 +293,6 @@ -(void)monitor {
if (buffer != sbuffer) free(buffer);

if (actual == 0) {
NSLog(@"closing fd");
dispatch_source_cancel(_read_source);
dispatch_release(_read_source);
_read_source = nullptr;
Expand All @@ -276,13 +302,15 @@ -(void)monitor {


dispatch_source_set_cancel_handler(_read_source, ^{
NSLog(@"closing fd");
_fd = -1;
[_emulatorView setFd: -1];
close(fd);
});

dispatch_resume(_read_source);
}
#endif
}

#pragma mark -
Expand Down

0 comments on commit 38dad13

Please sign in to comment.