/[LeafOK_CVS]/lbbs/src/bbs_net.c
ViewVC logotype

Diff of /lbbs/src/bbs_net.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.34 by sysadm, Sun May 11 06:07:09 2025 UTC Revision 1.37 by sysadm, Sun May 11 14:52:26 2025 UTC
# Line 150  static void process_bar(int n, int len) Line 150  static void process_bar(int n, int len)
150    
151  int bbsnet_connect(int n)  int bbsnet_connect(int n)
152  {  {
153          int sock, flags, ret, loop, error;          int sock, ret, loop, error;
154          ssize_t len;          int flags_sock;
155            int flags_stdin;
156            int flags_stdout;
157            int len;
158          struct sockaddr_in sin;          struct sockaddr_in sin;
159          char buf[LINE_BUFFER_LEN];          char input_buf[LINE_BUFFER_LEN];
160            char output_buf[LINE_BUFFER_LEN];
161            int input_buf_len = 0;
162            int output_buf_len = 0;
163            int input_buf_offset = 0;
164            int output_buf_offset = 0;
165          fd_set read_fds;          fd_set read_fds;
166          fd_set write_fds;          fd_set write_fds;
167          struct timeval timeout;          struct timeval timeout;
# Line 165  int bbsnet_connect(int n) Line 173  int bbsnet_connect(int n)
173          time_t t_used;          time_t t_used;
174          struct tm *tm_used;          struct tm *tm_used;
175          int ch;          int ch;
         int offset;  
176    
177          clearscr();          clearscr();
178    
# Line 216  int bbsnet_connect(int n) Line 223  int bbsnet_connect(int n)
223          process_bar(0, MAX_PROCESS_BAR_LEN);          process_bar(0, MAX_PROCESS_BAR_LEN);
224    
225          // Set socket as non-blocking          // Set socket as non-blocking
226          flags = fcntl(sock, F_GETFL, 0);          flags_sock = fcntl(sock, F_GETFL, 0);
227          fcntl(sock, F_SETFL, flags | O_NONBLOCK);          fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK);
228    
229          if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)          if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)
230          {          {
# Line 280  int bbsnet_connect(int n) Line 287  int bbsnet_connect(int n)
287                  return -1;                  return -1;
288          }          }
289    
         fcntl(sock, F_SETFL, flags); /* restore file status flags */  
   
