libH/SocketServer.cpp

Go to the documentation of this file.
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 #include "SocketServer.hpp"
00030 #include "SocketException.hpp"
00031 #include "UtilTime.hpp"
00032 #include "Debug.hpp"
00033 #include <string>
00034 #include <boost/thread/thread.hpp>
00035 #include <errno.h>
00036 
00037 using namespace H;
00038 using namespace boost;
00039 using namespace std;
00040 
00042 // Defines / Type Defs
00044 
00046 // Construction / Deconstruction
00048 
00052 SocketServer::SocketServer() : mThreadProc(this) {
00053 }
00054 
00058 SocketServer::~SocketServer() {
00059         shutdown();
00060 }
00061 
00063 // Class Body
00065 
00072 void SocketServer::acceptConnections(int ListenPort, SocketDomain Domain, SocketType Type) {
00073         if (mSocket != SOCKET_ERROR)
00074                 throw SocketException("Socket Already Created!", __FILE__, __FUNCTION__, __LINE__);
00075         
00076         // create the listen socket
00077         createSocket(Domain, Type);
00078 
00079         // bind to a specified port
00080         bind(mPort = ListenPort);
00081 
00082         // listen for connections!
00083         listen();
00084         
00085         // initialize the listen thread
00086         thread thrd(mThreadProc);
00087 }
00088 
00095 void SocketServer::onSocketConnect(SocketInterface const & iSocket) {
00096         // this is only for client socket connections
00097 }
00098 
00105 void SocketServer::onSocketDisconnect(SocketInterface const & iSocket) {
00106         shared_ptr<Socket> pSocket = mSockets[iSocket.getOldSocket()];
00107         if (!pSocket) {
00108                 cdbg << "SocketServer :: Socket Disconnect from Unhandled Socket Detected" << endl;
00109         } else {
00110                 onSocketServerDisconnect(*pSocket);
00111                 mSockets.erase(iSocket.getOldSocket());
00112         }
00113 }
00114 
00122 void SocketServer::onSocketMessage(SocketInterface const & iSocket, std::string const & Message) {
00123         onSocketServerMessage(static_cast<Socket const &>(iSocket), Message);
00124 }
00125 
00133 void SocketServer::onSocketRead(SocketInterface const & iSocket, DynamicBuffer<char> & ReadBuffer) {
00134         onSocketServerRead(static_cast<Socket const &>(iSocket), ReadBuffer);
00135 }
00136 
00141 void SocketServer::onSocketServerConnect(boost::shared_ptr<Socket> pSocket) {
00142         // override me
00143         cdbg << "SocketServer :: New Socket Connection Detected" << endl;
00144 }
00145 
00150 void SocketServer::onSocketServerDisconnect(Socket const & socket) {
00151         // override me
00152         cdbg << "SocketServer :: Socket Disconnect Detected" << endl;
00153 }
00154 
00160 void SocketServer::onSocketServerMessage(Socket const & socket, std::string const & Message) {
00161         // override me
00162         cdbg << "SocketServer :: Socket Message [" << Message.length() << "] Bytes -- " << Message << endl;
00163 }
00164 
00170 void SocketServer::onSocketServerRead(Socket const & socket, DynamicBuffer<char> & ReadBuffer) {
00171         // override me
00172         cdbg << "SocketServer :: Socket Read [" << ReadBuffer.length() << "] Bytes" << endl;
00173 }
00174 
00178 void SocketServer::shutdown() {
00179         // wait until thead finishes done 
00180         mProcessing = false;
00181         while (mThreading) {
00182                 cdbg5 << "Waiting on SocketServer Thread to Finish..." << endl;
00183                 UtilTime::sleep(0.1f);
00184         }
00185 }
00186 
00190 void SocketServer::threadProc() {
00191         cdbg << "Listening for connections on port [" << mPort << "]..." << endl;
00192         mProcessing = true;
00193         while (mProcessing) {
00194                 shared_ptr<Socket> pSocket = accept();
00195                 if ( (!pSocket) || (pSocket->getSocket() == SOCKET_ERROR) )
00196                         continue;
00197                 
00198                 // configure the socket
00199                 pSocket->setEventWatcher(this);
00200                 pSocket->setMessageMode(true);
00201                 
00202                 // add the socket to the map
00203                 mSockets.insert(make_pair(pSocket->getSocket(), pSocket));
00204                 
00205                 // fire the event
00206                 onSocketServerConnect(pSocket);
00207                 
00208                 // start processing events on the new socket
00209                 pSocket->processEvents();
00210         }
00211         cdbg5 << "No longer accepting connections" << endl;
00212 }

Generated on Wed Nov 7 10:04:16 2007 for gizmod by  doxygen 1.5.3