#include "autoconf.h" #include "dclient/asio_service_manager.h" #include "dclient/client.h" #include "dclient/input_handler.h" #include "dclient/inventory_handler.h" #include "dclient/query_handler.h" #include "dclient/renderer.h" #include "dclient/widgets.h" #include "deliantra/asio_communicator.h" #include "deliantra/command_parser.h" #include "deliantra/handshake_handler.h" #include "deliantra/logs.h" #include "deliantra/message_logger.h" #include "deliantra/data/item.h" #include "adt/bind.h" #include #include #include #include #include template struct fd_listener { fd_listener (boost::asio::io_service &io_service, int fd, Handler &handler) : in (io_service, fd) , handler (handler) , reading (false) { read_more (); } private: void read_more () { if (!reading) { reading = true; in.async_read_some (boost::asio::null_buffers (), bind (&fd_listener::handle_read, this)); } } void handle_read (boost::system::error_code const &error, size_t length) { reading = false; handler.handle_read (error, length); read_more (); } boost::asio::posix::stream_descriptor in; Handler &handler; bool reading; }; static void start_game ( std::string const &server , std::string const &port , std::string const &user , std::string const &pass ) { // ncurses std::auto_ptr app (&ndk::app ()); if (app->width () < 109) throw "need at least 109 columns"; if (app->height () < 24) throw "need at least 24 lines"; // asio boost::asio::io_service io_service; // deliantra asio_communicator comm (io_service, server, port); command_parser cmdp; handshake_handler hh (comm); message_logger logger; // dclient client cli (comm, cmdp); widgets ui; renderer rend (ui); asio_service_manager svs (io_service); input_handler input (comm, svs, ui); fd_listener in (io_service, STDIN_FILENO, input); query_handler qh (comm, ui, user, pass); inventory_handler invh (comm, ui); // link them all together comm.set_packet_handler (cli); cmdp.add_command_handler (hh); cmdp.add_command_handler (qh); cmdp.add_command_handler (invh); cmdp.add_command_handler (rend); cmdp.add_command_handler (logger); // run the event loop io_service.run (); } int main (int argc, char *argv[]) try { if (argc == 2 && !strcmp (argv[1], "--version")) { printf ("%s version %s\n", PACKAGE_NAME, PACKAGE_VERSION); return EXIT_SUCCESS; } if (argc < 3 || !strcmp (argv[1], "--help")) { printf ("Usage: %s [user] [password]\n", PACKAGE); return EXIT_SUCCESS; } std::string server, port, user, pass; server = argv[1]; port = argv[2]; if (argc == 5) { user = argv[3]; pass = argv[4]; } start_game (server, port, user, pass); return EXIT_SUCCESS; } catch (std::exception const &e) { logs << "exception: " << e.what () << "\n"; printf ("exception caught: %s\n", e.what ()); } catch (int i) { logs << "exception: 0" << std::oct << std::setfill ('0') << std::setw (3) << i << "\n"; printf ("exception caught: 0%03o\n", i); } catch (char const *s) { logs << "exception: " << s << "\n"; printf ("exception caught: %s\n", s); } catch (...) { logs << "exception\n"; puts ("exception caught"); }