-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocket.cpp
206 lines (190 loc) · 4.97 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "Socket.hpp"
#include "IPv4.hpp"
#include <iostream>
namespace Tool {
namespace Network{
static WSADATA data;
//Socket::Socket(Socket & socket) :
uint16_t addressSize(AddressFamily addressFamily)
{
switch (addressFamily)
{
case ipv4:
return sizeof(SOCKADDR_IN);
break;
case ipv6:
return sizeof(SOCKADDR_IN6);
break;
case irda:
return sizeof(SOCKADDR_IRDA);
break;
default:
return sizeof(SOCKADDR_STORAGE);
}
}
Socket::Socket(SOCKET socket, AddressFamily addressFamily, Type socketType, Protocol protocol)
: socket(socket), addressFamily(addressFamily), type(socketType), protocol(protocol),
accepted(false), bound(false), listening(false)
{
}
Socket::Socket(SOCKET socket, SOCKADDR_STORAGE clientAddress, Type socketType, Protocol protocol)
: socket(socket), address(clientAddress), type(socketType), protocol(protocol),
accepted(true ), bound(false), listening(false)
{
addressFamily = (AddressFamily)clientAddress.ss_family;
}
Socket::Socket(AddressFamily addressFamily, Type socketType, Protocol protocol)
:addressFamily(addressFamily), type(socketType), protocol(protocol),
accepted(false), bound(false), listening(false)
{
WSAStartup(MAKEWORD(2, 2), &data);
socket = ::socket(addressFamily, socketType, protocol);
}
Socket Socket::Accept(bool blocking)
{
SOCKADDR_STORAGE acceptedAddress;
int addrlen = sizeof(acceptedAddress);
SOCKET sock;
u_long param = !blocking;
if (ioctlsocket(socket, FIONBIO, ¶m))
throw "ioctl failed!";
sock = accept(socket, (SOCKADDR*)&acceptedAddress, &addrlen);
if (sock == INVALID_SOCKET)
throw "Invalid socket!";
return Socket(socket, acceptedAddress, type, protocol);
}
void Socket::Listen()
{
return Listen(SOMAXCONN);
}
void Socket::Listen(size_t backlog)
{
if (backlog > SOMAXCONN)
throw std::out_of_range("Backlog is to big!");
this->backlog = backlog;
if (listen(this->socket, backlog))
throw WSAGetLastError();
listening = true;
}
void Socket::Bind(SOCKADDR* address)
{
this->address = *((SOCKADDR_STORAGE*)address);
if (address->sa_family != this->addressFamily)
throw std::invalid_argument("Address has the wrong Family!");
if (bind(this->socket, address, addressSize(this->addressFamily)))
throw WSAGetLastError();
bound = true;
}
int Socket::Connect(SOCKADDR* address)
{
this->address = *((SOCKADDR_STORAGE*)address);
if (address->sa_family != this->addressFamily)
throw std::invalid_argument("Address has the wrong Family!");
return connect(this->socket, address, addressSize(this->addressFamily));
}
std::vector<uint8_t> Socket::Recieve()
{
u_long n;
int returnvalue;
if (ioctlsocket(socket, FIONREAD, &n) == SOCKET_ERROR)
throw -1;
std::vector<uint8_t> result;
result.resize(n);
returnvalue = recv(socket, (char*)result.data(), 100, 0);
if (returnvalue == -1)
n = 0;
else if (returnvalue < 1)
throw returnvalue;
result.resize(n);
return result;
}
std::vector<uint8_t> Socket::Recieve(size_t n)
{
int returnvalue;
std::vector<uint8_t> result;
result.resize(n);
returnvalue = recv(socket, (char*)result.data(), 100, 0);
if (returnvalue == -1)
n = 0;
else if (returnvalue < 1)
throw returnvalue;
result.resize(n);
return result;
}
int Socket::Send(std::vector<uint8_t> data)
{
return send(socket, (char*)data.data(), data.size(), 0);
}
bool Socket::IsPendingRead(uint32_t timeoutinmicrosec)
{
int selectresult;
TIMEVAL tv;
tv.tv_usec = timeoutinmicrosec;
fd_set fs;
fs.fd_array[0] = socket;
fs.fd_count = 1;
selectresult = select(0, &fs, 0, 0, &tv);
if (selectresult > 0)
return true;
else if (selectresult < 0)
throw WSAGetLastError();
return false;
}
bool Socket::IsPendingWrite(uint32_t timeoutinmicrosec)
{
int selectresult;
TIMEVAL tv;
tv.tv_usec = timeoutinmicrosec;
fd_set fs;
fs.fd_array[0] = socket;
fs.fd_count = 1;
selectresult = select(0, 0, &fs, 0, &tv);
if (selectresult > 0)
return true;
else if (selectresult < 0)
throw WSAGetLastError();
return false;
}
SOCKET Socket::GetSocket()
{
return this->socket;
}
Socket::~Socket()
{
closesocket(socket);
WSACleanup();
}
AddressFamily Socket::GetaddressFamily()
{
return addressFamily;
}
Socket::Type Socket::GetType()
{
return type;
}
Socket::Protocol Socket::GetProtocol()
{
return protocol;
}
SOCKADDR_STORAGE Socket::GetAddress()
{
return address;
}
size_t Socket::GetBacklog()
{
return backlog;
}
bool Socket::HasAccepted()
{
return accepted;
}
bool Socket::IsBound()
{
return bound;
}
bool Socket::IsListening()
{
return listening;
}
}
}