mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 12:45:35 +08:00
Refactor connection create process
This commit is contained in:
@@ -11,7 +11,7 @@ using nlohmann::json;
|
||||
static const std::map<std::string, unsigned int> siganl_types{
|
||||
{"ws_connection_id", 1},
|
||||
{"offer", 2},
|
||||
{"transport_id", 3},
|
||||
{"transmission_id", 3},
|
||||
{"remote_sdp", 4},
|
||||
{"candidate", 5}};
|
||||
|
||||
@@ -19,7 +19,7 @@ PeerConnection::PeerConnection() {}
|
||||
|
||||
PeerConnection::~PeerConnection() {}
|
||||
|
||||
int PeerConnection::Init(PeerConnectionParams params) {
|
||||
int PeerConnection::Create(PeerConnectionParams params, const std::string &id) {
|
||||
INIReader reader(params.cfg_path);
|
||||
std::string cfg_signal_server_ip = reader.Get("signal server", "ip", "-1");
|
||||
std::string cfg_signal_server_port =
|
||||
@@ -35,7 +35,7 @@ int PeerConnection::Init(PeerConnectionParams params) {
|
||||
|
||||
on_receive_ws_msg_ = [this](const std::string &msg) {
|
||||
do {
|
||||
} while (!ice_transport_);
|
||||
} while (!ice_transmission_);
|
||||
auto j = json::parse(msg);
|
||||
std::string type = j["type"];
|
||||
auto itr = siganl_types.find(type);
|
||||
@@ -50,7 +50,7 @@ int PeerConnection::Init(PeerConnectionParams params) {
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
ice_transport_->OnReceiveMessage(msg);
|
||||
ice_transmission_->OnReceiveMessage(msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -62,24 +62,24 @@ int PeerConnection::Init(PeerConnectionParams params) {
|
||||
LOG_INFO("Receive data: [{}]", msg.c_str());
|
||||
};
|
||||
|
||||
ws_transport_ = new WsTransport(on_receive_ws_msg_);
|
||||
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_transport_ = new IceTransport(ws_transport_, on_receive_ice_msg_);
|
||||
ice_transport_->InitIceTransport(cfg_stun_server_ip, stun_server_port);
|
||||
ice_transmission_ = new IceTransmission(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_transport_->CreateTransport();
|
||||
ice_transmission_->CreateTransmission(id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PeerConnection::Init(PeerConnectionParams params, std::string const &id) {
|
||||
int PeerConnection::Join(PeerConnectionParams params, const std::string &id) {
|
||||
INIReader reader(params.cfg_path);
|
||||
std::string cfg_signal_server_ip = reader.Get("signal server", "ip", "-1");
|
||||
std::string cfg_signal_server_port =
|
||||
@@ -93,7 +93,7 @@ int PeerConnection::Init(PeerConnectionParams params, std::string const &id) {
|
||||
|
||||
on_receive_ws_msg_ = [this](const std::string &msg) {
|
||||
do {
|
||||
} while (!ice_transport_);
|
||||
} while (!ice_transmission_);
|
||||
auto j = json::parse(msg);
|
||||
std::string type = j["type"];
|
||||
auto itr = siganl_types.find(type);
|
||||
@@ -108,7 +108,7 @@ int PeerConnection::Init(PeerConnectionParams params, std::string const &id) {
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
ice_transport_->OnReceiveMessage(msg);
|
||||
ice_transmission_->OnReceiveMessage(msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -120,22 +120,23 @@ int PeerConnection::Init(PeerConnectionParams params, std::string const &id) {
|
||||
LOG_INFO("Receive data: [{}]", msg.c_str());
|
||||
};
|
||||
|
||||
transport_id_ = id;
|
||||
transmission_id_ = id;
|
||||
|
||||
ws_transport_ = new WsTransport(on_receive_ws_msg_);
|
||||
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_transport_ = new IceTransport(ws_transport_, on_receive_ice_msg_);
|
||||
ice_transport_->InitIceTransport(cfg_stun_server_ip, stun_server_port, id);
|
||||
ice_transmission_ = new IceTransmission(ws_transport_, on_receive_ice_msg_);
|
||||
ice_transmission_->InitIceTransmission(cfg_stun_server_ip, stun_server_port,
|
||||
id);
|
||||
|
||||
do {
|
||||
LOG_INFO("GetSignalStatus = {}", GetSignalStatus());
|
||||
} while (SignalStatus::Connected != GetSignalStatus());
|
||||
|
||||
ice_transport_->CreateTransport(transport_id_);
|
||||
ice_transmission_->JoinTransmission(transmission_id_);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -149,6 +150,6 @@ int PeerConnection::Destroy() {
|
||||
SignalStatus PeerConnection::GetSignalStatus() { return signal_status_; }
|
||||
|
||||
int PeerConnection::SendData(const char *data, size_t size) {
|
||||
ice_transport_->SendData(data, size);
|
||||
ice_transmission_->SendData(data, size);
|
||||
return 0;
|
||||
}
|
||||
@@ -3,8 +3,8 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "ice_transport.h"
|
||||
#include "ws_transport.h"
|
||||
#include "ice_transmission.h"
|
||||
#include "ws_transmission.h"
|
||||
|
||||
enum SignalStatus { Connecting = 0, Connected, Closed };
|
||||
|
||||
@@ -25,8 +25,8 @@ class PeerConnection {
|
||||
~PeerConnection();
|
||||
|
||||
public:
|
||||
int Init(PeerConnectionParams params);
|
||||
int Init(PeerConnectionParams params, std::string const &id);
|
||||
int Create(PeerConnectionParams params, const std::string &id = "");
|
||||
int Join(PeerConnectionParams params, const std::string &id);
|
||||
int Destroy();
|
||||
|
||||
SignalStatus GetSignalStatus();
|
||||
@@ -35,12 +35,12 @@ class PeerConnection {
|
||||
|
||||
private:
|
||||
std::string uri_ = "";
|
||||
WsTransport *ws_transport_ = nullptr;
|
||||
IceTransport *ice_transport_ = nullptr;
|
||||
WsTransmission *ws_transport_ = nullptr;
|
||||
IceTransmission *ice_transmission_ = nullptr;
|
||||
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 transport_id_ = "";
|
||||
std::string transmission_id_ = "";
|
||||
SignalStatus signal_status_ = SignalStatus::Closed;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user