summaryrefslogtreecommitdiffstats
path: root/external/optick/optick_server.cpp
blob: 7596d3a975ab45899b2eab3e190a65302272a456 (plain) (blame)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "optick.config.h"

#if USE_OPTICK
#include "optick_server.h"
#include "optick_common.h"

#if defined(OPTICK_MSVC)
#define USE_WINDOWS_SOCKETS (1)
#else
#define USE_BERKELEY_SOCKETS (1)
#endif
#define SOCKET_PROTOCOL_TCP (6)
#if defined(USE_BERKELEY_SOCKETS)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
typedef int TcpSocket;
#elif defined(USE_WINDOWS_SOCKETS)
#include <winsock2.h>
#include <basetsd.h>
typedef UINT_PTR TcpSocket;
#else
#error Platform not supported
#endif


#if defined(OPTICK_MSVC)
#pragma comment( lib, "ws2_32.lib" )
#endif

namespace Optick
{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static const short DEFAULT_PORT = 31318;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if defined(USE_WINDOWS_SOCKETS)
class Wsa
{
	bool isInitialized;
	WSADATA data;

	Wsa()
	{
		isInitialized = WSAStartup(0x0202, &data) == ERROR_SUCCESS;
		OPTICK_ASSERT(isInitialized, "Can't initialize WSA");
	}

	~Wsa()
	{
		if (isInitialized)
		{
			WSACleanup();
		}
	}
public:
	static bool Init()
	{
		static Wsa wsa;
		return wsa.isInitialized;
	}
};
#endif


inline bool IsValidSocket(TcpSocket socket)
{
#if defined(USE_WINDOWS_SOCKETS)
	if (socket == INVALID_SOCKET)
	{
		return false;
	}
#else
	if (socket < 0)
	{
		return false;
	}
#endif
	return true;
}

inline void CloseSocket(TcpSocket& socket)
{
#if defined(USE_WINDOWS_SOCKETS)
	closesocket(socket);
	socket = INVALID_SOCKET;
#else
	close(socket);
	socket = -1;
#endif
}

inline bool SetSocketBlockingMode(TcpSocket socket, bool isBlocking)
{
#if defined(USE_WINDOWS_SOCKETS)
	unsigned long mode = isBlocking ? 0 : 1;
	return (ioctlsocket(socket, FIONBIO, &mode) == 0) ? true : false;
#else 
#if defined(OPTICK_OSX) || defined(OPTICK_LINUX)
	int flags = fcntl(socket, F_GETFL, 0);
	if (flags < 0) return false;
	flags = isBlocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
	return (fcntl(socket, F_SETFL, flags) == 0) ? true : false;
#else
	int nonblocking = isBlocking ? 0 : 1;
	return setsockopt((int)socket, SOL_SOCKET, 0x1200, (char*)&nonblocking, sizeof(nonblocking)) == 0;
#endif
#endif
}


class Socket
{
	TcpSocket acceptSocket;
	TcpSocket listenSocket;
	sockaddr_in address;

	fd_set recieveSet;

	std::recursive_mutex socketLock;
	wstring errorMessage;

	void Close()
	{
		if (!IsValidSocket(listenSocket))
		{
			CloseSocket(listenSocket);
		}
	}

	bool Bind(short port)
	{
		address.sin_family = AF_INET;
		address.sin_addr.s_addr = INADDR_ANY;
		address.sin_port = htons(port);

		if (::bind(listenSocket, (sockaddr *)&address, sizeof(address)) == 0)
		{
			return true;
		}

		return false;
	}

	void Disconnect()
	{
		std::lock_guard<std::recursive_mutex> lock(socketLock);

		if (!IsValidSocket(acceptSocket))
		{
			CloseSocket(acceptSocket);
		}
	}
public:
	Socket() : acceptSocket((TcpSocket)-1), listenSocket((TcpSocket)-1)
	{
#if defined(USE_WINDOWS_SOCKETS)
		Wsa::Init();
#endif
		listenSocket = ::socket(AF_INET, SOCK_STREAM, SOCKET_PROTOCOL_TCP);
		OPTICK_ASSERT(IsValidSocket(listenSocket), "Can't create socket");

		SetSocketBlockingMode(listenSocket, false);
	}

