Przeglądaj źródła

Change -1, -2, -3 to TIMEOUT, HANGUP, OUTOFTIME

Steve Thielemann 3 lat temu
rodzic
commit
95161f245c
3 zmienionych plików z 67 dodań i 13 usunięć
  1. 61 12
      door.cpp
  2. 5 0
      door.h
  3. 1 1
      test-door.cpp

+ 61 - 12
door.cpp

@@ -656,6 +656,9 @@ ofstream &Door::log(void) {
  *
  * This uses select to check if we have received any keys.  This does not use
  * pushback.
+ *
+ * If HANGUP, OUTOFTIME, return true
+ *
  * @return true
  * @return false
  */
@@ -665,10 +668,10 @@ bool Door::haskey(void) {
   int select_ret = -1;
 
   if (hangup)
-    return -2;
+    return true; // HANGUP;
 
   if (time_left < 2)
-    return -3;
+    return true; // OUTOFTIME;
 
   while (select_ret == -1) {
     FD_ZERO(&socket_set);
@@ -683,7 +686,7 @@ bool Door::haskey(void) {
         continue;
       log() << "hangup detected" << std::endl;
       hangup = true;
-      return (-2);
+      return true; // HANGUP;
     }
     if (select_ret == 0)
       return false;
@@ -709,10 +712,10 @@ signed int Door::getch(void) {
   char key;
 
   if (door::hangup)
-    return -2;
+    return HANGUP;
 
   if (time_left < 2)
-    return -3;
+    return OUTOFTIME;
 
   while (select_ret == -1) {
     FD_ZERO(&socket_set);
@@ -731,10 +734,10 @@ signed int Door::getch(void) {
         continue;
       log() << "hangup detected" << std::endl;
       door::hangup = true;
-      return (-2);
+      return HANGUP;
     }
     if (select_ret == 0)
-      return (-1);
+      return TIMEOUT;
   }
 
   recv_ret = read(STDIN_FILENO, &key, 1);
@@ -742,7 +745,7 @@ signed int Door::getch(void) {
     // possibly log this.
     log() << "hangup" << std::endl;
     hangup = true;
-    return -2;
+    return HANGUP;
   }
   // debug weird keys/layouts.
   // log() << "read " << std::hex << (int)key << std::endl;
@@ -1024,10 +1027,10 @@ signed int Door::sleep_key(int secs) {
   char key;
   */
   if (hangup)
-    return -2;
+    return HANGUP;
 
   if (time_left < 2)
-    return -3;
+    return OUTOFTIME;
 
   while (select_ret == -1) {
     FD_ZERO(&socket_set);
@@ -1043,10 +1046,56 @@ signed int Door::sleep_key(int secs) {
         continue;
       hangup = true;
       log() << "hangup detected" << std::endl;
-      return (-2);
+      return HANGUP;
+    }
+    if (select_ret == 0)
+      return TIMEOUT;
+  }
+  return getkey();
+}
+
+/**
+ * @brief Waits miliseconds for a keypress.
+ *
+ * returns key, or -1 on timeout (seconds passed).
+ * -2 hangup
+ * -3 out of time
+ *
+ * @param msecs
+ * @return signed int
+ */
+signed int Door::sleep_ms_key(int msecs) {
+  fd_set socket_set;
+  struct timeval tv;
+  int select_ret = -1;
+  /*
+  int recv_ret;
+  char key;
+  */
+  if (hangup)
+    return HANGUP;
+
+  if (time_left < 2)
+    return OUTOFTIME;
+
+  while (select_ret == -1) {
+    FD_ZERO(&socket_set);
+    FD_SET(STDIN_FILENO, &socket_set);
+
+    tv.tv_sec = 0;
+    tv.tv_usec = msecs * 1000;
+
+    select_ret = select(STDIN_FILENO + 1, &socket_set, NULL, NULL, &tv);
+    // select(STDIN_FILENO + 1, &socket_set, NULL, NULL, bWait ? NULL : &tv);
+    if (select_ret == -1) {
+      if (errno == EINTR)
+        continue;
+      hangup = true;
+      log() << "hangup detected" << std::endl;
+      return HANGUP;
     }
     if (select_ret == 0)
-      return (-1);
+      return TIMEOUT;
   }
   return getkey();
 }

+ 5 - 0
door.h

@@ -49,6 +49,10 @@
 
 #define XKEY_UNKNOWN 0x1111
 
+#define TIMEOUT -1
+#define HANGUP -2
+#define OUTOFTIME -3
+
 /**
  * @brief The BBS door project.
  * This is an attempt at writing a C++ BBS door toolkit.
@@ -268,6 +272,7 @@ public:
   signed int getkey(void);
   bool haskey(void);
   signed int sleep_key(int secs);
+  signed int sleep_ms_key(int msecs);
   std::string input_string(int max);
   int get_one_of(const char *keys);
 };

+ 1 - 1
test-door.cpp

@@ -146,7 +146,7 @@ TEST_F(DoorTest, GetKeyEnterNull) {
   EXPECT_EQ(0x0d, d->getkey());
   EXPECT_EQ('!', d->getkey());
   EXPECT_TRUE(door::pushback.empty());
-  EXPECT_EQ(-1, d->getkey());
+  EXPECT_EQ(TIMEOUT, d->getkey());
 }
 
 TEST_F(DoorTest, LineOutput) {