--- lbbs/src/bbs_net.c 2025/05/10 02:14:46 1.26 +++ lbbs/src/bbs_net.c 2025/05/11 14:52:26 1.37 @@ -20,16 +20,21 @@ #include "io.h" #include "screen.h" #include "menu.h" -#include "tcplib.h" #include #include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include +#include #include -#include -#include -#include #define MENU_CONF_DELIM " \t\r\n" @@ -117,43 +122,52 @@ int load_bbsnet_conf(const char *file_co static void process_bar(int n, int len) { - char buf[256]; - char buf2[256]; - char *ptr; - char *ptr2; - char *ptr3; + char buf[LINE_BUFFER_LEN]; + char buf2[LINE_BUFFER_LEN]; + + if (len > LINE_BUFFER_LEN) + { + len = LINE_BUFFER_LEN - 1; + } + if (n < 0) + { + n = 0; + } + else if (n > len) + { + n = len; + } moveto(4, 0); prints(" ------------------------------ \r\n"); - snprintf(buf2, sizeof(buf2), " %3d%% ", n * 100 / len); - ptr = buf; - ptr2 = buf2; - ptr3 = buf + n; - while (ptr != ptr3) - *ptr++ = *ptr2++; - *ptr++ = '\x1b'; - *ptr++ = '['; - *ptr++ = '4'; - *ptr++ = '4'; - *ptr++ = 'm'; - while (*ptr2 != '\0') - *ptr++ = *ptr2++; - *ptr++ = '\0'; - prints("|\033[46m%s\033[m|\r\n", buf); + snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); + strncpy(buf2, buf, (size_t)n); + buf2[n] = '\0'; + prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n); prints(" ------------------------------ \r\n"); iflush(); } int bbsnet_connect(int n) { - int sock, result, loop; - ssize_t len; + int sock, ret, loop, error; + int flags_sock; + int flags_stdin; + int flags_stdout; + int len; struct sockaddr_in sin; - char buf[LINE_BUFFER_LEN]; - fd_set testfds; + char input_buf[LINE_BUFFER_LEN]; + char output_buf[LINE_BUFFER_LEN]; + int input_buf_len = 0; + int output_buf_len = 0; + int input_buf_offset = 0; + int output_buf_offset = 0; + fd_set read_fds; + fd_set write_fds; struct timeval timeout; struct hostent *p_host = NULL; - int rv, tos = 020, i; + int tos; + int i; char remote_addr[IP_ADDR_LEN]; int remote_port; time_t t_used; @@ -186,14 +200,13 @@ int bbsnet_connect(int n) } sin.sin_family = AF_INET; - sin.sin_addr.s_addr = - (strnlen(hostaddr_server, sizeof(hostaddr_server)) > 0 ? inet_addr(hostaddr_server) : INADDR_ANY); + sin.sin_addr.s_addr = (hostaddr_server[0] != '\0' ? inet_addr(hostaddr_server) : INADDR_ANY); sin.sin_port = 0; if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) { - log_error("Bind address %s:%u failed\n", - inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); + log_error("Bind address %s:%u failed (%d)\n", + inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), errno); return -2; } @@ -206,33 +219,65 @@ int bbsnet_connect(int n) remote_addr[sizeof(remote_addr) - 1] = '\0'; remote_port = ntohs(sin.sin_port); - prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按Ctrl-C中断。\033[m\r\n"); + prints("\033[1;32m穿梭进度条提示您当前已使用的时间,按\033[1;33mCtrl+C\033[1;32m中断。\033[m\r\n"); process_bar(0, MAX_PROCESS_BAR_LEN); + + // Set socket as non-blocking + flags_sock = fcntl(sock, F_GETFL, 0); + fcntl(sock, F_SETFL, flags_sock | O_NONBLOCK); + + if ((ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0) + { + if (errno != EINPROGRESS) + { + prints("\033[1;31m连接失败!\033[m\r\n"); + press_any_key(); + return -1; + } + } + for (i = 0; i < MAX_PROCESS_BAR_LEN; i++) { - ch = igetch(0); // 100 ms - if (ch == KEY_NULL || ch == Ctrl('C') || SYS_server_exit) + ch = igetch(0); // 0.1 second + if (ch == Ctrl('C') || SYS_server_exit) { return 0; } - rv = NonBlockConnectEx(sock, (struct sockaddr *)&sin, sizeof(sin), - 400, (i == 0 ? 1 : 0)); // 400 ms + FD_ZERO(&read_fds); + FD_SET(sock, &read_fds); + + FD_ZERO(&write_fds); + FD_SET(sock, &write_fds); - if (rv == ERR_TCPLIB_TIMEOUT) + timeout.tv_sec = 0; + timeout.tv_usec = 400 * 1000; // 0.4 second + + ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout); + + if (ret == 0) // Timeout { process_bar(i + 1, MAX_PROCESS_BAR_LEN); - continue; } - else if (rv == 0) + else if (ret < 0) { - break; + if (errno != EINTR) + { + log_error("select() error (%d) !\n", errno); + return -1; + } } - else + // ret > 0 + else if (FD_ISSET(sock, &read_fds) || FD_ISSET(sock, &write_fds)) { - prints("\033[1;31m连接失败!\033[m\r\n"); - press_any_key(); - return -1; + len = sizeof(error); + if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len) < 0) + { + log_error("getsockopt() error (%d) !\n", error); + return -1; + } + + break; // connected } } if (i == MAX_PROCESS_BAR_LEN) @@ -241,63 +286,155 @@ int bbsnet_connect(int n) press_any_key(); return -1; } - setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(int)); + + tos = IPTOS_LOWDELAY; + if (setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) + { + log_error("setsockopt IP_TOS=%d error (%d)\n", tos, errno); + } prints("\033[1;31m连接成功!\033[m\r\n"); + iflush(); log_std("BBSNET connect to %s:%d\n", remote_addr, remote_port); - BBS_last_access_tm = t_used = time(0); + // Set STDIN/STDOUT as non-blocking + flags_stdin = fcntl(STDIN_FILENO, F_GETFL, 0); + flags_stdout = fcntl(STDOUT_FILENO, F_GETFL, 0); + fcntl(STDIN_FILENO, F_SETFL, flags_stdin | O_NONBLOCK); + fcntl(STDOUT_FILENO, F_SETFL, flags_stdout | O_NONBLOCK); + BBS_last_access_tm = t_used = time(0); loop = 1; while (loop && !SYS_server_exit) { - FD_ZERO(&testfds); - FD_SET(STDIN_FILENO, &testfds); - FD_SET(sock, &testfds); - + FD_ZERO(&read_fds); + FD_SET(STDIN_FILENO, &read_fds); + FD_SET(sock, &read_fds); + + FD_ZERO(&write_fds); + FD_SET(STDOUT_FILENO, &write_fds); + FD_SET(sock, &write_fds); + timeout.tv_sec = 0; timeout.tv_usec = 100 * 1000; // 0.1 second - result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL, - (fd_set *)NULL, &timeout); + ret = select(sock + 1, &read_fds, &write_fds, NULL, &timeout); - if (result == 0) + if (ret == 0) // timeout { if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) { loop = 0; } } - if (result < 0) + else if (ret < 0) { - log_error("select() error (%d) !\n", result); - loop = 0; + if (errno != EINTR) + { + log_error("select() error (%d) !\n", errno); + loop = 0; + } } - if (result > 0) + else // if (ret > 0) { - if (FD_ISSET(STDIN_FILENO, &testfds)) + if ((input_buf_offset >= input_buf_len) && FD_ISSET(STDIN_FILENO, &read_fds)) { - len = read(STDIN_FILENO, buf, sizeof(buf)); - if (len == 0) + ret = (int)read(STDIN_FILENO, input_buf, sizeof(input_buf)); + if (ret < 0) + { + if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) + { + log_error("read(STDIN) error (%d)\n", errno); + loop = 0; + } + } + else if (ret == 0) // broken pipe { loop = 0; } - write(sock, buf, (size_t)len); + else + { + input_buf_len = ret; + input_buf_offset = 0; + + BBS_last_access_tm = time(0); + } } - if (FD_ISSET(sock, &testfds)) + + if ((input_buf_offset < input_buf_len) && FD_ISSET(sock, &write_fds)) { - len = read(sock, buf, sizeof(buf)); - if (len == 0) + ret = (int)write(sock, input_buf + input_buf_offset, (size_t)(input_buf_len - input_buf_offset)); + if (ret < 0) + { + if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) + { + log_error("write(socket) error (%d)\n", errno); + loop = 0; + } + } + else if (ret == 0) // broken pipe { loop = 0; } - write(STDOUT_FILENO, buf, (size_t)len); + else + { + input_buf_offset += ret; + } + } + + if ((output_buf_offset >= output_buf_len) && FD_ISSET(sock, &read_fds)) + { + ret = (int)read(sock, output_buf, sizeof(output_buf)); + if (ret < 0) + { + if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) + { + log_error("read(socket) error (%d)\n", errno); + loop = 0; + } + } + else if (ret == 0) // broken pipe + { + loop = 0; + } + else + { + output_buf_len = ret; + output_buf_offset = 0; + } + } + + if ((output_buf_offset < output_buf_len) && FD_ISSET(STDOUT_FILENO, &write_fds)) + { + ret = (int)write(STDOUT_FILENO, output_buf + output_buf_offset, (size_t)(output_buf_len - output_buf_offset)); + if (ret < 0) + { + if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) + { + log_error("write(STDOUT) error (%d)\n", errno); + loop = 0; + } + } + else if (ret == 0) // broken pipe + { + loop = 0; + } + else + { + output_buf_offset += ret; + } } - BBS_last_access_tm = time(0); } } + // Restore STDIN/STDOUT flags + fcntl(STDIN_FILENO, F_SETFL, flags_stdin); + fcntl(STDOUT_FILENO, F_SETFL, flags_stdout); + + // Restore socket flags + fcntl(sock, F_SETFL, flags_sock); + if (close(sock) == -1) { log_error("Close socket failed\n"); @@ -380,16 +517,15 @@ int bbs_net() ch = igetch(0); switch (ch) { - case KEY_NULL: - return -1; - case Ctrl('C'): + case KEY_NULL: // broken pipe + case Ctrl('C'): // user cancel return 0; case KEY_TIMEOUT: if (time(0) - BBS_last_access_tm >= MAX_DELAY_TIME) { return 0; } - break; + continue; case CR: pos = bbsnet_menu.p_menu[0]->item_cur_pos; bbsnet_connect(pos);