00001 00012 /* 00013 00014 Copyright (c) 2007, Tim Burrell 00015 Licensed under the Apache License, Version 2.0 (the "License"); 00016 you may not use this file except in compliance with the License. 00017 You may obtain a copy of the License at 00018 00019 http://www.apache.org/licenses/LICENSE-2.0 00020 00021 Unless required by applicable law or agreed to in writing, software 00022 distributed under the License is distributed on an "AS IS" BASIS, 00023 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00024 See the License for the specific language governing permissions and 00025 limitations under the License. 00026 00027 */ 00028 00029 #ifndef __SocketServer_h 00030 #define __SocketServer_h 00031 00032 #if HAVE_CONFIG_H 00033 #include "config.h" 00034 #endif 00035 00036 #include "Socket.hpp" 00037 #include "SocketEventWatcher.hpp" 00038 #include <cstdlib> 00039 #include <map> 00040 00042 // Namespace 00044 00045 namespace H { 00046 00048 // Typedef's / Defines 00050 00052 // Class Definition 00054 00061 class SocketServer : public Socket, private SocketEventWatcher { 00062 public: 00063 // Public Member Functions 00064 void acceptConnections(int ListenPort, SocketDomain Domain = SOCKET_INTERNET, SocketType Type = SOCKET_STREAM); 00065 virtual void onSocketServerConnect(boost::shared_ptr<Socket> pSocket); 00066 virtual void onSocketServerDisconnect(Socket const & socket); 00067 virtual void onSocketServerMessage(Socket const & socket, std::string const & Message); 00068 virtual void onSocketServerRead(Socket const & socket, DynamicBuffer<char> & ReadBuffer); 00069 void shutdown(); 00070 void threadProc(); 00071 00072 // Construction / Deconstruction 00073 SocketServer(); 00074 virtual ~SocketServer(); 00075 00076 private: 00077 // private member functions 00078 void onSocketDisconnect(SocketInterface const & iSocket); 00079 void onSocketConnect(SocketInterface const & iSocket); 00080 void onSocketMessage(SocketInterface const & iSocket, std::string const & Message); 00081 void onSocketRead(SocketInterface const & iSocket, DynamicBuffer<char> & ReadBuffer); 00082 00083 // Private Member Variables 00084 std::map< int, boost::shared_ptr<Socket> > mSockets; 00085 00089 struct SocketServerThreadProc { 00090 SocketServerThreadProc(SocketServer * pSocketServer) : mpSocketServer(pSocketServer) { 00091 mpSocketServer->mThreading = false; 00092 }; 00093 00095 void operator()() { 00096 mpSocketServer->mThreading = true; 00097 mpSocketServer->threadProc(); 00098 mpSocketServer->mThreading = false; 00099 } 00100 00101 SocketServer * mpSocketServer; 00102 }; 00103 bool mThreading; 00104 SocketServerThreadProc mThreadProc; 00105 }; 00106 00108 00109 } // H namespace 00110 00111 #endif // __SocketServer_h