290          tos = IPTOS_LOWDELAY;          tos = IPTOS_LOWDELAY;
291          if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)          if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
292          {          {
# Line 292  int bbsnet_connect(int n) Line 297  int bbsnet_connect(int n)
297          iflush();          iflush();
298          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);
299    
300            // Set STDIN/STDOUT as non-blocking
301            flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0);
302            flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0);
303            fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK);
304            fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK);
305    
306          BBS_last_access_tm = t_used = time(0);          BBS_last_access_tm = t_used = time(0);
307          loop = 1;          loop = 1;
308    
# Line 302  int bbsnet_connect(int n) Line 313  int bbsnet_connect(int n)
313                  FD_SET(sock, &read_fds);                  FD_SET(sock, &read_fds);
314    
315                  FD_ZERO(&write_fds);                  FD_ZERO(&write_fds);
316                  FD_SET(STDIN_FILENO, &write_fds);                  FD_SET(STDOUT_FILENO, &write_fds);
317                  FD_SET(sock, &write_fds);                  FD_SET(sock, &write_fds);
318    
319                  timeout.tv_sec = 0;                  timeout.tv_sec = 0;
# Line 325  int bbsnet_connect(int n) Line 336  int bbsnet_connect(int n)
336                                  loop = 0;                                  loop = 0;
337                          }                          }
338                  }                  }
339                  else if (ret > 0)                  else // if (ret > 0)
340                  {                  {
341                          if (FD_ISSET(STDIN_FILENO, &read_fds) && FD_ISSET(sock, &write_fds))                          if ((input_buf_offset >= input_buf_len) && FD_ISSET(STDIN_FILENO, &read_fds))
342                          {                          {
343                                  // Set STDIN as non-blocking                                  ret = (int)read(STDIN_FILENO, input_buf, sizeof(input_buf));
344                                  flags = fcntl(STDIN_FILENO, F_GETFL, 0);                                  if (ret < 0)
                                 fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);  
   
                                 len = read(STDIN_FILENO, buf, sizeof(buf));  
                                 if (len < 0)  
345                                  {                                  {
346                                          if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)                                          if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
347                                          {                                          {
# Line 342  int bbsnet_connect(int n) Line 349  int bbsnet_connect(int n)
349                                                  loop = 0;                                                  loop = 0;
350                                          }                                          }
351                                  }                                  }
352                                  else if (len == 0)                                  else if (ret == 0) // broken pipe
353                                  {                                  {
                                         log_error("read(STDIN) reach EOF\n");  
354                                          loop = 0;                                          loop = 0;
355                                  }                                  }
356                                  else                                  else
357                                  {                                  {
358                                          offset = 0;                                          input_buf_len = ret;
359                                          do                                          input_buf_offset = 0;
                                         {  
                                                 ret = (int)write(sock, buf + offset, (size_t)(len - offset));  
                                                 if (ret < 0)  
                                                 {  
                                                         log_error("write(socket) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
                                                 offset += ret;  
                                         } while (offset < len);  
360    
361                                          BBS_last_access_tm = time(0);                                          BBS_last_access_tm = time(0);
362                                  }                                  }
   
                                 // Restore STDIN flags  
                                 fcntl(STDIN_FILENO, F_SETFL, flags);  
363                          }                          }
364                          if (FD_ISSET(sock, &read_fds) && FD_ISSET(STDIN_FILENO, &write_fds))  
365                            if ((input_buf_offset < input_buf_len) && FD_ISSET(sock, &write_fds))
366                          {                          {
367                                  // Set socket as non-blocking                                  ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));
368                                  flags = fcntl(sock, F_GETFL, 0);                                  if (ret < 0)
369                                  fcntl(sock, F_SETFL, flags | O_NONBLOCK);                                  {
370                                            if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
371                                            {
372                                                    log_error("write(socket) error (%d)\n", errno);
373                                                    loop = 0;
374                                            }
375                                    }
376                                    else if (ret == 0) // broken pipe
377                                    {
378                                            loop = 0;
379                                    }
380                                    else
381                                    {
382                                            input_buf_offset += ret;
383                                    }
384                            }
385    
386                                  len = read(sock, buf, sizeof(buf));                          if ((output_buf_offset >= output_buf_len) && FD_ISSET(sock, &read_fds))
387                                  if (len < 0)                          {
388                                    ret = (int)read(sock, output_buf, sizeof(output_buf));
389                                    if (ret < 0)
390                                  {                                  {
391                                          if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)                                          if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
392                                          {                                          {
# Line 383  int bbsnet_connect(int n) Line 394  int bbsnet_connect(int n)
394                                                  loop = 0;                                                  loop = 0;
395                                          }                                          }
396                                  }                                  }
397                                  else if (len == 0)                                  else if (ret == 0) // broken pipe
398                                  {                                  {
                                         log_error("read(socket) reach EOF\n");  
399                                          loop = 0;                                          loop = 0;
400                                  }                                  }
401                                  else                                  else
402                                  {                                  {
403                                          offset = 0;                                          output_buf_len = ret;
404                                          do                                          output_buf_offset = 0;
                                         {  
                                                 ret = (int)write(STDOUT_FILENO, buf + offset, (size_t)(len - offset));  
                                                 if (ret < 0)  
                                                 {  
                                                         log_error("write(STDOUT) error (%d)\n", errno);  
                                                         loop = 0;  
                                                         break;  
                                                 }  
                                                 offset += ret;  
                                         } while (offset < len);  
405                                  }                                  }
406                            }
407    
408                                  // Restore socket flags                          if ((output_buf_offset < output_buf_len) && FD_ISSET(STDOUT_FILENO, &write_fds))
409                                  fcntl(sock, F_SETFL, flags);                          {
410                                    ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset));
411                                    if (ret < 0)
412                                    {
413                                            if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
414                                            {
415                                                    log_error("write(STDOUT) error (%d)\n", errno);
416                                                    loop = 0;
417                                            }
418                                    }
419                                    else if (ret == 0) // broken pipe
420                                    {
421                                            loop = 0;
422                                    }
423                                    else
424                                    {
425                                            output_buf_offset += ret;
426                                    }
427                          }                          }
428                  }                  }
429          }          }
430    
431            // Restore STDIN/STDOUT flags
432            fcntl(STDIN_FILENO, F_SETFL, flags_stdin);
433            fcntl(STDOUT_FILENO, F_SETFL, flags_stdout);
434    
435            // Restore socket flags
436            fcntl(sock, F_SETFL, flags_sock);
437    
438          if (close(sock) == -1)          if (close(sock) == -1)
439          {          {
440                  log_error("Close socket failed\n");                  log_error("Close socket failed\n");
# Line 492  int bbs_net() Line 517  int bbs_net()
517                  ch = igetch(0);                  ch = igetch(0);
518                  switch (ch)                  switch (ch)
519                  {                  {
520                  case Ctrl('C'):                  case KEY_NULL:  // broken pipe
521                    case Ctrl('C'): // user cancel
522                          return 0;                          return 0;
                 case KEY_NULL:  
523                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
524                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
525                          {                          {


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1