From 1052591a8573a4e4a7781e0ba891f22fdbcab2ab Mon Sep 17 00:00:00 2001 From: benaclejames Date: Wed, 10 Aug 2022 07:57:14 +0100 Subject: [PATCH] Segmented osc and socket helper functions --- CMakeLists.txt | 4 ++++ obsc.cpp | 24 +++++++----------------- osc_message.cpp | 23 +++++++++++++++++++++++ osc_message.h | 22 ++++++++++++++++++++++ socket_helper.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ socket_helper.h | 24 ++++++++++++++++++++++++ 6 files changed, 124 insertions(+), 17 deletions(-) create mode 100644 osc_message.cpp create mode 100644 osc_message.h create mode 100644 socket_helper.cpp create mode 100644 socket_helper.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 085801b..250e2e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,10 +4,14 @@ project(OBSC) set(OBSC_HEADERS stored.h + socket_helper.h + osc_message.h ) set(OBSC_SOURCES obsc.cpp + socket_helper.cpp + osc_message.cpp ) link_directories($ENV{OBS_DIR}/build/libobs/Debug $ENV{OBS_DIR}/build/UI/obs-frontend-api/Debug) diff --git a/obsc.cpp b/obsc.cpp index 2c550bd..8c70c6a 100644 --- a/obsc.cpp +++ b/obsc.cpp @@ -2,18 +2,14 @@ #include #include #include -#include -#include #include "stored.h" +#include "build/socket_helper.h" -#pragma comment(lib,"WS2_32") OBS_DECLARE_MODULE() -WSADATA data; -SOCKADDR_IN addr; -SOCKET sock; -struct stored stored; +socket_helper* sockets; +stored stored; std::thread* osc_thread; int create_osc_bool_message(char* message, const char* address, bool value) { @@ -55,13 +51,13 @@ void update_osc() int msgLen; msgLen = create_osc_bool_message(message, "/recording", stored.get_recording_active()); - sendto(sock, message, msgLen, 0, (struct sockaddr*) &addr, sizeof(addr)); + sockets->send(message, msgLen); msgLen = create_osc_bool_message(message, "/streaming", stored.get_streaming_active()); - sendto(sock, message, msgLen, 0, (struct sockaddr*) &addr, sizeof(addr)); + sockets->send(message, msgLen); msgLen = create_osc_int_message(message, "/replaybuffer", stored.get_replay_buffer_save_count()); - sendto(sock, message, msgLen, 0, (struct sockaddr*) &addr, sizeof(addr)); + sockets->send(message, msgLen); } void frontend_cb(enum obs_frontend_event event, void *priv_data) @@ -98,13 +94,7 @@ void periodic_update_loop() bool obs_module_load() { - WSAStartup(MAKEWORD(2,2), &data); - - addr.sin_family = AF_INET; - InetPton(AF_INET, "127.0.0.1", &addr.sin_addr.s_addr); - addr.sin_port = htons(9000); - - sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + sockets = new socket_helper("127.0.0.1", 9001, 9000); obs_frontend_add_event_callback(frontend_cb, nullptr); diff --git a/osc_message.cpp b/osc_message.cpp new file mode 100644 index 0000000..4d8ee10 --- /dev/null +++ b/osc_message.cpp @@ -0,0 +1,23 @@ +#include "osc_message.h" + +osc_message::osc_message(std::string address, char type) +{ + +} + +osc_message::osc_message(char* data, int size) +{ + +} + +osc_float_message::osc_float_message(std::string address, float value): osc_message(address, 'f') +{ + +} + +osc_string_message::osc_string_message(std::string address, std::string value) : osc_message(address, 's') +{ + +} + + diff --git a/osc_message.h b/osc_message.h new file mode 100644 index 0000000..6394c42 --- /dev/null +++ b/osc_message.h @@ -0,0 +1,22 @@ +#pragma once +#include + +class osc_message +{ +protected: + osc_message(std::string address, char type); +public: + osc_message(char* data, int size); +}; + +class osc_float_message : public osc_message +{ +public: + osc_float_message(std::string address, float value); +}; + +class osc_string_message : public osc_message +{ +public: + osc_string_message(std::string address, std::string value); +}; diff --git a/socket_helper.cpp b/socket_helper.cpp new file mode 100644 index 0000000..9ada2c3 --- /dev/null +++ b/socket_helper.cpp @@ -0,0 +1,44 @@ +#include "socket_helper.h" + +#include + +void socket_helper::recv_loop() +{ + int bytes_received; + char serverBuf[1025]; + int serverBufLen = 1024; + + // Keep a seperate address struct to store sender information. + struct sockaddr_in SenderAddr; + int SenderAddrSize = sizeof (SenderAddr); + + printf("Receiving datagrams on %s\n", "127.0.0.1"); + while (true) + { + bytes_received = recvfrom(inSock, serverBuf, serverBufLen, 0 /* no flags*/, (SOCKADDR *) & SenderAddr, &SenderAddrSize); + if (bytes_received == SOCKET_ERROR) { + printf("recvfrom failed with error %d\n", WSAGetLastError()); + } + printf("recv bytes"); + } +} + +socket_helper::socket_helper(std::string ip, int inPort, int outPort) +{ + WSAStartup(MAKEWORD(2,2), &data); + + outAddr.sin_family = AF_INET; + InetPton(AF_INET, ip.c_str(), &outAddr.sin_addr.s_addr); + outAddr.sin_port = htons(outPort); + + outSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + + inAddr.sin_family = AF_INET; + inAddr.sin_port = htons(inPort); + InetPton(AF_INET, ip.c_str(), &inAddr.sin_addr.s_addr); + + if(bind(inSock, (SOCKADDR*)&inAddr, sizeof(inAddr))) + blog(LOG_ERROR, "[OBSC] Failed to bind in socket"); + + recvThread = new std::thread(recv_loop); +} diff --git a/socket_helper.h b/socket_helper.h new file mode 100644 index 0000000..8bb1e7f --- /dev/null +++ b/socket_helper.h @@ -0,0 +1,24 @@ +#pragma once +#include +#include +#include +#include + +#pragma comment(lib,"WS2_32") + +class socket_helper +{ + WSADATA data; + SOCKADDR_IN outAddr, inAddr; + SOCKET inSock, outSock; + std::thread* recvThread; + + void recv_loop(); +public: + socket_helper(std::string ip, int inPort, int outPort); + + void send(char* data, int size) + { + sendto(outSock, data, size, 0, (SOCKADDR*)&outAddr, sizeof(outAddr)); + } +};