2018-02-08 18:26:31 +08:00
|
|
|
#include "controller.h"
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2019-11-28 04:11:40 +08:00
|
|
|
#include <assert.h>
|
2019-03-03 06:52:22 +08:00
|
|
|
|
2018-08-15 22:45:22 +08:00
|
|
|
#include "config.h"
|
2019-11-24 18:53:00 +08:00
|
|
|
#include "util/lock.h"
|
|
|
|
#include "util/log.h"
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-05-29 03:03:54 +08:00
|
|
|
controller_init(struct controller *controller, socket_t control_socket) {
|
2019-05-30 03:24:30 +08:00
|
|
|
cbuf_init(&controller->queue);
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2019-05-30 06:25:37 +08:00
|
|
|
if (!receiver_init(&controller->receiver, control_socket)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:38:44 +08:00
|
|
|
if (!(controller->mutex = SDL_CreateMutex())) {
|
2019-05-30 06:25:37 +08:00
|
|
|
receiver_destroy(&controller->receiver);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
2019-05-31 20:55:11 +08:00
|
|
|
if (!(controller->msg_cond = SDL_CreateCond())) {
|
2019-05-30 06:25:37 +08:00
|
|
|
receiver_destroy(&controller->receiver);
|
2017-12-14 18:38:44 +08:00
|
|
|
SDL_DestroyMutex(controller->mutex);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
2019-05-29 03:03:54 +08:00
|
|
|
controller->control_socket = control_socket;
|
2019-03-03 06:52:22 +08:00
|
|
|
controller->stopped = false;
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
controller_destroy(struct controller *controller) {
|
2019-05-31 20:55:11 +08:00
|
|
|
SDL_DestroyCond(controller->msg_cond);
|
2017-12-14 18:38:44 +08:00
|
|
|
SDL_DestroyMutex(controller->mutex);
|
2019-05-30 03:24:30 +08:00
|
|
|
|
2019-05-31 20:55:11 +08:00
|
|
|
struct control_msg msg;
|
|
|
|
while (cbuf_take(&controller->queue, &msg)) {
|
|
|
|
control_msg_destroy(&msg);
|
2019-05-30 03:24:30 +08:00
|
|
|
}
|
2019-05-30 06:25:37 +08:00
|
|
|
|
|
|
|
receiver_destroy(&controller->receiver);
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-05-31 20:55:11 +08:00
|
|
|
controller_push_msg(struct controller *controller,
|
|
|
|
const struct control_msg *msg) {
|
2017-12-14 18:38:44 +08:00
|
|
|
mutex_lock(controller->mutex);
|
2019-05-30 03:24:30 +08:00
|
|
|
bool was_empty = cbuf_is_empty(&controller->queue);
|
2019-05-31 20:55:11 +08:00
|
|
|
bool res = cbuf_push(&controller->queue, *msg);
|
2017-12-14 18:38:44 +08:00
|
|
|
if (was_empty) {
|
2019-05-31 20:55:11 +08:00
|
|
|
cond_signal(controller->msg_cond);
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
mutex_unlock(controller->mutex);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2019-05-31 20:55:11 +08:00
|
|
|
process_msg(struct controller *controller,
|
|
|
|
const struct control_msg *msg) {
|
|
|
|
unsigned char serialized_msg[CONTROL_MSG_SERIALIZED_MAX_SIZE];
|
|
|
|
int length = control_msg_serialize(msg, serialized_msg);
|
2017-12-14 18:38:44 +08:00
|
|
|
if (!length) {
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
2019-05-31 20:55:11 +08:00
|
|
|
int w = net_send_all(controller->control_socket, serialized_msg, length);
|
2017-12-14 18:38:44 +08:00
|
|
|
return w == length;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static int
|
|
|
|
run_controller(void *data) {
|
2017-12-14 18:38:44 +08:00
|
|
|
struct controller *controller = data;
|
|
|
|
|
|
|
|
for (;;) {
|
2018-05-26 21:14:46 +08:00
|
|
|
mutex_lock(controller->mutex);
|
2019-05-30 03:24:30 +08:00
|
|
|
while (!controller->stopped && cbuf_is_empty(&controller->queue)) {
|
2019-05-31 20:55:11 +08:00
|
|
|
cond_wait(controller->msg_cond, controller->mutex);
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
if (controller->stopped) {
|
2019-05-31 20:55:11 +08:00
|
|
|
// stop immediately, do not process further msgs
|
2018-05-26 21:14:46 +08:00
|
|
|
mutex_unlock(controller->mutex);
|
2017-12-14 18:38:44 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-05-31 20:55:11 +08:00
|
|
|
struct control_msg msg;
|
|
|
|
bool non_empty = cbuf_take(&controller->queue, &msg);
|
2019-11-28 04:11:40 +08:00
|
|
|
assert(non_empty);
|
|
|
|
(void) non_empty;
|
2018-05-26 21:14:46 +08:00
|
|
|
mutex_unlock(controller->mutex);
|
|
|
|
|
2019-05-31 20:55:11 +08:00
|
|
|
bool ok = process_msg(controller, &msg);
|
|
|
|
control_msg_destroy(&msg);
|
2018-05-26 21:14:46 +08:00
|
|
|
if (!ok) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGD("Could not write msg to socket");
|
2018-05-26 21:14:46 +08:00
|
|
|
break;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 03:09:56 +08:00
|
|
|
controller_start(struct controller *controller) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGD("Starting controller thread");
|
2017-12-14 18:38:44 +08:00
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
controller->thread = SDL_CreateThread(run_controller, "controller",
|
|
|
|
controller);
|
2017-12-14 18:38:44 +08:00
|
|
|
if (!controller->thread) {
|
2018-02-13 17:10:18 +08:00
|
|
|
LOGC("Could not start controller thread");
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
2019-05-30 06:25:37 +08:00
|
|
|
if (!receiver_start(&controller->receiver)) {
|
|
|
|
controller_stop(controller);
|
|
|
|
SDL_WaitThread(controller->thread, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
controller_stop(struct controller *controller) {
|
2017-12-14 18:38:44 +08:00
|
|
|
mutex_lock(controller->mutex);
|
2019-03-03 06:52:22 +08:00
|
|
|
controller->stopped = true;
|
2019-05-31 20:55:11 +08:00
|
|
|
cond_signal(controller->msg_cond);
|
2017-12-14 18:38:44 +08:00
|
|
|
mutex_unlock(controller->mutex);
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
controller_join(struct controller *controller) {
|
2017-12-14 18:38:44 +08:00
|
|
|
SDL_WaitThread(controller->thread, NULL);
|
2019-05-30 06:25:37 +08:00
|
|
|
receiver_join(&controller->receiver);
|
2017-12-14 18:38:44 +08:00
|
|
|
}
|