/*************************************************************************** * Copyright (C) 2006 by Gerd v. Egidy * * gve@intra2net.com * * * * This library is free software; you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License version * * 2.1 as published by the Free Software Foundation. * * * * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifndef __LIBT2N_SERVER #define __LIBT2N_SERVER #include #include #include namespace libt2n { /** Basic connection class */ class connection { private: int timeout; int last_action_time; bool closed; std::string buffer; protected: connection(int _timeout) { set_timeout(_timeout); reset_timeout(); closed=false; } public: ~connection() { this->close(); } void check_timeout(); void reset_timeout(); void set_timeout(int _timeout) { timeout=_timeout; } bool is_closed() { return closed; } virtual void close() { closed=true; } virtual void fill_buffer(void)=0; bool get_packet(std::string& data, unsigned int& conn_id); virtual void write(const std::string& data)=0; }; /** Basic server class */ class server { public: enum log_level_values { none=0, error=1, debug=2 }; private: int default_timeout; log_level_values log_level; std::ostream *logstream; unsigned int next_id; std::map connections; protected: server() { set_default_timeout(30); set_logging(NULL,none); next_id=0; } public: virtual ~server(); void set_default_timeout(int _default_timeout) { default_timeout=_default_timeout; } void set_logging(std::ostream *_logstream, log_level_values _log_level) { log_level=_log_level; logstream=_logstream; } connection* get_connection(unsigned int conn_id); virtual void fill_buffer(long long usec_timeout=-1)=0; void check_timeout(); bool get_packet(std::string& data, unsigned int& conn_id); void fill_connection_buffers(void); protected: void log(log_level_values level, const std::string& message) { log(level,message.c_str()); } void log(log_level_values level, const char* message); }; } #endif