summaryrefslogtreecommitdiffstats
path: root/lib/base/socket.hpp
blob: f7acf7f52c788f311ecb0202ecd889aa398b63a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#ifndef SOCKET_H
#define SOCKET_H

#include "base/i2-base.hpp"
#include "base/object.hpp"
#include <mutex>

namespace icinga
{

/**
 * Base class for connection-oriented sockets.
 *
 * @ingroup base
 */
class Socket : public Object
{
public:
	DECLARE_PTR_TYPEDEFS(Socket);

	Socket() = default;
	Socket(SOCKET fd);
	~Socket() override;

	SOCKET GetFD() const;

	void Close();

	std::pair<String, String> GetClientAddressDetails();
	String GetClientAddress();
	std::pair<String, String> GetPeerAddressDetails();
	String GetPeerAddress();

	size_t Read(void *buffer, size_t size);
	size_t Write(const void *buffer, size_t size);

	void Listen();
	Socket::Ptr Accept();

	bool Poll(bool read, bool write, struct timeval *timeout = nullptr);

	void MakeNonBlocking();

	static void SocketPair(SOCKET s[2]);

protected:
	void SetFD(SOCKET fd);

	int GetError() const;

	mutable std::mutex m_SocketMutex;

private:
	SOCKET m_FD{INVALID_SOCKET}; /**< The socket descriptor. */

	static std::pair<String, String> GetDetailsFromSockaddr(sockaddr *address, socklen_t len);
	static String GetHumanReadableAddress(const std::pair<String, String>& socketDetails);
};

class socket_error : virtual public std::exception, virtual public boost::exception { };

}

#endif /* SOCKET_H */