From 62c4ed1c2e81615f54ade6b4e3b1d77798468105 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Fri, 17 Feb 2012 13:09:56 +0000 Subject: Fixed *nix hang on server stop (socket needs shutting down) git-svn-id: http://mc-server.googlecode.com/svn/trunk@283 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- WebServer/Socket.h | 86 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 36 deletions(-) (limited to 'WebServer/Socket.h') diff --git a/WebServer/Socket.h b/WebServer/Socket.h index e38df69ea..5f1248107 100644 --- a/WebServer/Socket.h +++ b/WebServer/Socket.h @@ -39,6 +39,7 @@ #ifndef _WIN32 typedef int SOCKET; #define SOCKET_ERROR (-1) + #define INVALID_SOCKET (-1) #define closesocket close #endif // !_WIN32 @@ -48,66 +49,79 @@ enum TypeSocket {BlockingSocket, NonBlockingSocket}; -class Socket { + + + + +class Socket +{ public: - virtual ~Socket(); - Socket(const Socket&); - Socket& operator=(Socket&); + virtual ~Socket(); + Socket(const Socket&); + Socket& operator=(Socket&); - std::string ReceiveLine(); - std::string ReceiveBytes( unsigned int a_Length ); + std::string ReceiveLine(); + std::string ReceiveBytes( unsigned int a_Length ); + + bool IsValid(void) const; - void Close( bool a_WaitSend = false ); + void Close( bool a_WaitSend = false ); - // The parameter of SendLine is not a const reference - // because SendLine modifes the std::string passed. - void SendLine (std::string); + // The parameter of SendLine is not a const reference + // because SendLine modifes the std::string passed. + void SendLine (std::string); - // The parameter of SendBytes is a const reference - // because SendBytes does not modify the std::string passed - // (in contrast to SendLine). - void SendBytes(const std::string&); + // The parameter of SendBytes is a const reference + // because SendBytes does not modify the std::string passed + // (in contrast to SendLine). + void SendBytes(const std::string&); protected: - friend class SocketServer; - friend class SocketSelect; + friend class SocketServer; + friend class SocketSelect; - Socket(SOCKET s); - Socket(); + Socket(SOCKET s); + Socket(); - SOCKET s_; + SOCKET s_; - int* refCounter_; + int* refCounter_; private: - static void Start(); - static void End(); - static int nofSockets_; + static void Start(); + static void End(); + static int nofSockets_; }; -class SocketClient : public Socket { -public: - SocketClient(const std::string& host, int port); -}; -class SocketServer : public Socket { + + + +class SocketServer : + public Socket +{ public: - SocketServer(int port, int connections, TypeSocket type=BlockingSocket); + SocketServer(int port, int connections, TypeSocket type=BlockingSocket); - Socket* Accept(); + Socket* Accept(); }; + + + + // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/wsapiref_2tiq.asp -class SocketSelect { - public: - SocketSelect(Socket const * const s1, Socket const * const s2=NULL, TypeSocket type=BlockingSocket); +class SocketSelect +{ +public: + SocketSelect(Socket const * const s1, Socket const * const s2=NULL, TypeSocket type=BlockingSocket); - bool Readable(Socket const * const s); + bool Readable(Socket const * const s); - private: - fd_set fds_; +private: + fd_set fds_; }; #endif -- cgit v1.2.3