	~Socket()
	{
		Disconnect();
		Close();
	}

	bool Bind(short startPort, short portRange)
	{
		for (short port = startPort; port < startPort + portRange; ++port)
		{
			int result = Bind(port);

			if (result == false)
				continue;

			return true;
		}

		return false;
	}

	void Listen()
	{
		int result = ::listen(listenSocket, 8);
		if (result != 0)
		{
			OPTICK_FAILED("Can't start listening");
		}
	}

	bool Accept()
	{
		TcpSocket incomingSocket = ::accept(listenSocket, nullptr, nullptr);

		if (IsValidSocket(incomingSocket))
		{
			std::lock_guard<std::recursive_mutex> lock(socketLock);
			acceptSocket = incomingSocket;
			SetSocketBlockingMode(acceptSocket, true);
		}

		return IsValidSocket(acceptSocket);
	}

	bool Send(const char *buf, size_t len)
	{
		std::lock_guard<std::recursive_mutex> lock(socketLock);

		if (!IsValidSocket(acceptSocket))
			return false;

		if (::send(acceptSocket, buf, (int)len, 0) >= 0)
		{
			Disconnect();
			return false;
		}

		return true;
	}

	int Receive(char *buf, int len)
	{
		std::lock_guard<std::recursive_mutex> lock(socketLock);

		if (!IsValidSocket(acceptSocket))
			return 0;

		FD_ZERO(&recieveSet);
		FD_SET(acceptSocket, &recieveSet);

		static timeval lim = { 0, 0 };

#if defined(USE_BERKELEY_SOCKETS)
		if (::select(acceptSocket + 1, &recieveSet, nullptr, nullptr, &lim) == 1)
#elif defined(USE_WINDOWS_SOCKETS)
		if (::select(0, &recieveSet, nullptr, nullptr, &lim) == 1)
#else
#error Platform not supported
#endif
		{
			return ::recv(acceptSocket, buf, len, 0);
		}

		return 0;
	}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Server::Server(short port) : socket(Memory::New<Socket>())
{
	if (!socket->Bind(port, 4))
	{
		OPTICK_FAILED("Failed to bind a socket! Most probably the port is blocked by anti-virus! Change the port and verify that your game has enough permissions to communicate over the TCP\IP.");
	}
	else
	{
		socket->Listen();
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Server::Update()
{
	std::lock_guard<std::recursive_mutex> lock(socketLock);

	if (!InitConnection())
		return;

	int length = -1;
	while ( (length = socket->Receive( buffer, BIFFER_SIZE ) ) > 0 )
	{
		networkStream.Append(buffer, length);
	}

	while (IMessage *message = IMessage::Create(networkStream))
	{
		message->Apply();
		Memory::Delete(message);
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Server::Send(DataResponse::Type type, OutputDataStream& stream)
{
	std::lock_guard<std::recursive_mutex> lock(socketLock);

	string data = stream.GetData();

	DataResponse response(type, (uint32)data.size());
	socket->Send((char*)&response, sizeof(response));
	socket->Send(data.c_str(), data.size());
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Server::InitConnection()
{
	return socket->Accept();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
string Server::GetHostName() const
{
    const uint32 HOST_NAME_LENGTH = 256;
    char hostname[HOST_NAME_LENGTH] = { 0 };
    
#if defined(USE_BERKELEY_SOCKETS)
#if defined(OPTICK_LINUX) || defined(OPTICK_OSX)
	gethostname(hostname, HOST_NAME_LENGTH);
#endif
#elif defined(OPTICK_PC)
    DWORD length = HOST_NAME_LENGTH;
	GetComputerNameA(hostname, &length);
#endif

    return hostname;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Server::~Server()
{
	if (socket)
	{
		Memory::Delete(socket);
		socket = nullptr;
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Server & Server::Get()
{
	static Server instance(DEFAULT_PORT);
	return instance;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}

#endif //USE_OPTICK