00001 #ifndef IONFLUX_TOOLS_TCPSERVER 00002 #define IONFLUX_TOOLS_TCPSERVER 00003 /* ========================================================================== 00004 * Ionflux Tools 00005 * Copyright (c) 2004 Joern P. Meier 00006 * mail@ionflux.org 00007 * -------------------------------------------------------------------------- 00008 * TCPServer.hpp Generic TCP server. 00009 * ========================================================================== 00010 * 00011 * This file is part of Ionflux Tools. 00012 * 00013 * Ionflux Tools is free software; you can redistribute it and/or modify it 00014 * under the terms of the GNU General Public License as published by the Free 00015 * Software Foundation; either version 2 of the License, or (at your option) 00016 * any later version. 00017 * 00018 * Ionflux Tools is distributed in the hope that it will be useful, but 00019 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 00020 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00021 * for more details. 00022 * 00023 * You should have received a copy of the GNU General Public License 00024 * along with Ionflux Tools; if not, write to the Free Software Foundation, 00025 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00026 * 00027 * ========================================================================== */ 00028 00029 #include <string> 00030 #include <iostream> 00031 #include <vector> 00032 #include <signal.h> 00033 #include "ionflux/tools.hpp" 00034 #include "ionflux/Reporter.hpp" 00035 #include "ionflux/TCPSocket.hpp" 00036 #include "ionflux/TCPRemotePeer.hpp" 00037 #include "ionflux/IOHandler.hpp" 00038 #include "ionflux/SelectMultiplexer.hpp" 00039 00040 namespace Ionflux 00041 { 00042 00043 namespace Tools 00044 { 00045 00053 class TCPServer 00054 : public IOHandler 00055 { 00056 protected: 00058 Ionflux::Tools::Reporter log; 00060 TCPSocket serverSocket; 00062 std::vector<TCPRemotePeer *> clients; 00064 std::vector<TCPRemotePeer *> trash; 00066 unsigned int maxClients; 00068 int currentClientID; 00070 IOMultiplexer *iomp; 00072 bool manageIomp; 00074 IOEvent serverSocketEvent; 00075 00082 virtual void addClient(TCPRemotePeer *client); 00083 00090 virtual void removeClient(TCPRemotePeer *client); 00091 00096 virtual void cleanupClients(); 00097 00106 virtual void onConnect(TCPRemotePeer &client); 00107 00120 virtual void onReject(TCPRemotePeer &client, int reason); 00121 00134 virtual void onReceive(TCPRemotePeer &client); 00135 00147 virtual void onDisconnect(TCPRemotePeer &client); 00148 00155 virtual void disconnect(TCPRemotePeer *peer); 00156 00157 public: 00159 static const int DEFAULT_MAX_CLIENTS; 00161 static const int REJECTED_REASON_MAX_CLIENTS; 00162 00167 TCPServer(); 00168 00175 TCPServer(IOMultiplexer *initIomp); 00176 00181 virtual ~TCPServer(); 00182 00189 virtual bool init(); 00190 00198 virtual void run(); 00199 00205 virtual void cleanup(); 00206 00213 virtual void broadcast(const std::string &bytes); 00214 00223 virtual void onIO(const IOEvent &event); 00224 00235 virtual void setMaxClients(unsigned int newMaxClients); 00236 00244 virtual void setPort(int newPort); 00245 00250 virtual unsigned int getMaxClients(); 00251 00256 virtual int getPort(); 00257 00264 static void shutdownHandler(int signum); 00265 00270 virtual Reporter &getLog(); 00271 }; 00272 00273 } 00274 00275 } 00276 00280 #endif