]> granicus.if.org Git - icinga2/blob - base/socket.h
Replaced custom event code with Boost.Signals.
[icinga2] / base / socket.h
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #ifndef SOCKET_H
21 #define SOCKET_H
22
23 namespace icinga {
24
25 /**
26  * Event arguments for socket errors.
27  *
28  * @ingroup base
29  */
30 struct I2_BASE_API SocketErrorEventArgs : public EventArgs
31 {
32         const std::exception& Exception;
33
34         SocketErrorEventArgs(const std::exception& ex)
35             : Exception(ex) { }
36 };
37
38 /**
39  * Base class for sockets.
40  *
41  * @ingroup base
42  */
43 class I2_BASE_API Socket : public Object
44 {
45 public:
46         typedef shared_ptr<Socket> Ptr;
47         typedef weak_ptr<Socket> WeakPtr;
48
49         typedef list<Socket::WeakPtr> CollectionType;
50
51         static Socket::CollectionType Sockets;
52
53         ~Socket(void);
54
55         void SetFD(SOCKET fd);
56         SOCKET GetFD(void) const;
57
58         boost::signal<void (const EventArgs&)> OnReadable;
59         boost::signal<void (const EventArgs&)> OnWritable;
60         boost::signal<void (const EventArgs&)> OnException;
61
62         boost::signal<void (const SocketErrorEventArgs&)> OnError;
63         boost::signal<void (const EventArgs&)> OnClosed;
64
65         virtual bool WantsToRead(void) const;
66         virtual bool WantsToWrite(void) const;
67
68         virtual void Start(void);
69         virtual void Stop(void);
70
71         void Close(void);
72
73         string GetClientAddress(void);
74         string GetPeerAddress(void);
75
76 protected:
77         Socket(void);
78
79         int GetError(void) const;
80         static int GetLastSocketError(void);
81         void HandleSocketError(const std::exception& ex);
82
83         virtual void CloseInternal(bool from_dtor);
84
85 private:
86         SOCKET m_FD; /**< The socket descriptor. */
87
88         int ExceptionEventHandler(const EventArgs& ea);
89
90         static string GetAddressFromSockaddr(sockaddr *address, socklen_t len);
91 };
92
93 /**
94  * A socket exception.
95  */
96 class SocketException : public Exception
97 {
98 public:
99         SocketException(const string& message, int errorCode);
100 };
101
102 }
103
104 #endif /* SOCKET_H */