#ifndef SOCKETSERVER_H #define SOCKETSERVER_H ///////////////////////////////////////////////////////////////////// // SocketServer.h - Provides listener that spawns client-handler // // ver 1.1 threads // // // // Language: Visual C++, 2005 // // Platform: Dell Dimension 9150, Windows XP Pro, SP 2.0 // // Application: Utility for CSE687 and CSE775 projects // // Author: Jim Fawcett, Syracuse University, CST 2-187 // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////// /* Module Operations: ================== This module provides a multi-threaded socket listener. You define processing to be executed by a client handler thread in a class derived from the abstract processing class. All of the client handlers activities are coded in a run() function, and may be passed parameters via member data passed during construction. Public Interface: ================= class myClientHandler : public processing // define processing { public: myClientHandler(T1 arg1, T2 arg2, ... ) { } void run() { ... } }; SocketServer server(2048); // declare server instance myClientHandler handler(arg1, arg2, ... ); // declare handler instance server.registerClientHandler(&handler); // register handler server.start(); // start server // other processing here server.stop(); // wait until all handler // threads finish */ // /* Build Process: ============== Required Files: SocketServer.h, SocketServer.cpp, Sockets.h, Sockets.cpp Threads.h, Threads.cpp Compile Command: ================ cl /EHa SocketServer.cpp Sockets.cpp Threads.cpp WSock32.lib Maintenance History: ==================== ver 1.1 : 26 Apr 06 - changed the algorithm in SocketServer::stop to allow timeouts - added more tracing and included diagnostics.h ver 1.0 : 17 Apr 06 - first release */ // #include "Sockets.h" #include "Threads.h" class IProc : public processing { public: void acceptSocket(Socket* pSocket) { pSocket_ = pSocket; } virtual IProc* clone()=0; virtual ~IProc() {} protected: Socket* pSocket_; }; class listenProc; class SocketServer { friend class listenProc; public: SocketServer() : pListener(0), pHandler_(0), pListenerThread(0) {} void start(int port); void stop(int wait=-1); void registerClientHandler(IProc* pHandlerProc); private: SocketListener* pListener; IProc* pHandler_; thread* pListenerThread; threadPool tp; }; #endif