forked from RipcordSoftware/libhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.h
More file actions
123 lines (88 loc) · 3.14 KB
/
Copy pathsocket.h
File metadata and controls
123 lines (88 loc) · 3.14 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef RS_LIBHTTPSERVER_SOCKET_H
#define RS_LIBHTTPSERVER_SOCKET_H
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <boost/noncopyable.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include "types.h"
#include "header_buffer.h"
namespace rs {
namespace httpserver {
class Socket final : public boost::enable_shared_from_this<Socket>, private boost::noncopyable {
public:
using byte = unsigned char;
class ScopedClose final {
public:
ScopedClose(socket_ptr socket) : socket_(socket) {}
~ScopedClose() { socket_->Close(); }
private:
socket_ptr socket_;
};
~Socket() {
Shutdown();
}
static socket_ptr Create(server_ptr server, asio_socket_ptr socket);
std::size_t Receive(byte* buffer, int length, bool peek = false);
std::size_t Receive(int timeout, byte* buffer, int length, bool peek = false);
std::size_t Send(const std::string& s) {
auto bytesSent = socket_->send(boost::asio::const_buffers_1(static_cast<const void*>(&s[0]), s.length()));
totalBytesSent_ += bytesSent;
return bytesSent;
}
std::size_t Send(const byte* buffer, int length) {
auto bytesSent = socket_->send(boost::asio::const_buffers_1(static_cast<const void*>(buffer), length));
totalBytesSent_ += bytesSent;
return bytesSent;
}
void Shutdown() {
if (Connected()) {
boost::system::error_code err;
socket_->shutdown(boost::asio::socket_base::shutdown_type::shutdown_both, err);
}
}
void Close() {
if (Connected()) {
Shutdown();
boost::system::error_code err;
socket_->close(err);
}
}
bool Connected() {
return socket_->is_open();
}
bool Available() {
boost::system::error_code err;
return socket_->available(err) > 0;
}
void Flush();
void NoDelay(bool enable);
asio_socket_ptr getAsioSocket() { return socket_; }
boost::asio::ip::tcp::endpoint getLocalEndpoint() { return socket_->local_endpoint(); }
boost::asio::ip::tcp::endpoint getRemoteEndpoint() { return socket_->remote_endpoint(); }
long getTotalBytesSent() {
return totalBytesSent_;
}
long getTotalBytesReceived() {
return totalBytesReceived_;
}
private:
Socket(server_ptr server, asio_socket_ptr socket) :
server_(server), socket_(socket), defaultReceiveTimeout_(-1), currentReceiveTimeout_(-1),
totalBytesSent_(0), totalBytesReceived_(0) {
}
static int getReceiveTimeout(asio_socket_ptr socket);
static bool setReceiveTimeout(asio_socket_ptr socket, int seconds);
void setDefaultReceiveTimeout();
void setCustomReceiveTimeout(int seconds);
bool Peek(int timeout);
server_ptr server_;
asio_socket_ptr socket_;
int defaultReceiveTimeout_;
int currentReceiveTimeout_;
long totalBytesSent_;
long totalBytesReceived_;
};
}}
#endif /* RS_LIBHTTPSERVER_SOCKET_H */