-
Notifications
You must be signed in to change notification settings - Fork 1
/
RemoteProcessClient.cpp
112 lines (98 loc) · 2.96 KB
/
RemoteProcessClient.cpp
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
#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "RemoteProcessClient.h"
using namespace std;
using namespace model;
using namespace rapidjson;
const int32 BUFFER_SIZE = 8 * 1024;
string RemoteProcessClient::readline() {
while (true) {
size_t eol = buffer.find('\n');
if (eol != string::npos) {
string line = buffer.substr(0, eol);
buffer = buffer.substr(eol + 1);
return line;
}
int32 received = socket.Receive(BUFFER_SIZE);
if (received < 0) {
cerr << "Error reading from socket" << endl;
exit(10002);
}
if (received == 0) {
return "";
}
buffer.append(socket.GetData(), socket.GetData() + received);
}
}
void RemoteProcessClient::writeline(string line) {
line.push_back('\n');
if (socket.Send(reinterpret_cast<const uint8*>(line.c_str()), static_cast<int32_t>(line.length())) < 0) {
cerr << "Failed to send data" << endl;
exit(10003);
}
}
RemoteProcessClient::RemoteProcessClient(string host, int port) {
#ifdef FROM_LOG
fin = ifstream("logs/346639.log", std::ifstream::in);
std::string wat;
std::getline(fin, wat);
#endif
socket.Initialize();
socket.DisableNagleAlgoritm();
if (!socket.Open(reinterpret_cast<const uint8*>(host.c_str()), static_cast<int16>(port))) {
cerr << "Failed to connect to " << host << ":" << port << endl;
exit(10001);
}
writeline("json");
}
unique_ptr<Rules> RemoteProcessClient::read_rules() {
string line = readline();
if (line.empty()) {
return unique_ptr<Rules>();
}
Document d;
d.Parse(line.c_str());
unique_ptr<Rules> result(new Rules());
result->read(d);
return result;
}
unique_ptr<Game> RemoteProcessClient::read_game() {
string line = readline();
if (line.empty()) {
return unique_ptr<Game>();
}
Document d;
d.Parse(line.c_str());
unique_ptr<Game> result(new Game());
result->read(d);
#ifdef FROM_LOG
string line2;
std::getline(fin, line2);
if (line2.empty()) {
return unique_ptr<Game>();
}
Document d2;
d2.Parse(line2.c_str());
unique_ptr<Game> result2(new Game());
result2->read2(d2);
return result2;
#endif
return result;
}
void RemoteProcessClient::write(const unordered_map<int, Action>& actions, const string& custom_rendering) {
Document d;
d.SetObject();
Document::AllocatorType& allocator = d.GetAllocator();
for (auto it : actions) {
d.AddMember(Value(to_string(it.first).c_str(), allocator).Move(), it.second.to_json(allocator).Move(), allocator);
}
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
d.Accept(writer);
writeline(string(buffer.GetString()) + "|" + custom_rendering + "\n<end>");
}
void RemoteProcessClient::write_token(const string& token) {
writeline(token);
}