LIPH's C++ Codes
socket.h
Go to the documentation of this file.
1#ifndef LIPH_NET_SOCKET_H_
2#define LIPH_NET_SOCKET_H_
3
4#include "liph/macros.h"
5
6#ifdef OS_UNIX
7
8#include <sys/types.h>
9
10#include <memory>
11
12#include "liph/io/descriptor.h"
13
14namespace liph::net {
15
16class socket : public io::descriptor {
17public:
18 enum { IPV4 = 1, IPV6 = 2, TCP = 4, UDP = 8 };
19
20 static const int BUFFSIZE = 1024;
21
22public:
23 socket() : descriptor(-1) {}
24 socket(int type);
25 socket(const socket& other);
26
27 ~socket();
28
29 bool set_non_block();
30
31 bool bind(int& port);
32
33 bool listen(int backlog);
34
35 std::shared_ptr<socket> accept();
36 bool connect(const char *ip, int port);
37
38 bool is_tcp() const { return type_ & socket::TCP; }
39
40 bool is_udp() const { return !is_tcp(); }
41
42 bool is_ipv4() const { return type_ & socket::IPV4; }
43
44 bool is_ipv6() const { return !is_ipv4(); }
45
46 ssize_t recv(void *buf, size_t nbytes, int flags);
47 ssize_t send(const void *buf, size_t nbytes, int flags);
48
49private:
50 int type_;
51};
52
53} // namespace liph::net
54
55#endif
56
57#endif // LIPH_NET_SOCKET_H_