-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArduino-Minecraft-Monitor.cpp
More file actions
202 lines (193 loc) · 5.4 KB
/
Arduino-Minecraft-Monitor.cpp
File metadata and controls
202 lines (193 loc) · 5.4 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
#include "Arduino-Minecraft-Monitor.h"
ArduinoMinecraftMonitor::ArduinoMinecraftMonitor(IPAddress ip, uint16_t port) {
this->minecraftServerIP = ip;
this->minecraftQueryPort = port;
}
void ArduinoMinecraftMonitor::resetTimeout() {
this->previousMillis = millis();
this->errorState = false;
}
bool ArduinoMinecraftMonitor::waitTimeout() {
unsigned long currentMillis = millis();
if (currentMillis - this->previousMillis >= this->timeout) {
this->previousMillis = millis();
//Error: Connection Timeout
this->errorState = true;
return false;
} else {
this->errorState = false;
return true;
}
}
bool ArduinoMinecraftMonitor::getStats() {
//Getting Minecraft Status...
String token = runHandshake();
if (errorState) {
//Error in Handshake - return false
return false;
}
getServerStats(token);
if (errorState) {
//Error in Getting Stats - return false
return false;
}
//Finished Getting Status
return true;
}
String ArduinoMinecraftMonitor::runHandshake() {
//Executing Minecraft Handshake...
//Sending Handshake Packet - 0xFE, 0xFD, 0x09, 0x04, 0x05, 0x06, 0x07
udpPacket.begin(minecraftQueryPort);
udpPacket.beginPacket(minecraftServerIP, minecraftQueryPort);
char handshare[] = {0xFE, 0xFD, 0x09, 0x04, 0x05, 0x06, 0x07 };
udpPacket.write(handshare, 7);
udpPacket.endPacket();
char* receiveData;
String returnData;
int packetSize = 0;
//Packet Sent, Waiting for reply...
resetTimeout();
while (waitTimeout()) {
packetSize = udpPacket.parsePacket();
if (packetSize) {
//Server Replied
receiveData = new char[packetSize];
udpPacket.read(receiveData, packetSize);
if (receiveData[0] != 0x09) {
//Handshake - Packet Error
delete receiveData;
errorState = true;
return "";
}
for (int i = 5; i < packetSize; i++ ) {
if (receiveData[i] == '\0') {
break;
} else {
returnData += receiveData[i];
}
}
break;
}
}
if (errorState) {
//Handshake - Timeout Error
return "";
}
delete receiveData;
return returnData;
}
void ArduinoMinecraftMonitor::getServerStats(String token) {
//Retrieving Server Status...
udpPacket.beginPacket(minecraftServerIP, minecraftQueryPort);
char handshare[] = {0xFE, 0xFD, 0x00, 0x04, 0x05, 0x06, 0x07 };
//Sending Handshake Packet - 0xFE, 0xFD, 0x09, 0x04, 0x05, 0x06, 0x07
udpPacket.write(handshare, 7);
const int32_t number = strtol(token.c_str(), NULL, 10);
unsigned char bytes[4];
bytes[0] = (number >> 24) & 0xFF;
bytes[1] = (number >> 16) & 0xFF;
bytes[2] = (number >> 8) & 0xFF;
bytes[3] = number & 0xFF;
//Plus Token
udpPacket.write(bytes, 4);
char padding[] = { 0x00, 0x00, 0x00, 0x00};
//Plus Padding - 0x00, 0x00, 0x00, 0x00
udpPacket.write(padding, 4);
udpPacket.endPacket();
//Packet Sent, Waiting for reply...
char* receiveData;
int packetSize = 0;
resetTimeout();
while (waitTimeout()) {
packetSize = udpPacket.parsePacket();
if (packetSize) {
//Server Replied!
receiveData = new char[packetSize];
udpPacket.read(receiveData, packetSize);
if (receiveData[0] != 0x00) {
//Handshake - Packet Error
delete receiveData;
errorState = true;
return;
}
break;
}
}
if (errorState) {
//GetStats - Timeout Error
return;
}
interpretStatusPacket(receiveData, packetSize);
}
int ArduinoMinecraftMonitor::readUntilNull(String* data, char* receiveData, int counter, int increment) {
*data = "";
counter += increment;
while (receiveData[counter] != '\0') {
*data += receiveData[counter];
counter++;
}
return counter;
}
void ArduinoMinecraftMonitor::interpretStatusPacket(char* receiveData, int packetSize) {
//Interpret Status Packet...
int i = 0;
i = readUntilNull(&motd, receiveData, i, 25);
i = readUntilNull(&gametype, receiveData, i, 10);
i = readUntilNull(&gameID, receiveData, i, 9);
i = readUntilNull(&mcversion, receiveData, i, 9);
i = readUntilNull(&plugins, receiveData, i, 9);
i = readUntilNull(&mcmap, receiveData, i, 5);
String tmpStr = "";
i = readUntilNull(&tmpStr, receiveData, i, 12);
onlinePlayers = strtol(tmpStr.c_str(), NULL, 10);
i = readUntilNull(&tmpStr, receiveData, i, 12);
maxPlayers = strtol(tmpStr.c_str(), NULL, 10);
i = readUntilNull(&tmpStr, receiveData, i, 10);
hostport = strtol(tmpStr.c_str(), NULL, 10);
i = readUntilNull(&hostIP, receiveData, i, 8);
players = "";
i += 12;
while (i < (packetSize - 1)) {
if (receiveData[i] == '\0') {
players += ",";
} else {
players += receiveData[i];
}
i++;
}
players.remove(players.length() - 1);
delete receiveData;
}
String ArduinoMinecraftMonitor::getMOTD() {
return motd;
}
String ArduinoMinecraftMonitor::getGameType() {
return gametype;
}
String ArduinoMinecraftMonitor::getGameID() {
return gameID;
}
String ArduinoMinecraftMonitor::getMCVersion() {
return mcversion;
}
String ArduinoMinecraftMonitor::getPlugins() {
return plugins;
}
String ArduinoMinecraftMonitor::getMCMap() {
return mcmap;
}
int ArduinoMinecraftMonitor::getOnlinePlayers() {
return onlinePlayers;
}
int ArduinoMinecraftMonitor::getMaxPlayers() {
return maxPlayers;
}
int ArduinoMinecraftMonitor::getHostPort() {
return hostport;
}
String ArduinoMinecraftMonitor::getHostIP() {
return hostIP;
}
String ArduinoMinecraftMonitor::getPlayers() {
return players;
}