-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVRChatOSC.cpp
101 lines (76 loc) · 2.32 KB
/
VRChatOSC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "VRChatOSC.h"
#include <windows.h>
#include <winsock.h>
#include "tinyosc.h"
#include <string>
#define SEND_BUFFER_SIZE 1024
#define VRCHAT_SEND_PORT 9000
#define VRCHAT_RECV_PORT 9001
namespace VRChatOSC
{
SOCKET vrchatSocket = -1;
struct sockaddr_in vrchatSocketServer;
bool connected = false;
char vrchatTargetAddress[32];
char oscBuffer[SEND_BUFFER_SIZE];
tosc_bundle oscBundle;
int Connect(int* ipAddress)
{
WSADATA Data;
WSAStartup(0x202, &Data);
if ((vrchatSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
{
return 1;
}
sprintf(vrchatTargetAddress, "%d.%d.%d.%d", ipAddress[0], ipAddress[1], ipAddress[2], ipAddress[3]);
memset(&vrchatSocketServer, 0, sizeof(vrchatSocketServer));
vrchatSocketServer.sin_family = AF_INET;
vrchatSocketServer.sin_port = htons(VRCHAT_SEND_PORT);
vrchatSocketServer.sin_addr.s_addr = inet_addr(vrchatTargetAddress);
if (connect(vrchatSocket, (struct sockaddr*)&vrchatSocketServer, sizeof(vrchatSocketServer)) == SOCKET_ERROR)
{
return 2;
}
connected = true;
return 0;
}
void Disconnect()
{
if (!connected)
return;
shutdown(vrchatSocket, 2);
closesocket(vrchatSocket);
vrchatSocket = -1;
connected = false;
}
bool IsConnected()
{
return connected;
}
char* GetAddress()
{
return vrchatTargetAddress;
}
void NewMessage()
{
memset(&oscBuffer, 0, sizeof(oscBuffer));
tosc_writeBundle(&oscBundle, 0, oscBuffer, sizeof(oscBuffer));
}
void WritePosition(int trackerID, float x, float y, float z)
{
char positionAddress[32];
sprintf(positionAddress, "/tracking/trackers/%d/position", trackerID);
tosc_writeNextMessage(&oscBundle, positionAddress, "fff", x, y, z);
}
void WriteRotation(int trackerID, float x, float y, float z)
{
char rotationAddress[32];
sprintf(rotationAddress, "/tracking/trackers/%d/rotation", trackerID);
tosc_writeNextMessage(&oscBundle, rotationAddress, "fff", x, y, z);
}
void SendMessage()
{
if (connected)
send(vrchatSocket, oscBuffer, tosc_getBundleLength(&oscBundle), 0);
}
}