session.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #ifndef SESSION_H
  2. #define SESSION_H
  3. #include <boost/asio.hpp>
  4. #include <boost/asio/ip/basic_resolver.hpp>
  5. #include <boost/asio/signal_set.hpp>
  6. #include <map>
  7. #include <string>
  8. #include "director.h"
  9. #include "session_common.h"
  10. // don't want this here!
  11. #include "dispatchers.h"
  12. #define MAX_BUFFER 256
  13. /*
  14. The Session:
  15. */
  16. class Session : public std::enable_shared_from_this<Session> {
  17. public:
  18. Session(boost::asio::ip::tcp::socket socket,
  19. boost::asio::io_service &io_service, std::string hostname,
  20. std::string port, bool server_telnet_);
  21. ~Session();
  22. void start(void);
  23. const std::string &get_prompt(void);
  24. void set_prompt(const std::string &prompt);
  25. void to_client(const std::string &message, bool log = true);
  26. void to_server(const std::string &message);
  27. /*
  28. DispatchSettings save_settings(void);
  29. void restore_settings(const DispatchSettings &ss);
  30. */
  31. // The session line parsing needs show_client
  32. // these move to Director
  33. // bool show_client = true;
  34. bool talk_direct = true;
  35. bool server_telnet;
  36. void post(notifyFunc nf);
  37. private:
  38. Director director;
  39. void on_server_line(const std::string &line, const std::string &raw_line);
  40. void on_server_prompt(const std::string &prompt,
  41. const std::string &raw_prompt);
  42. void parse_auth(void);
  43. void on_connect(const boost::system::error_code error);
  44. void server_read(void);
  45. void on_resolve(const boost::system::error_code error,
  46. const boost::asio::ip::tcp::resolver::results_type results);
  47. void client_read(void);
  48. void client_input(const std::string &input);
  49. void on_shutdown(boost::system::error_code ec);
  50. void split_lines(std::string line);
  51. void process_lines(std::string &received);
  52. private:
  53. /*
  54. Move to director -?
  55. StringFunc SL_parser;
  56. void SL_cimline(const std::string &line);
  57. void SL_thiefline(const std::string &line);
  58. void SL_sectorline(const std::string &line);
  59. void SL_portline(const std::string &line);
  60. void SL_warpline(const std::string &line);
  61. */
  62. void set_prompt_timer(void);
  63. void reset_prompt_timer(void);
  64. void on_prompt_timeout(const boost::system::error_code error);
  65. void stayin_alive(const boost::system::error_code error);
  66. void start_keepin_alive(void);
  67. int time_ms;
  68. int keepalive_secs;
  69. // Move this to director. This controls if the "keep alive" fires or not.
  70. bool active = false;
  71. /* Move to director.
  72. MainDispatch main;
  73. void proxy_activate(void);
  74. void proxy_deactivate(void);
  75. // std::stack<Dispatch *> director;
  76. bool active = false;
  77. */
  78. /**
  79. * The client's socket
  80. */
  81. boost::asio::ip::tcp::socket socket_;
  82. boost::asio::io_service &io_service_;
  83. boost::asio::ip::tcp::resolver resolver_;
  84. /**
  85. * The server's socket
  86. */
  87. boost::asio::ip::tcp::socket server_;
  88. /**
  89. * The time that we'll use to fire off the "prompt" event.
  90. *
  91. * The idea being that we'd receive chars, processing lines.
  92. * And if we have something in server_prompt start the timer.
  93. * If we receive more, process lines, if !server_prompt.empty()
  94. * reset the timer.
  95. *
  96. * If the timer fires (and isn't aborted), fire off the prompt.
  97. *
  98. * I'm not so sure about this -- because this adds delay to the
  99. * proxy. [It might be better to just fire off "some" text, and
  100. * have it ignored, rather then adding a delay.] Or, this might
  101. * not matter at all, I'm thinking milliseconds here!
  102. */
  103. boost::asio::high_resolution_timer prompt_timer_;
  104. /**
  105. * Keep connection alive, don't timeout.
  106. *
  107. * This gets set by to_server config[keepalive], and sends a
  108. * space ' ' if we haven't sent anything to the server in that
  109. * many seconds.
  110. */
  111. boost::asio::high_resolution_timer keep_alive_;
  112. /**
  113. * What characters have been received from the server,
  114. * that weren't \n terminated?
  115. *
  116. * This needs to be reset/cleared if there's a \r (carriage return).
  117. */
  118. std::string server_prompt;
  119. /**
  120. * The client read buffer.
  121. *
  122. * This is too big, we don't get that many characters from the client.
  123. *
  124. */
  125. char read_buffer[MAX_BUFFER];
  126. /**
  127. * The server read buffer.
  128. */
  129. char server_buffer[MAX_BUFFER];
  130. /**
  131. * The rlogin information received from the client.
  132. *
  133. * We check this, and if it isn't valid, we spoof some rlogin
  134. * connection.
  135. */
  136. std::string rlogin_auth;
  137. /**
  138. * The username passed in via rlogin. We need this so we know what
  139. * name we need to store the data under.
  140. */
  141. std::string rlogin_name;
  142. std::string host;
  143. std::string port;
  144. char game = 0;
  145. /**
  146. * Are we connected to the server?
  147. *
  148. * Don't shutdown the server socket if we aren't connected.
  149. */
  150. bool connected = false;
  151. };
  152. /*
  153. maybe move the resolver part to the server, so I don't need io_service?
  154. I'm not sure what the socket connection part is going to need just yet,
  155. so I probably won't move that just yet. [NNY!]
  156. */
  157. class Server {
  158. public:
  159. Server(boost::asio::io_service &io_service,
  160. const boost::asio::ip::tcp::endpoint &endpoint,
  161. const std::string &host, const std::string &port, bool server_telnet_);
  162. ~Server();
  163. private:
  164. void do_accept(void);
  165. void on_signal(const boost::system::error_code &ec, int signal);
  166. bool server_telnet;
  167. boost::asio::io_service &io_service_;
  168. boost::asio::ip::tcp::acceptor acceptor_;
  169. boost::asio::signal_set signal_;
  170. bool keep_accepting;
  171. /**
  172. * The host to connect to (from config)
  173. */
  174. std::string host_;
  175. /**
  176. * The port to connect to (from config)
  177. *
  178. */
  179. std::string port_;
  180. };
  181. #endif