/* net6 - Library providing IPv4/IPv6 network access * Copyright (C) 2005 Armin Burgmeier / 0x539 dev group * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * 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 library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef _NET6_HOST_HPP_ #define _NET6_HOST_HPP_ #include "local.hpp" #include "server.hpp" namespace net6 { /** High-level TCP host object that is used as a server with a local user. */ template class basic_host : virtual public basic_local, virtual public basic_server { public: /** Creates a new basic_host object. * @param username user name to use for the local user. * @param ipv6 Whether to use IPv6 when no parameter is given to * reopen. */ basic_host(const std::string& username, bool ipv6 = true); /** Creates a new basic_host object which will accept incoming * connections on port port and use the user name * username for the local user. */ basic_host(unsigned int port, const std::string& username, bool ipv6 = true); /** Sends a packet to all users. */ virtual void send(const packet& pack); /** Sends a packet to a single user. The request is ignored if * to is the local user. */ virtual void send(const packet& pack, const user& to); /** @brief Requests encryption to the given user. * * Throws an error if to is the local user. */ virtual void request_encryption(const user& to); /** Returns the local user. */ virtual user& get_self(); /** Returns the local user. */ virtual const user& get_self() const; protected: user* self; }; typedef basic_host host; template basic_host::basic_host(const std::string& username, bool ipv6) : basic_object(), basic_local(), basic_server(ipv6), self(new user(++ basic_server::id_counter, NULL) ) { self->login(username); basic_object::user_add(self); } template basic_host:: basic_host(unsigned int port, const std::string& username, bool ipv6) : basic_object(), basic_local(), basic_server(port, ipv6), self(new user(++ basic_server::id_counter, NULL) ) { self->login(username); basic_object::user_add(self); } template void basic_host::send(const packet& pack) { basic_server::send(pack); } template void basic_host::send(const packet& pack, const user& to) { if(&to != self) basic_server::send(pack, to); } template void basic_host::request_encryption(const user& to) { if(&to == self) { throw std::logic_error( "net6::basic_host::request_encryption:\n" "Cannot request encryption to local client" ); } basic_server::request_encryption(to); } template user& basic_host::get_self() { return *self; } template const user& basic_host::get_self() const { return *self; } } // namespace net6 #endif // _NET6_HOST_HPP_