-
-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathNetworkProtocol.cpp
More file actions
83 lines (70 loc) · 2.54 KB
/
Copy pathNetworkProtocol.cpp
File metadata and controls
83 lines (70 loc) · 2.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
/*---------------------------------------------------------*\
| NetworkProtocol.cpp |
| |
| OpenRGB SDK network protocol |
| |
| Adam Honse (CalcProgrammer1) 09 May 2020 |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include <cstring>
#include <iostream>
#if defined(_WIN32) || defined(_WIN64)
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#else
#include <unistd.h>
#include <climits>
#endif
#include "NetworkProtocol.h"
/*-----------------------------------------------------*\
| OpenRGB SDK Magic Value "ORGB" |
\*-----------------------------------------------------*/
const char openrgb_sdk_magic[OPENRGB_SDK_MAGIC_SIZE] = { 'O', 'R', 'G', 'B' };
std::string GetHostname()
{
/*-----------------------------------------------------*\
| Determine maximum hostname limit or fallback to |
| defensive size |
\*-----------------------------------------------------*/
#if defined(HOST_NAME_MAX)
const size_t buffer_size = HOST_NAME_MAX + 1;
#else
const size_t buffer_size = 256;
#endif
char buffer[buffer_size];
#if defined(_WIN32) || defined(_WIN64)
/*-------------------------------------------------*\
| Initialize Winsock data required on Windows |
| before socket calls |
\*-------------------------------------------------*/
WSADATA wsa_data;
if(WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0)
{
return "unknown";
}
#endif
std::string hostname = "unknown";
if(gethostname(buffer, sizeof(buffer)) == 0)
{
hostname = buffer;
}
#if defined(_WIN32) || defined(_WIN64)
WSACleanup();
#endif
return(hostname);
}
void InitNetPacketHeader
(
NetPacketHeader * pkt_hdr,
unsigned int pkt_dev_id,
unsigned int pkt_id,
unsigned int pkt_size
)
{
memcpy(pkt_hdr->pkt_magic, openrgb_sdk_magic, sizeof(openrgb_sdk_magic));
pkt_hdr->pkt_dev_id = pkt_dev_id;
pkt_hdr->pkt_id = pkt_id;
pkt_hdr->pkt_size = pkt_size;
}