threaded_server.hpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:5k
- /*
- * ===========================================================================
- * PRODUCTION $Log: threaded_server.hpp,v $
- * PRODUCTION Revision 1000.0 2003/10/29 16:33:19 gouriano
- * PRODUCTION PRODUCTION: IMPORTED [ORIGINAL] Dev-tree R6.8
- * PRODUCTION
- * ===========================================================================
- */
- #ifndef CONNECT___THREADED_SERVER__HPP
- #define CONNECT___THREADED_SERVER__HPP
- /* $Id: threaded_server.hpp,v 1000.0 2003/10/29 16:33:19 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Author: Aaron Ucko
- *
- * File Description:
- * Framework for a multithreaded network server
- *
- */
- #include <corelib/ncbistd.hpp>
- #include <connect/ncbi_conn_exception.hpp>
- #include <connect/ncbi_socket.h>
- /** @addtogroup ThreadedServer
- *
- * @{
- */
- BEGIN_NCBI_SCOPE
- /// Exceptions thrown by CThreadedServer::Run
- class NCBI_XCONNECT_EXPORT CThreadedServerException
- : EXCEPTION_VIRTUAL_BASE public CConnException
- {
- public:
- enum EErrCode {
- eBadParameters, ///< Out-of-range parameters given
- eCouldntListen ///< Unable to bind listening port
- };
- virtual const char* GetErrCodeString(void) const
- {
- switch (GetErrCode()) {
- case eBadParameters: return "eBadParameters";
- case eCouldntListen: return "eCouldntListen";
- default: return CException::GetErrCodeString();
- }
- }
- NCBI_EXCEPTION_DEFAULT(CThreadedServerException, CConnException);
- };
- // CThreadedServer - abstract class for network servers using thread pools.
- // This code maintains a pool of threads (initially m_InitThreads, but
- // potentially as many as m_MaxThreads) to deal with incoming connections;
- // each connection gets assigned to one of the worker threads, allowing
- // the server to handle multiple requests in parallel while still checking
- // for new requests.
- //
- // You must define Process() to indicate what to do with each incoming
- // connection; .../src/connect/test_threaded_server.cpp illustrates
- // how you might do this.
- class NCBI_XCONNECT_EXPORT CThreadedServer
- {
- public:
- CThreadedServer(unsigned int port) :
- m_InitThreads(5), m_MaxThreads(10), m_QueueSize(20),
- m_SpawnThreshold(1), m_TemporarilyStopListening(false), m_Port(port)
- {}
- void Run(void);
- // Runs asynchronously (from a separate thread) for each request
- // Implementor must take care of closing socket when done
- virtual void Process(SOCK sock) = 0;
- protected:
- // Runs synchronously when request queue is full
- // Implementor must take care of closing socket when done
- virtual void ProcessOverflow(SOCK sock) { SOCK_Close(sock); }
- // Called at the beginning of Run, before creating thread pool
- virtual void SetParams() {}
- // Settings for thread pool
- unsigned int m_InitThreads; // Number of initial threads
- unsigned int m_MaxThreads; // Maximum simultaneous threads
- unsigned int m_QueueSize; // Maximum size of request queue
- unsigned int m_SpawnThreshold; // Controls when to spawn more threads
- // Temporarily close listener when queue fills?
- bool m_TemporarilyStopListening;
- private:
- unsigned int m_Port; // TCP port to listen on
- };
- END_NCBI_SCOPE
- /* @} */
- /*
- * ---------------------------------------------------------------------------
- * $Log: threaded_server.hpp,v $
- * Revision 1000.0 2003/10/29 16:33:19 gouriano
- * PRODUCTION: IMPORTED [ORIGINAL] Dev-tree R6.8
- *
- * Revision 6.8 2003/08/12 19:27:52 ucko
- * +CThreadedServerException
- *
- * Revision 6.7 2003/04/09 19:06:05 siyan
- * Added doxygen support
- *
- * Revision 6.6 2002/12/19 14:51:48 dicuccio
- * Added export specifier for Win32 DLL builds.
- *
- * Revision 6.5 2002/09/19 18:06:04 lavr
- * Header file guard macro changed
- *
- * Revision 6.4 2002/01/25 15:39:29 ucko
- * Completely reorganized threaded servers.
- *
- * Revision 6.3 2002/01/24 20:18:56 ucko
- * Add comments
- * Add magic TemporarilyStopListening overflow processor
- *
- * Revision 6.2 2002/01/24 18:36:07 ucko
- * Allow custom queue-overflow handling.
- *
- * Revision 6.1 2001/12/11 19:55:21 ucko
- * Introduce thread-pool-based servers.
- *
- */
- #endif /* CONNECT___THREADED_SERVER__HPP */