mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 20:25:34 +08:00
Use hash in switch method instead of map list
This commit is contained in:
@@ -9,7 +9,7 @@
|
|||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
static const std::map<std::string, unsigned int> siganl_types{
|
static const std::map<std::string, unsigned int> siganl_types{
|
||||||
{"connection_id", 1},
|
{"ws_connection_id", 1},
|
||||||
{"offer", 2},
|
{"offer", 2},
|
||||||
{"transport_id", 3},
|
{"transport_id", 3},
|
||||||
{"remote_sdp", 4},
|
{"remote_sdp", 4},
|
||||||
@@ -43,8 +43,9 @@ int PeerConnection::Init(PeerConnectionParams params) {
|
|||||||
LOG_INFO("msg type :{}", itr->first);
|
LOG_INFO("msg type :{}", itr->first);
|
||||||
switch (itr->second) {
|
switch (itr->second) {
|
||||||
case 1: {
|
case 1: {
|
||||||
connection_id_ = j["connection_id"].get<unsigned int>();
|
ws_connection_id_ = j["ws_connection_id"].get<unsigned int>();
|
||||||
LOG_INFO("Receive local peer connection_id [{}]", connection_id_);
|
LOG_INFO("Receive local peer websocket connection id [{}]",
|
||||||
|
ws_connection_id_);
|
||||||
signal_status_ = SignalStatus::Connected;
|
signal_status_ = SignalStatus::Connected;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -100,8 +101,9 @@ int PeerConnection::Init(PeerConnectionParams params, std::string const &id) {
|
|||||||
LOG_INFO("msg type :{}", itr->first);
|
LOG_INFO("msg type :{}", itr->first);
|
||||||
switch (itr->second) {
|
switch (itr->second) {
|
||||||
case 1: {
|
case 1: {
|
||||||
connection_id_ = j["connection_id"].get<unsigned int>();
|
ws_connection_id_ = j["ws_connection_id"].get<unsigned int>();
|
||||||
LOG_INFO("Receive local peer connection_id [{}]", connection_id_);
|
LOG_INFO("Receive local peer websocket connection id [{}]",
|
||||||
|
ws_connection_id_);
|
||||||
signal_status_ = SignalStatus::Connected;
|
signal_status_ = SignalStatus::Connected;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ class PeerConnection {
|
|||||||
IceTransport *ice_transport_ = nullptr;
|
IceTransport *ice_transport_ = nullptr;
|
||||||
std::function<void(const std::string &)> on_receive_ws_msg_ = nullptr;
|
std::function<void(const std::string &)> on_receive_ws_msg_ = nullptr;
|
||||||
std::function<void(const char *, size_t)> on_receive_ice_msg_ = nullptr;
|
std::function<void(const char *, size_t)> on_receive_ice_msg_ = nullptr;
|
||||||
unsigned int connection_id_ = 0;
|
unsigned int ws_connection_id_ = 0;
|
||||||
std::string transport_id_ = "";
|
std::string transport_id_ = "";
|
||||||
SignalStatus signal_status_ = SignalStatus::Closed;
|
SignalStatus signal_status_ = SignalStatus::Closed;
|
||||||
};
|
};
|
||||||
|
|||||||
6
tests/signal_server/sdp_manager.cpp
Normal file
6
tests/signal_server/sdp_manager.cpp
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
#include "sdp_manager.h"
|
||||||
|
|
||||||
|
SdpManager ::SdpManager() {}
|
||||||
|
|
||||||
|
SdpManager ::~SdpManager() {}
|
||||||
28
tests/signal_server/sdp_manager.h
Normal file
28
tests/signal_server/sdp_manager.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef _SDP_MANAGER_H_
|
||||||
|
#define _SDP_MANAGER_H_
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
class SdpManager {
|
||||||
|
public:
|
||||||
|
SdpManager();
|
||||||
|
~SdpManager();
|
||||||
|
|
||||||
|
public:
|
||||||
|
int AddOfferSdpToConnection(std::string &sdp, std::string &connection_id);
|
||||||
|
int AddAnswerSdpToConnection(std::string &sdp, std::string &connection_id);
|
||||||
|
int UpdateOfferSdpToConnection(std::string &sdp, std::string &connection_id);
|
||||||
|
int UpdateAnswerSdpToConnection(std::string &sdp, std::string &connection_id);
|
||||||
|
|
||||||
|
const std::string &GetOfferSdpFromConnection(std::string &connection_id);
|
||||||
|
const std::string &GetAnswerSdpFromConnection(std::string &connection_id);
|
||||||
|
|
||||||
|
int RemoveConnetion(std::string &connection_id);
|
||||||
|
|
||||||
|
private:
|
||||||
|
// <connection_id, <offer_sdp, answer_sdp>.
|
||||||
|
std::map<std::string, std::pair<std::string, std::string>> answer_hdl_map_;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -2,9 +2,17 @@
|
|||||||
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
static const std::map<std::string, unsigned int> siganl_types{
|
constexpr size_t HASH_STRING_PIECE(const char* string_piece) {
|
||||||
{"create_transport", 1}, {"offer", 2}, {"query_remote_sdp", 3},
|
std::size_t result = 0;
|
||||||
{"answer", 4}, {"offer_candidate", 5}, {"answer_candidate", 6}};
|
while (*string_piece) {
|
||||||
|
result = (result * 131) + *string_piece++;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr size_t operator"" _H(const char* string_piece, size_t) {
|
||||||
|
return HASH_STRING_PIECE(string_piece);
|
||||||
|
}
|
||||||
|
|
||||||
std::string gen_random_6() {
|
std::string gen_random_6() {
|
||||||
static const char alphanum[] = "0123456789";
|
static const char alphanum[] = "0123456789";
|
||||||
@@ -15,7 +23,6 @@ std::string gen_random_6() {
|
|||||||
tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
|
tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
|
||||||
}
|
}
|
||||||
|
|
||||||
// return tmp_s;
|
|
||||||
return "000000";
|
return "000000";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,18 +54,19 @@ SignalServer::SignalServer() {
|
|||||||
SignalServer::~SignalServer() {}
|
SignalServer::~SignalServer() {}
|
||||||
|
|
||||||
bool SignalServer::on_open(websocketpp::connection_hdl hdl) {
|
bool SignalServer::on_open(websocketpp::connection_hdl hdl) {
|
||||||
connections_[hdl] = connection_id_;
|
ws_connections_[hdl] = ws_connection_id_++;
|
||||||
LOG_INFO("New connection [{}] established", connection_id_++);
|
LOG_INFO("New websocket connection [{}] established", ws_connection_id_);
|
||||||
|
|
||||||
json message = {{"type", "connection_id"}, {"connection_id", connection_id_}};
|
json message = {{"type", "ws_connection_id"},
|
||||||
|
{"ws_connection_id", ws_connection_id_}};
|
||||||
server_.send(hdl, message.dump(), websocketpp::frame::opcode::text);
|
server_.send(hdl, message.dump(), websocketpp::frame::opcode::text);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SignalServer::on_close(websocketpp::connection_hdl hdl) {
|
bool SignalServer::on_close(websocketpp::connection_hdl hdl) {
|
||||||
LOG_INFO("Connection [{}] closed", connection_id_++);
|
LOG_INFO("Websocket onnection [{}] closed", ws_connection_id_++);
|
||||||
connections_.erase(hdl);
|
ws_connections_.erase(hdl);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,67 +102,63 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
|||||||
std::string payload = msg->get_payload();
|
std::string payload = msg->get_payload();
|
||||||
|
|
||||||
auto j = json::parse(payload);
|
auto j = json::parse(payload);
|
||||||
|
|
||||||
std::string type = j["type"];
|
std::string type = j["type"];
|
||||||
auto itr = siganl_types.find(type);
|
|
||||||
if (itr != siganl_types.end()) {
|
switch (HASH_STRING_PIECE(type.c_str())) {
|
||||||
LOG_INFO("msg type: {}", itr->first);
|
case "create_transport"_H: {
|
||||||
switch (itr->second) {
|
transport_id_ = gen_random_6();
|
||||||
case 1: {
|
LOG_INFO("Generate transport_id [{}]", transport_id_);
|
||||||
transport_id_ = gen_random_6();
|
json message = {{"type", "transport_id"},
|
||||||
LOG_INFO("Generate transport_id [{}]", transport_id_);
|
{"transport_id", transport_id_}};
|
||||||
json message = {{"type", "transport_id"},
|
send_msg(hdl, message);
|
||||||
{"transport_id", transport_id_}};
|
break;
|
||||||
send_msg(hdl, message);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 2: {
|
|
||||||
std::string transport_id = j["transport_id"];
|
|
||||||
std::string sdp = j["sdp"];
|
|
||||||
LOG_INFO("Save transport_id[{}] with offer sdp[{}]", transport_id, sdp);
|
|
||||||
offer_sdp_map_[transport_id] = sdp;
|
|
||||||
offer_hdl_map_[transport_id] = hdl;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 3: {
|
|
||||||
std::string transport_id = j["transport_id"];
|
|
||||||
std::string sdp = offer_sdp_map_[transport_id];
|
|
||||||
LOG_INFO("send offer sdp [{}]", sdp.c_str());
|
|
||||||
json message = {{"type", "remote_sdp"}, {"sdp", sdp}};
|
|
||||||
send_msg(hdl, message);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 4: {
|
|
||||||
std::string transport_id = j["transport_id"];
|
|
||||||
std::string sdp = j["sdp"];
|
|
||||||
LOG_INFO("Save transport_id[{}] with answer sdp[{}]", transport_id,
|
|
||||||
sdp);
|
|
||||||
answer_sdp_map_[transport_id] = sdp;
|
|
||||||
answer_hdl_map_[transport_id] = hdl;
|
|
||||||
LOG_INFO("send answer sdp [{}]", sdp.c_str());
|
|
||||||
json message = {{"type", "remote_sdp"}, {"sdp", sdp}};
|
|
||||||
send_msg(offer_hdl_map_[transport_id], message);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 5: {
|
|
||||||
std::string transport_id = j["transport_id"];
|
|
||||||
std::string candidate = j["sdp"];
|
|
||||||
LOG_INFO("send candidate [{}]", candidate.c_str());
|
|
||||||
json message = {{"type", "candidate"}, {"sdp", candidate}};
|
|
||||||
send_msg(answer_hdl_map_[transport_id], message);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 6: {
|
|
||||||
std::string transport_id = j["transport_id"];
|
|
||||||
std::string candidate = j["sdp"];
|
|
||||||
LOG_INFO("send candidate [{}]", candidate.c_str());
|
|
||||||
json message = {{"type", "candidate"}, {"sdp", candidate}};
|
|
||||||
send_msg(offer_hdl_map_[transport_id], message);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
case "offer"_H: {
|
||||||
|
std::string transport_id = j["transport_id"];
|
||||||
|
std::string sdp = j["sdp"];
|
||||||
|
LOG_INFO("Save transport_id[{}] with offer sdp[{}]", transport_id, sdp);
|
||||||
|
// ws_handle_manager_.BindHandleToConnection(hdl, );
|
||||||
|
offer_sdp_map_[transport_id] = sdp;
|
||||||
|
offer_hdl_map_[transport_id] = hdl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "query_remote_sdp"_H: {
|
||||||
|
std::string transport_id = j["transport_id"];
|
||||||
|
std::string sdp = offer_sdp_map_[transport_id];
|
||||||
|
LOG_INFO("send offer sdp [{}]", sdp.c_str());
|
||||||
|
json message = {{"type", "remote_sdp"}, {"sdp", sdp}};
|
||||||
|
send_msg(hdl, message);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "answer"_H: {
|
||||||
|
std::string transport_id = j["transport_id"];
|
||||||
|
std::string sdp = j["sdp"];
|
||||||
|
LOG_INFO("Save transport_id[{}] with answer sdp[{}]", transport_id, sdp);
|
||||||
|
answer_sdp_map_[transport_id] = sdp;
|
||||||
|
answer_hdl_map_[transport_id] = hdl;
|
||||||
|
LOG_INFO("send answer sdp [{}]", sdp.c_str());
|
||||||
|
json message = {{"type", "remote_sdp"}, {"sdp", sdp}};
|
||||||
|
send_msg(offer_hdl_map_[transport_id], message);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "offer_candidate"_H: {
|
||||||
|
std::string transport_id = j["transport_id"];
|
||||||
|
std::string candidate = j["sdp"];
|
||||||
|
LOG_INFO("send candidate [{}]", candidate.c_str());
|
||||||
|
json message = {{"type", "candidate"}, {"sdp", candidate}};
|
||||||
|
send_msg(answer_hdl_map_[transport_id], message);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "answer_candidate"_H: {
|
||||||
|
std::string transport_id = j["transport_id"];
|
||||||
|
std::string candidate = j["sdp"];
|
||||||
|
LOG_INFO("send candidate [{}]", candidate.c_str());
|
||||||
|
json message = {{"type", "candidate"}, {"sdp", candidate}};
|
||||||
|
send_msg(offer_hdl_map_[transport_id], message);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// std::string sdp = j["sdp"];
|
// std::string sdp = j["sdp"];
|
||||||
|
|||||||
@@ -8,6 +8,9 @@
|
|||||||
#include <websocketpp/config/asio_no_tls.hpp>
|
#include <websocketpp/config/asio_no_tls.hpp>
|
||||||
#include <websocketpp/server.hpp>
|
#include <websocketpp/server.hpp>
|
||||||
|
|
||||||
|
#include "sdp_manager.h"
|
||||||
|
#include "ws_handle_manager.h"
|
||||||
|
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
typedef websocketpp::server<websocketpp::config::asio> server;
|
typedef websocketpp::server<websocketpp::config::asio> server;
|
||||||
@@ -37,14 +40,17 @@ class SignalServer {
|
|||||||
server server_;
|
server server_;
|
||||||
std::map<websocketpp::connection_hdl, connection_id,
|
std::map<websocketpp::connection_hdl, connection_id,
|
||||||
std::owner_less<websocketpp::connection_hdl>>
|
std::owner_less<websocketpp::connection_hdl>>
|
||||||
connections_;
|
ws_connections_;
|
||||||
std::map<room_id, connection_id> rooms_;
|
std::map<room_id, connection_id> rooms_;
|
||||||
unsigned int connection_id_ = 0;
|
unsigned int ws_connection_id_ = 0;
|
||||||
std::string transport_id_ = "000000";
|
std::string transport_id_ = "000000";
|
||||||
std::map<std::string, std::string> offer_sdp_map_;
|
std::map<std::string, std::string> offer_sdp_map_;
|
||||||
std::map<std::string, std::string> answer_sdp_map_;
|
std::map<std::string, std::string> answer_sdp_map_;
|
||||||
std::map<std::string, websocketpp::connection_hdl> offer_hdl_map_;
|
std::map<std::string, websocketpp::connection_hdl> offer_hdl_map_;
|
||||||
std::map<std::string, websocketpp::connection_hdl> answer_hdl_map_;
|
std::map<std::string, websocketpp::connection_hdl> answer_hdl_map_;
|
||||||
|
|
||||||
|
WsHandleManager ws_handle_manager_;
|
||||||
|
SdpManager sdp_manager_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
5
tests/signal_server/ws_handle_manager.cpp
Normal file
5
tests/signal_server/ws_handle_manager.cpp
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#include "ws_handle_manager.h"
|
||||||
|
|
||||||
|
WsHandleManager::WsHandleManager() {}
|
||||||
|
|
||||||
|
WsHandleManager::~WsHandleManager() {}
|
||||||
24
tests/signal_server/ws_handle_manager.h
Normal file
24
tests/signal_server/ws_handle_manager.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#ifndef _WS_HANDLE_MANAGER_H_
|
||||||
|
#define _WS_HANDLE_MANAGER_H_
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <websocketpp/server.hpp>
|
||||||
|
|
||||||
|
class WsHandleManager {
|
||||||
|
public:
|
||||||
|
WsHandleManager();
|
||||||
|
~WsHandleManager();
|
||||||
|
|
||||||
|
public:
|
||||||
|
bool BindHandleToConnection(websocketpp::connection_hdl hdl,
|
||||||
|
std::string& connection_id);
|
||||||
|
bool ReleaseHandleFromConnection(websocketpp::connection_hdl hdl,
|
||||||
|
std::string& connection_id);
|
||||||
|
|
||||||
|
const std::string& GetConnectionId(websocketpp::connection_hdl hdl);
|
||||||
|
websocketpp::connection_hdl GetWsHandle(std::string& connection_id);
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user