-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathsockethelpers.cpp
More file actions
232 lines (196 loc) · 5.66 KB
/
sockethelpers.cpp
File metadata and controls
232 lines (196 loc) · 5.66 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
Copyright 2015-2020 Clément Gallet <clement.gallet@ens-lyon.org>
This file is part of libTAS.
libTAS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libTAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libTAS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sockethelpers.h"
#include <sys/socket.h>
#include <cstdlib>
#include <unistd.h>
#include <sys/un.h>
#include <iostream>
#include <vector>
#include <mutex>
#include <errno.h>
#include <fcntl.h>
#ifdef SOCKET_LOG
#include "lcf.h"
#include "../library/logging.h"
#else
#include <iostream>
#endif
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
/* Socket to communicate between the program and the game */
static int socket_fd = 0;
static std::mutex mutex;
void setSocket(int sock) {
socket_fd = sock;
}
void initSocketGame(void)
{
socket_fd = std::stoi(getenv("LIBTAS_SOCKET_FD"));
if (fcntl(socket_fd, F_GETFD) == -1) {
if (errno == EBADF) {
perror("LIBTAS_SOCKET_FD has been closed");
} else {
perror("Unable to use game socket");
}
exit(-1);
}
/* Don't generate SIGPIPE */
#if defined(__APPLE__) && defined(__MACH__)
int option_value = 1;
if (setsockopt(socket_fd, SOL_SOCKET, SO_NOSIGPIPE, &option_value, sizeof(option_value)))
{
std::cerr << "Couldn't set SO_NOSIGPIPE option." << std::endl;
exit(-1);
}
#endif
}
void closeSocket(void)
{
close(socket_fd);
}
void lockSocket(void)
{
mutex.lock();
}
void unlockSocket(void)
{
mutex.unlock();
}
int sendData(const void* elem, unsigned int size)
{
#ifdef SOCKET_LOG
libtas::debuglogstdio(LCF_SOCKET, "Send socket data of size %u", size);
#endif
ssize_t ret = 0;
do {
ret = send(socket_fd, elem, size, MSG_NOSIGNAL);
} while ((ret == -1) && (errno == EINTR));
if (ret == -1) {
#ifdef SOCKET_LOG
libtas::debuglogstdio(LCF_SOCKET | LCF_ERROR, "send() returns -1 with error %s", strerror(errno));
#else
std::cerr << "send() returns -1 with error " << strerror(errno) << std::endl;
#endif
}
else if (ret != static_cast<ssize_t>(size)) {
#ifdef SOCKET_LOG
libtas::debuglogstdio(LCF_SOCKET | LCF_ERROR, "send() %u bytes instead of %u", ret, size);
#else
std::cerr << "send() " << ret << " bytes instead of " << size << std::endl;
#endif
}
return ret;
}
int sendMessage(int message)
{
#ifdef SOCKET_LOG
libtas::debuglogstdio(LCF_SOCKET, "Send socket message %d", message);
#endif
return sendData(&message, sizeof(int));
}
void sendString(const std::string& str)
{
#ifdef SOCKET_LOG
libtas::debuglogstdio(LCF_SOCKET, "Send socket string %s", str.c_str());
#endif
unsigned int str_size = str.size();
sendData(&str_size, sizeof(unsigned int));
sendData(str.c_str(), str_size);
}
int receiveData(void* elem, unsigned int size)
{
#ifdef SOCKET_LOG
libtas::debuglogstdio(LCF_SOCKET, "Receive socket data of size %u", size);
#endif
ssize_t ret = 0;
do {
ret = recv(socket_fd, elem, size, MSG_WAITALL);
} while ((ret == -1) && (errno == EINTR));
if (ret == -1) {
#ifdef SOCKET_LOG
libtas::debuglogstdio(LCF_SOCKET | LCF_ERROR, "recv() returns -1 with error %s", strerror(errno));
#else
std::cerr << "recv() returns -1 with error " << strerror(errno) << std::endl;
#endif
}
else if (ret == 0) { // socket has been closed
#ifdef SOCKET_LOG
libtas::debuglogstdio(LCF_SOCKET | LCF_WARNING, "recv() returns 0 -> socket closed");
#else
std::cerr << "recv() returns 0 -> socket closed" << std::endl;
#endif
}
else if (ret != static_cast<ssize_t>(size)) {
#ifdef SOCKET_LOG
libtas::debuglogstdio(LCF_SOCKET | LCF_ERROR, "recv() %u bytes instead of %u", ret, size);
#else
std::cerr << "recv() " << ret << " bytes instead of " << size << std::endl;
#endif
}
return ret;
}
int receiveMessage()
{
int msg;
int ret = receiveData(&msg, sizeof(int));
#ifdef SOCKET_LOG
libtas::debuglogstdio(LCF_SOCKET, "Receive socket message %d", msg);
#endif
/* Handle special case for closed socket */
if (ret == 0)
return -2;
if (ret < 0)
return ret;
return msg;
}
int receiveMessageNonBlocking()
{
int msg;
int ret = recv(socket_fd, &msg, sizeof(int), MSG_WAITALL | MSG_DONTWAIT);
if (ret < 0)
return ret;
#ifdef SOCKET_LOG
libtas::debuglogstdio(LCF_SOCKET, "Receive non-blocking socket message %d", msg);
#endif
/* Handle special case for closed socket */
if (ret == 0)
return -2;
return msg;
}
std::string receiveString()
{
unsigned int str_size;
receiveData(&str_size, sizeof(unsigned int));
/* TODO: There might be a better way to do this...? */
std::vector<char> buf(str_size, 0x00);
receiveData(buf.data(), str_size);
std::string str(buf.data(), str_size);
#ifdef SOCKET_LOG
libtas::debuglogstdio(LCF_SOCKET, "Receive socket string %s", str.c_str());
#endif
return str;
}
void receiveCString(char* str)
{
unsigned int str_size;
receiveData(&str_size, sizeof(unsigned int));
receiveData(str, str_size);
str[str_size] = '\0';
#ifdef SOCKET_LOG
libtas::debuglogstdio(LCF_SOCKET, "Receive socket C string %s", str);
#endif
}