session.h 5.6 KB

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