Skip to content

Commit

Permalink
More cleanup, added recv loop and destructor to socket. Added incomin…
Browse files Browse the repository at this point in the history
…g data parser to osc
  • Loading branch information
benaclejames committed Aug 11, 2022
1 parent 12dcaea commit 7c6bd9e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 55 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# OBSC/CMakeLists.txt
cmake_minimum_required(VERSION 3.23)

project(OBSC)

Expand Down
50 changes: 4 additions & 46 deletions obsc.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <obs-module.h>
#include <obs-frontend-api.h>
#include <stdio.h>
#include <thread>
#include "stored.h"
#include "socket_helper.h"
Expand All @@ -11,40 +10,6 @@ OBS_DECLARE_MODULE()

socket_helper* sockets;
stored stored;
std::thread* osc_thread;

int create_osc_bool_message(char* message, const char* address, bool value) {
int len = strlen(address)+1;
int paddedLen = len + 4 - len % 4;
memcpy(message, address, paddedLen);

message[paddedLen++] = ',';
message[paddedLen++] = value ? 'T' : 'F';
message[paddedLen++] = '\0';
message[paddedLen++] = '\0';

return paddedLen;
}

int create_osc_int_message(char* message, const char* address, int value) {
int len = strlen(address)+1;
int paddedLen = len + 4 - len % 4;
memcpy(message, address, paddedLen);

message[paddedLen++] = ',';
message[paddedLen++] = 'i';
message[paddedLen++] = '\0';
message[paddedLen++] = '\0';

// Convert value from big endian to little endian and copy to message
char* valuePtr = (char*)&value;
message[paddedLen++] = valuePtr[3];
message[paddedLen++] = valuePtr[2];
message[paddedLen++] = valuePtr[1];
message[paddedLen++] = valuePtr[0];

return paddedLen;
}

void update_osc()
{
Expand Down Expand Up @@ -75,28 +40,21 @@ void frontend_cb(enum obs_frontend_event event, void *priv_data)
break;

case OBS_FRONTEND_EVENT_EXIT:
osc_thread->detach();
delete osc_thread;
delete sockets;
break;
}
}

void periodic_update_loop()
{
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(5));
void on_update_recv(osc_message message) {
if (strcmp(message.address, "/avatar/change") == 0)
update_osc();
}
}

bool obs_module_load()
{
sockets = new socket_helper("127.0.0.1", 9001, 9000);
sockets = new socket_helper("127.0.0.1", 9001, 9000, on_update_recv);

obs_frontend_add_event_callback(frontend_cb, nullptr);

// Start the osc update loop on a separate thread
osc_thread = new std::thread(periodic_update_loop);
return true;
}
18 changes: 16 additions & 2 deletions osc_message.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "osc_message.h"

osc_message::osc_message(char* address, char type)
osc_message::osc_message(char* address, char type) : address(address), type(type)
{
int len = strlen(address)+1;
writerIndex = quantize(len, 4);
Expand All @@ -14,7 +14,21 @@ osc_message::osc_message(char* address, char type)

osc_message::osc_message(char* data, int size)
{

// Read the address string
int len = strlen(data)+1;
address = new char[len];
memcpy(address, data, len);

// Offset the writerIndex by the address length quantized to 4 bytes
writerIndex = quantize(len, 4);

// Ensure the next 2 bytes are a comma and the type
if (data[writerIndex++] != ',') {
throw "Invalid OSC message";
}

type = data[writerIndex++];
writerIndex += 2;
}

osc_float_message::osc_float_message(char* address, float value): osc_message(address, 'f')
Expand Down
2 changes: 2 additions & 0 deletions osc_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class osc_message

public:
osc_message(char* data, int size);
char* address;
char type;
int writerIndex;
char message[256];
};
Expand Down
22 changes: 16 additions & 6 deletions socket_helper.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "socket_helper.h"
#include "osc_message.h"

#include <util/base.h>

Expand All @@ -12,18 +13,16 @@ void socket_helper::recv_loop()
struct sockaddr_in SenderAddr;
int SenderAddrSize = sizeof (SenderAddr);

printf("Receiving datagrams on %s\n", "127.0.0.1");
blog(LOG_INFO,"Receiving datagrams");
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");
osc_message msg = osc_message(serverBuf, bytes_received);
onMsgRecv(msg);
}
}

socket_helper::socket_helper(std::string ip, int inPort, int outPort)
socket_helper::socket_helper(std::string ip, int inPort, int outPort, void(*onMsgRecv)(osc_message)) : onMsgRecv(onMsgRecv)
{
WSAStartup(MAKEWORD(2,2), &data);

Expand All @@ -37,8 +36,19 @@ socket_helper::socket_helper(std::string ip, int inPort, int outPort)
inAddr.sin_port = htons(inPort);
InetPton(AF_INET, ip.c_str(), &inAddr.sin_addr.s_addr);

inSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

if(bind(inSock, (SOCKADDR*)&inAddr, sizeof(inAddr)))
blog(LOG_ERROR, "[OBSC] Failed to bind in socket");

recvThread = new std::thread(&socket_helper::recv_loop, this);
}

socket_helper::~socket_helper() {
recvThread->detach();
delete recvThread;

closesocket(inSock);
closesocket(outSock);
WSACleanup();
}
5 changes: 4 additions & 1 deletion socket_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <thread>
#include <WS2tcpip.h>
#include <WinSock2.h>
#include "osc_message.h"

#pragma comment(lib,"WS2_32")

Expand All @@ -12,10 +13,12 @@ class socket_helper
SOCKADDR_IN outAddr, inAddr;
SOCKET inSock, outSock;
std::thread* recvThread;
void (*onMsgRecv)(osc_message);

void recv_loop();
public:
socket_helper(std::string ip, int inPort, int outPort);
socket_helper(std::string ip, int inPort, int outPort, void(*onMsgRecv)(osc_message));
~socket_helper();

void send(char* data, int size)
{
Expand Down

0 comments on commit 7c6bd9e

Please sign in to comment.