#include "muse_server_private.h"
+#define EPOLL_ERR -1
+
static const char *connection_cmd[API_MAX] = {
"connect",
"disconnect"
event.events = EPOLLIN;
event.data.ptr = GINT_TO_POINTER(fd);
- if (epoll_ctl(connection->epfd, EPOLL_CTL_ADD, fd, &event) == MUSE_ERR) {
+ if (epoll_ctl(connection->epfd, EPOLL_CTL_ADD, fd, &event) == EPOLL_ERR) {
strerror_r(errno, err_msg, MUSE_MSG_LEN_MAX);
LOGE("epoll ctl error - %s", err_msg);
}
{
int fd;
GQueue *queue;
- char err_msg[MUSE_MSG_LEN_MAX] = {'\0',};
ms_connection_t *connection = ms_get_instance()->connection;
LOGD("Enter");
fd = m->ch[MUSE_CHANNEL_MSG].sock_fd;
- if (epoll_ctl(connection->epfd, EPOLL_CTL_DEL, fd, NULL) == MUSE_ERR) {
- strerror_r(errno, err_msg, MUSE_MSG_LEN_MAX);
- LOGE("epoll ctl error - %s", err_msg);
- }
-
g_queue_remove(queue, (gpointer)m);
connection->instance_count[m->idx]--;
return MM_ERROR_NONE;
}
-ms_event_e ms_connection_event_trigger(int *value)
+ms_event_e ms_connection_event_handler(int *state_value)
{
- int idx, fd_count;
+ int idx, fd, fd_count;
ms_event_e event_value = MUSE_EVENT_UNKNOWN;
struct epoll_event *p_event;
char err_msg[MUSE_MSG_LEN_MAX] = {'\0',};
ms_connection_t *connection = ms_get_instance()->connection;
- g_return_val_if_fail(connection, MM_ERROR_INVALID_ARGUMENT);
+ g_return_val_if_fail(connection, MUSE_EVENT_UNKNOWN);
fd_count = epoll_wait(connection->epfd, connection->events, MS_EVENT_MAX, MS_TIMEOUT_MSEC);
for (idx = 0; idx < fd_count; idx++) {
p_event = &connection->events[idx];
+ fd = GPOINTER_TO_INT(p_event->data.ptr);
+ event_value = MUSE_EVENT_UNKNOWN;
+
+ if (p_event->events == EPOLLIN) {
+ LOGI("CONNECTED [epoll fd : %d] [client fd : %d]", connection->epfd, fd);
+
+ p_event->events = EPOLLRDHUP;
+ if (epoll_ctl(connection->epfd, EPOLL_CTL_MOD, fd, p_event) == EPOLL_ERR) {
+ strerror_r(errno, err_msg, MUSE_MSG_LEN_MAX);
+ LOGE("epoll ctl error - %s", err_msg);
+ }
- if (p_event->events == EPOLLIN || p_event->events == EPOLLOUT) {
- if (*value != MUSE_CONNECTION_STATE_CONNECTED)
- event_value = MUSE_EVENT_CONNECTION_STATE_CHANGED;
- *value = MUSE_CONNECTION_STATE_CONNECTED;
- } else {
- if (*value != MUSE_CONNECTION_STATE_DISCONNECTED)
- event_value = MUSE_EVENT_CONNECTION_STATE_CHANGED;
- *value = MUSE_CONNECTION_STATE_DISCONNECTED;
+ event_value = MUSE_EVENT_CONNECTION_STATE_CHANGED;
+ } else if (p_event->events & EPOLLRDHUP) {
+ LOGI("DISCONNECTED [epoll fd : %d] [client fd : %d]", connection->epfd, fd);
+
+ if (epoll_ctl(connection->epfd, EPOLL_CTL_DEL, fd, p_event) == EPOLL_ERR) {
+ strerror_r(errno, err_msg, MUSE_MSG_LEN_MAX);
+ LOGE("epoll ctl error - %s", err_msg);
+ }
+
+ *state_value = MUSE_CONNECTION_STATE_DISCONNECTED;
+ event_value = MUSE_EVENT_CONNECTION_STATE_CHANGED;
}
}
{
g_return_if_fail(connection);
+ g_queue_free(connection->instance_queue);
+
g_mutex_clear(&connection->lock);
close(connection->epfd);
gpointer _ms_diag_thread(gpointer data)
{
- ms_event_e event_value;
- int value = MUSE_ERR;
+ int state_value = MUSE_CONNECTION_STATE_INVALID;
g_return_val_if_fail(muse_server, NULL);
while (1) {
- event_value = ms_connection_event_trigger(&value);
-
- if (event_value == MUSE_EVENT_CONNECTION_STATE_CHANGED) {
- if (value == MUSE_CONNECTION_STATE_CONNECTED) {
+ if (ms_connection_event_handler(&state_value) == MUSE_EVENT_CONNECTION_STATE_CHANGED) {
+ if (state_value == MUSE_CONNECTION_STATE_CONNECTED) {
/* will be updated about connection at the next patch */
- } else if (value == MUSE_CONNECTION_STATE_DISCONNECTED) {
+ } else if (state_value == MUSE_CONNECTION_STATE_DISCONNECTED) {
LOGD("Diagnostic thread checks the memory of idle");
ms_check_memory(muse_server->pid);
- } else {
- LOGW("Need to check the value (%d)", value);
}
}
}