-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocklib_generic.cpp
More file actions
76 lines (60 loc) · 1.57 KB
/
socklib_generic.cpp
File metadata and controls
76 lines (60 loc) · 1.57 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
#include <iostream>
#include <string.h>
#include "socklib.h"
std::string to_string(const ByteString &s) {
std::string str(s.begin(), s.end());
return str;
}
Socket::Socket() {
memset(_data.data, 0, sizeof(_data.data));
_has_socket = false;
_last_error = 0;
}
Socket::Socket(Socket::Family family, Socket::Type type) : Socket() {
Create(family, type);
}
Socket::Socket(Socket &&other) {
_has_socket = other._has_socket;
_last_error = other._last_error;
memcpy(_data.data, other._data.data, sizeof(_data.data));
other._has_socket = false;
memset(other._data.data, 0, sizeof(other._data.data));
}
Socket::~Socket() {
if (_has_socket) native_destroy(*this);
}
int Socket::GetLastError() {
return _last_error;
}
size_t Socket::SendAll(const ByteString &data) {
return SendAll(data.data(), data.size());
}
size_t Socket::SendAll(const char *data, size_t len) {
size_t send_count = 0;
while (send_count < len) {
size_t count = Send(data + send_count, len - send_count);
send_count += count;
}
return send_count;
}
int Socket::Recv(ByteString &buffer) {
return Recv(buffer.data(), buffer.size());
}
PoolView Socket::RecvIntoPool(unsigned int max_len) {
PoolView pool = get_pool(max_len);
pool.name = "Recv Temp Pool";
Recv(*pool);
return pool;
}
ByteString to_bytestring(const char *msg, size_t len) {
ByteString str;
str.reserve(len);
for (const char *p = msg; *p != '\0'; p++) {
str.push_back(*p);
}
return str;
}
std::ostream &operator<<(std::ostream &s, const ByteString &b) {
s.write(b.data(), b.size());
return s;
}