tcp_server.h

00001 /*
00002  * tcp_server.h
00003  * This file is part of dbPager Server
00004  *
00005  * Copyright (c) 2009 Dennis Prochko <wolfsoft@mail.ru>
00006  *
00007  * dbPager Server is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation version 3.
00010  *
00011  * dbPager Server is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with dbPager Server; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor,
00019  * Boston, MA  02110-1301  USA
00020  */
00021 
00022 #ifndef _TCP_SERVER_H_
00023 #define _TCP_SERVER_H_
00024 
00025 #include <list>
00026 #include <queue>
00027 #include <string>
00028 #include <iostream>
00029 
00030 #include <dcl/datetime.h>
00031 #include <dcl/delegate.h>
00032 #include <dcl/event.h>
00033 #include <dcl/mutex.h>
00034 #include <dcl/socket.h>
00035 #include <dcl/thread.h>
00036 
00037 namespace dbp {
00038 
00040 
00051 class tcp_server {
00052 public:
00053         typedef delegate2<const socket&, const socket&, socket*>
00054           on_io_handler;
00055         typedef delegate3<const socket&, std::istream&, std::ostream&, void>
00056           on_connect_handler;
00057         typedef delegate1<socket&, void> on_disconnect_handler;
00058         typedef delegate3<const socket&, std::istream&, std::ostream&,
00059           bool> on_process_data_handler;
00060         typedef delegate1<const dbp::exception&, void> on_exception_handler;
00062         tcp_server(size_t worker_threads = 2, size_t queue_size = 32768);
00064         ~tcp_server();
00066         int timeout() {
00067                 return _timeout;
00068         }
00070         tcp_server& timeout(int value) {
00071                 _timeout = value;
00072                 return *this;
00073         }
00075 
00089         void start(const std::string &bind = "*:0");
00091 
00094         void stop();
00096 
00099         bool is_running();
00101 
00111         void on_create_io_handler(on_io_handler handler) {
00112                 create_io_handler = handler;
00113         }
00114         void on_connect(on_connect_handler handler) {
00115                 connect_handler = handler;
00116         }
00117         void on_disconnect(on_disconnect_handler handler) {
00118                 disconnect_handler = handler;
00119         }
00120         void on_process_data(on_process_data_handler handler) {
00121                 process_data_handler = handler;
00122         }
00123         void on_exception(on_exception_handler handler) {
00124                 exception_handler = handler;
00125         }
00126 private:
00127         // Options
00128         int _timeout;
00129         // Stop flag
00130         bool is_stopped;
00131         // Parameters
00132         size_t _worker_threads;
00133         size_t _queue_size;
00134         // Listen sockets
00135         typedef std::vector<socket> sockets;
00136         sockets listen_sockets;
00137         // Listen thread
00138         thread lt;
00139         // Requests queue
00140         struct request {
00141                 enum states {
00142                         WAIT_DATA,
00143                         TIME_OUT,
00144                         CLOSING
00145                 };
00146                 request(socket *conn): cur_state(WAIT_DATA), last_state(WAIT_DATA),
00147                   connection(conn) {
00148                         read_buffer.setstate(std::ios::eofbit);
00149                         write_buffer.setstate(std::ios::eofbit);
00150                 }
00151                 ~request() {
00152                         connection->shutdown();
00153                         delete connection;
00154                 }
00155                 states cur_state, last_state;
00156                 socket *connection;
00157                 std::stringstream read_buffer, write_buffer;
00158                 datetime last_access;
00159         };
00160         // Active requests
00161         typedef std::list<request*> active_requests;
00162         active_requests a_reqs;
00163         // Processing requests
00164         typedef std::queue<request*> requests;
00165         requests reqs;
00166         // Worker threads
00167         typedef std::vector<thread> threads;
00168         threads wkt;
00169         // Synchronization
00170         mutex _lock;
00171         event _event;
00172         // Custom handlers
00173         on_io_handler create_io_handler;
00174         on_connect_handler connect_handler;
00175         on_disconnect_handler disconnect_handler;
00176         on_process_data_handler process_data_handler;
00177         on_exception_handler exception_handler;
00178         // Process handlers
00179         void io_process(thread_int&);
00180         void working_process(thread_int&);
00181         // Utility functions
00182         void connection_accept(socket &s);
00183         void connection_write(request &r);
00184         void connection_read(request &r);
00185         void disconnect_client(request*);
00186 };
00187 
00188 }
00189 
00190 #endif /*_TCP_SERVER_H_*/

 
Support This Project
SourceForge.net Logo