/[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.32 by sysadm, Sun May 11 04:09:08 2025 UTC Revision 1.34 by sysadm, Sun May 11 06:07:09 2025 UTC
# Line 165  int bbsnet_connect(int n) Line 165  int bbsnet_connect(int n)
165          time_t t_used;          time_t t_used;
166          struct tm *tm_used;          struct tm *tm_used;
167          int ch;          int ch;
168            int offset;
169    
170          clearscr();          clearscr();
171    
# Line 288  int bbsnet_connect(int n) Line 289  int bbsnet_connect(int n)
289          }          }
290    
291          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
292            iflush();
293          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);          log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port);
294    
295          BBS_last_access_tm = t_used = time(0);          BBS_last_access_tm = t_used = time(0);
   
296          loop = 1;          loop = 1;
297    
298          while (loop && !SYS_server_exit)          while (loop && !SYS_server_exit)
# Line 300  int bbsnet_connect(int n) Line 301  int bbsnet_connect(int n)
301                  FD_SET(STDIN_FILENO, &read_fds);                  FD_SET(STDIN_FILENO, &read_fds);
302                  FD_SET(sock, &read_fds);                  FD_SET(sock, &read_fds);
303    
304                    FD_ZERO(&write_fds);
305                    FD_SET(STDIN_FILENO, &write_fds);
306                    FD_SET(sock, &write_fds);
307    
308                  timeout.tv_sec = 0;                  timeout.tv_sec = 0;
309                  timeout.tv_usec = 100 * 1000; // 0.1 second                  timeout.tv_usec = 100 * 1000; // 0.1 second
310    
311                  ret = select(sock + 1, &read_fds, NULL, NULL, &timeout);                  ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout);
312    
313                  if (ret == 0) // timeout                  if (ret == 0) // timeout
314                  {                  {
# Line 322  int bbsnet_connect(int n) Line 327  int bbsnet_connect(int n)
327                  }                  }
328                  else if (ret > 0)                  else if (ret > 0)
329                  {                  {
330                          if (FD_ISSET(STDIN_FILENO, &read_fds))                          if (FD_ISSET(STDIN_FILENO, &read_fds) && FD_ISSET(sock, &write_fds))
331                          {                          {
332                                    // Set STDIN as non-blocking
333                                    flags = fcntl(STDIN_FILENO, F_GETFL, 0);
334                                    fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
335    
336                                  len = read(STDIN_FILENO, buf, sizeof(buf));                                  len = read(STDIN_FILENO, buf, sizeof(buf));
337                                  if (len == 0)                                  if (len < 0)
338                                    {
339                                            if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
340                                            {
341                                                    log_error("read(STDIN) error (%d)\n", errno);
342                                                    loop = 0;
343                                            }
344                                    }
345                                    else if (len == 0)
346                                  {                                  {
347                                            log_error("read(STDIN) reach EOF\n");
348                                          loop = 0;                                          loop = 0;
349                                  }                                  }
350                                  write(sock, buf, (size_t)len);                                  else
351                                    {
352                                            offset = 0;
353                                            do
354                                            {
355                                                    ret = (int)write(sock, buf + offset, (size_t)(len - offset));
356                                                    if (ret < 0)
357                                                    {
358                                                            log_error("write(socket) error (%d)\n", errno);
359                                                            loop = 0;
360                                                            break;
361                                                    }
362                                                    offset += ret;
363                                            } while (offset < len);
364    
365                                            BBS_last_access_tm = time(0);
366                                    }
367    
368                                  BBS_last_access_tm = time(0);                                  // Restore STDIN flags
369                                    fcntl(STDIN_FILENO, F_SETFL, flags);
370                          }                          }
371                          if (FD_ISSET(sock, &read_fds))                          if (FD_ISSET(sock, &read_fds) && FD_ISSET(STDIN_FILENO, &write_fds))
372                          {                          {
373                                    // Set socket as non-blocking
374                                    flags = fcntl(sock, F_GETFL, 0);
375                                    fcntl(sock, F_SETFL, flags | O_NONBLOCK);
376    
377                                  len = read(sock, buf, sizeof(buf));                                  len = read(sock, buf, sizeof(buf));
378                                  if (len == 0)                                  if (len < 0)
379                                  {                                  {
380                                            if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
381                                            {
382                                                    log_error("read(socket) error (%d)\n", errno);
383                                                    loop = 0;
384                                            }
385                                    }
386                                    else if (len == 0)
387                                    {
388                                            log_error("read(socket) reach EOF\n");
389                                          loop = 0;                                          loop = 0;
390                                  }                                  }
391                                  write(STDOUT_FILENO, buf, (size_t)len);                                  else
392                                    {
393                                            offset = 0;
394                                            do
395                                            {
396                                                    ret = (int)write(STDOUT_FILENO, buf + offset, (size_t)(len - offset));
397                                                    if (ret < 0)
398                                                    {
399                                                            log_error("write(STDOUT) error (%d)\n", errno);
400                                                            loop = 0;
401                                                            break;
402                                                    }
403                                                    offset += ret;
404                                            } while (offset < len);
405                                    }
406    
407                                    // Restore socket flags
408                                    fcntl(sock, F_SETFL, flags);
409                          }                          }
410                  }                  }
411          }          }


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

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