You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #define _CRT_SECURE_NO_DEPRECATE 1
  28. #ifdef POSIX
  29. #include <signal.h>
  30. #include <termios.h>
  31. #include <unistd.h>
  32. #endif // POSIX
  33. #include <cassert>
  34. #include "talk/base/logging.h"
  35. #include "talk/base/messagequeue.h"
  36. #include "talk/base/stringutils.h"
  37. #include "talk/examples/call/console.h"
  38. #include "talk/examples/call/callclient.h"
  39. #ifdef POSIX
  40. static void DoNothing(int unused) {}
  41. #endif
  42. Console::Console(talk_base::Thread *thread, CallClient *client) :
  43. client_(client),
  44. client_thread_(thread),
  45. console_thread_(new talk_base::Thread()) {}
  46. Console::~Console() {
  47. Stop();
  48. }
  49. void Console::Start() {
  50. if (!console_thread_) {
  51. // stdin was closed in Stop(), so we can't restart.
  52. LOG(LS_ERROR) << "Cannot re-start";
  53. return;
  54. }
  55. if (console_thread_->started()) {
  56. LOG(LS_WARNING) << "Already started";
  57. return;
  58. }
  59. console_thread_->Start();
  60. console_thread_->Post(this, MSG_START);
  61. }
  62. void Console::Stop() {
  63. if (console_thread_ && console_thread_->started()) {
  64. #ifdef WIN32
  65. CloseHandle(GetStdHandle(STD_INPUT_HANDLE));
  66. #else
  67. close(fileno(stdin));
  68. // This forces the read() in fgets() to return with errno = EINTR. fgets()
  69. // will retry the read() and fail, thus returning.
  70. pthread_kill(console_thread_->GetPThread(), SIGUSR1);
  71. #endif
  72. console_thread_->Stop();
  73. console_thread_.reset();
  74. }
  75. }
  76. void Console::SetEcho(bool on) {
  77. #ifdef WIN32
  78. HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
  79. if ((hIn == INVALID_HANDLE_VALUE) || (hIn == NULL))
  80. return;
  81. DWORD mode;
  82. if (!GetConsoleMode(hIn, &mode))
  83. return;
  84. if (on) {
  85. mode = mode | ENABLE_ECHO_INPUT;
  86. } else {
  87. mode = mode & ~ENABLE_ECHO_INPUT;
  88. }
  89. SetConsoleMode(hIn, mode);
  90. #else
  91. const int fd = fileno(stdin);
  92. if (fd == -1)
  93. return;
  94. struct termios tcflags;
  95. if (tcgetattr(fd, &tcflags) == -1)
  96. return;
  97. if (on) {
  98. tcflags.c_lflag |= ECHO;
  99. } else {
  100. tcflags.c_lflag &= ~ECHO;
  101. }
  102. tcsetattr(fd, TCSANOW, &tcflags);
  103. #endif
  104. }
  105. void Console::PrintLine(const char* format, ...) {
  106. va_list ap;
  107. va_start(ap, format);
  108. char buf[4096];
  109. int size = vsnprintf(buf, sizeof(buf), format, ap);
  110. assert(size >= 0);
  111. assert(size < static_cast<int>(sizeof(buf)));
  112. buf[size] = '\0';
  113. printf("%s\n", buf);
  114. fflush(stdout);
  115. va_end(ap);
  116. }
  117. void Console::RunConsole() {
  118. char input_buffer[128];
  119. while (fgets(input_buffer, sizeof(input_buffer), stdin) != NULL) {
  120. client_thread_->Post(this, MSG_INPUT,
  121. new talk_base::TypedMessageData<std::string>(input_buffer));
  122. }
  123. }
  124. void Console::OnMessage(talk_base::Message *msg) {
  125. switch (msg->message_id) {
  126. case MSG_START:
  127. #ifdef POSIX
  128. // Install a no-op signal so that we can abort RunConsole() by raising
  129. // SIGUSR1.
  130. struct sigaction act;
  131. act.sa_handler = &DoNothing;
  132. sigemptyset(&act.sa_mask);
  133. act.sa_flags = 0;
  134. if (sigaction(SIGUSR1, &act, NULL) < 0) {
  135. LOG(LS_WARNING) << "Can't install signal";
  136. }
  137. #endif
  138. RunConsole();
  139. break;
  140. case MSG_INPUT:
  141. talk_base::TypedMessageData<std::string> *data =
  142. static_cast<talk_base::TypedMessageData<std::string>*>(msg->pdata);
  143. client_->ParseLine(data->data());
  144. break;
  145. }
  146. }