forked from awwit/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocket.h
More file actions
96 lines (75 loc) · 2.02 KB
/
Socket.h
File metadata and controls
96 lines (75 loc) · 2.02 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once
#ifdef WIN32
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
#undef max
#elif POSIX
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <poll.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <fcntl.h>
#else
#error "Undefine platform"
#endif
#include "System.h"
#include <cstddef>
#include <cstdint>
#include <vector>
#include <string>
#include <chrono>
namespace HttpServer
{
class Socket
{
protected:
System::native_socket_type socket_handle;
public:
bool static Startup();
bool static Cleanup();
public:
Socket();
Socket(const System::native_socket_type);
Socket(const Socket &);
Socket(Socket &&);
~Socket() = default;
System::native_socket_type open();
int close();
inline bool is_open() const
{
#ifdef WIN32
return INVALID_SOCKET != socket_handle;
#elif POSIX
return ~0 != socket_handle;
#else
#error "Undefine platform"
#endif
}
int bind(const int) const;
int listen() const;
Socket accept() const;
Socket nonblock_accept() const;
Socket nonblock_accept(const std::chrono::milliseconds &) const;
int shutdown() const;
bool nonblock(const bool = true) const;
// bool is_nonblock() const;
bool tcp_nodelay(const bool = true) const;
size_t recv(std::vector<std::string::value_type> &) const;
size_t nonblock_recv(std::vector<std::string::value_type> &, const std::chrono::milliseconds &) const;
size_t send(const std::string &) const;
size_t send(const std::vector<std::string::value_type> &, const size_t) const;
size_t nonblock_send(const std::string &, const std::chrono::milliseconds &) const;
size_t nonblock_send(const std::vector<std::string::value_type> &, const size_t, const std::chrono::milliseconds &) const;
void nonblock_send_sync() const;
inline System::native_socket_type get_handle() const
{
return socket_handle;
}
Socket &operator =(const Socket);
bool operator ==(const Socket &sock) const;
bool operator !=(const Socket &sock) const;
};
};