-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocket.cpp
More file actions
199 lines (171 loc) · 3.54 KB
/
Socket.cpp
File metadata and controls
199 lines (171 loc) · 3.54 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
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
#include "Socket.h"
// some specific code for winsock and BSD sockets
static void closesocket_(socket_t sockToClose)
{
// closesocket() on winodws, close() on POSIX
#ifdef WIN32
closesocket(sockToClose);
#else
close(sockToClose);
#endif
}
static void WSACleanup_()
{
#ifdef WIN32
WSACleanup();
#endif
}
int Socket::GetLastError()
{
#ifdef WIN32
return WSAGetLastError();
#else
return errno;
#endif
}
// inits socket(windows)
Socket::Socket()
{
#ifdef WIN32
WSADATA wsaData;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0)
{
spdlog::error("Failed WSAStartup");
WSACleanup_();
exit(1);
}
#endif
}
Socket::Socket(socket_t sockfd)
{
#ifdef WIN32
WSADATA wsaData;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0)
{
spdlog::error("Failed WSAStartup: {}", WSAGetLastError());
WSACleanup_();
exit(1);
}
#endif
m_Sockfd = sockfd;
}
void Socket::Bind(const char* iNode, const char* iPort)
{
struct addrinfo* result = nullptr;
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
iResult = getaddrinfo(iNode, iPort, &hints, &result);
if (iResult != 0)
{
spdlog::error("getaddrinfo failed: {}", gai_strerror(iResult));
freeaddrinfo(result);
WSACleanup_();
exit(1);
}
// create listening socket
m_Sockfd = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (m_Sockfd == INVALID_SOCKET)
{
spdlog::error("socket failed. Error: {}", GetLastError());
freeaddrinfo(result);
WSACleanup_();
exit(1);
}
// bind socket
iResult = bind(m_Sockfd, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR)
{
spdlog::error("bind failed. Error: {}", GetLastError());
freeaddrinfo(result);
WSACleanup_();
exit(1);
}
freeaddrinfo(result); // finished binding
}
Socket::~Socket()
{
}
void Socket::Listen(int backlog)
{
iResult = listen(m_Sockfd, backlog);
if (iResult != 0)
{
spdlog::error("listen failed. Error: {}", GetLastError());
closesocket_(m_Sockfd);
WSACleanup_();
exit(1);
}
}
Socket Socket::Accept()
{
socket_t clientSocket = INVALID_SOCKET;
clientSocket = accept(m_Sockfd, NULL, NULL);
if (clientSocket == INVALID_SOCKET)
{
spdlog::error("accept failed. Error: {}", GetLastError());
closesocket_(m_Sockfd);
WSACleanup_();
exit(1);
}
return Socket(clientSocket);
}
std::string Socket::Recv(int bytesToRecv)
{
// iResult -> no. of bytes recv'd
std::string recvData;
recvData.resize(bytesToRecv);
iResult = recv(m_Sockfd, recvData.data(), bytesToRecv, 0);
if (iResult > 0)
{
spdlog::info("Received bytes: {}", iResult);
}
else if (iResult == 0)
spdlog::info("Closing connection...");
else
{
// failed
spdlog::error("recv failed. Error: {}", GetLastError());
closesocket_(m_Sockfd);
WSACleanup_();
exit(1);
}
return recvData;
}
void Socket::Send(const std::string& sendString)
{
int iResult;
iResult = send(m_Sockfd, sendString.c_str(), sendString.length(), 0);
spdlog::info("Bytes sent: {}", iResult);
if (iResult == SOCKET_ERROR)
{
spdlog::error("send failed. Error: {}", GetLastError());
closesocket_(m_Sockfd);
WSACleanup_();
exit(1);
}
}
void Socket::CloseSocket()
{
closesocket_(m_Sockfd);
}
void Socket::Shutdown()
{
int iResult;
iResult = shutdown(m_Sockfd, 1);
if (iResult == SOCKET_ERROR)
{
spdlog::error("shutdown failed. Error: {}", GetLastError());
closesocket_(m_Sockfd);
WSACleanup_();
exit(1);
}
// cleanup
closesocket_(m_Sockfd);
WSACleanup_();
}