forked from jorgegarcia/UnityOSC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSCMaster.cs
161 lines (129 loc) · 4 KB
/
OSCMaster.cs
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Net;
using UnityEngine;
using System.Collections.Generic;
using UnityOSC;
public class OSCMaster : MonoBehaviour
{
private static OSCMaster _instance;
public static OSCMaster Instance
{
get
{
if (_instance == null)
{
_instance = new GameObject("OSCMaster").AddComponent<OSCMaster>();
}
return _instance;
}
set
{
_instance = value;
}
}
public static Dictionary<string, OSCReceiver> Receivers;
public static Dictionary<string, OSCClient> Clients;
public bool ShowDebug;
public bool LogIncoming;
public bool LogOutgoing;
void Awake()
{
Instance = this;
Receivers = new Dictionary<string, OSCReceiver>();
Clients = new Dictionary<string, OSCClient>();
}
private void Update()
{
foreach(var receiver in Receivers)
{
while (receiver.Value.WaitingMessagesCount() > 0) //Allow to switch from receiver/server thread to main thread
receiver.Value.PropagateEvent();
}
}
public static bool HasClient(string clientId)
{
return Clients.ContainsKey(clientId);
}
public static bool HasReceiver(string receiverId)
{
return Receivers.ContainsKey(receiverId);
}
public static void CreateClient(string clientId, string destination, int port)
{
CreateClient(clientId, IPAddress.Parse(destination), port);
}
public static void CreateClient(string clientId, IPAddress destination, int port)
{
var client = new OSCClient(destination, port)
{
Name = clientId
};
Clients.Add(clientId, client);
if (Instance.ShowDebug)
Debug.Log("Client " + clientId + " on " + destination + ":" + port + "created.");
}
public static void RemoveClient(string clientId)
{
Clients[clientId].Close();
Clients.Remove(clientId);
if (Instance.ShowDebug)
Debug.Log("Client " + clientId + " removed.");
}
public static OSCReceiver CreateReceiver(string receiverId, int port)
{
var receiver = new OSCReceiver
{
Name = receiverId
};
if(!receiver.Open(port))
{
return null;
}
Receivers.Add(receiverId, receiver);
if (Instance.ShowDebug)
Debug.Log("Receiver " + receiverId + " on " + port + " created.");
return receiver;
}
public static void RemoveReceiver(string receiverId)
{
Receivers[receiverId].Close();
Receivers.Remove(receiverId);
if (Instance.ShowDebug)
Debug.Log("Receiver " + receiverId + " removed.");
}
public static void SendMessageUsingClient(string clientId, OSCMessage msg)
{
Clients[clientId].Send(msg);
if (Instance.LogOutgoing)
{
Debug.Log("[" + clientId + " to " + Clients[clientId].ClientIPAddress.ToString() + ":" + Clients[clientId].Port + "|" + DateTime.Now.ToLocalTime() + "] " + msg.Address);
foreach (var data in msg.Data)
Debug.Log(data);
}
}
public static void SendMessage(OSCMessage m, string host, int port)
{
if (Instance.LogOutgoing)
{
string args = "";
for (int i = 0; i < m.Data.Count; i++)
args += (i > 0 ? ", " : "") + m.Data[i].ToString();
Debug.Log("[OSCMaster to" + host + ":" + port +" | " + DateTime.Now.ToLocalTime() + "] " + m.Address + " : " + args);
}
var tempClient = new OSCClient(System.Net.IPAddress.Loopback, port);
tempClient.SendTo(m, host, port);
tempClient.Close();
}
void OnApplicationQuit()
{
foreach (var pair in Clients)
{
pair.Value.Close();
}
foreach (var pair in Receivers)
{
pair.Value.Close();
}
_instance = null;
}
}