This repository was archived by the owner on Jan 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocket.cpp
158 lines (136 loc) · 2.89 KB
/
Socket.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "Socket.hpp"
#include "Utility.hpp"
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <csignal>
#include <cstdarg>
#include <cstdlib>
#include <cstring>
Socket::Socket() :
sock(0),
valid(true) {
}
Socket::Socket(int _sock) :
sock(_sock),
valid(true) {
}
void Socket::makeInvalid(std::string s) {
valid = false;
Utility::info(s.c_str());
}
bool Socket::checkValid() {
return checkValid("");
}
bool Socket::checkValid(std::string pref) {
if (!valid) {
Utility::info((pref + " Socket is invalid").c_str());
return false;
}
return true;
}
void Socket::Socket_(int domain, int type, int protocol) {
if (!checkValid("Socket::Socket_")) {
return;
}
sock = socket(domain, type, protocol);
if (sock < 0) {
Utility::info("socket");
makeInvalid("Socket_");
}
}
Socket::Socket(const Socket &c) :
sock(c.sock),
valid(c.valid) {
}
void Socket::Connect(const sockaddr *addr, socklen_t addrlen) {
if (!checkValid("Socket::Connect")) {
return;
}
int err = connect(sock, addr, addrlen);
if (err < 0) {
makeInvalid("connect");
}
}
int Socket::get() const {
return sock;
}
void Socket::makeNonBlocking() {
if (!checkValid("Socket::makeNonBlocking")) {
return;
}
int flags, s;
flags = fcntl(sock, F_GETFL, 0);
if (flags == -1) {
return makeInvalid("fcntl");
}
flags |= O_NONBLOCK;
s = fcntl(sock, F_SETFL, flags);
if (s == -1) {
return makeInvalid("fcntl");
}
}
void Socket::Close() {
if (!checkValid("Close")) {
return;
}
int err = close(sock);
if (err < 0) {
makeInvalid("close");
}
}
void Socket::Listen() {
if (!checkValid("Socket::Listen")) {
return;
}
if (listen(sock, SOMAXCONN)) {
makeInvalid("listen");
}
}
bool Socket::Bind(const sockaddr *addr, socklen_t addrlen) {
if (!checkValid("Socket::Bind")) {
return false;
}
int res = bind(sock, addr, addrlen);
if (res != 0) {
makeInvalid("bind");
}
return res == 0;
}
std::string Socket::receive(int max_len) {
if (!checkValid("Socket::receive")) {
return "";
}
static char buffer[BUFFER_LEN];
std::string result = INVALID_DATA;
while (true) {
ssize_t count = Read(buffer, BUFFER_LEN);
if (count == -1 && errno == EAGAIN) {
break;
}
if (count == 0) {
throw ClosedConnectionException();
}
result += std::string(buffer, count);
if (max_len != INF && static_cast<int>(result.size()) >= max_len) {
throw TooMuchDataException();
}
}
return result;
}
void Socket::Send(const std::string &s) {
if (!checkValid("Socket::Send")) {
return;
}
Write(s.c_str(), s.size());
}
std::string Socket::receiveOnce() {
static char buffer[BUFFER_LEN];
ssize_t count = Read(buffer, BUFFER_LEN);
if (count == 0) {
throw ClosedConnectionException();
}
return std::string(buffer, count);
}