This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectionRepository.cpp
179 lines (146 loc) · 4.76 KB
/
ConnectionRepository.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
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
#include "ConnectionRepository.h"
#include "DistributedObject.h"
vector<string> astron_keywords {"clsend", "ownsend", "clrecv", "ownrecv",
"airecv", "broadcast", "ram", "required", "db"};
ConnectionRepository::ConnectionRepository(boost::asio::io_service* io_service, string host, uint16_t port, string dcFile) :
m_dcFile(dcFile),
m_host(host),
m_port(port),
m_connection(Connection(io_service, host, port))
{
cout << "Initializing ConnectionRepository with dcFile " << dcFile << endl;
m_module = new Module();
for(string keyword : astron_keywords) {
m_module->add_keyword(keyword);
}
parse_dcfile(m_module, m_dcFile);
}
void ConnectionRepository::send(Datagram dg) {
m_connection.send(dg);
}
void ConnectionRepository::close() {
m_connection.close();
}
void ConnectionRepository::on_data(uint8_t* data, uint16_t len) {
}
void ConnectionRepository::loop() {
m_connection.poll(
[&](uint8_t* data, uint16_t len) {
on_data(data, len);
}
);
}
void ConnectionRepository::addRequiredFields(Datagram* dg, DistributedObject* obj) {
Class* dclass = m_module->class_by_name(obj->classname());
for(int i = 0; i < dclass->num_fields(); ++i) {
Field* field = dclass->get_field(i);
if(field->has_keyword("required")) {
vector<DValue> arguments = obj->get(field->name());
for(DValue argument : arguments) {
addDatagramDValue(dg, argument);
}
}
}
}
void ConnectionRepository::handleEnterObject(DatagramIterator* di, bool optionals, bool owner, bool ai) {
uint32_t do_id = di->read_uint32();
uint32_t parent_id = di->read_uint32();
uint32_t zone_id = di->read_uint32();
uint16_t dclass_id = di->read_uint16();
cout << "enterObject " << do_id << " dclass " << dclass_id <<
"(" << parent_id << "," << zone_id << ")" << endl;
Class* dclass = m_module->class_by_id(dclass_id);
// TODO: create object here
for(int i = 0; i < dclass->num_fields(); ++i) {
Field* field = dclass->get_field(i);
if(field->has_keyword("required")) {
// TODO: decode field
}
}
}
void ConnectionRepository::handleSetField(DatagramIterator* di) {
uint32_t do_id = di->read_uint32();
uint16_t field_id = di->read_uint16();
Module* module = getModule();
Field* field = module->field_by_id(field_id);
Type* fieldType = field->type();
Method* method = field->type()->as_method();
vector<DValue> arguments;
arguments.reserve(method->num_parameters());
for(int i = 0; i < method->num_parameters(); ++i) {
Parameter* param = method->get_parameter(i);
Type* ptype = param->type();
DValue v;
if(ptype->subtype() == kTypeVarstring) {
v = $(di->read_string());
} else if(ptype->subtype() == kTypeUint64) {
v = duint64(di->read_uint64());
} else {
cout << "TODO: support actually reading type " << ptype->to_string() << endl;
}
arguments.push_back(v);
}
DistributedObject* distObj = m_doId2do[do_id];
if(!distObj->fieldUpdate(field->name(), arguments)) {
cout << "Warning: unhandled field update for doId " << do_id << " field name " << field->name() << endl;
}
}
void ConnectionRepository::addDatagramDValue(Datagram* dg, DValue v) {
switch(v.d_type) {
case d_uint8:
dg->add_uint8(v.v_uint);
break;
case d_uint16:
dg->add_uint16(v.v_uint);
break;
case d_uint32:
dg->add_uint32(v.v_uint);
break;
case d_uint64:
dg->add_uint64(v.v_uint);
break;
case d_int8:
dg->add_int8(v.v_int);
break;
case d_int16:
dg->add_int16(v.v_int);
break;
case d_int32:
dg->add_int32(v.v_int);
break;
case d_int64:
dg->add_int64(v.v_int);
break;
case d_float32:
dg->add_float32(v.v_double);
break;
case d_string:
dg->add_string(v.v_string);
break;
default:
cout << "Attempting to add an unknown value of type " << v.d_type << endl;
dprint(v);
break;
}
}
void ConnectionRepository::registerDOG(DistributedObject* dog) {
m_doId2do[dog->getDoId()] = dog;
}
// registers a generic class to the classMap
// should not be used directly;
// sane developers should use abstracted macros to this interface
void ConnectionRepository::classRegister(
string className,
DistributedObject*(*construct)(uint64_t pDoId),
ClassType dtype)
{
ClassDefinition* def = (ClassDefinition*) malloc(sizeof(ClassDefinition));
def->dtype = dtype;
def->constructor = construct;
string accessName = className + (dtype == T_AI ? "AI" :
dtype == T_UD ? "UD" :
dtype == T_DOG ? "DOG" :
"");
cout << "accessName" << accessName << endl;
m_classMap[accessName] = def;
}