f087a697423f38b9d159d360ce92080159441032
[m6w6/libmemcached] / test / lib / Connection.hpp
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #include "common.hpp"
19
20 #include <sys/socket.h>
21 #include <sys/un.h>
22
23 class Connection {
24 public:
25 explicit Connection(socket_or_port_t socket_or_port);
26 ~Connection();
27
28 friend void swap(Connection &a, Connection &b);
29 void swap(Connection &conn);
30
31 Connection(const Connection &conn);
32 Connection &operator=(const Connection &conn);
33
34 Connection(Connection &&conn) noexcept;
35 Connection &operator=(Connection &&conn) noexcept;
36
37 int getError();
38 int getLastError();
39
40 bool isWritable();
41 bool isOpen();
42
43 bool open();
44 void close();
45
46 private:
47 int sock{-1}, last_err{-1};
48 sockaddr_storage addr{};
49 enum sockaddr_size {
50 NONE = 0,
51 UNIX = sizeof(sockaddr_un),
52 INET = sizeof(sockaddr_in),
53 INET6 = sizeof(sockaddr_in6)
54 } size = NONE;
55 bool connected{false};
56
57 static string error(const initializer_list<string> &args);
58 };