Skip to content

Commit

Permalink
add events.
Browse files Browse the repository at this point in the history
  • Loading branch information
hecomi committed Nov 28, 2021
1 parent d5caaea commit ef7d9d5
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 24 deletions.
21 changes: 21 additions & 0 deletions Assets/uOSC/Runtime/Core/Event.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using UnityEngine.Events;

namespace uOSC
{

[System.Serializable]
public class ServerStartEvent : UnityEvent<int> {};

[System.Serializable]
public class ServerStopEvent : UnityEvent<int> {};

[System.Serializable]
public class ClientStartEvent : UnityEvent<string, int> {};

[System.Serializable]
public class ClientStopEvent : UnityEvent<string, int> {};

[System.Serializable]
public class DataReceiveEvent : UnityEvent<Message> {};

}
11 changes: 11 additions & 0 deletions Assets/uOSC/Runtime/Core/Event.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Assets/uOSC/Runtime/uOscClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class uOscClient : MonoBehaviour
Queue<object> messages_ = new Queue<object>();
object lockObject_ = new object();

public ClientStartEvent onClientStarted = new ClientStartEvent();
public ClientStopEvent onClientStopped = new ClientStopEvent();

string address_ = "";
int port_ = 0;

Expand All @@ -49,12 +52,14 @@ public void StartClient()
thread_.Start(UpdateSend);
address_ = address;
port_ = port;
onClientStarted.Invoke(address, port);
}

public void StopClient()
{
thread_.Stop();
udp_.Stop();
onClientStopped.Invoke(address, port);
}

void Update()
Expand Down
19 changes: 7 additions & 12 deletions Assets/uOSC/Runtime/uOscServer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using UnityEngine;
using UnityEngine.Events;

namespace uOSC
{
Expand All @@ -21,8 +20,9 @@ public class uOscServer : MonoBehaviour
#endif
Parser parser_ = new Parser();

public class DataReceiveEvent : UnityEvent<Message> {};
public DataReceiveEvent onDataReceived { get; private set; } = new DataReceiveEvent();
public DataReceiveEvent onDataReceived = new DataReceiveEvent();
public ServerStartEvent onServerStarted = new ServerStartEvent();
public ServerStopEvent onServerStopped = new ServerStopEvent();

int port_ = 0;
bool isStarted_ = false;
Expand Down Expand Up @@ -50,15 +50,6 @@ void OnDisable()
StopServer();
}

public void SetPort(int port)
{
if (this.port == port) return;

this.port = port;
StopServer();
StartServer();
}

public void StartServer()
{
if (isStarted_) return;
Expand All @@ -67,6 +58,8 @@ public void StartServer()
thread_.Start(UpdateMessage);

isStarted_ = true;

onServerStarted.Invoke(port);
}

public void StopServer()
Expand All @@ -77,6 +70,8 @@ public void StopServer()
udp_.Stop();

isStarted_ = false;

onServerStopped.Invoke(port);
}

