Created
October 7, 2019 07:57
-
-
Save togucchi/b9497eb84302c02f711314f38675161f to your computer and use it in GitHub Desktop.
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
using System; | |
using UnityEngine; | |
using UnityEngine.Events; | |
using UniRx; | |
namespace uOSC | |
{ | |
public class uOscObservableServer : MonoBehaviour | |
{ | |
[SerializeField] | |
int port = 3333; | |
#if NETFX_CORE | |
Udp udp_ = new Uwp.Udp(); | |
Thread thread_ = new Uwp.Thread(); | |
#else | |
Udp udp_ = new DotNet.Udp(); | |
Thread thread_ = new DotNet.Thread(); | |
#endif | |
Parser parser_ = new Parser(); | |
private Subject<Message> dataReceivedSubject = new Subject<Message>(); | |
public IObservable<Message> OnDataReceived => dataReceivedSubject; | |
void OnEnable() | |
{ | |
udp_.StartServer(port); | |
thread_.Start(UpdateMessage); | |
} | |
void OnDisable() | |
{ | |
thread_.Stop(); | |
udp_.Stop(); | |
} | |
void Update() | |
{ | |
while (parser_.messageCount > 0) | |
{ | |
var message = parser_.Dequeue(); | |
dataReceivedSubject.OnNext(message); | |
} | |
} | |
void UpdateMessage() | |
{ | |
while (udp_.messageCount > 0) | |
{ | |
var buf = udp_.Receive(); | |
int pos = 0; | |
parser_.Parse(buf, ref pos, buf.Length); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment