p9y: bsd
[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 <netinet/in.h>
22 #include <sys/un.h>
23
24 class Connection {
25 public:
26 explicit Connection(socket_or_port_t socket_or_port);
27 ~Connection();
28
29 friend void swap(Connection &a, Connection &b);
30 void swap(Connection &conn);
31
32 Connection(const Connection &conn);
33 Connection &operator=(const Connection &conn);
34
35 Connection(Connection &&conn) noexcept;
36 Connection &operator=(Connection &&conn) noexcept;
37
38 int getError();
39 int getLastError();
40
41 bool isWritable();
42 bool isOpen();
43
44 bool open();
45 void close();
46
47 private:
48 int sock{-1}, last_err{-1};
49 sockaddr_storage addr{};
50 enum sockaddr_size {
51 NONE = 0,
52 UNIX = sizeof(sockaddr_un),
53 INET = sizeof(sockaddr_in),
54 INET6 = sizeof(sockaddr_in6)
55 } size = NONE;
56 bool connected{false};
57
58 static string error(const initializer_list<string> &args);
59 };