/[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.27 by sysadm, Sat May 10 04:15:09 2025 UTC Revision 1.30 by sysadm, Sat May 10 14:37:04 2025 UTC
# Line 20  Line 20 
20  #include "io.h"  #include "io.h"
21  #include "screen.h"  #include "screen.h"
22  #include "menu.h"  #include "menu.h"
 #include "tcplib.h"  
23  #include <stdio.h>  #include <stdio.h>
24  #include <stdarg.h>  #include <stdarg.h>
25    #include <errno.h>
26    #include <string.h>
27    #include <stdlib.h>
28    #include <fcntl.h>
29    #include <time.h>
30    #include <unistd.h>
31    #include <netdb.h>
32    #include <sys/select.h>
33  #include <sys/ioctl.h>  #include <sys/ioctl.h>
34  #include <sys/socket.h>  #include <sys/socket.h>
35  #include <netinet/in.h>  #include <netinet/in.h>
36  #include <arpa/inet.h>  #include <arpa/inet.h>
 #include <time.h>  
 #include <unistd.h>  
 #include <netdb.h>  
37    
38  #define MENU_CONF_DELIM " \t\r\n"  #define MENU_CONF_DELIM " \t\r\n"
39    
# Line 136  static void process_bar(int n, int len) Line 140  static void process_bar(int n, int len)
140          moveto(4, 0);          moveto(4, 0);
141          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
142          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);          snprintf(buf, sizeof(buf), "            %3d%%              ", n * 100 / len);
143          strncpy(buf2, buf, (size_t) n);          strncpy(buf2, buf, (size_t)n);
144          buf2[n] = '\0';          buf2[n] = '\0';
145          prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);          prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n);
146          prints(" ------------------------------ \r\n");          prints(" ------------------------------ \r\n");
# Line 145  static void process_bar(int n, int len) Line 149  static void process_bar(int n, int len)
149    
150  int bbsnet_connect(int n)  int bbsnet_connect(int n)
151  {  {
152          int sock, result, loop;          int sock, flags, ret, loop, error;
153          ssize_t len;          ssize_t len;
154          struct sockaddr_in sin;          struct sockaddr_in sin;
155          char buf[LINE_BUFFER_LEN];          char buf[LINE_BUFFER_LEN];
156          fd_set testfds;          fd_set read_fds;
157            fd_set write_fds;
158          struct timeval timeout;          struct timeval timeout;
159          struct hostent *p_host = NULL;          struct hostent *p_host = NULL;
160          int rv, tos = 020, i;          int tos = 020, i;
161          char remote_addr[IP_ADDR_LEN];          char remote_addr[IP_ADDR_LEN];
162          int remote_port;          int remote_port;
163          time_t t_used;          time_t t_used;
# Line 207  int bbsnet_connect(int n) Line 212  int bbsnet_connect(int n)
212    
213          prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");          prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n");
214          process_bar(0, MAX_PROCESS_BAR_LEN);          process_bar(0, MAX_PROCESS_BAR_LEN);
215    
216            // Set socket as non-blocking
217            flags = fcntl(sock, F_GETFL, 0);
218            fcntl(sock, F_SETFL, flags | O_NONBLOCK);
219    
220            if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)
221            {
222                    if (errno != EINPROGRESS)
223                    {
224                            prints("\033[1;31m连接失败!\033[m\r\n");
225                            press_any_key();
226                            return -1;
227                    }
228            }
229    
230          for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)          for (i = 0; i < MAX_PROCESS_BAR_LEN; i++)
231          {          {
232                  ch = igetch(0); // 100 ms                  ch = igetch(0); // 0.1 second
233                  if (ch == KEY_NULL || ch == Ctrl('C') || SYS_server_exit)                  if (ch == Ctrl('C') || SYS_server_exit)
234                  {                  {
235                          return 0;                          return 0;
236                  }                  }
237    
238                  rv = NonBlockConnectEx(sock, (struct sockaddr *)&sin, sizeof(sin),                  FD_ZERO(&read_fds);
239                                                             400, (i == 0 ? 1 : 0)); // 400 ms                  FD_SET(sock, &read_fds);
240    
241                    FD_ZERO(&write_fds);
242                    FD_SET(sock, &write_fds);
243    
244                    timeout.tv_sec = 0;
245                    timeout.tv_usec = 400 * 1000; // 0.4 second
246    
247                    ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout);
248    
249                  if (rv == ERR_TCPLIB_TIMEOUT)                  if (ret == 0) // Timeout
250                  {                  {
251                          process_bar(i + 1, MAX_PROCESS_BAR_LEN);                          process_bar(i + 1, MAX_PROCESS_BAR_LEN);
                         continue;  
252                  }                  }
253                  else if (rv == 0)                  else if (ret < 0)
254                  {                  {
255                          break;                          if (errno != EINTR)
256                            {
257                                    log_error("select() error (%d) !\n", errno);
258                                    return -1;
259                            }
260                  }                  }
261                  else                  // ret > 0
262                    else if (FD_ISSET(sock, &read_fds) || FD_ISSET(sock, &write_fds))
263                  {                  {
264                          prints("\033[1;31m连接失败!\033[m\r\n");                          len = sizeof(error);
265                          press_any_key();                          if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0)
266                          return -1;                          {
267                                    log_error("getsockopt() error (%d) !\n", error);
268                                    return -1;
269                            }
270    
271                            break; // connected
272                  }                  }
273          }          }
274          if (i == MAX_PROCESS_BAR_LEN)          if (i == MAX_PROCESS_BAR_LEN)
# Line 240  int bbsnet_connect(int n) Line 277  int bbsnet_connect(int n)
277                  press_any_key();                  press_any_key();
278                  return -1;                  return -1;
279          }          }
280    
281            fcntl(sock, F_SETFL, flags); /* restore file status flags */
282          setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(int));          setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(int));
283    
284          prints("\033[1;31m连接成功!\033[m\r\n");          prints("\033[1;31m连接成功!\033[m\r\n");
# Line 251  int bbsnet_connect(int n) Line 290  int bbsnet_connect(int n)
290    
291          while (loop && !SYS_server_exit)          while (loop && !SYS_server_exit)
292          {          {
293                  FD_ZERO(&testfds);                  FD_ZERO(&read_fds);
294                  FD_SET(STDIN_FILENO, &testfds);                  FD_SET(STDIN_FILENO, &read_fds);
295                  FD_SET(sock, &testfds);                  FD_SET(sock, &read_fds);
296            
297                  timeout.tv_sec = 0;                  timeout.tv_sec = 0;
298                  timeout.tv_usec = 100 * 1000; // 0.1 second                  timeout.tv_usec = 100 * 1000; // 0.1 second
299    
300                  result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL,                  ret = select(FD_SETSIZE, &read_fds, NULL, NULL, &timeout);
                                                                   (fd_set *)NULL, &timeout);  
301    
302                  if (result == 0)                  if (ret == 0) // timeout
303                  {                  {
304                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
305                          {                          {
306                                  loop = 0;                                  loop = 0;
307                          }                          }
308                  }                  }
309                  if (result < 0)                  else if (ret < 0)
310                  {                  {
311                          log_error("select() error (%d) !\n", result);                          if (errno != EINTR)
312                          loop = 0;                          {
313                                    log_error("select() error (%d) !\n", errno);
314                                    loop = 0;
315                            }
316                  }                  }
317                  if (result > 0)                  else if (ret > 0)
318                  {                  {
319                          if (FD_ISSET(STDIN_FILENO, &testfds))                          if (FD_ISSET(STDIN_FILENO, &read_fds))
320                          {                          {
321                                  len = read(STDIN_FILENO, buf, sizeof(buf));                                  len = read(STDIN_FILENO, buf, sizeof(buf));
322                                  if (len == 0)                                  if (len == 0)
# Line 283  int bbsnet_connect(int n) Line 324  int bbsnet_connect(int n)
324                                          loop = 0;                                          loop = 0;
325                                  }                                  }
326                                  write(sock, buf, (size_t)len);                                  write(sock, buf, (size_t)len);
327    
328                                    BBS_last_access_tm = time(0);
329                          }                          }
330                          if (FD_ISSET(sock, &testfds))                          if (FD_ISSET(sock, &read_fds))
331                          {                          {
332                                  len = read(sock, buf, sizeof(buf));                                  len = read(sock, buf, sizeof(buf));
333                                  if (len == 0)                                  if (len == 0)
# Line 293  int bbsnet_connect(int n) Line 336  int bbsnet_connect(int n)
336                                  }                                  }
337                                  write(STDOUT_FILENO, buf, (size_t)len);                                  write(STDOUT_FILENO, buf, (size_t)len);
338                          }                          }
                         BBS_last_access_tm = time(0);  
339                  }                  }
340          }          }
341    
# Line 379  int bbs_net() Line 421  int bbs_net()
421                  ch = igetch(0);                  ch = igetch(0);
422                  switch (ch)                  switch (ch)
423                  {                  {
                 case KEY_NULL:  
                         return -1;  
424                  case Ctrl('C'):                  case Ctrl('C'):
425                          return 0;                          return 0;
426                    case KEY_NULL:
427                  case KEY_TIMEOUT:                  case KEY_TIMEOUT:
428                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)                          if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME)
429                          {                          {
430                                  return 0;                                  return 0;
431                          }                          }
432                          break;                          continue;
433                  case CR:                  case CR:
434                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;                          pos = bbsnet_menu.p_menu[0]->item_cur_pos;
435                          bbsnet_connect(pos);                          bbsnet_connect(pos);


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

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