ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/dclient/src/frontend/client/main.cpp
Revision: 1.2
Committed: Mon Oct 18 06:54:04 2010 UTC (16 years ago) by sf-pippijn
Branch: MAIN
Changes since 1.1: +6 -3 lines
Log Message:
gcc 4.5; moved some code; background colour support

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