AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
DirectPairing.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 
6 namespace HoloToolkit.Sharing.Utilities
7 {
13  public class DirectPairing : MonoBehaviour
14  {
15  public enum Role
16  {
17  Connector,
18  Receiver
19  }
20 
21  public Role PairingRole = Role.Connector;
22  public string RemoteAddress = "localhost";
23  public ushort RemotePort = 0x507B;
24  public ushort LocalPort = 0x507B;
25  public bool AutoReconnect = true;
26 
27  private SharingManager sharingMgr;
28  private PairMaker pairMaker;
29  private PairingAdapter pairingAdapter;
30  private NetworkConnectionAdapter connectionAdapter;
31 
32  private void Start()
33  {
34  if (PairingRole == Role.Connector)
35  {
36  pairMaker = new DirectPairConnector(RemoteAddress, RemotePort);
37  }
38  else
39  {
40  pairMaker = new DirectPairReceiver(LocalPort);
41  }
42 
43  pairingAdapter = new PairingAdapter();
44  pairingAdapter.SuccessEvent += OnPairingConnectionSucceeded;
45  pairingAdapter.FailureEvent += OnPairingConnectionFailed;
46 
47  // Register to listen for disconnections, so we can reconnect automatically
49  {
50  sharingMgr = SharingStage.Instance.Manager;
51 
52  if (sharingMgr != null)
53  {
54  connectionAdapter = new NetworkConnectionAdapter();
55  connectionAdapter.DisconnectedCallback += OnDisconnected;
56 
57  NetworkConnection pairedConnection = sharingMgr.GetPairedConnection();
58  pairedConnection.AddListener((byte)MessageID.StatusOnly, connectionAdapter);
59  }
60  }
61 
62  StartPairing();
63  }
64 
65  private void OnDestroy()
66  {
67  // SharingStage's OnDestroy() might execute first in the script order. Therefore we should check if
68  // SharingStage.Instance still exists. Without the instance check, the execution of GetPairingManager
69  // on a disposed sharing manager will crash the Unity Editor and application.
70  if (SharingStage.IsInitialized && sharingMgr != null)
71  {
72  PairingManager pairingMgr = sharingMgr.GetPairingManager();
73  pairingMgr.CancelPairing(); // Safe to call, even if no pairing is in progress. Will not cause a disconnect
74 
75  // Remove our listener from the paired connection
76  NetworkConnection pairedConnection = sharingMgr.GetPairedConnection();
77  pairedConnection.RemoveListener((byte)MessageID.StatusOnly, connectionAdapter);
78  }
79  }
80 
81  private void OnPairingConnectionSucceeded()
82  {
83  if (SharingStage.Instance.ShowDetailedLogs)
84  {
85  Debug.Log("Direct Pairing Succeeded");
86  }
87  }
88 
89  private void OnPairingConnectionFailed(PairingResult result)
90  {
91  Debug.LogWarning("Direct pairing failed: " + result);
92 
93  if (AutoReconnect)
94  {
95  Debug.LogWarning("Attempting to reconnect...");
96  StartPairing();
97  }
98  }
99 
100  private void OnDisconnected(NetworkConnection connection)
101  {
102  Debug.LogWarning("Remote client disconnected");
103 
104  if (AutoReconnect)
105  {
106  StartPairing();
107  }
108  }
109 
110  private void StartPairing()
111  {
112  if (sharingMgr != null)
113  {
114  PairingManager pairingMgr = sharingMgr.GetPairingManager();
115 
116  PairingResult result = pairingMgr.BeginPairing(pairMaker, pairingAdapter);
117  if (result != PairingResult.Ok)
118  {
119  Debug.LogError("Failed to start pairing");
120  }
121  }
122  }
123  }
124 }
Allows users of the pairing API to register to receive pairing event callbacks without having their c...
virtual PairingResult BeginPairing(PairMaker pairMaker, PairingListener listener)
virtual void RemoveListener(byte messageType, NetworkConnectionListener oldListener)
System.Action< PairingResult > FailureEvent
This class enables users to pair with a remote client directly. One side should use the Receiver role...
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
virtual PairingManager GetPairingManager()
Allows users of NetworkConnection to register to receive event callbacks without having their classes...
The SharingStage is in charge of managing the core networking layer for the application.
Definition: SharingStage.cs:14
virtual NetworkConnection GetPairedConnection()
System.Action< NetworkConnection > DisconnectedCallback
static bool IsInitialized
Returns whether the instance has been initialized or not.
Definition: Singleton.cs:58
virtual void AddListener(byte messageType, NetworkConnectionListener newListener)