-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Segmented osc and socket helper functions
- Loading branch information
1 parent
ecbd1df
commit 1052591
Showing
6 changed files
with
124 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
{ | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
#include <string> | ||
|
||
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include "socket_helper.h" | ||
|
||
#include <util/base.h> | ||
|
||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
#include <string> | ||
#include <thread> | ||
#include <WS2tcpip.h> | ||
#include <WinSock2.h> | ||
|
||
#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)); | ||
} | ||
}; |