void Update()
Expand Down
30 changes: 30 additions & 0 deletions Assets/uOSC/Samples/Bundle/Bundle.unity
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ MonoBehaviour:
m_EditorClassIdentifier:
address: 127.0.0.1
port: 3333
maxQueueSize: 100
dataTransimissionInterval: 0
onClientStarted:
m_PersistentCalls:
m_Calls: []
onClientStopped:
m_PersistentCalls:
m_Calls: []
--- !u!4 &857163816
Transform:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -225,6 +233,28 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
port: 3333
autoStart: 1
onDataReceived:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 1608706208}
m_TargetAssemblyTypeName: uOSC.Samples.ServerTest, Assembly-CSharp
m_MethodName: OnDataReceived
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
onServerStarted:
m_PersistentCalls:
m_Calls: []
onServerStopped:
m_PersistentCalls:
m_Calls: []
--- !u!4 &1608706210
Transform:
m_ObjectHideFlags: 0
Expand Down
78 changes: 78 additions & 0 deletions Assets/uOSC/Samples/Client to Server/Client to Server.unity
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,38 @@ MonoBehaviour:
m_EditorClassIdentifier:
address: 127.0.0.1
port: 3333
maxQueueSize: 100
dataTransimissionInterval: 0
onClientStarted:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 857163814}
m_TargetAssemblyTypeName: ClientTest, Assembly-CSharp
m_MethodName: OnClientStarted
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
onClientStopped:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 857163814}
m_TargetAssemblyTypeName: ClientTest, Assembly-CSharp
m_MethodName: OnClientStopped
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!4 &857163816
Transform:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -225,6 +257,52 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
port: 3333
autoStart: 1
onDataReceived:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 1608706208}
m_TargetAssemblyTypeName: ServerTest, Assembly-CSharp
m_MethodName: OnDataReceived
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
onServerStarted:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 1608706208}
m_TargetAssemblyTypeName: uOSC.Samples.ServerTest, Assembly-CSharp
m_MethodName: OnServerStarted
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
onServerStopped:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 1608706208}
m_TargetAssemblyTypeName: uOSC.Samples.ServerTest, Assembly-CSharp
m_MethodName: OnServerStopped
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!4 &1608706210
Transform:
m_ObjectHideFlags: 0
Expand Down
32 changes: 32 additions & 0 deletions Assets/uOSC/Samples/Client to Server/Client.unity
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,38 @@ MonoBehaviour:
m_EditorClassIdentifier:
address: 127.0.0.1
port: 3333
maxQueueSize: 100
dataTransimissionInterval: 0
onClientStarted:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 101573259}
m_TargetAssemblyTypeName: ClientTest, Assembly-CSharp
m_MethodName: OnClientStarted
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
onClientStopped:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 101573259}
m_TargetAssemblyTypeName: ClientTest, Assembly-CSharp
m_MethodName: OnClientStopped
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!4 &101573258
Transform:
m_ObjectHideFlags: 0
Expand Down
10 changes: 10 additions & 0 deletions Assets/uOSC/Samples/Client to Server/ClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@ void Update()
var client = GetComponent<uOscClient>();
client.Send("/uOSC/test", 10, "hoge", "hogehoge", 1.234f, 123f);
}

public void OnClientStarted(string address, int port)
{
Debug.Log($"<color=red>Start Client (address: {address}, port: {port})</color>");
}

public void OnClientStopped(string address, int port)
{
Debug.Log($"<color=red>Stop Client (address: {address}, port: {port})</color>");
}
}
46 changes: 46 additions & 0 deletions Assets/uOSC/Samples/Client to Server/Server.unity
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,52 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
port: 3333
autoStart: 1
onDataReceived:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 2020941922}
m_TargetAssemblyTypeName: uOSC.Samples.ServerTest, Assembly-CSharp
m_MethodName: OnDataReceived
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
onServerStarted:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 2020941922}
m_TargetAssemblyTypeName: uOSC.Samples.ServerTest, Assembly-CSharp
m_MethodName: OnServerStarted
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
onServerStopped:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 2020941922}
m_TargetAssemblyTypeName: uOSC.Samples.ServerTest, Assembly-CSharp
m_MethodName: OnServerStopped
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!4 &2020941921
Transform:
m_ObjectHideFlags: 0
Expand Down
25 changes: 16 additions & 9 deletions Assets/uOSC/Samples/Client to Server/ServerTest.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
using UnityEngine;
using uOSC;

[RequireComponent(typeof(uOscServer))]
public class ServerTest : MonoBehaviour
namespace uOSC.Samples
{
void Start()
{
var server = GetComponent<uOscServer>();
server.onDataReceived.AddListener(OnDataReceived);
}

void OnDataReceived(Message message)
public class ServerTest : MonoBehaviour
{
public void OnDataReceived(Message message)
{
// address
var msg = message.address + ": ";
Expand All @@ -26,4 +21,16 @@ void OnDataReceived(Message message)

Debug.Log(msg);
}

public void OnServerStarted(int port)
{
Debug.Log($"<color=blue>Start Server (port: {port})</color>");
}

public void OnServerStopped(int port)
{
Debug.Log($"<color=blue>Stop Server (port: {port})</color>");
}
}

}
Loading

0 comments on commit ef7d9d5

Please sign in to comment.