forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDirectPairing.cs
124 lines (105 loc) · 4.27 KB
/
DirectPairing.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using UnityEngine;
namespace HoloToolkit.Sharing.Utilities
{
/// <summary>
/// This class enables users to pair with a remote client directly.
/// One side should use the Receiver role, the other side should use the Connector role.
/// RemoteAddress and RemotePort are used by the Connector role, LocalPort is used by the Receiver.
/// </summary>
public class DirectPairing : MonoBehaviour
{
public enum Role
{
Connector,
Receiver
}
public Role PairingRole = Role.Connector;
public string RemoteAddress = "localhost";
public ushort RemotePort = 0x507B;
public ushort LocalPort = 0x507B;
public bool AutoReconnect = true;
private SharingManager sharingMgr;
private PairMaker pairMaker;
private PairingAdapter pairingAdapter;
private NetworkConnectionAdapter connectionAdapter;
private void Start()
{
if (PairingRole == Role.Connector)
{
pairMaker = new DirectPairConnector(RemoteAddress, RemotePort);
}
else
{
pairMaker = new DirectPairReceiver(LocalPort);
}
pairingAdapter = new PairingAdapter();
pairingAdapter.SuccessEvent += OnPairingConnectionSucceeded;
pairingAdapter.FailureEvent += OnPairingConnectionFailed;
// Register to listen for disconnections, so we can reconnect automatically
if (SharingStage.IsInitialized)
{
sharingMgr = SharingStage.Instance.Manager;
if (sharingMgr != null)
{
connectionAdapter = new NetworkConnectionAdapter();
connectionAdapter.DisconnectedCallback += OnDisconnected;
NetworkConnection pairedConnection = sharingMgr.GetPairedConnection();
pairedConnection.AddListener((byte)MessageID.StatusOnly, connectionAdapter);
}
}
StartPairing();
}
private void OnDestroy()
{
// SharingStage's OnDestroy() might execute first in the script order. Therefore we should check if
// SharingStage.Instance still exists. Without the instance check, the execution of GetPairingManager
// on a disposed sharing manager will crash the Unity Editor and application.
if (SharingStage.IsInitialized && sharingMgr != null)
{
PairingManager pairingMgr = sharingMgr.GetPairingManager();
pairingMgr.CancelPairing(); // Safe to call, even if no pairing is in progress. Will not cause a disconnect
// Remove our listener from the paired connection
NetworkConnection pairedConnection = sharingMgr.GetPairedConnection();
pairedConnection.RemoveListener((byte)MessageID.StatusOnly, connectionAdapter);
}
}
private void OnPairingConnectionSucceeded()
{
if (SharingStage.Instance.ShowDetailedLogs)
{
Debug.Log("Direct Pairing Succeeded");
}
}
private void OnPairingConnectionFailed(PairingResult result)
{
Debug.LogWarning("Direct pairing failed: " + result);
if (AutoReconnect)
{
Debug.LogWarning("Attempting to reconnect...");
StartPairing();
}
}
private void OnDisconnected(NetworkConnection connection)
{
Debug.LogWarning("Remote client disconnected");
if (AutoReconnect)
{
StartPairing();
}
}
private void StartPairing()
{
if (sharingMgr != null)
{
PairingManager pairingMgr = sharingMgr.GetPairingManager();
PairingResult result = pairingMgr.BeginPairing(pairMaker, pairingAdapter);
if (result != PairingResult.Ok)
{
Debug.LogError("Failed to start pairing");
}
}
}
}
}