mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 20:25:34 +08:00
Use user id instead of ice username
This commit is contained in:
@@ -1,127 +1,127 @@
|
||||
#include "ice_agent.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
IceAgent::IceAgent(std::string &ip, uint16_t port) : ip_(ip), port_(port) {}
|
||||
|
||||
IceAgent::~IceAgent() {}
|
||||
|
||||
int IceAgent::CreateIceAgent(juice_cb_state_changed_t on_state_changed,
|
||||
juice_cb_candidate_t on_candidate,
|
||||
juice_cb_gathering_done_t on_gathering_done,
|
||||
juice_cb_recv_t on_recv, void *user_ptr) {
|
||||
// juice_set_log_level(JUICE_LOG_LEVEL_DEBUG);
|
||||
|
||||
juice_config_t config;
|
||||
memset(&config, 0, sizeof(config));
|
||||
|
||||
LOG_INFO("stun server ip[{}] port[{}]", ip_, port_);
|
||||
|
||||
// STUN server example
|
||||
config.stun_server_host = ip_.c_str();
|
||||
config.stun_server_port = port_;
|
||||
|
||||
config.cb_state_changed = on_state_changed;
|
||||
config.cb_candidate = on_candidate;
|
||||
config.cb_gathering_done = on_gathering_done;
|
||||
config.cb_recv = on_recv;
|
||||
config.user_ptr = user_ptr;
|
||||
|
||||
agent_ = juice_create(&config);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IceAgent::DestoryIceAgent() {
|
||||
juice_destroy(agent_);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *IceAgent::GenerateLocalSdp() {
|
||||
if (nullptr == agent_) {
|
||||
LOG_INFO("agent_ is nullptr");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
juice_get_local_description(agent_, local_sdp_, JUICE_MAX_SDP_STRING_LEN);
|
||||
// LOG_INFO("Generate local sdp:[\n{}]", local_sdp_);
|
||||
LOG_INFO("Generate local sdp");
|
||||
|
||||
return local_sdp_;
|
||||
}
|
||||
|
||||
int IceAgent::SetRemoteSdp(const char *remote_sdp) {
|
||||
LOG_INFO("Set remote sdp");
|
||||
juice_set_remote_description(agent_, remote_sdp);
|
||||
// LOG_INFO("Remote description:[\n{}]", remote_sdp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IceAgent::GatherCandidates() {
|
||||
LOG_INFO("[{}] Gather candidates", (void *)this);
|
||||
juice_gather_candidates(agent_);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
juice_state_t IceAgent::GetIceState() {
|
||||
state_ = juice_get_state(agent_);
|
||||
|
||||
return state_;
|
||||
}
|
||||
|
||||
bool IceAgent::GetSelectedCandidates() {
|
||||
char local[JUICE_MAX_CANDIDATE_SDP_STRING_LEN];
|
||||
char remote[JUICE_MAX_CANDIDATE_SDP_STRING_LEN];
|
||||
|
||||
bool success = state_ == JUICE_STATE_COMPLETED;
|
||||
if (success &= (juice_get_selected_candidates(
|
||||
agent_, local, JUICE_MAX_CANDIDATE_SDP_STRING_LEN, remote,
|
||||
JUICE_MAX_CANDIDATE_SDP_STRING_LEN) == 0)) {
|
||||
LOG_INFO("Local candidate 1: {}", local);
|
||||
LOG_INFO("Remote candidate 1: {}", remote);
|
||||
if ((!strstr(local, "typ host") && !strstr(local, "typ prflx")) ||
|
||||
(!strstr(remote, "typ host") && !strstr(remote, "typ prflx")))
|
||||
success = false; // local connection should be possible
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool IceAgent::GetSelectedAddresses() {
|
||||
char localAddr[JUICE_MAX_ADDRESS_STRING_LEN];
|
||||
char remoteAddr[JUICE_MAX_ADDRESS_STRING_LEN];
|
||||
|
||||
bool success = state_ == JUICE_STATE_COMPLETED;
|
||||
if (success &= (juice_get_selected_addresses(
|
||||
agent_, localAddr, JUICE_MAX_ADDRESS_STRING_LEN,
|
||||
remoteAddr, JUICE_MAX_ADDRESS_STRING_LEN) == 0)) {
|
||||
LOG_INFO("Local address 1: {}", localAddr);
|
||||
LOG_INFO("Remote address 1: {}", remoteAddr);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
int IceAgent::AddRemoteCandidates(const char *remote_candidates) {
|
||||
juice_add_remote_candidate(agent_, remote_candidates);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IceAgent::SetRemoteGatheringDone() {
|
||||
juice_set_remote_gathering_done(agent_);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IceAgent::Send(const char *data, size_t size) {
|
||||
juice_send(agent_, data, size);
|
||||
return 0;
|
||||
#include "ice_agent.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
IceAgent::IceAgent(std::string &ip, uint16_t port) : ip_(ip), port_(port) {}
|
||||
|
||||
IceAgent::~IceAgent() {}
|
||||
|
||||
int IceAgent::CreateIceAgent(juice_cb_state_changed_t on_state_changed,
|
||||
juice_cb_candidate_t on_candidate,
|
||||
juice_cb_gathering_done_t on_gathering_done,
|
||||
juice_cb_recv_t on_recv, void *user_ptr) {
|
||||
// juice_set_log_level(JUICE_LOG_LEVEL_DEBUG);
|
||||
|
||||
juice_config_t config;
|
||||
memset(&config, 0, sizeof(config));
|
||||
|
||||
LOG_INFO("stun server ip[{}] port[{}]", ip_, port_);
|
||||
|
||||
// STUN server example
|
||||
config.stun_server_host = ip_.c_str();
|
||||
config.stun_server_port = port_;
|
||||
|
||||
config.cb_state_changed = on_state_changed;
|
||||
config.cb_candidate = on_candidate;
|
||||
config.cb_gathering_done = on_gathering_done;
|
||||
config.cb_recv = on_recv;
|
||||
config.user_ptr = user_ptr;
|
||||
|
||||
agent_ = juice_create(&config);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IceAgent::DestoryIceAgent() {
|
||||
juice_destroy(agent_);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *IceAgent::GenerateLocalSdp() {
|
||||
if (nullptr == agent_) {
|
||||
LOG_INFO("agent_ is nullptr");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
juice_get_local_description(agent_, local_sdp_, JUICE_MAX_SDP_STRING_LEN);
|
||||
// LOG_INFO("Generate local sdp:[\n{}]", local_sdp_);
|
||||
LOG_INFO("Generate local sdp");
|
||||
|
||||
return local_sdp_;
|
||||
}
|
||||
|
||||
int IceAgent::SetRemoteSdp(const char *remote_sdp) {
|
||||
LOG_INFO("[{}] Set remote sdp", (void *)this);
|
||||
juice_set_remote_description(agent_, remote_sdp);
|
||||
// LOG_INFO("Remote description:[\n{}]", remote_sdp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IceAgent::GatherCandidates() {
|
||||
LOG_INFO("[{}] Gather candidates", (void *)this);
|
||||
juice_gather_candidates(agent_);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
juice_state_t IceAgent::GetIceState() {
|
||||
state_ = juice_get_state(agent_);
|
||||
|
||||
return state_;
|
||||
}
|
||||
|
||||
bool IceAgent::GetSelectedCandidates() {
|
||||
char local[JUICE_MAX_CANDIDATE_SDP_STRING_LEN];
|
||||
char remote[JUICE_MAX_CANDIDATE_SDP_STRING_LEN];
|
||||
|
||||
bool success = state_ == JUICE_STATE_COMPLETED;
|
||||
if (success &= (juice_get_selected_candidates(
|
||||
agent_, local, JUICE_MAX_CANDIDATE_SDP_STRING_LEN, remote,
|
||||
JUICE_MAX_CANDIDATE_SDP_STRING_LEN) == 0)) {
|
||||
LOG_INFO("Local candidate 1: {}", local);
|
||||
LOG_INFO("Remote candidate 1: {}", remote);
|
||||
if ((!strstr(local, "typ host") && !strstr(local, "typ prflx")) ||
|
||||
(!strstr(remote, "typ host") && !strstr(remote, "typ prflx")))
|
||||
success = false; // local connection should be possible
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool IceAgent::GetSelectedAddresses() {
|
||||
char localAddr[JUICE_MAX_ADDRESS_STRING_LEN];
|
||||
char remoteAddr[JUICE_MAX_ADDRESS_STRING_LEN];
|
||||
|
||||
bool success = state_ == JUICE_STATE_COMPLETED;
|
||||
if (success &= (juice_get_selected_addresses(
|
||||
agent_, localAddr, JUICE_MAX_ADDRESS_STRING_LEN,
|
||||
remoteAddr, JUICE_MAX_ADDRESS_STRING_LEN) == 0)) {
|
||||
LOG_INFO("Local address 1: {}", localAddr);
|
||||
LOG_INFO("Remote address 1: {}", remoteAddr);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
int IceAgent::AddRemoteCandidates(const char *remote_candidates) {
|
||||
juice_add_remote_candidate(agent_, remote_candidates);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IceAgent::SetRemoteGatheringDone() {
|
||||
juice_set_remote_gathering_done(agent_);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IceAgent::Send(const char *data, size_t size) {
|
||||
juice_send(agent_, data, size);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,46 +1,46 @@
|
||||
#ifndef _ICE_AGENT_H_
|
||||
#define _ICE_AGENT_H_
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "juice/juice.h"
|
||||
|
||||
class IceAgent {
|
||||
public:
|
||||
IceAgent(std::string& ip, uint16_t port);
|
||||
~IceAgent();
|
||||
|
||||
int CreateIceAgent(juice_cb_state_changed_t on_state_changed,
|
||||
juice_cb_candidate_t on_candidate,
|
||||
juice_cb_gathering_done_t on_gathering_done,
|
||||
juice_cb_recv_t on_recv, void* user_ptr);
|
||||
|
||||
int DestoryIceAgent();
|
||||
|
||||
char* GenerateLocalSdp();
|
||||
|
||||
int SetRemoteSdp(const char* remote_sdp);
|
||||
|
||||
int GatherCandidates();
|
||||
|
||||
juice_state_t GetIceState();
|
||||
|
||||
bool GetSelectedCandidates();
|
||||
|
||||
bool GetSelectedAddresses();
|
||||
|
||||
int AddRemoteCandidates(const char* remote_candidates);
|
||||
|
||||
int SetRemoteGatheringDone();
|
||||
|
||||
int Send(const char* data, size_t size);
|
||||
|
||||
private:
|
||||
std::string ip_ = "";
|
||||
uint16_t port_ = 0;
|
||||
juice_agent_t* agent_ = nullptr;
|
||||
char local_sdp_[JUICE_MAX_SDP_STRING_LEN];
|
||||
juice_state_t state_;
|
||||
};
|
||||
|
||||
#ifndef _ICE_AGENT_H_
|
||||
#define _ICE_AGENT_H_
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "juice/juice.h"
|
||||
|
||||
class IceAgent {
|
||||
public:
|
||||
IceAgent(std::string& ip, uint16_t port);
|
||||
~IceAgent();
|
||||
|
||||
int CreateIceAgent(juice_cb_state_changed_t on_state_changed,
|
||||
juice_cb_candidate_t on_candidate,
|
||||
juice_cb_gathering_done_t on_gathering_done,
|
||||
juice_cb_recv_t on_recv, void* user_ptr);
|
||||
|
||||
int DestoryIceAgent();
|
||||
|
||||
char* GenerateLocalSdp();
|
||||
|
||||
int SetRemoteSdp(const char* remote_sdp);
|
||||
|
||||
int GatherCandidates();
|
||||
|
||||
juice_state_t GetIceState();
|
||||
|
||||
bool GetSelectedCandidates();
|
||||
|
||||
bool GetSelectedAddresses();
|
||||
|
||||
int AddRemoteCandidates(const char* remote_candidates);
|
||||
|
||||
int SetRemoteGatheringDone();
|
||||
|
||||
int Send(const char* data, size_t size);
|
||||
|
||||
private:
|
||||
std::string ip_ = "";
|
||||
uint16_t port_ = 0;
|
||||
juice_agent_t* agent_ = nullptr;
|
||||
char local_sdp_[JUICE_MAX_SDP_STRING_LEN];
|
||||
juice_state_t state_;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -15,9 +15,11 @@ const std::vector<std::string> ice_status = {
|
||||
"JUICE_STATE_COMPLETED", "JUICE_STATE_FAILED"};
|
||||
|
||||
IceTransmission::IceTransmission(
|
||||
bool offer_peer, WsTransmission *ice_ws_transmission,
|
||||
bool offer_peer, std::string remote_ice_username,
|
||||
WsTransmission *ice_ws_transmission,
|
||||
std::function<void(const char *, size_t)> on_receive_ice_msg)
|
||||
: offer_peer_(offer_peer),
|
||||
remote_ice_username_(remote_ice_username),
|
||||
ice_ws_transport_(ice_ws_transmission),
|
||||
on_receive_ice_msg_cb_(on_receive_ice_msg) {}
|
||||
|
||||
@@ -45,6 +47,7 @@ int IceTransmission::InitIceTransmission(std::string &ip, int port) {
|
||||
if (ice_transmission_obj->offer_peer_) {
|
||||
ice_transmission_obj->GetLocalSdp();
|
||||
ice_transmission_obj->SendOffer();
|
||||
LOG_INFO("[{}] SendOffer", (void *)ice_transmission_obj)
|
||||
} else {
|
||||
ice_transmission_obj->CreateAnswer();
|
||||
ice_transmission_obj->SendAnswer();
|
||||
@@ -90,10 +93,18 @@ int IceTransmission::CreateTransmission(const std::string &transmission_id) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IceTransmission::JoinTransmission(const std::string &transmission_id) {
|
||||
int IceTransmission::SetTransmissionId(const std::string &transmission_id) {
|
||||
transmission_id_ = transmission_id;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IceTransmission::JoinTransmission(const std::string &transmission_id,
|
||||
const std::string &user_id) {
|
||||
LOG_INFO("Join transport");
|
||||
offer_peer_ = true;
|
||||
transmission_id_ = transmission_id;
|
||||
user_id_ = user_id;
|
||||
|
||||
// if (SignalStatus::Connected != signal_status_) {
|
||||
// LOG_ERROR("Not connect to signalserver");
|
||||
@@ -112,6 +123,7 @@ int IceTransmission::GatherCandidates() {
|
||||
|
||||
int IceTransmission::GetLocalSdp() {
|
||||
local_sdp_ = ice_agent_->GenerateLocalSdp();
|
||||
LOG_INFO("Local ice username: [{}]", GetIceUsername(local_sdp_));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -127,7 +139,7 @@ int IceTransmission::AddRemoteCandidate(const std::string &remote_candidate) {
|
||||
}
|
||||
|
||||
int IceTransmission::CreateOffer() {
|
||||
LOG_INFO("Create offer");
|
||||
LOG_INFO("[{}] Create offer", (void *)this);
|
||||
GatherCandidates();
|
||||
return 0;
|
||||
}
|
||||
@@ -135,6 +147,8 @@ int IceTransmission::CreateOffer() {
|
||||
int IceTransmission::SendOffer() {
|
||||
json message = {{"type", "offer"},
|
||||
{"transmission_id", transmission_id_},
|
||||
{"user_id", user_id_},
|
||||
{"remote_peer", remote_ice_username_},
|
||||
{"sdp", local_sdp_}};
|
||||
// LOG_INFO("Send offer:\n{}", message.dump().c_str());
|
||||
LOG_INFO("Send offer");
|
||||
@@ -166,7 +180,7 @@ int IceTransmission::SendAnswer() {
|
||||
{"transmission_id", transmission_id_},
|
||||
{"sdp", local_sdp_},
|
||||
{"guest", remote_ice_username_}};
|
||||
// LOG_INFO("Send answer:\n{}", message.dump().c_str());
|
||||
|
||||
LOG_INFO("[{}] Send answer to [{}]", GetIceUsername(local_sdp_),
|
||||
remote_ice_username_);
|
||||
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
|
||||
class IceTransmission {
|
||||
public:
|
||||
IceTransmission(bool offer_peer, WsTransmission *ice_ws_transmission,
|
||||
IceTransmission(bool offer_peer, std::string remote_ice_username,
|
||||
WsTransmission *ice_ws_transmission,
|
||||
std::function<void(const char *, size_t)> on_receive_ice_msg);
|
||||
|
||||
~IceTransmission();
|
||||
@@ -18,7 +19,10 @@ class IceTransmission {
|
||||
int DestroyIceTransmission();
|
||||
|
||||
int CreateTransmission(const std::string &transmission_id);
|
||||
int JoinTransmission(const std::string &transmission_id);
|
||||
int JoinTransmission(const std::string &transmission_id,
|
||||
const std::string &user_id);
|
||||
|
||||
int SetTransmissionId(const std::string &transmission_id);
|
||||
|
||||
int SendData(const char *data, size_t size);
|
||||
|
||||
@@ -55,12 +59,13 @@ class IceTransmission {
|
||||
std::function<void(const char *, size_t)> on_receive_ice_msg_cb_ = nullptr;
|
||||
std::string local_sdp_;
|
||||
std::string remote_sdp_;
|
||||
std::string remote_ice_username_;
|
||||
std::string local_candidates_;
|
||||
std::string remote_candidates_;
|
||||
unsigned int connection_id_ = 0;
|
||||
std::string transmission_id_ = "";
|
||||
std::string user_id_ = "";
|
||||
bool offer_peer_ = true;
|
||||
std::string remote_ice_username_ = "";
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,38 +1,39 @@
|
||||
#ifndef _X_H_
|
||||
#define _X_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
enum ws_status { WS_CONNECTING = 0, WS_OPEN, WS_FAILED, WS_CLOSED, WS_UNKNOWN };
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct Peer PeerPtr;
|
||||
|
||||
typedef void (*OnReceiveBuffer)(unsigned char*, size_t, const char*,
|
||||
const size_t);
|
||||
|
||||
typedef void (*NetStatusReport)(const unsigned short, const unsigned short);
|
||||
|
||||
typedef struct {
|
||||
const char* cfg_path;
|
||||
OnReceiveBuffer on_receive_buffer;
|
||||
NetStatusReport net_status_report;
|
||||
} Params;
|
||||
|
||||
PeerPtr* CreatePeer(const Params* params);
|
||||
|
||||
int CreateConnection(PeerPtr* peer_ptr);
|
||||
|
||||
int JoinConnection(PeerPtr* peer_ptr, const char* connection_id);
|
||||
|
||||
int SendData(PeerPtr* peer_ptr, const char* data, size_t size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef _X_H_
|
||||
#define _X_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
enum ws_status { WS_CONNECTING = 0, WS_OPEN, WS_FAILED, WS_CLOSED, WS_UNKNOWN };
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct Peer PeerPtr;
|
||||
|
||||
typedef void (*OnReceiveBuffer)(unsigned char*, size_t, const char*,
|
||||
const size_t);
|
||||
|
||||
typedef void (*NetStatusReport)(const unsigned short, const unsigned short);
|
||||
|
||||
typedef struct {
|
||||
const char* cfg_path;
|
||||
OnReceiveBuffer on_receive_buffer;
|
||||
NetStatusReport net_status_report;
|
||||
} Params;
|
||||
|
||||
PeerPtr* CreatePeer(const Params* params);
|
||||
|
||||
int CreateConnection(PeerPtr* peer_ptr);
|
||||
|
||||
int JoinConnection(PeerPtr* peer_ptr, const char* transmission_id,
|
||||
const char* user_id);
|
||||
|
||||
int SendData(PeerPtr* peer_ptr, const char* data, size_t size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
254
src/log/log.h
254
src/log/log.h
@@ -1,127 +1,127 @@
|
||||
#ifndef _LOG_H_
|
||||
#define _LOG_H_
|
||||
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "spdlog/common.h"
|
||||
#include "spdlog/logger.h"
|
||||
#include "spdlog/sinks/base_sink.h"
|
||||
#include "spdlog/sinks/rotating_file_sink.h"
|
||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
using namespace std::chrono;
|
||||
|
||||
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
|
||||
|
||||
// SPDLOG_TRACE(...)
|
||||
// SPDLOG_DEBUG(...)
|
||||
// SPDLOG_INFO(...)
|
||||
// SPDLOG_WARN(...)
|
||||
// SPDLOG_ERROR(...)
|
||||
// SPDLOG_CRITICAL(...)
|
||||
|
||||
#ifdef SIGNAL_LOGGER
|
||||
constexpr auto LOGGER_NAME = "siganl";
|
||||
#else
|
||||
constexpr auto LOGGER_NAME = "rtc";
|
||||
#endif
|
||||
|
||||
#define LOG_INFO(...) \
|
||||
if (nullptr == spdlog::get(LOGGER_NAME)) { \
|
||||
auto now = std::chrono::system_clock::now() + std::chrono::hours(8); \
|
||||
auto timet = std::chrono::system_clock::to_time_t(now); \
|
||||
auto localTime = *std::gmtime(&timet); \
|
||||
std::stringstream ss; \
|
||||
std::string filename; \
|
||||
ss << LOGGER_NAME; \
|
||||
ss << std::put_time(&localTime, "-%Y%m%d-%H%M%S.log"); \
|
||||
ss >> filename; \
|
||||
std::string path = "logs/" + filename; \
|
||||
std::vector<spdlog::sink_ptr> sinks; \
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>()); \
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>( \
|
||||
path, 1048576 * 5, 3)); \
|
||||
auto combined_logger = std::make_shared<spdlog::logger>( \
|
||||
LOGGER_NAME, begin(sinks), end(sinks)); \
|
||||
combined_logger->flush_on(spdlog::level::info); \
|
||||
spdlog::register_logger(combined_logger); \
|
||||
SPDLOG_LOGGER_INFO(combined_logger, __VA_ARGS__); \
|
||||
} else { \
|
||||
SPDLOG_LOGGER_INFO(spdlog::get(LOGGER_NAME), __VA_ARGS__); \
|
||||
}
|
||||
|
||||
#define LOG_WARN(...) \
|
||||
if (nullptr == spdlog::get(LOGGER_NAME)) { \
|
||||
auto now = std::chrono::system_clock::now() + std::chrono::hours(8); \
|
||||
auto timet = std::chrono::system_clock::to_time_t(now); \
|
||||
auto localTime = *std::gmtime(&timet); \
|
||||
std::stringstream ss; \
|
||||
std::string filename; \
|
||||
ss << LOGGER_NAME; \
|
||||
ss << std::put_time(&localTime, "-%Y%m%d-%H%M%S.log"); \
|
||||
ss >> filename; \
|
||||
std::string path = "logs/" + filename; \
|
||||
std::vector<spdlog::sink_ptr> sinks; \
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>()); \
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>( \
|
||||
path, 1048576 * 5, 3)); \
|
||||
auto combined_logger = std::make_shared<spdlog::logger>( \
|
||||
LOGGER_NAME, begin(sinks), end(sinks)); \
|
||||
spdlog::register_logger(combined_logger); \
|
||||
SPDLOG_LOGGER_WARN(combined_logger, __VA_ARGS__); \
|
||||
} else { \
|
||||
SPDLOG_LOGGER_WARN(spdlog::get(LOGGER_NAME), __VA_ARGS__); \
|
||||
}
|
||||
|
||||
#define LOG_ERROR(...) \
|
||||
if (nullptr == spdlog::get(LOGGER_NAME)) { \
|
||||
auto now = std::chrono::system_clock::now() + std::chrono::hours(8); \
|
||||
auto timet = std::chrono::system_clock::to_time_t(now); \
|
||||
auto localTime = *std::gmtime(&timet); \
|
||||
std::stringstream ss; \
|
||||
std::string filename; \
|
||||
ss << LOGGER_NAME; \
|
||||
ss << std::put_time(&localTime, "-%Y%m%d-%H%M%S.log"); \
|
||||
ss >> filename; \
|
||||
std::string path = "logs/" + filename; \
|
||||
std::vector<spdlog::sink_ptr> sinks; \
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>()); \
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>( \
|
||||
path, 1048576 * 5, 3)); \
|
||||
auto combined_logger = std::make_shared<spdlog::logger>( \
|
||||
LOGGER_NAME, begin(sinks), end(sinks)); \
|
||||
spdlog::register_logger(combined_logger); \
|
||||
SPDLOG_LOGGER_ERROR(combined_logger, __VA_ARGS__); \
|
||||
} else { \
|
||||
SPDLOG_LOGGER_ERROR(spdlog::get(LOGGER_NAME), __VA_ARGS__); \
|
||||
}
|
||||
|
||||
#define LOG_FATAL(...) \
|
||||
if (nullptr == spdlog::get(LOGGER_NAME)) { \
|
||||
auto now = std::chrono::system_clock::now() + std::chrono::hours(8); \
|
||||
auto timet = std::chrono::system_clock::to_time_t(now); \
|
||||
auto localTime = *std::gmtime(&timet); \
|
||||
std::stringstream ss; \
|
||||
std::string filename; \
|
||||
ss << LOGGER_NAME; \
|
||||
ss << std::put_time(&localTime, "-%Y%m%d-%H%M%S.log"); \
|
||||
ss >> filename; \
|
||||
std::string path = "logs/" + filename; \
|
||||
std::vector<spdlog::sink_ptr> sinks; \
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>()); \
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>( \
|
||||
path, 1048576 * 5, 3)); \
|
||||
auto combined_logger = std::make_shared<spdlog::logger>( \
|
||||
LOGGER_NAME, begin(sinks), end(sinks)); \
|
||||
spdlog::register_logger(combined_logger); \
|
||||
SPDLOG_LOGGER_CRITICAL(combined_logger, __VA_ARGS__); \
|
||||
} else { \
|
||||
SPDLOG_LOGGER_CRITICAL(spdlog::get(LOGGER_NAME), __VA_ARGS__); \
|
||||
}
|
||||
|
||||
#endif
|
||||
#ifndef _LOG_H_
|
||||
#define _LOG_H_
|
||||
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "spdlog/common.h"
|
||||
#include "spdlog/logger.h"
|
||||
#include "spdlog/sinks/base_sink.h"
|
||||
#include "spdlog/sinks/rotating_file_sink.h"
|
||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
using namespace std::chrono;
|
||||
|
||||
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
|
||||
|
||||
// SPDLOG_TRACE(...)
|
||||
// SPDLOG_DEBUG(...)
|
||||
// SPDLOG_INFO(...)
|
||||
// SPDLOG_WARN(...)
|
||||
// SPDLOG_ERROR(...)
|
||||
// SPDLOG_CRITICAL(...)
|
||||
|
||||
#ifdef SIGNAL_LOGGER
|
||||
constexpr auto LOGGER_NAME = "siganl";
|
||||
#else
|
||||
constexpr auto LOGGER_NAME = "rtc";
|
||||
#endif
|
||||
|
||||
#define LOG_INFO(...) \
|
||||
if (nullptr == spdlog::get(LOGGER_NAME)) { \
|
||||
auto now = std::chrono::system_clock::now() + std::chrono::hours(8); \
|
||||
auto timet = std::chrono::system_clock::to_time_t(now); \
|
||||
auto localTime = *std::gmtime(&timet); \
|
||||
std::stringstream ss; \
|
||||
std::string filename; \
|
||||
ss << LOGGER_NAME; \
|
||||
ss << std::put_time(&localTime, "-%Y%m%d-%H%M%S.log"); \
|
||||
ss >> filename; \
|
||||
std::string path = "logs/" + filename; \
|
||||
std::vector<spdlog::sink_ptr> sinks; \
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>()); \
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>( \
|
||||
path, 1048576 * 5, 3)); \
|
||||
auto combined_logger = std::make_shared<spdlog::logger>( \
|
||||
LOGGER_NAME, begin(sinks), end(sinks)); \
|
||||
combined_logger->flush_on(spdlog::level::info); \
|
||||
spdlog::register_logger(combined_logger); \
|
||||
SPDLOG_LOGGER_INFO(combined_logger, __VA_ARGS__); \
|
||||
} else { \
|
||||
SPDLOG_LOGGER_INFO(spdlog::get(LOGGER_NAME), __VA_ARGS__); \
|
||||
}
|
||||
|
||||
#define LOG_WARN(...) \
|
||||
if (nullptr == spdlog::get(LOGGER_NAME)) { \
|
||||
auto now = std::chrono::system_clock::now() + std::chrono::hours(8); \
|
||||
auto timet = std::chrono::system_clock::to_time_t(now); \
|
||||
auto localTime = *std::gmtime(&timet); \
|
||||
std::stringstream ss; \
|
||||
std::string filename; \
|
||||
ss << LOGGER_NAME; \
|
||||
ss << std::put_time(&localTime, "-%Y%m%d-%H%M%S.log"); \
|
||||
ss >> filename; \
|
||||
std::string path = "logs/" + filename; \
|
||||
std::vector<spdlog::sink_ptr> sinks; \
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>()); \
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>( \
|
||||
path, 1048576 * 5, 3)); \
|
||||
auto combined_logger = std::make_shared<spdlog::logger>( \
|
||||
LOGGER_NAME, begin(sinks), end(sinks)); \
|
||||
spdlog::register_logger(combined_logger); \
|
||||
SPDLOG_LOGGER_WARN(combined_logger, __VA_ARGS__); \
|
||||
} else { \
|
||||
SPDLOG_LOGGER_WARN(spdlog::get(LOGGER_NAME), __VA_ARGS__); \
|
||||
}
|
||||
|
||||
#define LOG_ERROR(...) \
|
||||
if (nullptr == spdlog::get(LOGGER_NAME)) { \
|
||||
auto now = std::chrono::system_clock::now() + std::chrono::hours(8); \
|
||||
auto timet = std::chrono::system_clock::to_time_t(now); \
|
||||
auto localTime = *std::gmtime(&timet); \
|
||||
std::stringstream ss; \
|
||||
std::string filename; \
|
||||
ss << LOGGER_NAME; \
|
||||
ss << std::put_time(&localTime, "-%Y%m%d-%H%M%S.log"); \
|
||||
ss >> filename; \
|
||||
std::string path = "logs/" + filename; \
|
||||
std::vector<spdlog::sink_ptr> sinks; \
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>()); \
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>( \
|
||||
path, 1048576 * 5, 3)); \
|
||||
auto combined_logger = std::make_shared<spdlog::logger>( \
|
||||
LOGGER_NAME, begin(sinks), end(sinks)); \
|
||||
spdlog::register_logger(combined_logger); \
|
||||
SPDLOG_LOGGER_ERROR(combined_logger, __VA_ARGS__); \
|
||||
} else { \
|
||||
SPDLOG_LOGGER_ERROR(spdlog::get(LOGGER_NAME), __VA_ARGS__); \
|
||||
}
|
||||
|
||||
#define LOG_FATAL(...) \
|
||||
if (nullptr == spdlog::get(LOGGER_NAME)) { \
|
||||
auto now = std::chrono::system_clock::now() + std::chrono::hours(8); \
|
||||
auto timet = std::chrono::system_clock::to_time_t(now); \
|
||||
auto localTime = *std::gmtime(&timet); \
|
||||
std::stringstream ss; \
|
||||
std::string filename; \
|
||||
ss << LOGGER_NAME; \
|
||||
ss << std::put_time(&localTime, "-%Y%m%d-%H%M%S.log"); \
|
||||
ss >> filename; \
|
||||
std::string path = "logs/" + filename; \
|
||||
std::vector<spdlog::sink_ptr> sinks; \
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>()); \
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>( \
|
||||
path, 1048576 * 5, 3)); \
|
||||
auto combined_logger = std::make_shared<spdlog::logger>( \
|
||||
LOGGER_NAME, begin(sinks), end(sinks)); \
|
||||
spdlog::register_logger(combined_logger); \
|
||||
SPDLOG_LOGGER_CRITICAL(combined_logger, __VA_ARGS__); \
|
||||
} else { \
|
||||
SPDLOG_LOGGER_CRITICAL(spdlog::get(LOGGER_NAME), __VA_ARGS__); \
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,257 +1,320 @@
|
||||
#include "peer_connection.h"
|
||||
|
||||
#include <regex>
|
||||
|
||||
#include "INIReader.h"
|
||||
#include "common.h"
|
||||
#include "log.h"
|
||||
#include "nlohmann/json.hpp"
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
static const std::map<std::string, unsigned int> siganl_types{
|
||||
{"ws_connection_id", 1},
|
||||
{"offer", 2},
|
||||
{"transmission_id", 3},
|
||||
{"remote_sdp", 4},
|
||||
{"candidate", 5}};
|
||||
|
||||
PeerConnection::PeerConnection() {}
|
||||
|
||||
PeerConnection::~PeerConnection() {}
|
||||
|
||||
int PeerConnection::Create(PeerConnectionParams params,
|
||||
const std::string &transmission_id) {
|
||||
INIReader reader(params.cfg_path);
|
||||
cfg_signal_server_ip_ = reader.Get("signal server", "ip", "-1");
|
||||
cfg_signal_server_port_ = reader.Get("signal server", "port", "-1");
|
||||
cfg_stun_server_ip_ = reader.Get("stun server", "ip", "-1");
|
||||
cfg_stun_server_port_ = reader.Get("stun server", "port", "-1");
|
||||
std::regex regex("\n");
|
||||
|
||||
LOG_INFO("Read config success");
|
||||
|
||||
signal_server_port_ = stoi(cfg_signal_server_port_);
|
||||
stun_server_port_ = stoi(cfg_stun_server_port_);
|
||||
|
||||
on_receive_ws_msg_ = [this](const std::string &msg) {
|
||||
auto j = json::parse(msg);
|
||||
std::string type = j["type"];
|
||||
LOG_INFO("msg type :{}", type.c_str());
|
||||
switch (HASH_STRING_PIECE(type.c_str())) {
|
||||
case "ws_connection_id"_H: {
|
||||
ws_connection_id_ = j["ws_connection_id"].get<unsigned int>();
|
||||
LOG_INFO("Receive local peer websocket connection id [{}]",
|
||||
ws_connection_id_);
|
||||
signal_status_ = SignalStatus::Connected;
|
||||
break;
|
||||
}
|
||||
case "transmission_id"_H: {
|
||||
if (j["status"].get<std::string>() == "success") {
|
||||
transmission_id_ = j["transmission_id"].get<std::string>();
|
||||
LOG_INFO("Create transmission success with id [{}]",
|
||||
transmission_id_);
|
||||
|
||||
} else if (j["status"].get<std::string>() == "fail") {
|
||||
LOG_WARN("Create transmission failed with id [{}], due to [{}]",
|
||||
transmission_id_, j["reason"].get<std::string>().c_str());
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "offer"_H: {
|
||||
std::string remote_sdp = j["sdp"].get<std::string>();
|
||||
|
||||
if (remote_sdp.empty()) {
|
||||
LOG_INFO("Invalid remote sdp");
|
||||
} else {
|
||||
std::string ice_username = GetIceUsername(remote_sdp);
|
||||
LOG_INFO("Receive remote sdp from [{}]", ice_username);
|
||||
|
||||
// IceTransmission *ice_transmission =
|
||||
// new IceTransmission(false, ws_transport_, on_receive_ice_msg_);
|
||||
|
||||
ice_transmission_list_[ice_username] =
|
||||
new IceTransmission(false, ws_transport_, on_receive_ice_msg_);
|
||||
ice_transmission_list_[ice_username]->InitIceTransmission(
|
||||
cfg_stun_server_ip_, stun_server_port_);
|
||||
|
||||
ice_transmission_list_[ice_username]->SetRemoteSdp(remote_sdp);
|
||||
|
||||
ice_transmission_list_[ice_username]->GatherCandidates();
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
// ice_transmission_->OnReceiveMessage(msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
on_receive_ice_msg_ = [this](const char *data, size_t size) {
|
||||
std::string msg(data, size);
|
||||
LOG_INFO("Receive data: [{}]", msg.c_str());
|
||||
};
|
||||
|
||||
ws_transport_ = new WsTransmission(on_receive_ws_msg_);
|
||||
uri_ = "ws://" + cfg_signal_server_ip_ + ":" + cfg_signal_server_port_;
|
||||
if (ws_transport_) {
|
||||
ws_transport_->Connect(uri_);
|
||||
}
|
||||
|
||||
do {
|
||||
LOG_INFO("GetSignalStatus = {}", GetSignalStatus());
|
||||
} while (SignalStatus::Connected != GetSignalStatus());
|
||||
|
||||
// ice_transmission_->CreateTransmission(transmission_id);
|
||||
|
||||
json message = {{"type", "create_transmission"},
|
||||
{"transmission_id", transmission_id}};
|
||||
if (ws_transport_) {
|
||||
ws_transport_->Send(message.dump());
|
||||
LOG_INFO("Send create transmission request: {}", message.dump().c_str());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PeerConnection::Join(PeerConnectionParams params,
|
||||
const std::string &transmission_id) {
|
||||
INIReader reader(params.cfg_path);
|
||||
cfg_signal_server_ip_ = reader.Get("signal server", "ip", "-1");
|
||||
cfg_signal_server_port_ = reader.Get("signal server", "port", "-1");
|
||||
cfg_stun_server_ip_ = reader.Get("stun server", "ip", "-1");
|
||||
cfg_stun_server_port_ = reader.Get("stun server", "port", "-1");
|
||||
std::regex regex("\n");
|
||||
|
||||
LOG_INFO("Read config success");
|
||||
|
||||
signal_server_port_ = stoi(cfg_signal_server_port_);
|
||||
stun_server_port_ = stoi(cfg_stun_server_port_);
|
||||
|
||||
on_receive_ws_msg_ = [this](const std::string &msg) {
|
||||
do {
|
||||
} while (ice_transmission_list_.empty());
|
||||
auto j = json::parse(msg);
|
||||
std::string type = j["type"];
|
||||
LOG_INFO("msg type :{}", type.c_str());
|
||||
switch (HASH_STRING_PIECE(type.c_str())) {
|
||||
case "ws_connection_id"_H: {
|
||||
ws_connection_id_ = j["ws_connection_id"].get<unsigned int>();
|
||||
LOG_INFO("Receive local peer websocket connection id [{}]",
|
||||
ws_connection_id_);
|
||||
signal_status_ = SignalStatus::Connected;
|
||||
break;
|
||||
}
|
||||
case "offer"_H: {
|
||||
std::string remote_sdp = j["sdp"].get<std::string>();
|
||||
|
||||
if (remote_sdp.empty()) {
|
||||
LOG_INFO("Invalid remote sdp");
|
||||
} else {
|
||||
std::string ice_username = GetIceUsername(remote_sdp);
|
||||
LOG_INFO("Receive remote sdp from [{}]", ice_username);
|
||||
|
||||
// IceTransmission *ice_transmission =
|
||||
// new IceTransmission(false, ws_transport_, on_receive_ice_msg_);
|
||||
|
||||
ice_transmission_list_[ice_username] =
|
||||
new IceTransmission(false, ws_transport_, on_receive_ice_msg_);
|
||||
ice_transmission_list_[ice_username]->InitIceTransmission(
|
||||
cfg_stun_server_ip_, stun_server_port_);
|
||||
|
||||
ice_transmission_list_[ice_username]->SetRemoteSdp(remote_sdp);
|
||||
|
||||
ice_transmission_list_[ice_username]->GatherCandidates();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "remote_sdp"_H: {
|
||||
std::string remote_sdp = j["sdp"].get<std::string>();
|
||||
if (remote_sdp.empty()) {
|
||||
LOG_INFO("remote_sdp is empty");
|
||||
} else {
|
||||
std::string ice_username = GetIceUsername(remote_sdp);
|
||||
LOG_INFO("Receive remote sdp from [{}]", ice_username);
|
||||
// LOG_INFO("Receive remote sdp [{}]", remote_sdp);
|
||||
|
||||
if (ice_transmission_list_.size() == 1 &&
|
||||
ice_transmission_list_.begin()->first == "self") {
|
||||
ice_transmission_list_["self"]->SetRemoteSdp(remote_sdp);
|
||||
} else if (ice_transmission_list_.find(ice_username) ==
|
||||
ice_transmission_list_.end()) {
|
||||
ice_transmission_list_[ice_username] =
|
||||
new IceTransmission(false, ws_transport_, on_receive_ice_msg_);
|
||||
ice_transmission_list_[ice_username]->InitIceTransmission(
|
||||
cfg_stun_server_ip_, stun_server_port_);
|
||||
ice_transmission_list_[ice_username]->SetRemoteSdp(remote_sdp);
|
||||
}
|
||||
|
||||
// if (!offer_peer_) {
|
||||
// GatherCandidates();
|
||||
// }
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "candidate"_H: {
|
||||
std::string remote_sdp_with_candidates = j["sdp"].get<std::string>();
|
||||
std::string ice_username = GetIceUsername(remote_sdp_with_candidates);
|
||||
LOG_INFO("Receive remote candidates from [{}]", ice_username);
|
||||
// LOG_INFO("Receive candidate [{}]", candidate);
|
||||
|
||||
ice_transmission_list_[ice_username]->AddRemoteCandidate(
|
||||
remote_sdp_with_candidates);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
ice_transmission_->OnReceiveMessage(msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
on_receive_ice_msg_ = [this](const char *data, size_t size) {
|
||||
std::string msg(data, size);
|
||||
LOG_INFO("Receive data: [{}]", msg.c_str());
|
||||
};
|
||||
|
||||
transmission_id_ = transmission_id;
|
||||
|
||||
ws_transport_ = new WsTransmission(on_receive_ws_msg_);
|
||||
uri_ = "ws://" + cfg_signal_server_ip_ + ":" + cfg_signal_server_port_;
|
||||
if (ws_transport_) {
|
||||
ws_transport_->Connect(uri_);
|
||||
}
|
||||
|
||||
ice_transmission_list_["self"] =
|
||||
new IceTransmission(true, ws_transport_, on_receive_ice_msg_);
|
||||
ice_transmission_list_["self"]->InitIceTransmission(cfg_stun_server_ip_,
|
||||
stun_server_port_);
|
||||
// ice_transmission_ =
|
||||
// new IceTransmission(true, ws_transport_, on_receive_ice_msg_);
|
||||
// ice_transmission_->InitIceTransmission(cfg_stun_server_ip,
|
||||
// stun_server_port);
|
||||
|
||||
do {
|
||||
// LOG_INFO("GetSignalStatus = {}", GetSignalStatus());
|
||||
} while (SignalStatus::Connected != GetSignalStatus());
|
||||
|
||||
// ice_transmission_->JoinTransmission(transmission_id_);
|
||||
ice_transmission_list_["self"]->JoinTransmission(transmission_id_);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PeerConnection::Destroy() {
|
||||
if (ws_transport_) {
|
||||
delete ws_transport_;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
SignalStatus PeerConnection::GetSignalStatus() { return signal_status_; }
|
||||
|
||||
int PeerConnection::SendData(const char *data, size_t size) {
|
||||
for (auto ice_trans : ice_transmission_list_) {
|
||||
ice_trans.second->SendData(data, size);
|
||||
}
|
||||
return 0;
|
||||
#include "peer_connection.h"
|
||||
|
||||
#include <regex>
|
||||
|
||||
#include "INIReader.h"
|
||||
#include "common.h"
|
||||
#include "log.h"
|
||||
#include "nlohmann/json.hpp"
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
static const std::map<std::string, unsigned int> siganl_types{
|
||||
{"ws_connection_id", 1},
|
||||
{"offer", 2},
|
||||
{"transmission_id", 3},
|
||||
{"remote_sdp", 4},
|
||||
{"candidate", 5}};
|
||||
|
||||
PeerConnection::PeerConnection() {}
|
||||
|
||||
PeerConnection::~PeerConnection() {}
|
||||
|
||||
int PeerConnection::Create(PeerConnectionParams params,
|
||||
const std::string &transmission_id,
|
||||
const std::string &user_id) {
|
||||
user_id_ = user_id;
|
||||
|
||||
INIReader reader(params.cfg_path);
|
||||
cfg_signal_server_ip_ = reader.Get("signal server", "ip", "-1");
|
||||
cfg_signal_server_port_ = reader.Get("signal server", "port", "-1");
|
||||
cfg_stun_server_ip_ = reader.Get("stun server", "ip", "-1");
|
||||
cfg_stun_server_port_ = reader.Get("stun server", "port", "-1");
|
||||
std::regex regex("\n");
|
||||
|
||||
LOG_INFO("Read config success");
|
||||
|
||||
signal_server_port_ = stoi(cfg_signal_server_port_);
|
||||
stun_server_port_ = stoi(cfg_stun_server_port_);
|
||||
|
||||
on_receive_ws_msg_ = [this](const std::string &msg) {
|
||||
auto j = json::parse(msg);
|
||||
std::string type = j["type"];
|
||||
LOG_INFO("msg type :{}", type.c_str());
|
||||
switch (HASH_STRING_PIECE(type.c_str())) {
|
||||
case "ws_connection_id"_H: {
|
||||
ws_connection_id_ = j["ws_connection_id"].get<unsigned int>();
|
||||
LOG_INFO("Receive local peer websocket connection id [{}]",
|
||||
ws_connection_id_);
|
||||
signal_status_ = SignalStatus::Connected;
|
||||
break;
|
||||
}
|
||||
case "transmission_id"_H: {
|
||||
if (j["status"].get<std::string>() == "success") {
|
||||
transmission_id_ = j["transmission_id"].get<std::string>();
|
||||
LOG_INFO("Create transmission success with id [{}]",
|
||||
transmission_id_);
|
||||
|
||||
} else if (j["status"].get<std::string>() == "fail") {
|
||||
LOG_WARN("Create transmission failed with id [{}], due to [{}]",
|
||||
transmission_id_, j["reason"].get<std::string>().c_str());
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "offer"_H: {
|
||||
std::string remote_sdp = j["sdp"].get<std::string>();
|
||||
|
||||
if (remote_sdp.empty()) {
|
||||
LOG_INFO("Invalid remote sdp");
|
||||
} else {
|
||||
std::string ice_username = GetIceUsername(remote_sdp);
|
||||
LOG_INFO("Receive remote sdp from [{}]", ice_username);
|
||||
|
||||
// IceTransmission *ice_transmission =
|
||||
// new IceTransmission(false, ws_transport_, on_receive_ice_msg_);
|
||||
|
||||
ice_transmission_list_[ice_username] = new IceTransmission(
|
||||
false, ice_username, ws_transport_, on_receive_ice_msg_);
|
||||
|
||||
ice_transmission_list_[ice_username]->InitIceTransmission(
|
||||
cfg_stun_server_ip_, stun_server_port_);
|
||||
|
||||
ice_transmission_list_[ice_username]->SetTransmissionId(
|
||||
transmission_id_);
|
||||
|
||||
ice_transmission_list_[ice_username]->SetRemoteSdp(remote_sdp);
|
||||
|
||||
ice_transmission_list_[ice_username]->GatherCandidates();
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
// ice_transmission_->OnReceiveMessage(msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
on_receive_ice_msg_ = [this](const char *data, size_t size) {
|
||||
std::string msg(data, size);
|
||||
LOG_INFO("Receive data: [{}]", msg.c_str());
|
||||
};
|
||||
|
||||
ws_transport_ = new WsTransmission(on_receive_ws_msg_);
|
||||
uri_ = "ws://" + cfg_signal_server_ip_ + ":" + cfg_signal_server_port_;
|
||||
if (ws_transport_) {
|
||||
ws_transport_->Connect(uri_);
|
||||
}
|
||||
|
||||
do {
|
||||
LOG_INFO("GetSignalStatus = {}", GetSignalStatus());
|
||||
} while (SignalStatus::Connected != GetSignalStatus());
|
||||
|
||||
json message = {{"type", "create_transmission"},
|
||||
{"user_id", user_id},
|
||||
{"transmission_id", transmission_id}};
|
||||
if (ws_transport_) {
|
||||
ws_transport_->Send(message.dump());
|
||||
LOG_INFO("Send create transmission request: {}", message.dump().c_str());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PeerConnection::Join(PeerConnectionParams params,
|
||||
const std::string &transmission_id,
|
||||
const std::string &user_id) {
|
||||
// Todo: checkout user_id unique or not
|
||||
user_id_ = user_id;
|
||||
|
||||
INIReader reader(params.cfg_path);
|
||||
cfg_signal_server_ip_ = reader.Get("signal server", "ip", "-1");
|
||||
cfg_signal_server_port_ = reader.Get("signal server", "port", "-1");
|
||||
cfg_stun_server_ip_ = reader.Get("stun server", "ip", "-1");
|
||||
cfg_stun_server_port_ = reader.Get("stun server", "port", "-1");
|
||||
std::regex regex("\n");
|
||||
|
||||
LOG_INFO("Read config success");
|
||||
|
||||
signal_server_port_ = stoi(cfg_signal_server_port_);
|
||||
stun_server_port_ = stoi(cfg_stun_server_port_);
|
||||
|
||||
on_receive_ws_msg_ = [this](const std::string &msg) {
|
||||
// do {
|
||||
// } while (ice_transmission_list_.empty());
|
||||
auto j = json::parse(msg);
|
||||
std::string type = j["type"];
|
||||
LOG_INFO("msg type :{}", type);
|
||||
switch (HASH_STRING_PIECE(type.c_str())) {
|
||||
case "transmission_members"_H: {
|
||||
transmission_member_list_ = j["transmission_members"];
|
||||
std::string transmission_id = j["transmission_id"];
|
||||
|
||||
LOG_INFO("Transmission [{}] members: [", transmission_id);
|
||||
for (auto member : transmission_member_list_) {
|
||||
LOG_INFO("{}", member);
|
||||
}
|
||||
LOG_INFO("]");
|
||||
|
||||
if (transmission_member_list_.size() == 1 &&
|
||||
transmission_member_list_[0] == "host") {
|
||||
ice_transmission_list_["host"] = new IceTransmission(
|
||||
true, "host", ws_transport_, on_receive_ice_msg_);
|
||||
ice_transmission_list_["host"]->InitIceTransmission(
|
||||
cfg_stun_server_ip_, stun_server_port_);
|
||||
ice_transmission_list_["host"]->JoinTransmission(transmission_id,
|
||||
user_id_);
|
||||
} else {
|
||||
for (auto &member : transmission_member_list_) {
|
||||
ice_transmission_list_[member] = new IceTransmission(
|
||||
true, member, ws_transport_, on_receive_ice_msg_);
|
||||
ice_transmission_list_[member]->InitIceTransmission(
|
||||
cfg_stun_server_ip_, stun_server_port_);
|
||||
ice_transmission_list_[member]->JoinTransmission(transmission_id,
|
||||
user_id_);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case "ws_connection_id"_H: {
|
||||
ws_connection_id_ = j["ws_connection_id"].get<unsigned int>();
|
||||
LOG_INFO("Receive local peer websocket connection id [{}]",
|
||||
ws_connection_id_);
|
||||
signal_status_ = SignalStatus::Connected;
|
||||
break;
|
||||
}
|
||||
case "offer"_H: {
|
||||
std::string remote_sdp = j["sdp"].get<std::string>();
|
||||
|
||||
if (remote_sdp.empty()) {
|
||||
LOG_INFO("Invalid remote sdp");
|
||||
} else {
|
||||
std::string ice_username = GetIceUsername(remote_sdp);
|
||||
LOG_INFO("Receive remote sdp from [{}]", ice_username);
|
||||
|
||||
// IceTransmission *ice_transmission =
|
||||
// new IceTransmission(false, ws_transport_, on_receive_ice_msg_);
|
||||
|
||||
ice_transmission_list_[ice_username] = new IceTransmission(
|
||||
false, ice_username, ws_transport_, on_receive_ice_msg_);
|
||||
ice_transmission_list_[ice_username]->InitIceTransmission(
|
||||
cfg_stun_server_ip_, stun_server_port_);
|
||||
|
||||
ice_transmission_list_[ice_username]->SetRemoteSdp(remote_sdp);
|
||||
|
||||
ice_transmission_list_[ice_username]->GatherCandidates();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "remote_sdp"_H: {
|
||||
std::string remote_sdp = j["sdp"].get<std::string>();
|
||||
if (remote_sdp.empty()) {
|
||||
LOG_INFO("remote_sdp is empty");
|
||||
} else {
|
||||
std::string ice_username = GetIceUsername(remote_sdp);
|
||||
LOG_INFO("Receive remote sdp from [{}]", ice_username);
|
||||
// LOG_INFO("Receive remote sdp [{}]", remote_sdp);
|
||||
|
||||
// if (ice_transmission_list_.size() == 1 &&
|
||||
// ice_transmission_list_.begin()->first == "host") {
|
||||
// ice_transmission_list_["host"]->SetRemoteSdp(remote_sdp);
|
||||
// } else if (ice_transmission_list_.find(ice_username) ==
|
||||
// ice_transmission_list_.end()) {
|
||||
// ice_transmission_list_[ice_username] = new IceTransmission(
|
||||
// false, ice_username, ws_transport_, on_receive_ice_msg_);
|
||||
// ice_transmission_list_[ice_username]->InitIceTransmission(
|
||||
// cfg_stun_server_ip_, stun_server_port_);
|
||||
// ice_transmission_list_[ice_username]->SetRemoteSdp(remote_sdp);
|
||||
// }
|
||||
|
||||
if (ice_transmission_list_.size() == 1 &&
|
||||
ice_transmission_list_.begin()->first == "host") {
|
||||
ice_transmission_list_["host"]->SetRemoteSdp(remote_sdp);
|
||||
} else if (ice_transmission_list_.find(ice_username) !=
|
||||
ice_transmission_list_.end()) {
|
||||
ice_transmission_list_[ice_username]->SetRemoteSdp(remote_sdp);
|
||||
}
|
||||
|
||||
// if (!offer_peer_) {
|
||||
// GatherCandidates();
|
||||
// }
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "candidate"_H: {
|
||||
std::string remote_sdp_with_candidates = j["sdp"].get<std::string>();
|
||||
std::string ice_username = GetIceUsername(remote_sdp_with_candidates);
|
||||
LOG_INFO("Receive remote candidates from [{}]", ice_username);
|
||||
// LOG_INFO("Receive candidate [{}]", candidate);
|
||||
|
||||
ice_transmission_list_[ice_username]->AddRemoteCandidate(
|
||||
remote_sdp_with_candidates);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
ice_transmission_->OnReceiveMessage(msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
on_receive_ice_msg_ = [this](const char *data, size_t size) {
|
||||
std::string msg(data, size);
|
||||
LOG_INFO("Receive data: [{}]", msg.c_str());
|
||||
};
|
||||
|
||||
transmission_id_ = transmission_id;
|
||||
|
||||
ws_transport_ = new WsTransmission(on_receive_ws_msg_);
|
||||
uri_ = "ws://" + cfg_signal_server_ip_ + ":" + cfg_signal_server_port_;
|
||||
if (ws_transport_) {
|
||||
ws_transport_->Connect(uri_);
|
||||
}
|
||||
|
||||
// ice_transmission_list_["self"] =
|
||||
// new IceTransmission(true, ws_transport_, on_receive_ice_msg_);
|
||||
// ice_transmission_list_["self"]->InitIceTransmission(cfg_stun_server_ip_,
|
||||
// stun_server_port_);
|
||||
// ice_transmission_ =
|
||||
// new IceTransmission(true, ws_transport_, on_receive_ice_msg_);
|
||||
// ice_transmission_->InitIceTransmission(cfg_stun_server_ip,
|
||||
// stun_server_port);
|
||||
|
||||
do {
|
||||
// LOG_INFO("GetSignalStatus = {}", GetSignalStatus());
|
||||
} while (SignalStatus::Connected != GetSignalStatus());
|
||||
|
||||
RequestTransmissionMemberList(transmission_id_);
|
||||
// ice_transmission_->JoinTransmission(transmission_id_);
|
||||
// ice_transmission_list_["self"]->JoinTransmission(transmission_id_);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PeerConnection::RequestTransmissionMemberList(
|
||||
const std::string &transmission_id) {
|
||||
LOG_INFO("Request member list");
|
||||
|
||||
json message = {{"type", "query_members"},
|
||||
{"transmission_id", transmission_id_}};
|
||||
|
||||
if (ws_transport_) {
|
||||
ws_transport_->Send(message.dump());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PeerConnection::Destroy() {
|
||||
if (ws_transport_) {
|
||||
delete ws_transport_;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
SignalStatus PeerConnection::GetSignalStatus() { return signal_status_; }
|
||||
|
||||
int PeerConnection::SendData(const char *data, size_t size) {
|
||||
for (auto ice_trans : ice_transmission_list_) {
|
||||
ice_trans.second->SendData(data, size);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -1,55 +1,62 @@
|
||||
#ifndef _PEER_CONNECTION_H_
|
||||
#define _PEER_CONNECTION_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
#include "ice_transmission.h"
|
||||
#include "ws_transmission.h"
|
||||
|
||||
enum SignalStatus { Connecting = 0, Connected, Closed };
|
||||
|
||||
typedef void (*OnReceiveBuffer)(unsigned char *, size_t, const char *,
|
||||
const size_t);
|
||||
|
||||
typedef void (*NetStatusReport)(const unsigned short, const unsigned short);
|
||||
|
||||
typedef struct {
|
||||
const char *cfg_path;
|
||||
OnReceiveBuffer on_receive_buffer;
|
||||
NetStatusReport net_status_report;
|
||||
} PeerConnectionParams;
|
||||
|
||||
class PeerConnection {
|
||||
public:
|
||||
PeerConnection();
|
||||
~PeerConnection();
|
||||
|
||||
public:
|
||||
int Create(PeerConnectionParams params, const std::string &id = "");
|
||||
int Join(PeerConnectionParams params, const std::string &id);
|
||||
int Destroy();
|
||||
|
||||
SignalStatus GetSignalStatus();
|
||||
|
||||
int SendData(const char *data, size_t size);
|
||||
|
||||
private:
|
||||
std::string uri_ = "";
|
||||
std::string cfg_signal_server_ip_;
|
||||
std::string cfg_signal_server_port_;
|
||||
std::string cfg_stun_server_ip_;
|
||||
std::string cfg_stun_server_port_;
|
||||
int signal_server_port_ = 0;
|
||||
int stun_server_port_ = 0;
|
||||
WsTransmission *ws_transport_ = nullptr;
|
||||
IceTransmission *ice_transmission_ = nullptr;
|
||||
std::map<std::string, IceTransmission *> ice_transmission_list_;
|
||||
std::function<void(const std::string &)> on_receive_ws_msg_ = nullptr;
|
||||
std::function<void(const char *, size_t)> on_receive_ice_msg_ = nullptr;
|
||||
unsigned int ws_connection_id_ = 0;
|
||||
std::string transmission_id_ = "";
|
||||
SignalStatus signal_status_ = SignalStatus::Closed;
|
||||
};
|
||||
|
||||
#ifndef _PEER_CONNECTION_H_
|
||||
#define _PEER_CONNECTION_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
#include "ice_transmission.h"
|
||||
#include "ws_transmission.h"
|
||||
|
||||
enum SignalStatus { Connecting = 0, Connected, Closed };
|
||||
|
||||
typedef void (*OnReceiveBuffer)(unsigned char *, size_t, const char *,
|
||||
const size_t);
|
||||
|
||||
typedef void (*NetStatusReport)(const unsigned short, const unsigned short);
|
||||
|
||||
typedef struct {
|
||||
const char *cfg_path;
|
||||
OnReceiveBuffer on_receive_buffer;
|
||||
NetStatusReport net_status_report;
|
||||
} PeerConnectionParams;
|
||||
|
||||
class PeerConnection {
|
||||
public:
|
||||
PeerConnection();
|
||||
~PeerConnection();
|
||||
|
||||
public:
|
||||
int Create(PeerConnectionParams params,
|
||||
const std::string &transmission_id = "",
|
||||
const std::string &user_id = "");
|
||||
int Join(PeerConnectionParams params, const std::string &transmission_id,
|
||||
const std::string &user_id = "");
|
||||
int Destroy();
|
||||
|
||||
int RequestTransmissionMemberList(const std::string &transmission_id);
|
||||
|
||||
SignalStatus GetSignalStatus();
|
||||
|
||||
int SendData(const char *data, size_t size);
|
||||
|
||||
private:
|
||||
std::string uri_ = "";
|
||||
std::string cfg_signal_server_ip_;
|
||||
std::string cfg_signal_server_port_;
|
||||
std::string cfg_stun_server_ip_;
|
||||
std::string cfg_stun_server_port_;
|
||||
int signal_server_port_ = 0;
|
||||
int stun_server_port_ = 0;
|
||||
WsTransmission *ws_transport_ = nullptr;
|
||||
IceTransmission *ice_transmission_ = nullptr;
|
||||
std::vector<std::string> transmission_member_list_;
|
||||
std::map<std::string, IceTransmission *> ice_transmission_list_;
|
||||
std::function<void(const std::string &)> on_receive_ws_msg_ = nullptr;
|
||||
std::function<void(const char *, size_t)> on_receive_ice_msg_ = nullptr;
|
||||
unsigned int ws_connection_id_ = 0;
|
||||
std::string user_id_ = "";
|
||||
std::string transmission_id_ = "";
|
||||
SignalStatus signal_status_ = SignalStatus::Closed;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,45 +1,49 @@
|
||||
#include "x_inner.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "ice_agent.h"
|
||||
#include "log.h"
|
||||
#include "ws_transmission.h"
|
||||
#include "x.h"
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
static PeerConnection *peer_connection;
|
||||
|
||||
PeerPtr *CreatePeer(const Params *params) {
|
||||
PeerPtr *peer_ptr = new PeerPtr;
|
||||
peer_ptr->peer_connection = new PeerConnection();
|
||||
peer_ptr->pc_params.cfg_path = params->cfg_path;
|
||||
peer_ptr->pc_params.on_receive_buffer = params->on_receive_buffer;
|
||||
peer_ptr->pc_params.net_status_report = params->net_status_report;
|
||||
|
||||
return peer_ptr;
|
||||
}
|
||||
|
||||
int CreateConnection(PeerPtr *peer_ptr) {
|
||||
peer_ptr->peer_connection->Create(peer_ptr->pc_params);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CreateConnection(PeerPtr *peer_ptr, const char *connection_id) {
|
||||
peer_ptr->peer_connection->Create(peer_ptr->pc_params, connection_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int JoinConnection(PeerPtr *peer_ptr, const char *connection_id) {
|
||||
peer_ptr->peer_connection->Join(peer_ptr->pc_params, connection_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SendData(PeerPtr *peer_ptr, const char *data, size_t size) {
|
||||
peer_ptr->peer_connection->SendData(data, size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include "x_inner.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "ice_agent.h"
|
||||
#include "log.h"
|
||||
#include "ws_transmission.h"
|
||||
#include "x.h"
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
static PeerConnection *peer_connection;
|
||||
|
||||
PeerPtr *CreatePeer(const Params *params) {
|
||||
PeerPtr *peer_ptr = new PeerPtr;
|
||||
peer_ptr->peer_connection = new PeerConnection();
|
||||
peer_ptr->pc_params.cfg_path = params->cfg_path;
|
||||
peer_ptr->pc_params.on_receive_buffer = params->on_receive_buffer;
|
||||
peer_ptr->pc_params.net_status_report = params->net_status_report;
|
||||
|
||||
return peer_ptr;
|
||||
}
|
||||
|
||||
int CreateConnection(PeerPtr *peer_ptr) {
|
||||
peer_ptr->peer_connection->Create(peer_ptr->pc_params);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CreateConnection(PeerPtr *peer_ptr, const char *transmission_id,
|
||||
const char *user_id) {
|
||||
peer_ptr->peer_connection->Create(peer_ptr->pc_params, transmission_id,
|
||||
user_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int JoinConnection(PeerPtr *peer_ptr, const char *transmission_id,
|
||||
const char *user_id) {
|
||||
peer_ptr->peer_connection->Join(peer_ptr->pc_params, transmission_id,
|
||||
user_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SendData(PeerPtr *peer_ptr, const char *data, size_t size) {
|
||||
peer_ptr->peer_connection->SendData(data, size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rtc() { return 0; }
|
||||
@@ -1,11 +1,11 @@
|
||||
#ifndef _X_INNER_H_
|
||||
#define _X_INNER_H_
|
||||
|
||||
#include "peer_connection.h"
|
||||
|
||||
struct Peer {
|
||||
PeerConnection *peer_connection;
|
||||
PeerConnectionParams pc_params;
|
||||
};
|
||||
|
||||
#ifndef _X_INNER_H_
|
||||
#define _X_INNER_H_
|
||||
|
||||
#include "peer_connection.h"
|
||||
|
||||
struct Peer {
|
||||
PeerConnection *peer_connection;
|
||||
PeerConnectionParams pc_params;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,136 +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());
|
||||
#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());
|
||||
}
|
||||
106
src/ws/ws_core.h
106
src/ws/ws_core.h
@@ -1,54 +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";
|
||||
};
|
||||
|
||||
#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
|
||||
@@ -1,16 +1,16 @@
|
||||
#include "ws_transmission.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
WsTransmission::WsTransmission(
|
||||
std::function<void(const std::string &)> on_receive_msg_cb)
|
||||
: on_receive_msg_(on_receive_msg_cb) {}
|
||||
|
||||
WsTransmission::~WsTransmission() {}
|
||||
|
||||
void WsTransmission::OnReceiveMessage(const std::string &msg) {
|
||||
// LOG_INFO("Receive msg: {}", msg);
|
||||
if (on_receive_msg_) {
|
||||
on_receive_msg_(msg);
|
||||
}
|
||||
#include "ws_transmission.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
WsTransmission::WsTransmission(
|
||||
std::function<void(const std::string &)> on_receive_msg_cb)
|
||||
: on_receive_msg_(on_receive_msg_cb) {}
|
||||
|
||||
WsTransmission::~WsTransmission() {}
|
||||
|
||||
void WsTransmission::OnReceiveMessage(const std::string &msg) {
|
||||
// LOG_INFO("Receive msg: {}", msg);
|
||||
if (on_receive_msg_) {
|
||||
on_receive_msg_(msg);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,18 @@
|
||||
#ifndef _WS_TRANSMISSION_H_
|
||||
#define _WS_TRANSMISSION_H_
|
||||
|
||||
#include "ws_core.h"
|
||||
|
||||
class WsTransmission : public WsCore {
|
||||
public:
|
||||
WsTransmission(std::function<void(const std::string &)> on_receive_msg_cb);
|
||||
~WsTransmission();
|
||||
|
||||
public:
|
||||
void OnReceiveMessage(const std::string &msg);
|
||||
|
||||
private:
|
||||
std::function<void(const std::string &)> on_receive_msg_ = nullptr;
|
||||
};
|
||||
|
||||
#ifndef _WS_TRANSMISSION_H_
|
||||
#define _WS_TRANSMISSION_H_
|
||||
|
||||
#include "ws_core.h"
|
||||
|
||||
class WsTransmission : public WsCore {
|
||||
public:
|
||||
WsTransmission(std::function<void(const std::string &)> on_receive_msg_cb);
|
||||
~WsTransmission();
|
||||
|
||||
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