@@ -238,24 +238,66 @@ int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
238238 assert (1 == 2 ); // unreachable
239239}
240240
241+ static int crn_fd_would_blocking (int fd , int iswr ) {
242+
243+ fd_set read_fds ;
244+ struct timeval timeout = {.tv_sec = 0 , .tv_usec = 0 };
245+ int fd_stdin = fd ;//fileno(stdin); // Get file descriptor for stdin
246+ int activity ;
247+
248+ // Clear the file descriptor set
249+ FD_ZERO (& read_fds );
250+ // Add stdin to the set
251+ FD_SET (fd_stdin , & read_fds );
252+
253+ // Wait for activity on the file descriptor
254+ if (iswr ) {
255+ activity = select_f (fd_stdin + 1 , NULL , & read_fds , NULL , & timeout );
256+ }else {
257+ activity = select_f (fd_stdin + 1 , & read_fds , NULL , NULL , & timeout );
258+ }
259+ // printf("activity=%d, %s\n", activity, strerror(activity));
260+ if (activity < 0 ) {
261+ perror ("select error" );
262+ return activity ;
263+ } else if (activity == 0 ) {
264+ printf ("Timeout occurred (5 seconds). Still waiting...\n" );
265+ } else {
266+ return 0 ; // nonblocking
267+ }
268+ return 1 ;
269+ }
270+
241271ssize_t read (int fd , void * buf , size_t count )
242272{
243273 if (!read_f ) initHook ();
244274 if (!crn_in_procer ()) return read_f (fd , buf , count );
245275
246- // linfo("%d fdnb=%d bufsz=%d\n", fd, fd_is_nonblocking(fd), count);
247- fdcontext * ctx = hookcb_get_fdcontext (fd );
248- if (ctx == nilptr ) {
249- linfo ("wtf, why not found fd %d\n" , fd );
250- assert (1 == 2 );
251- }
252- bool isfile = fdcontext_is_file (ctx );
253- if (fd_is_nonblocking (fd ) == 0 && !isfile ) {
254- linfo ("%d fdnb=%d bufsz=%d\n" , fd , fd_is_nonblocking (fd ), count );
255- assert (fd_is_nonblocking (fd ) == 1 );
256- }
276+ // // linfo("%d fdnb=%d bufsz=%d\n", fd, fd_is_nonblocking(fd), count);
277+ // fdcontext* ctx = hookcb_get_fdcontext(fd);
278+ // if (ctx == nilptr) {
279+ // lwarn ("wtf, why not found fd %d\n", fd);
280+ // assert(0 );
281+ // }
282+ // bool isfile = fdcontext_is_file(ctx);
283+ // if (fd_is_nonblocking(fd) == 0 && !isfile) {
284+ // linfo("%d fdnb=%d bufsz=%d\n", fd, fd_is_nonblocking(fd), count);
285+ // assert(fd_is_nonblocking(fd) == 1);
286+ // }
257287 //
258288 while (1 ){
289+ int beblk = crn_fd_would_blocking (fd , 0 );
290+ if (beblk ) {
291+ bool inpoll = hookcb_getin_poll (fd , true);
292+ // linfo("read yeild %d n %d nb %d inpoll %d\n", fd, count, fd_is_nonblocking(fd), inpoll);
293+ if (inpoll ) { // dont yeild inpoll fd read
294+ // hookcb_setin_poll(fd, false, true); // cannot clear flag, or unexpected yeild/suspend
295+ // return rv;
296+ }
297+ crn_procer_yield (fd , YIELD_TYPE_READ );
298+ continue ;
299+ }
300+
259301 ssize_t rv = read_f (fd , buf , count );
260302 int eno = rv < 0 ? errno : 0 ;
261303 if (rv >= 0 ) {
0 commit comments