diff options
author | Newe <speedy.wolfy@outlook.com> | 2021-05-21 15:48:56 +0200 |
---|---|---|
committer | Newe <speedy.wolfy@outlook.com> | 2021-05-21 15:48:56 +0200 |
commit | f8eceb3124ac4149f180b496082d9965f6b8c1b6 (patch) | |
tree | 47487bac892663390c61c622acc2e2dec7606b64 | |
parent | [edit] Set rpcStub.server to public (diff) | |
download | server-f8eceb3124ac4149f180b496082d9965f6b8c1b6.tar.xz |
[edit] Hypothetical gRPC handshake
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | src/main.cpp | 45 | ||||
-rw-r--r-- | src/protodefs/protos.proto | 4 | ||||
-rw-r--r-- | src/rpcStub.cpp | 38 | ||||
-rw-r--r-- | src/rpcStub.hpp | 7 | ||||
-rw-r--r-- | src/rtcPeerHandler.cpp | 94 | ||||
-rw-r--r-- | src/rtcPeerHandler.hpp | 5 |
7 files changed, 119 insertions, 78 deletions
diff --git a/.gitignore b/.gitignore index 788c5adb..539bb001 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,10 @@ # Executables *.o +# Protobuffer builds +*.pb.cc +*.pb.h + # Directories build/ .vscode/ diff --git a/src/main.cpp b/src/main.cpp index 88adc97c..2fdeceee 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,37 +1,36 @@ // $$$$$$\ $$\ -// $$ __$$\ $$ | -// $$ / \__|$$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$\ $$$$$$\ $$$$$$$ | -// $$$$\ $$ __$$\ $$ _____|$$ _____|$$ _____|$$ __$$\ $$ __$$\ $$ __$$ | -// $$ _| $$ / $$ |\$$$$$$\ \$$$$$$\ $$ / $$ / $$ |$$ | \__|$$ / $$ | -// $$ | $$ | $$ | \____$$\ \____$$\ $$ | $$ | $$ |$$ | $$ | $$ | -// $$ | \$$$$$$ |$$$$$$$ |$$$$$$$ |\$$$$$$$\ \$$$$$$ |$$ | \$$$$$$$ | -// \__| \______/ \_______/ \_______/ \_______| \______/ \__| \_______| -// -// -// +// $$ __$$\ $$ | +// $$ / \__|$$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$\ $$$$$$\ $$$$$$$ | +// $$$$\ $$ __$$\ $$ _____|$$ _____|$$ _____|$$ __$$\ $$ __$$\ $$ __$$ | +// $$ _| $$ / $$ |\$$$$$$\ \$$$$$$\ $$ / $$ / $$ |$$ | \__|$$ / $$ | +// $$ | $$ | $$ | \____$$\ \____$$\ $$ | $$ | $$ |$$ | $$ | $$ | +// $$ | \$$$$$$ |$$$$$$$ |$$$$$$$ |\$$$$$$$\ \$$$$$$ |$$ | \$$$$$$$ | +// \__| \______/ \_______/ \_______/ \_______| \______/ \__| \_______| +// +// +// // $$\ $$$$$$\ // \__| $$ __$$\ // $$\ $$\ $$$$$$\ $$\ $$$$$$$\ $$$$$$\ $$ / \__| $$$$$$\ $$$$$$\ $$\ $$\ $$$$$$\ $$$$$$\ // \$$\ $$ |$$ __$$\ $$ |$$ _____|$$ __$$\ \$$$$$$\ $$ __$$\ $$ __$$\\$$\ $$ |$$ __$$\ $$ __$$\ // \$$\$$ / $$ / $$ |$$ |$$ / $$$$$$$$ | \____$$\ $$$$$$$$ |$$ | \__|\$$\$$ / $$$$$$$$ |$$ | \__| -// \$$$ / $$ | $$ |$$ |$$ | $$ ____|$$\ $$ |$$ ____|$$ | \$$$ / $$ ____|$$ | -// \$ / \$$$$$$ |$$ |\$$$$$$$\ \$$$$$$$\ \$$$$$$ |\$$$$$$$\ $$ | \$ / \$$$$$$$\ $$ | -// \_/ \______/ \__| \_______| \_______| \______/ \_______|\__| \_/ \_______|\__| -// -// -// +// \$$$ / $$ | $$ |$$ |$$ | $$ ____|$$\ $$ |$$ ____|$$ | \$$$ / $$ ____|$$ | +// \$ / \$$$$$$ |$$ |\$$$$$$$\ \$$$$$$$\ \$$$$$$ |\$$$$$$$\ $$ | \$ / \$$$$$$$\ $$ | +// \_/ \______/ \__| \_______| \_______| \______/ \_______|\__| \_/ \_______|\__| +// +// +// +#include "rtcPeerHandler.hpp" //Handle peer connection requests +#include "rpcStub.hpp" //Handle gRPC communications between the different fosscord elements -#include "rtcPeerHandler.hpp" //HAndle peer connection requests -#include "rpcStub.hpp" //Handle gRPC communications between the different fosscord elements - -int main (int argc, char** argv){ +int main(int argc, char **argv){ auto commsHandler = std::make_shared<rtcPeerHandler>(); - auto rpcHandler = std::unique_ptr<rpcStub>(); + auto rpcHandler = std::unique_ptr<rpcStub>(new rpcStub(commsHandler, 8057)); - std::cout << "Server created" <<std::endl; + std::cout << "Server created" << std::endl; //rpcHandler->server->Wait(); //blocking, this will need to be threaded - return 0; + return 0; } \ No newline at end of file diff --git a/src/protodefs/protos.proto b/src/protodefs/protos.proto index a8c9f845..11face5f 100644 --- a/src/protodefs/protos.proto +++ b/src/protodefs/protos.proto @@ -3,13 +3,13 @@ syntax = "proto3"; package fosscordMedia; service fosscordInternals{ - rpc requestProtocol(voiceRequest) returns (voiceAnswer) {} + rpc vRequest(voiceRequest) returns (voiceAnswer) {} } message voiceRequest{ //OP1 from gw uint64 userid = 1; uint64 guildid = 2; - string IP=3; + string ip=3; uint32 port=4; string protocol=5; string rtcConnectionId=6; diff --git a/src/rpcStub.cpp b/src/rpcStub.cpp index 3d848924..1633aab8 100644 --- a/src/rpcStub.cpp +++ b/src/rpcStub.cpp @@ -1,24 +1,32 @@ #include "rpcStub.hpp" -class fossCordInternalsImpl final : public fosscordMedia::fosscordInternals::Service{ - grpc::Status sendRequest( - grpc::ServerContext* ctx, - const fosscordMedia::rpcRequest* req, - fosscordMedia::rpcResponse* resp - ) override{ - resp->set_b(333); - return grpc::Status::OK; +class fossCordInternalsImpl final : public fosscordMedia::fosscordInternals::Service { + std::shared_ptr<rtcPeerHandler> ph; + fossCordInternalsImpl(std::shared_ptr<rtcPeerHandler> handler){ + this->ph= handler; } + grpc::Status vRequest(grpc::ServerContext* ctx, + const fosscordMedia::voiceRequest* req, + fosscordMedia::voiceAnswer* resp) override { + this->ph->initiateConnection(req->ip(), req->port()); + return grpc::Status::OK; + } }; -rpcStub::rpcStub(int port){ - grpc::ServerBuilder builder; +rpcStub::rpcStub(std::shared_ptr<rtcPeerHandler> handler, int port) { + if (not port) { + port = 8057; + } + this->ph = handler; - fossCordInternalsImpl* service; - builder.AddListeningPort("0.0.0.0:8057", grpc::InsecureServerCredentials() ); - builder.RegisterService(service); + fossCordInternalsImpl* service; + grpc::ServerBuilder builder; + builder.AddListeningPort("0.0.0.0:" + std::to_string(port), + grpc::InsecureServerCredentials()); + builder.RegisterService(service); - std::unique_ptr<grpc::Server> server(builder.BuildAndStart()); - std::cout << "Server listening on port 8057 " << std::endl; + this->server = builder.BuildAndStart(); + + std::cout << "RPC stub listening on port " << port << std::endl; } \ No newline at end of file diff --git a/src/rpcStub.hpp b/src/rpcStub.hpp index 99d6d582..f567cd97 100644 --- a/src/rpcStub.hpp +++ b/src/rpcStub.hpp @@ -1,11 +1,12 @@ #include <grpc++/grpc++.h> #include "protodefs/include/protos.grpc.pb.h" +#include "rtcPeerHandler.hpp" class rpcStub{ public: - rpcStub(int port); + rpcStub(std::shared_ptr<rtcPeerHandler> peerHandler, int port); std::unique_ptr<grpc::Server> server; - private: - + private: + std::shared_ptr<rtcPeerHandler> ph; }; \ No newline at end of file diff --git a/src/rtcPeerHandler.cpp b/src/rtcPeerHandler.cpp index 4696c4c7..9bfc6466 100644 --- a/src/rtcPeerHandler.cpp +++ b/src/rtcPeerHandler.cpp @@ -1,45 +1,44 @@ #include "rtcPeerHandler.hpp" -rtcPeerHandler::rtcPeerHandler() -{ - rtc::InitLogger(rtc::LogLevel::Verbose, NULL); -} -void rtcPeerHandler::initiateConnection(std::string peerIP, int peerPort) -{ - //Socket connection between client and server - SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); - sockaddr_in addr; - addr.sin_addr.s_addr = inet_addr(peerIP.c_str()); - addr.sin_port = htons(peerPort); - addr.sin_family = AF_INET; +rtcPeerHandler::rtcPeerHandler() { + rtc::InitLogger(rtc::LogLevel::Verbose, NULL); +} - rtc::Configuration conf; - conf.enableIceTcp = false; - conf.disableAutoNegotiation = false; +void rtcPeerHandler::initiateConnection(std::string peerIP, int peerPort) { + // Socket connection between client and server + SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); + sockaddr_in addr; + addr.sin_addr.s_addr = inet_addr(peerIP.c_str()); + addr.sin_port = htons(peerPort); + addr.sin_family = AF_INET; - auto pc = std::make_shared<rtc::PeerConnection>(conf); + rtc::Configuration conf; + conf.enableIceTcp = false; + conf.disableAutoNegotiation = false; - rtc::Description::Audio media("audio", rtc::Description::Direction::SendRecv); - media.addOpusCodec(96); - media.setBitrate(64); + auto pc = std::make_shared<rtc::PeerConnection>(conf); + rtc::Description::Audio media("audio", + rtc::Description::Direction::SendRecv); + media.addOpusCodec(96); + media.setBitrate(64); - auto track = pc->addTrack(media); + auto track = pc->addTrack(media); - //auto session = std::make_shared<rtc::MediaHandler>(); + // auto session = std::make_shared<rtc::MediaHandler>(); - //track->setMediaHandler(session); + // track->setMediaHandler(session); - rtc::Reliability rtcRel; - rtcRel.unordered = true; - rtcRel.type = rtc::Reliability::Type::Timed; - rtcRel.rexmit = 500; + rtc::Reliability rtcRel; + rtcRel.unordered = true; + rtcRel.type = rtc::Reliability::Type::Timed; + rtcRel.rexmit = 500; - rtc::DataChannelInit rtcConf; - rtcConf.reliability = rtcRel; - rtcConf.negotiated = false; + rtc::DataChannelInit rtcConf; + rtcConf.reliability = rtcRel; + rtcConf.negotiated = false; - pc->onStateChange([](rtc::PeerConnection::State state) { + pc->onStateChange([](rtc::PeerConnection::State state) { std::cout << "State: " << state << std::endl; if (state == rtc::PeerConnection::State::Disconnected || state == rtc::PeerConnection::State::Failed || @@ -48,10 +47,37 @@ void rtcPeerHandler::initiateConnection(std::string peerIP, int peerPort) } }); - pc->onGatheringStateChange( - [](rtc::PeerConnection::GatheringState state) { std::cout << "Gathering State: " << state << std::endl; }); + pc->onGatheringStateChange([](rtc::PeerConnection::GatheringState state) { + std::cout << "Gathering State: " << state << std::endl; + }); + + /*std::tuple<rtc::Track*, rtc::RtcpSrReporter*> addAudio( + + const std::shared_ptr<rtc::PeerConnection> pc, + const uint8_t payloadType, const uint32_t ssrc, const std::string cname, + const std::string msid, const std::function<void(void)> onOpen) { + auto audio = Description::Audio(cname); + audio.addOpusCodec(payloadType); + audio.addSSRC(ssrc, cname, msid, cname); + auto track = pc->addTrack(audio); + // create RTP configuration + auto rtpConfig = make_shared<RtpPacketizationConfig>( + ssrc, cname, payloadType, OpusRtpPacketizer::defaultClockRate); + // create packetizer + auto packetizer = make_shared<OpusRtpPacketizer>(rtpConfig); + // create opus handler + auto opusHandler = make_shared<OpusPacketizationHandler>(packetizer); + + // add RTCP SR handler + auto srReporter = make_shared<RtcpSrReporter>(rtpConfig); + opusHandler->addToChain(srReporter); + // set handler + track->setMediaHandler(opusHandler); + track->onOpen(onOpen); + auto trackData = make_shared<ClientTrackData>(track, srReporter); + return trackData; + }*/ - pc->createDataChannel("Fosscord voice connection", rtcConf); + pc->createDataChannel("Fosscord voice connection", rtcConf); } - \ No newline at end of file diff --git a/src/rtcPeerHandler.hpp b/src/rtcPeerHandler.hpp index 030f41fa..3ba32a83 100644 --- a/src/rtcPeerHandler.hpp +++ b/src/rtcPeerHandler.hpp @@ -13,6 +13,8 @@ typedef int SOCKET; using json = nlohmann::json; +#ifndef RTCPEERHANDLER +#define RTCPEERHANDLER class rtcPeerHandler{ public: rtcPeerHandler(); @@ -26,4 +28,5 @@ public: private: std::map<SOCKET, client> clients; -}; \ No newline at end of file +}; +#endif \ No newline at end of file |