/[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.31 by sysadm, Sun May 11 02:42:38 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 215  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 279  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 288  int bbsnet_connect(int n) Line 294  int bbsnet_connect(int n)
294          }          }
295    
296          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
297            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          BBS_last_access_tm = t_used = time(0);          // 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);
307          loop = 1;          loop = 1;
308    
309          while (loop && !SYS_server_exit)          while (loop && !SYS_server_exit)
# Line 300  int bbsnet_connect(int n) Line 312  int bbsnet_connect(int n)
312                  FD_SET(STDIN_FILENO, &read_fds);                  FD_SET(STDIN_FILENO, &read_fds);
313                  FD_SET(sock, &read_fds);                  FD_SET(sock, &read_fds);
314    
315                    FD_ZERO(&write_fds);
316                    FD_SET(STDOUT_FILENO, &write_fds);
317                    FD_SET(sock, &write_fds);
318    
319                  timeout.tv_sec = 0;                  timeout.tv_sec = 0;
320                  timeout.tv_usec = 100 * 1000; // 0.1 second                  timeout.tv_usec = 100 * 1000; // 0.1 second
321    
322                  ret = select(FD_SETSIZE, &read_fds, NULL, NULL, &timeout);                  ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout);
323    
324                  if (ret == 0) // timeout                  if (ret == 0) // timeout
325                  {                  {
# Line 320  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))                          if ((input_buf_offset >= input_buf_len) && FD_ISSET(STDIN_FILENO, &read_fds))
342                          {                          {
343                                  len = read(STDIN_FILENO, buf, sizeof(buf));                                  ret = (int)read(STDIN_FILENO, input_buf, sizeof(input_buf));
344                                  if (len == 0)                                  if (ret < 0)
345                                    {
346                                            if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
347                                            {
348                                                    log_error("read(STDIN) error (%d)\n", errno);
349                                                    loop = 0;
350                                            }
351                                    }
352                                    else if (ret == 0) // broken pipe
353                                  {                                  {
354                                          loop = 0;                                          loop = 0;
355                                  }                                  }
356                                  write(sock, buf, (size_t)len);                                  else
357                                    {
358                                            input_buf_len = ret;
359                                            input_buf_offset = 0;
360    
361                                  BBS_last_access_tm = time(0);                                          BBS_last_access_tm = time(0);
362                                    }
363                          }                          }
364                          if (FD_ISSET(sock, &read_fds))  
365                            if ((input_buf_offset < input_buf_len) && FD_ISSET(sock, &write_fds))
366                          {                          {
367                                  len = read(sock, buf, sizeof(buf));                                  ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset));
368                                  if (len == 0)                                  if (ret < 0)
369                                    {
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;                                          loop = 0;
379                                  }                                  }
380                                  write(STDOUT_FILENO, buf, (size_t)len);                                  else
381                                    {
382                                            input_buf_offset += ret;
383                                    }
384                            }
385    
386                            if ((output_buf_offset >= output_buf_len) && FD_ISSET(sock, &read_fds))
387                            {
388                                    ret = (int)read(sock, output_buf, sizeof(output_buf));
389                                    if (ret < 0)
390                                    {
391                                            if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
392                                            {
393                                                    log_error("read(socket) error (%d)\n", errno);
394                                                    loop = 0;
395                                            }
396                                    }
397                                    else if (ret == 0) // broken pipe
398                                    {
399                                            loop = 0;
400                                    }
401                                    else
402                                    {
403                                            output_buf_len = ret;
404                                            output_buf_offset = 0;
405                                    }
406                            }
407    
408                            if ((output_buf_offset < output_buf_len) && FD_ISSET(STDOUT_FILENO, &write_fds))
409                            {
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 427  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