|
|
|
@ -18,8 +18,8 @@
|
|
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <event2/buffer.h>
|
|
|
|
|
#include <event2/bufferevent.h>
|
|
|
|
|
#include <event2/buffer_compat.h>
|
|
|
|
|
#include <event2/bufferevent.h>
|
|
|
|
|
#include <event2/event.h>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
@ -27,20 +27,23 @@
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
#include "ps/core/comm_util.h"
|
|
|
|
|
|
|
|
|
|
namespace mindspore {
|
|
|
|
|
namespace ps {
|
|
|
|
|
namespace core {
|
|
|
|
|
|
|
|
|
|
event_base *TcpClient::event_base_ = nullptr;
|
|
|
|
|
|
|
|
|
|
TcpClient::TcpClient(const std::string &address, std::uint16_t port)
|
|
|
|
|
: event_base_(nullptr),
|
|
|
|
|
event_timeout_(nullptr),
|
|
|
|
|
: event_timeout_(nullptr),
|
|
|
|
|
buffer_event_(nullptr),
|
|
|
|
|
server_address_(std::move(address)),
|
|
|
|
|
server_port_(port) {
|
|
|
|
|
server_port_(port),
|
|
|
|
|
is_stop_(true) {
|
|
|
|
|
message_handler_.SetCallback([this](const CommMessage &message) {
|
|
|
|
|
if (message_callback_) {
|
|
|
|
|
message_callback_(*this, message);
|
|
|
|
@ -61,6 +64,7 @@ void TcpClient::SetCallback(const OnConnected &conn, const OnDisconnected &disco
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TcpClient::Init() {
|
|
|
|
|
std::lock_guard<std::mutex> lock(connection_mutex_);
|
|
|
|
|
if (buffer_event_) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -68,7 +72,13 @@ void TcpClient::Init() {
|
|
|
|
|
MS_LOG(EXCEPTION) << "The tcp client ip:" << server_address_ << " is illegal!";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
event_base_ = event_base_new();
|
|
|
|
|
int result = evthread_use_pthreads();
|
|
|
|
|
if (result != 0) {
|
|
|
|
|
MS_LOG(EXCEPTION) << "Use event pthread failed!";
|
|
|
|
|
}
|
|
|
|
|
if (event_base_ == nullptr) {
|
|
|
|
|
event_base_ = event_base_new();
|
|
|
|
|
}
|
|
|
|
|
MS_EXCEPTION_IF_NULL(event_base_);
|
|
|
|
|
|
|
|
|
|
sockaddr_in sin{};
|
|
|
|
@ -94,6 +104,7 @@ void TcpClient::Init() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TcpClient::StartWithDelay(int seconds) {
|
|
|
|
|
std::lock_guard<std::mutex> lock(connection_mutex_);
|
|
|
|
|
if (buffer_event_) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -111,16 +122,28 @@ void TcpClient::StartWithDelay(int seconds) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TcpClient::Stop() {
|
|
|
|
|
if (buffer_event_) {
|
|
|
|
|
bufferevent_free(buffer_event_);
|
|
|
|
|
buffer_event_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
std::lock_guard<std::mutex> lock(connection_mutex_);
|
|
|
|
|
MS_LOG(INFO) << "Stop tcp client event buffer!";
|
|
|
|
|
if (!is_stop_.load()) {
|
|
|
|
|
if (buffer_event_) {
|
|
|
|
|
bufferevent_free(buffer_event_);
|
|
|
|
|
buffer_event_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event_timeout_) {
|
|
|
|
|
event_free(event_timeout_);
|
|
|
|
|
event_timeout_ = nullptr;
|
|
|
|
|
if (event_timeout_) {
|
|
|
|
|
event_free(event_timeout_);
|
|
|
|
|
event_timeout_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
is_stop_ = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TcpClient::StopEventBase() {
|
|
|
|
|
MS_LOG(INFO) << "Stop tcp client event base!";
|
|
|
|
|
int ret = event_base_loopbreak(event_base_);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
MS_LOG(EXCEPTION) << "Event base loop break failed!";
|
|
|
|
|
}
|
|
|
|
|
if (event_base_) {
|
|
|
|
|
event_base_free(event_base_);
|
|
|
|
|
event_base_ = nullptr;
|
|
|
|
@ -167,21 +190,12 @@ void TcpClient::OnReadHandler(const void *buf, size_t num) {
|
|
|
|
|
message_handler_.ReceiveMessage(buf, num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TcpClient::SendHeartBeatCallback(evutil_socket_t, int16_t, void *arg) {
|
|
|
|
|
void TcpClient::TimerCallback(evutil_socket_t, int16_t, void *arg) {
|
|
|
|
|
MS_EXCEPTION_IF_NULL(arg);
|
|
|
|
|
auto tcp_client = reinterpret_cast<TcpClient *>(arg);
|
|
|
|
|
MessageMeta meta;
|
|
|
|
|
meta.set_cmd(ClusterCommand::HEARTBEAT);
|
|
|
|
|
CommMessage message;
|
|
|
|
|
message.set_allocated_pb_meta(&meta);
|
|
|
|
|
tcp_client->SendMessage(message);
|
|
|
|
|
|
|
|
|
|
struct event *ev;
|
|
|
|
|
struct timeval timeout {};
|
|
|
|
|
timeout.tv_sec = ClusterConfig::heartbeat_interval();
|
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
ev = evtimer_new(tcp_client->event_base_, SendHeartBeatCallback, arg);
|
|
|
|
|
evtimer_add(ev, &timeout);
|
|
|
|
|
if (tcp_client->on_timer_callback_) {
|
|
|
|
|
tcp_client->on_timer_callback_(*tcp_client);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TcpClient::EventCallback(struct bufferevent *bev, std::int16_t events, void *ptr) {
|
|
|
|
@ -211,6 +225,7 @@ void TcpClient::EventCallback(struct bufferevent *bev, std::int16_t events, void
|
|
|
|
|
|
|
|
|
|
void TcpClient::Start() {
|
|
|
|
|
MS_EXCEPTION_IF_NULL(event_base_);
|
|
|
|
|
is_stop_ = false;
|
|
|
|
|
int ret = event_base_dispatch(event_base_);
|
|
|
|
|
MSLOG_IF(INFO, ret == 0, NoExceptionType) << "Event base dispatch success!";
|
|
|
|
|
MSLOG_IF(mindspore::ERROR, ret == 1, NoExceptionType)
|
|
|
|
@ -220,6 +235,7 @@ void TcpClient::Start() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TcpClient::StartWithNoBlock() {
|
|
|
|
|
std::lock_guard<std::mutex> lock(connection_mutex_);
|
|
|
|
|
MS_LOG(INFO) << "Start tcp client with no block!";
|
|
|
|
|
MS_EXCEPTION_IF_NULL(event_base_);
|
|
|
|
|
int ret = event_base_loop(event_base_, EVLOOP_NONBLOCK);
|
|
|
|
@ -244,15 +260,24 @@ void TcpClient::SendMessage(const CommMessage &message) const {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TcpClient::SendMessageWithTimer() {
|
|
|
|
|
MS_EXCEPTION_IF_NULL(buffer_event_);
|
|
|
|
|
void TcpClient::StartTimer(const uint32_t &time) {
|
|
|
|
|
MS_EXCEPTION_IF_NULL(event_base_);
|
|
|
|
|
struct event *ev = nullptr;
|
|
|
|
|
if (time == 0) {
|
|
|
|
|
MS_LOG(EXCEPTION) << "The time should not be 0!";
|
|
|
|
|
}
|
|
|
|
|
struct timeval timeout {};
|
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
|
timeout.tv_sec = time;
|
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
ev = evtimer_new(event_base_, SendHeartBeatCallback, this);
|
|
|
|
|
ev = event_new(event_base_, -1, EV_PERSIST, TimerCallback, this);
|
|
|
|
|
MS_EXCEPTION_IF_NULL(ev);
|
|
|
|
|
evtimer_add(ev, &timeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TcpClient::set_timer_callback(const OnTimer &timer) { on_timer_callback_ = timer; }
|
|
|
|
|
|
|
|
|
|
const event_base &TcpClient::eventbase() { return *event_base_; }
|
|
|
|
|
|
|
|
|
|
} // namespace core
|
|
|
|
|
} // namespace ps
|
|
|
|
|
} // namespace mindspore
|
|
|
|
|