00001
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00044
00046
00048
00052 SocketServer::SocketServer() : mThreadProc(this) {
00053 }
00054
00058 SocketServer::~SocketServer() {
00059 shutdown();
00060 }
00061
00063
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
00077 createSocket(Domain, Type);
00078
00079
00080 bind(mPort = ListenPort);
00081
00082
00083 listen();
00084
00085
00086 thread thrd(mThreadProc);
00087 }
00088
00095 void SocketServer::onSocketConnect(SocketInterface const & iSocket) {
00096
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
00143 cdbg << "SocketServer :: New Socket Connection Detected" << endl;
00144 }
00145
00150 void SocketServer::onSocketServerDisconnect(Socket const & socket) {
00151
00152 cdbg << "SocketServer :: Socket Disconnect Detected" << endl;
00153 }
00154
00160 void SocketServer::onSocketServerMessage(Socket const & socket, std::string const & Message) {
00161
00162 cdbg << "SocketServer :: Socket Message [" << Message.length() << "] Bytes -- " << Message << endl;
00163 }
00164
00170 void SocketServer::onSocketServerRead(Socket const & socket, DynamicBuffer<char> & ReadBuffer) {
00171
00172 cdbg << "SocketServer :: Socket Read [" << ReadBuffer.length() << "] Bytes" << endl;
00173 }
00174
00178 void SocketServer::shutdown() {
00179
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
00199 pSocket->setEventWatcher(this);
00200 pSocket->setMessageMode(true);
00201
00202
00203 mSockets.insert(make_pair(pSocket->getSocket(), pSocket));
00204
00205
00206 onSocketServerConnect(pSocket);
00207
00208
00209 pSocket->processEvents();
00210 }
00211 cdbg5 << "No longer accepting connections" << endl;
00212 }