1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
26 * Event arguments for socket errors.
30 struct I2_BASE_API SocketErrorEventArgs : public EventArgs
32 const std::exception& Exception;
34 SocketErrorEventArgs(const std::exception& ex)
39 * Base class for sockets.
43 class I2_BASE_API Socket : public Object
46 typedef shared_ptr<Socket> Ptr;
47 typedef weak_ptr<Socket> WeakPtr;
49 typedef list<Socket::WeakPtr> CollectionType;
51 static Socket::CollectionType Sockets;
55 void SetFD(SOCKET fd);
56 SOCKET GetFD(void) const;
58 boost::signal<void (const EventArgs&)> OnReadable;
59 boost::signal<void (const EventArgs&)> OnWritable;
60 boost::signal<void (const EventArgs&)> OnException;
62 boost::signal<void (const SocketErrorEventArgs&)> OnError;
63 boost::signal<void (const EventArgs&)> OnClosed;
65 virtual bool WantsToRead(void) const;
66 virtual bool WantsToWrite(void) const;
68 virtual void Start(void);
69 virtual void Stop(void);
73 string GetClientAddress(void);
74 string GetPeerAddress(void);
79 int GetError(void) const;
80 static int GetLastSocketError(void);
81 void HandleSocketError(const std::exception& ex);
83 virtual void CloseInternal(bool from_dtor);
86 SOCKET m_FD; /**< The socket descriptor. */
88 int ExceptionEventHandler(const EventArgs& ea);
90 static string GetAddressFromSockaddr(sockaddr *address, socklen_t len);
96 class SocketException : public Exception
99 SocketException(const string& message, int errorCode);
104 #endif /* SOCKET_H */