mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
Init project
This commit is contained in:
136
src/ws/ws_core.cpp
Normal file
136
src/ws/ws_core.cpp
Normal file
@@ -0,0 +1,136 @@
|
||||
#include "ws_core.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
WsCore::WsCore() {
|
||||
m_endpoint_.clear_access_channels(websocketpp::log::alevel::all);
|
||||
m_endpoint_.clear_error_channels(websocketpp::log::elevel::all);
|
||||
|
||||
m_endpoint_.init_asio();
|
||||
m_endpoint_.start_perpetual();
|
||||
|
||||
m_thread_ = websocketpp::lib::make_shared<websocketpp::lib::thread>(
|
||||
&client::run, &m_endpoint_);
|
||||
}
|
||||
|
||||
WsCore::~WsCore() {
|
||||
m_endpoint_.stop_perpetual();
|
||||
|
||||
if (GetStatus() != "Open") {
|
||||
// Only close open connections
|
||||
return;
|
||||
}
|
||||
|
||||
websocketpp::lib::error_code ec;
|
||||
m_endpoint_.close(connection_handle_, websocketpp::close::status::going_away,
|
||||
"", ec);
|
||||
if (ec) {
|
||||
LOG_INFO("> Error closing connection {}", ec.message());
|
||||
}
|
||||
|
||||
m_thread_->join();
|
||||
}
|
||||
|
||||
int WsCore::Connect(std::string const &uri) {
|
||||
websocketpp::lib::error_code ec;
|
||||
|
||||
client::connection_ptr con = m_endpoint_.get_connection(uri, ec);
|
||||
|
||||
connection_handle_ = con->get_handle();
|
||||
|
||||
if (ec) {
|
||||
LOG_INFO("> Connect initialization error: {}", ec.message());
|
||||
return -1;
|
||||
}
|
||||
|
||||
con->set_open_handler(websocketpp::lib::bind(
|
||||
&WsCore::OnOpen, this, &m_endpoint_, websocketpp::lib::placeholders::_1));
|
||||
con->set_fail_handler(websocketpp::lib::bind(
|
||||
&WsCore::OnFail, this, &m_endpoint_, websocketpp::lib::placeholders::_1));
|
||||
con->set_close_handler(
|
||||
websocketpp::lib::bind(&WsCore::OnClose, this, &m_endpoint_,
|
||||
websocketpp::lib::placeholders::_1));
|
||||
|
||||
// con->set_ping_handler(websocketpp::lib::bind(
|
||||
// &WsCore::on_ping,
|
||||
// this,
|
||||
// websocketpp::lib::placeholders::_1,
|
||||
// websocketpp::lib::placeholders::_2
|
||||
// ));
|
||||
|
||||
con->set_pong_handler(websocketpp::lib::bind(
|
||||
&WsCore::OnPong, this, websocketpp::lib::placeholders::_1,
|
||||
websocketpp::lib::placeholders::_2));
|
||||
|
||||
con->set_pong_timeout(1000);
|
||||
|
||||
con->set_pong_timeout_handler(websocketpp::lib::bind(
|
||||
&WsCore::OnPongTimeout, this, websocketpp::lib::placeholders::_1,
|
||||
websocketpp::lib::placeholders::_2));
|
||||
|
||||
con->set_message_handler(websocketpp::lib::bind(
|
||||
&WsCore::OnMessage, this, websocketpp::lib::placeholders::_1,
|
||||
websocketpp::lib::placeholders::_2));
|
||||
|
||||
m_endpoint_.connect(con);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void WsCore::Close(websocketpp::close::status::value code, std::string reason) {
|
||||
websocketpp::lib::error_code ec;
|
||||
|
||||
m_endpoint_.close(connection_handle_, code, reason, ec);
|
||||
if (ec) {
|
||||
LOG_INFO("> Error initiating close: {}", ec.message());
|
||||
}
|
||||
}
|
||||
|
||||
void WsCore::Send(std::string message) {
|
||||
websocketpp::lib::error_code ec;
|
||||
|
||||
m_endpoint_.send(connection_handle_, message,
|
||||
websocketpp::frame::opcode::text, ec);
|
||||
if (ec) {
|
||||
LOG_INFO("> Error sending message: {}", ec.message());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void WsCore::Ping() {
|
||||
websocketpp::lib::error_code ec;
|
||||
|
||||
std::string message = "ping";
|
||||
|
||||
m_endpoint_.ping(connection_handle_, message, ec);
|
||||
if (ec) {
|
||||
LOG_INFO("> Error sending ping");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const std::string &WsCore::GetStatus() { return connection_status_; }
|
||||
|
||||
void WsCore::OnOpen(client *c, websocketpp::connection_hdl hdl) {
|
||||
connection_status_ = "Open";
|
||||
}
|
||||
|
||||
void WsCore::OnFail(client *c, websocketpp::connection_hdl hdl) {
|
||||
connection_status_ = "Failed";
|
||||
}
|
||||
|
||||
void WsCore::OnClose(client *c, websocketpp::connection_hdl hdl) {
|
||||
connection_status_ = "Closed";
|
||||
}
|
||||
|
||||
void WsCore::OnPong(websocketpp::connection_hdl, std::string msg) {}
|
||||
|
||||
void WsCore::OnPongTimeout(websocketpp::connection_hdl, std::string msg) {}
|
||||
|
||||
void WsCore::OnMessage(websocketpp::connection_hdl, client::message_ptr &msg) {
|
||||
OnReceiveMessage(msg->get_payload());
|
||||
}
|
||||
54
src/ws/ws_core.h
Normal file
54
src/ws/ws_core.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#ifndef _WS_CORE_H_
|
||||
#define _WS_CORE_H_
|
||||
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "websocketpp/client.hpp"
|
||||
#include "websocketpp/common/memory.hpp"
|
||||
#include "websocketpp/common/thread.hpp"
|
||||
#include "websocketpp/config/asio_no_tls_client.hpp"
|
||||
|
||||
typedef websocketpp::client<websocketpp::config::asio_client> client;
|
||||
|
||||
class WsCore {
|
||||
public:
|
||||
WsCore();
|
||||
|
||||
virtual ~WsCore();
|
||||
|
||||
int Connect(std::string const &uri);
|
||||
|
||||
void Close(websocketpp::close::status::value code, std::string reason);
|
||||
|
||||
void Send(std::string message);
|
||||
|
||||
void Ping();
|
||||
|
||||
const std::string &GetStatus();
|
||||
|
||||
// Callback
|
||||
void OnOpen(client *c, websocketpp::connection_hdl hdl);
|
||||
|
||||
void OnFail(client *c, websocketpp::connection_hdl hdl);
|
||||
|
||||
void OnClose(client *c, websocketpp::connection_hdl hdl);
|
||||
|
||||
void OnPong(websocketpp::connection_hdl, std::string msg);
|
||||
|
||||
void OnPongTimeout(websocketpp::connection_hdl, std::string msg);
|
||||
|
||||
void OnMessage(websocketpp::connection_hdl, client::message_ptr &msg);
|
||||
|
||||
virtual void OnReceiveMessage(const std::string &msg) = 0;
|
||||
|
||||
private:
|
||||
client m_endpoint_;
|
||||
websocketpp::connection_hdl connection_handle_;
|
||||
websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread_;
|
||||
|
||||
std::string connection_status_ = "Connecting";
|
||||
};
|
||||
|
||||
#endif
|
||||
16
src/ws/ws_transport.cpp
Normal file
16
src/ws/ws_transport.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#include "ws_transport.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
WsTransport::WsTransport(
|
||||
std::function<void(const std::string &)> on_receive_msg_cb)
|
||||
: on_receive_msg_(on_receive_msg_cb) {}
|
||||
|
||||
WsTransport::~WsTransport() {}
|
||||
|
||||
void WsTransport::OnReceiveMessage(const std::string &msg) {
|
||||
LOG_INFO("Receive msg: {}", msg);
|
||||
if (on_receive_msg_) {
|
||||
on_receive_msg_(msg);
|
||||
}
|
||||
}
|
||||
18
src/ws/ws_transport.h
Normal file
18
src/ws/ws_transport.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef _WS_TRANSPORT_H_
|
||||
#define _WS_TRANSPORT_H_
|
||||
|
||||
#include "ws_core.h"
|
||||
|
||||
class WsTransport : public WsCore {
|
||||
public:
|
||||
WsTransport(std::function<void(const std::string &)> on_receive_msg_cb);
|
||||
~WsTransport();
|
||||
|
||||
public:
|
||||
void OnReceiveMessage(const std::string &msg);
|
||||
|
||||
private:
|
||||
std::function<void(const std::string &)> on_receive_msg_ = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user