diff options
-rw-r--r-- | pttbbs/include/proto.h | 47 | ||||
-rw-r--r-- | pttbbs/mbbsd/chicken.c | 4 | ||||
-rw-r--r-- | pttbbs/mbbsd/io.c | 49 | ||||
-rw-r--r-- | pttbbs/mbbsd/talk.c | 13 |
4 files changed, 78 insertions, 35 deletions
diff --git a/pttbbs/include/proto.h b/pttbbs/include/proto.h index 7207df5f..d0777dda 100644 --- a/pttbbs/include/proto.h +++ b/pttbbs/include/proto.h @@ -282,23 +282,40 @@ void oflush(void); int process_pager_keys(int ch); // input api (old flavor) -int num_in_buf(void); -int wait_input(float f, int bIgnoreBuf); -int peek_input(float f, int c); -// int igetch(void); -// int input_isfull(); -// void drop_input(void); -void add_io(int fd, int timeout); +int num_in_buf(void); // vkey_is_ready() / vkey_is_typeahead() +int wait_input(float f, int bIgnoreBuf);// vkey_poll +int peek_input(float f, int c); // vkey_prefetch() / vkey_is_prefeteched +// int igetch(void); // -> vkey() +// int input_isfull(); // -> vkey_is_full() +// void drop_input(void); // -> vkey_purge() +// void add_io(int fd, int timeout); // -> vkey_attach / vkey_detach // new input api -int vkey(); // identical to igetch -void vkey_purge(); // identical to drop_input -int vkey_is_full(); // identical to input_isfull -int vkey_detach(void); // works like to add_io(0, 0) -int vkey_attach(int fd); // works like add_io(fd, ...) -int vkey_is_ready(); // works like (num_in_buf() > 0) -int vkey_is_typeahead(void); // quick check if input buffer has data arrived (maybe not ready yet) - +/* nios.c / io.c */ +///////// virtual key: convert from cin(fd) -> buffer -> virtual key ////////// +// timeout: in milliseconds. 0 for 'do not block' and INFTIM(-1) for 'forever' +/////////////////////////////////////////////////////////////////////////////// +// initialization +void vkey_init(void); // initialize virtual key system +// key value retrieval +int vkey(void); // receive and block infinite time for next key +int vkey_peek(void); // peek one key from queue (KEY_INCOMPLETE if empty) +// status +int vkey_poll(int timeout); // poll for timeout milliseconds. return 1 for ready oterwise 0 +int vkey_is_ready(void); // determine if input buffer is ready for a key input +int vkey_is_typeahead(void);// check if input buffer has data arrived (maybe not ready yet) +// additional fd to listen +int vkey_attach(int fd); // attach (and replace) additional fd to vkey API, returning previous attached fd +int vkey_detach(void); // detach any additional fd and return previous attached fd +// input buffer management +int vkey_is_full(void); // test if input buffer is full +void vkey_purge(void); // discard clear all data in input buffer +int vkey_prefetch(int timeout);// try to fetch data from fd to buffer unless timeout +int vkey_is_prefetched(char c);// check if c (in raw data form) is already in prefetched buffer +///////////////////////////////////////////////////////////////////////////// +#ifdef EXP_NIOS +#define USE_NIOS_VKEY +#endif /* kaede */ char*Ptt_prints(char *str, size_t size, int mode); diff --git a/pttbbs/mbbsd/chicken.c b/pttbbs/mbbsd/chicken.c index 5699632e..a69971c4 100644 --- a/pttbbs/mbbsd/chicken.c +++ b/pttbbs/mbbsd/chicken.c @@ -1029,11 +1029,11 @@ chickenpk(int fd) } show_chicken_data(ochicken, mychicken); - add_io(fd, 3); /* 把fd加到vkey監視 */ + vkey_attach(fd); // 把fd加到vkey監視 // add_io(fd, 3); while (1) { r = random(); - ch = vkey(); + ch = vkey_poll(3 * SECS2MILLISECONDS) ? vkey() : I_TIMEOUT; show_chicken_data(ochicken, mychicken); time_diff(mychicken); diff --git a/pttbbs/mbbsd/io.c b/pttbbs/mbbsd/io.c index 973518b7..36ea44ba 100644 --- a/pttbbs/mbbsd/io.c +++ b/pttbbs/mbbsd/io.c @@ -70,6 +70,10 @@ static inline int read_wrapper(int fd, void *buf, size_t count) { static inline int write_wrapper(int fd, void *buf, size_t count) { return (*write_type)(fd, buf, count); } +#else +#define write_wrapper write +#define read_wrapper read +// #define input_wrapper(buf,count) count #endif /* ----------------------------------------------------- */ @@ -120,11 +124,7 @@ oflush(void) { if (obufsize) { STATINC(STAT_SYSWRITESOCKET); -#ifdef CONVERT write_wrapper(1, outbuf, obufsize); -#else - write(1, outbuf, obufsize); -#endif obufsize = 0; } @@ -162,11 +162,7 @@ output(const char *s, int len) if (obufsize + len > OBUFSIZE) { STATINC(STAT_SYSWRITESOCKET); -#ifdef CONVERT write_wrapper(1, outbuf, obufsize); -#else - write(1, outbuf, obufsize); -#endif obufsize = 0; } memcpy(outbuf + obufsize, s, len); @@ -598,7 +594,8 @@ igetch(void) /* * wait user input anything for f seconds. - * if f < 0, then wait forever. + * if f == 0, return immediately + * if f < 0, wait forever. * Return 1 if anything available. */ inline int @@ -613,25 +610,44 @@ wait_input(float f, int bIgnoreBuf) FD_ZERO(&readfds); FD_SET(0, &readfds); + if (i_newfd) FD_SET(i_newfd, &readfds); + // adjust time if(f > 0) { tv.tv_sec = (long) f; tv.tv_usec = (f - (long)f) * 1000000L; - } else + } + else if (f == 0) + { + tv.tv_sec = 0; + tv.tv_usec = 0; + } + else if (f < 0) + { ptv = NULL; + } #ifdef STATINC STATINC(STAT_SYSSELECT); #endif do { - if(!bIgnoreBuf && num_in_buf() > 0) - return 1; - sel = select(1, &readfds, NULL, NULL, ptv); + assert(i_newfd >= 0); // if == 0, use only fd=0 => count sill u_newfd+1. + sel = select(i_newfd+1, &readfds, NULL, NULL, ptv); + } while (sel < 0 && errno == EINTR); /* EINTR, interrupted. I don't care! */ + // XXX should we abort? (from dogetch) + if (sel < 0 && errno != EINTR) + { + abort_bbs(0); + /* raise(SIGHUP); */ + } + + // syncnow(); + if(sel == 0) return 0; @@ -720,6 +736,13 @@ vkey(void) return igetch(); } +inline int +vkey_poll(int ms) +{ + if (ms) refresh(); + // XXX handle I_OTHERDATA? + return wait_input(ms / (double)MILLISECONDS, 0); +} /* vim:sw=4 */ diff --git a/pttbbs/mbbsd/talk.c b/pttbbs/mbbsd/talk.c index bd02a86d..aa844990 100644 --- a/pttbbs/mbbsd/talk.c +++ b/pttbbs/mbbsd/talk.c @@ -1167,11 +1167,13 @@ int make_connection_to_somebody(userinfo_t *uin, int timeout){ close(sock); return -1; } - add_io(sock, timeout); + + vkey_attach(sock); // add_io(sock, timeout); while (1) { - ch = vkey(); - if (ch == I_TIMEOUT) { + if (vkey_poll(timeout * MILLISECONDS)) { + ch = vkey(); + } else { // if (ch == I_TIMEOUT) { ch = uin->mode; if (!ch && uin->chatid[0] == 1 && uin->destuip == currutmp - &SHM->uinfo[0]) { @@ -1191,10 +1193,11 @@ int make_connection_to_somebody(userinfo_t *uin, int timeout){ return -1; } else { // change to longer timeout - add_io(sock, 20); + timeout = 20; // add_io(sock, 20); move(0, 0); outs("再"); bell(); + refresh(); uin->destuip = currutmp - &SHM->uinfo[0]; if (pid <= 0 || kill(pid, SIGUSR1) == -1) { @@ -1211,7 +1214,7 @@ int make_connection_to_somebody(userinfo_t *uin, int timeout){ if (ch == I_OTHERDATA) break; - if (ch == '\004') { + if (ch == Ctrl('D')) { vkey_detach(); close(sock); currutmp->sockactive = currutmp->destuid = 0; |