#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
+#include <map>
#define RESET_COLOR "\e[m"
#define MAKE_RED "\e[31m"
extern int interrupt_flag;
static int epollfd = 0;
-static int timerfd = 0;
static FILE *log_file = NULL;
static struct {
.is_sec = false,
};
+static std::map<vine_dp_h, int> timerfds;
static int send_message(vine_dp_h dp);
-static void _stop_message_timer();
+static void _stop_message_timer(vine_dp_h dp);
static void __print_received_data(unsigned char *buf, size_t len)
{
static void __terminated_cb(vine_dp_h dp, void *user_data)
{
printf("peer is terminated.\n");
- _stop_message_timer();
+ _stop_message_timer(dp);
}
static void __opened_cb(vine_dp_h dp, vine_error_e result, void *user_data)
if (sec <= 0)
return;
- timerfd = timerfd_create(CLOCK_MONOTONIC, 0);
+ int timerfd = timerfd_create(CLOCK_MONOTONIC, 0);
if (timerfd == -1)
return;
return;
}
+ timerfds.insert(std::make_pair(dp, timerfd));
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.ptr = (void *)dp;
epoll_ctl(epollfd, EPOLL_CTL_ADD, timerfd, &ev);
}
-static void _stop_message_timer()
+static void _stop_message_timer(vine_dp_h dp)
{
- if (timerfd <= 0)
+ auto it = timerfds.find(dp);
+ if (it == timerfds.end())
return;
+ int timerfd = it->second;
+ if (timerfd <= 0) {
+ timerfds.erase(it);
+ return;
+ }
+
epoll_ctl(epollfd, EPOLL_CTL_DEL, timerfd, NULL);
close(timerfd);
- timerfd = 0;
+ timerfds.erase(it);
+}
+
+static void _stop_message_timer_all()
+{
+ for (auto &it : timerfds) {
+ if (it.second <= 0)
+ continue;
+ close(it.second);
+ }
+ timerfds.clear();
}
static void _message_timer_handler(void *user_data)
{
- vine_dp_h dp = (vine_dp_h)user_data;
- _stop_message_timer();
+ vine_dp_h dp = (vine_dp_h) user_data;
+ _stop_message_timer(dp);
send_message(dp);
}
int ret = vine_dp_send(dp, buf, len);
printf("%s to send a message.\n", ret == VINE_ERROR_NONE ? "Succeeded" : "Failed");
-
_start_message_timer(dp, vine_configs.interval);
return ret;
}
{
tool_config_deinit();
- if (timerfd)
- _stop_message_timer();
+ _stop_message_timer_all();
if (log_file) {
fclose(log_file);