etisserant@355: /* etisserant@355: Socket.h etisserant@355: etisserant@355: Copyright (C) 2002-2004 René Nyffenegger etisserant@355: etisserant@355: This source code is provided 'as-is', without any express or implied etisserant@355: warranty. In no event will the author be held liable for any damages etisserant@355: arising from the use of this software. etisserant@355: etisserant@355: Permission is granted to anyone to use this software for any purpose, etisserant@355: including commercial applications, and to alter it and redistribute it etisserant@355: freely, subject to the following restrictions: etisserant@355: etisserant@355: 1. The origin of this source code must not be misrepresented; you must not etisserant@355: claim that you wrote the original source code. If you use this source code etisserant@355: in a product, an acknowledgment in the product documentation would be etisserant@355: appreciated but is not required. etisserant@355: etisserant@355: 2. Altered source versions must be plainly marked as such, and must not be etisserant@355: misrepresented as being the original source code. etisserant@355: etisserant@355: 3. This notice may not be removed or altered from any source distribution. etisserant@355: etisserant@355: René Nyffenegger rene.nyffenegger@adp-gmbh.ch etisserant@355: */ etisserant@355: etisserant@355: #ifndef SOCKET_H etisserant@355: #define SOCKET_H etisserant@355: etisserant@355: #include etisserant@355: etisserant@355: #include etisserant@355: etisserant@355: enum TypeSocket {BlockingSocket, NonBlockingSocket}; etisserant@355: etisserant@355: class Socket { etisserant@355: public: etisserant@355: etisserant@355: virtual ~Socket(); etisserant@355: Socket(const Socket&); etisserant@355: Socket& operator=(Socket&); etisserant@355: etisserant@355: std::string ReceiveLine(); etisserant@355: std::string ReceiveBytes(); etisserant@355: etisserant@355: void Close(); etisserant@355: etisserant@355: // The parameter of SendLine is not a const reference etisserant@355: // because SendLine modifes the std::string passed. etisserant@355: void SendLine (std::string); etisserant@355: etisserant@355: // The parameter of SendBytes is a const reference etisserant@355: // because SendBytes does not modify the std::string passed etisserant@355: // (in contrast to SendLine). etisserant@355: void SendBytes(const std::string&); etisserant@355: etisserant@355: protected: etisserant@355: friend class SocketServer; etisserant@355: friend class SocketSelect; etisserant@355: etisserant@355: Socket(SOCKET s); etisserant@355: Socket(); etisserant@355: etisserant@355: etisserant@355: SOCKET s_; etisserant@355: etisserant@355: int* refCounter_; etisserant@355: etisserant@355: private: etisserant@355: static void Start(); etisserant@355: static void End(); etisserant@355: static int nofSockets_; etisserant@355: }; etisserant@355: etisserant@355: class SocketClient : public Socket { etisserant@355: public: etisserant@355: SocketClient(const std::string& host, int port); etisserant@355: }; etisserant@355: etisserant@355: class SocketServer : public Socket { etisserant@355: public: etisserant@355: SocketServer(int port, int connections, TypeSocket type=BlockingSocket); etisserant@355: etisserant@355: Socket* Accept(); etisserant@355: }; etisserant@355: etisserant@355: // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/wsapiref_2tiq.asp etisserant@355: class SocketSelect { etisserant@355: public: etisserant@355: SocketSelect(Socket const * const s1, Socket const * const s2=NULL, TypeSocket type=BlockingSocket); etisserant@355: etisserant@355: bool Readable(Socket const * const s); etisserant@355: etisserant@355: private: etisserant@355: fd_set fds_; etisserant@355: }; etisserant@355: etisserant@355: etisserant@355: etisserant@355: #endif