forked from RoboCup-SSL/ssl-refbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.cc
63 lines (55 loc) · 978 Bytes
/
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
57
58
59
60
61
62
#include "socket.h"
#include "exception.h"
#ifdef WIN32
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#else
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#endif
namespace {
#ifdef WIN32
class WinsockInitializer {
public:
WinsockInitializer() {
WORD sockVersion;
WSADATA wsaData;
sockVersion = MAKEWORD(2, 2);
WSAStartup(sockVersion, &wsaData);
}
~WinsockInitializer() {
WSACleanup();
}
};
#endif
}
Socket::Socket(int domain, int type, int proto) {
init_system();
sock = socket(domain, type, proto);
if (sock < 0) {
throw SystemError("Cannot create socket");
}
}
Socket::~Socket() {
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
}
Socket &Socket::operator=(Socket &&moveref) {
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
sock = moveref.sock;
moveref.sock = -1;
return *this;
}
void Socket::init_system() {
#ifdef WIN32
static WinsockInitializer ws_init;
#endif
}