ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/dclient/src/frontend/client/main.cpp
Revision: 1.1
Committed: Sun Oct 17 08:15:11 2010 UTC (16 years ago) by sf-pippijn
Branch: MAIN
Log Message:
initial import

File Contents

# User Rev Content
1 sf-pippijn 1.1 #include "autoconf.h"
2    
3     #include "dclient/asio_service_manager.h"
4     #include "dclient/client.h"
5     #include "dclient/input_handler.h"
6     #include "dclient/inventory_handler.h"
7     #include "dclient/query_handler.h"
8     #include "dclient/renderer.h"
9     #include "dclient/widgets.h"
10    
11     #include "deliantra/asio_communicator.h"
12     #include "deliantra/command_parser.h"
13     #include "deliantra/handshake_handler.h"
14     #include "deliantra/logs.h"
15     #include "deliantra/message_logger.h"
16    
17     #include "deliantra/data/item.h"
18    
19     #include "adt/bind.h"
20    
21     #include <ndk++/application.hh>
22    
23     #include <boost/asio/posix/stream_descriptor.hpp>
24    
25     #include <cstring>
26     #include <iomanip>
27     #include <memory>
28    
29     template<typename Handler>
30     struct fd_listener
31     {
32     fd_listener (boost::asio::io_service &io_service, int fd, Handler &handler)
33     : in (io_service, fd)
34     , handler (handler)
35     , reading (false)
36     {
37     read_more ();
38     }
39    
40     private:
41     void read_more ()
42     {
43     if (!reading)
44     {
45     reading = true;
46     in.async_read_some (boost::asio::null_buffers (), bind (&fd_listener::handle_read, this));
47     }
48     }
49    
50     void handle_read (boost::system::error_code const &error, size_t length)
51     {
52     reading = false;
53     handler.handle_read (error, length);
54     read_more ();
55     }
56    
57     boost::asio::posix::stream_descriptor in;
58     Handler &handler;
59     bool reading;
60     };
61    
62     static void
63     start_game ( std::string const &server
64     , std::string const &port
65     , std::string const &user
66     , std::string const &pass
67     )
68     {
69     // ncurses
70     std::auto_ptr<ndk::application> app (&ndk::app ());
71    
72     if (app->width () < 109)
73     throw "need at least 109 columns";
74     if (app->height () < 24)
75     throw "need at least 24 lines";
76    
77     // asio
78     boost::asio::io_service io_service;
79    
80     // deliantra
81     asio_communicator comm (io_service, server, port);
82     command_parser cmdp;
83     handshake_handler hh (comm);
84     message_logger logger;
85    
86     // dclient
87     client cli (comm, cmdp);
88     widgets ui;
89     renderer rend (ui);
90     asio_service_manager svs (io_service);
91     input_handler input (comm, svs, ui);
92     fd_listener<input_handler> in (io_service, STDIN_FILENO, input);
93     query_handler qh (comm, ui, user, pass);
94     inventory_handler invh (comm, ui);
95    
96     // link them all together
97     comm.set_packet_handler (cli);
98     cmdp.add_command_handler (hh);
99     cmdp.add_command_handler (qh);
100     cmdp.add_command_handler (invh);
101     cmdp.add_command_handler (rend);
102     cmdp.add_command_handler (logger);
103    
104     // run the event loop
105     io_service.run ();
106     }
107    
108    
109     int
110     main (int argc, char *argv[])
111     try
112     {
113     if (argc == 2 && !strcmp (argv[1], "--version"))
114     {
115     printf ("%s version %s\n", PACKAGE_NAME, PACKAGE_VERSION);
116     return EXIT_SUCCESS;
117     }
118    
119     if (argc < 3 || !strcmp (argv[1], "--help"))
120     {
121     printf ("Usage: %s <host> <port> [user] [password]\n", PACKAGE);
122     return EXIT_SUCCESS;
123     }
124    
125     std::string server, port, user, pass;
126    
127     server = argv[1];
128     port = argv[2];
129     if (argc == 5)
130     {
131     user = argv[3];
132     pass = argv[4];
133     }
134    
135     start_game (server, port, user, pass);
136    
137     return EXIT_SUCCESS;
138     }
139     catch (std::exception const &e)
140     {
141     logs << "exception: " << e.what () << "\n";
142     printf ("exception caught: %s\n", e.what ());
143     }
144     catch (int i)
145     {
146     logs << "exception: 0" << std::oct << std::setfill ('0') << std::setw (3) << i << "\n";
147     printf ("exception caught: 0%03o\n", i);
148     }
149     catch (char const *s)
150     {
151     logs << "exception: " << s << "\n";
152     printf ("exception caught: %s\n", s);
153     }
154     catch (...)
155     {
156     logs << "exception\n";
157     puts ("exception caught");
158     }