Skip to content

Commit b27ad2f

Browse files
committed
chore: client
1 parent 69512e6 commit b27ad2f

File tree

1 file changed

+64
-12
lines changed

1 file changed

+64
-12
lines changed

client.cpp

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,35 +152,87 @@ void rpc_close(conn_t *conn) {
152152

153153
typedef void (*func)(void *data);
154154

155+
int host_funcs = 0;
156+
157+
void increment_host_nodes() {
158+
host_funcs++;
159+
}
160+
161+
void wait_for_callbacks() {
162+
while (host_funcs > 0) {}
163+
164+
host_funcs++;
165+
}
166+
155167
void *rpc_client_dispatch_thread(void *arg) {
156168
conn_t *conn = (conn_t *)arg;
157-
unsigned int op;
169+
int op;
158170

159171
while (true) {
160-
unsigned int op = rpc_dispatch(conn, 1);
172+
op = rpc_dispatch(conn, 1); // Removed shadowing issue
173+
174+
void* temp_mem;
175+
void* temp_udata;
176+
177+
if (rpc_read(conn, &temp_mem, sizeof(void*)) <= 0) {
178+
std::cerr << "rpc_read failed for mem. Closing connection." << std::endl;
179+
break;
180+
}
181+
if (rpc_read(conn, &temp_udata, sizeof(void*)) <= 0) {
182+
std::cerr << "rpc_read failed for udata. Closing connection." << std::endl;
183+
break;
184+
}
185+
186+
int request_id = rpc_read_end(conn);
187+
188+
void* mem = temp_mem;
189+
void* udata = temp_udata;
161190

162-
void* mem;
163-
void* udata;
164-
rpc_read(conn, &mem, sizeof(void*));
165-
rpc_read(conn, &udata, sizeof(void*));
166191
std::cout << "Got mem " << mem << std::endl;
167192
std::cout << "Got udata " << udata << std::endl;
168193

169-
func f = reinterpret_cast<func>(mem);
194+
if (mem == nullptr) {
195+
std::cerr << "Invalid function pointer!" << std::endl;
196+
continue;
197+
}
198+
199+
// func f = reinterpret_cast<func>(mem);
170200
try {
171-
f(&udata);
201+
// f(&udata);
202+
} catch (const std::exception& e) {
203+
std::cerr << "Exception: " << e.what() << std::endl;
204+
continue;
172205
} catch (...) {
173-
std::cerr << "Exception occurred while calling function pointer!" << std::endl;
206+
std::cerr << "Unknown exception occurred!" << std::endl;
207+
continue;
174208
}
175209

176-
rpc_write_start_response(conn, 1);
177-
rpc_write_end(conn);
210+
void * res;
211+
212+
if (rpc_write_start_response(conn, request_id) < 0) {
213+
std::cerr << "rpc_write_start_response failed. Closing connection." << std::endl;
214+
break;
215+
}
216+
std::cout << "responding..." << std::endl;
217+
if (rpc_write(conn, &res, sizeof(void*)) < 0) {
218+
std::cerr << "rpc_write failed. Closing connection." << std::endl;
219+
break;
220+
}
221+
if (rpc_write_end(conn) < 0) {
222+
std::cerr << "rpc_write_end failed. Closing connection." << std::endl;
223+
break;
224+
}
225+
226+
host_funcs--;
178227

179228
std::cout << "Function executed successfully!" << std::endl;
180-
return NULL;
181229
}
230+
231+
std::cerr << "Exiting dispatch thread due to an error." << std::endl;
232+
return nullptr;
182233
}
183234

235+
184236
int rpc_open() {
185237
set_segfault_handlers();
186238

0 commit comments

Comments
 (0)