forked from awwit/httpserverapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocket.h
More file actions
103 lines (81 loc) · 2.25 KB
/
Socket.h
File metadata and controls
103 lines (81 loc) · 2.25 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
97
98
99
100
101
102
103
#pragma once
#ifdef 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>
#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();
int static getLastError();
public:
Socket();
Socket(const System::native_socket_type fd);
Socket(const Socket &obj);
Socket(Socket &&obj);
~Socket() = default;
bool open();
bool close();
inline bool is_open() const
{
#ifdef WIN32
return INVALID_SOCKET != this->socket_handle;
#elif POSIX
return ~0 != this->socket_handle;
#else
#error "Undefine platform"
#endif
}
bool bind(const int port) const;
bool listen() const;
Socket accept() const;
Socket nonblock_accept() const;
Socket nonblock_accept(const std::chrono::milliseconds &timeout) const;
bool shutdown() const;
bool nonblock(const bool isNonBlock = true) const;
// bool is_nonblock() const;
bool tcp_nodelay(const bool nodelay = true) const;
long recv(std::vector<std::string::value_type> &buf) const;
long nonblock_recv(std::vector<std::string::value_type> &buf, const std::chrono::milliseconds &timeout) const;
long send(const std::string &buf) const;
long send(const std::vector<std::string::value_type> &buf, const size_t length) const;
long nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const;
long nonblock_send(const std::vector<std::string::value_type> &buf, const size_t length, const std::chrono::milliseconds &timeout) const;
void nonblock_send_sync() const;
inline System::native_socket_type get_handle() const
{
return this->socket_handle;
}
Socket &operator =(const Socket &obj);
bool operator ==(const Socket &obj) const;
bool operator !=(const Socket &obj) const;
};
};
namespace std
{
// Hash for Socket
template<> struct hash<HttpServer::Socket>
{
std::size_t operator()(const HttpServer::Socket &obj) const
{
return std::hash<System::native_socket_type>{}(obj.get_handle() );
}
};
};