Skip to content

Commit

Permalink
fix: WSA/UWP support
Browse files Browse the repository at this point in the history
  • Loading branch information
Iam1337 committed Nov 28, 2020
1 parent 75a6b0a commit 40c9051
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 96 deletions.
44 changes: 22 additions & 22 deletions Assets/extOSC/Scripts/Core/Network/OSCReceiverBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,43 @@

namespace extOSC.Core.Network
{
public abstract class OSCReceiverBackend
{
#region Extensions
public abstract class OSCReceiverBackend
{
#region Extensions

public delegate void OSCReceivedCallback(IOSCPacket packet);
public delegate void OSCReceivedCallback(IOSCPacket packet);

#endregion
#endregion

#region Public Methods
#region Public Methods

public static OSCReceiverBackend Create()
{
#if NETFX_CORE
public static OSCReceiverBackend Create()
{
#if UNITY_WSA && !UNITY_EDITOR
return new OSCReceiverWindowsStoreBackend();
#else
return new OSCReceiverStandaloneBackend();
return new OSCReceiverStandaloneBackend();
#endif
}
}

#endregion
#endregion

#region Public Vars
#region Public Vars

public abstract OSCReceivedCallback ReceivedCallback { get; set; }
public abstract OSCReceivedCallback ReceivedCallback { get; set; }

public abstract bool IsAvailable { get; }
public abstract bool IsAvailable { get; }

public abstract bool IsRunning { get; }
public abstract bool IsRunning { get; }

#endregion
#endregion

#region Public Methods
#region Public Methods

public abstract void Connect(string localHost, int localPort);
public abstract void Connect(string localHost, int localPort);

public abstract void Close();
public abstract void Close();

#endregion
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Copyright (c) 2020 ExT (V.Sigalkin) */

#if !NETFX_CORE
#if !UNITY_WSA || UNITY_EDITOR

using UnityEngine;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Copyright (c) 2020 ExT (V.Sigalkin) */

#if NETFX_CORE
#if UNITY_WSA && !UNITY_EDITOR

using UnityEngine;

Expand All @@ -17,27 +17,21 @@ namespace extOSC.Core.Network
{
internal class OSCReceiverWindowsStoreBackend : OSCReceiverBackend
{
#region Public Vars
#region Public Vars

public override OSCReceivedCallback ReceivedCallback
public override OSCReceivedCallback ReceivedCallback
{
get { return _receivedCallback; }
set { _receivedCallback = value; }
get => _receivedCallback;
set => _receivedCallback = value;
}

public override bool IsStarted
{
get { return _datagramSocket != null; }
}
public override bool IsAvailable => _datagramSocket != null;

public override bool IsRunning
{
get { return _isRunning; }
}
public override bool IsRunning => _isRunning;

#endregion
#endregion

#region Private Vars
#region Private Vars

private bool _isRunning;

Expand All @@ -49,9 +43,9 @@ public override bool IsRunning

private OSCReceivedCallback _receivedCallback;

#endregion
#endregion

#region Public Methods
#region Public Methods

public override void Connect(string localHost, int localPort)
{
Expand All @@ -70,26 +64,26 @@ public override void Close()
_isRunning = false;
}

#endregion
#endregion

#region Private Methods
#region Private Methods

private async void ConnectAsync(int localPort)
{
if (_datagramSocket != null)
Close();

_localPort = localPort.ToString();
try
{

try
{
_datagramSocket = new DatagramSocket();
_datagramSocket.MessageReceived += Receive;
_datagramSocket.MessageReceived += Receive;
_datagramSocket.Control.DontFragment = true;
await _datagramSocket.BindEndpointAsync(null, _localPort);
await _datagramSocket.BindEndpointAsync(null, _localPort);

InitMessage();
}
}
catch (ArgumentOutOfRangeException)
{
Debug.LogErrorFormat("[OSCReceiver] Invalid port: {0}", localPort);
Expand All @@ -106,14 +100,15 @@ private async void ConnectAsync(int localPort)

private async void InitMessage()
{
using (var dataWriter = new DataWriter(await _datagramSocket.GetOutputStreamAsync(new HostName("255.255.255.255"), _localPort)))
using (var dataWriter =
new DataWriter(await _datagramSocket.GetOutputStreamAsync(new HostName("255.255.255.255"), _localPort)))
{
try
{
var length = 0;
var buffer = OSCConverter.Pack(new OSCMessage("/wsainit"), out length);
var buffer = OSCConverter.Pack(new OSCMessage("/wsainit"));

dataWriter.WriteBuffer(buffer.AsBuffer(0, length));
dataWriter.WriteBuffer(buffer.AsBuffer(0, buffer.Length));
await dataWriter.StoreAsync();
}
catch (Exception exception)
Expand All @@ -124,7 +119,7 @@ private async void InitMessage()
}

private void Receive(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
{
try
{
using (var dataReader = args.GetDataReader())
Expand All @@ -144,9 +139,9 @@ private void Receive(DatagramSocket sender, DatagramSocketMessageReceivedEventAr
{
Debug.LogErrorFormat("[OSCReceiver] Receive error: " + e);
}
}
}

#endregion
#endregion
}
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/extOSC/Scripts/Core/Network/OSCStandaloneManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Copyright (c) 2020 ExT (V.Sigalkin) */

#if !NETFX_CORE
#if !UNITY_WSA || UNITY_EDITOR

using System;
using System.Net;
Expand Down
38 changes: 19 additions & 19 deletions Assets/extOSC/Scripts/Core/Network/OSCTransmitterBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@

namespace extOSC.Core.Network
{
public abstract class OSCTransmitterBackend
{
#region Static Public Methods
public abstract class OSCTransmitterBackend
{
#region Static Public Methods

public static OSCTransmitterBackend Create()
{
#if NETFX_CORE
public static OSCTransmitterBackend Create()
{
#if UNITY_WSA && !UNITY_EDITOR
return new OSCTransmitterWindowsStoreBackend();
#else
return new OSCTransmitterStandaloneBackend();
return new OSCTransmitterStandaloneBackend();
#endif
}
}

#endregion
#endregion

#region Public Vars
#region Public Vars

public abstract bool IsAvailable { get; }
public abstract bool IsAvailable { get; }

#endregion
#endregion

#region Public Methods
#region Public Methods

public abstract void Connect(string localHost, int localPort);
public abstract void Connect(string localHost, int localPort);

public abstract void RefreshRemote(string remoteHost, int remotePort);
public abstract void RefreshRemote(string remoteHost, int remotePort);

public abstract void Close();
public abstract void Close();

public abstract void Send(byte[] data, int length);
public abstract void Send(byte[] data, int length);

#endregion
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Copyright (c) 2020 ExT (V.Sigalkin) */

#if !NETFX_CORE
#if !UNITY_WSA || UNITY_EDITOR

using UnityEngine;

Expand All @@ -14,12 +14,9 @@ internal class OSCTransmitterStandaloneBackend : OSCTransmitterBackend
{
#region Public Vars

public override bool IsAvailable
{
get { return _client != null; }
}
public override bool IsAvailable => _client != null;

#endregion
#endregion

#region Private Vars

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Copyright (c) 2020 ExT (V.Sigalkin) */

#if NETFX_CORE
#if UNITY_WSA && !UNITY_EDITOR

using UnityEngine;

Expand All @@ -13,14 +13,11 @@

namespace extOSC.Core.Network
{
internal class OSCTransmitterWindowsStoreBackend : OSCTransmitterBackend
internal class OSCTransmitterWindowsStoreBackend : OSCTransmitterBackend
{
#region Public Vars

public override bool IsStarted
{
get { return _datagramSocket != null; }
}
public override bool IsAvailable => _datagramSocket != null;

#endregion

Expand All @@ -36,7 +33,7 @@ public override bool IsStarted

#region Public Methods

public override void Connect(string localHost, int localPort)
public override void Connect(string localHost, int localPort)
{
if (_datagramSocket != null)
Close();
Expand All @@ -47,7 +44,7 @@ public override void Connect(string localHost, int localPort)
_datagramSocket = new DatagramSocket();
}

public override void RefreshRemote(string remoteHost, int remotePort)
public override void RefreshRemote(string remoteHost, int remotePort)
{
_remoteHost = new HostName(remoteHost);
_remotePort = remotePort.ToString();
Expand All @@ -72,7 +69,8 @@ public override void Send(byte[] data, int length)

private async void SendAsync(IBuffer buffer)
{
using (var dataWriter = new DataWriter(await _datagramSocket.GetOutputStreamAsync(_remoteHost, _remotePort)))
using (var dataWriter =
new DataWriter(await _datagramSocket.GetOutputStreamAsync(_remoteHost, _remotePort)))
{
try
{
Expand All @@ -84,7 +82,7 @@ private async void SendAsync(IBuffer buffer)
Debug.LogWarningFormat("[OSCTranmitter] Error: {0}", exception);
}
}
}
}

#endregion
}
Expand Down
Loading

0 comments on commit 40c9051

Please sign in to comment.