forked from shekyan/slowhttptest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.cc
56 lines (46 loc) · 1.2 KB
/
socket.cc
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
// Wrappers around sockets.
//
// (c) Victor Agababov ([email protected]) 2011
// Apache license goes here.
#include "socket.h"
#include "slowlog.h"
#include <unistd.h>
namespace slowhttptest {
Socket::Socket() : the_socket_(-1) {
}
Socket::~Socket() {
Close();
}
void Socket::Close() {
if (the_socket_ >= 0) {
::shutdown(the_socket_, SHUT_RDWR);
::close(the_socket_);
the_socket_ = -1;
}
}
int Socket::Send(const char* data, const int size) {
check(the_socket_ >= 0, "Not Connected");
return ::send(the_socket_, data, size, 0);
}
int Socket::Recv(char* data, const int size) {
check(the_socket_ >= 0, "Not Connected");
return ::recv(the_socket_, data, size, 0);
}
bool Socket::Init(const addrinfo* addr) {
CHECK_NOTNULL(addr);
const int sock = ::socket(addr->ai_family, addr->ai_socktype,
addr->ai_protocol);
check(sock != -1, "Sockets cannot be created");
const int ret = ::connect(sock, addr->ai_addr, addr->ai_addrlen);
return ret == 0;
}
Socket* Socket::Create(const addrinfo* addr) {
CHECK_NOTNULL(addr);
Socket* sock = new Socket();
if (!sock->Init(addr)) {
delete sock;
sock = NULL;
}
return sock;
}
} // namespace slowhttptest