TrackerRole TryToBindTracker(string serialNumber, int trackerNumber)
        {
            TrackerRole role = TrackerRole.Invalid;

            // try to bind tracker from serial number
            if (!String.IsNullOrEmpty(serialNumber))
            {
                role = ViveRole.GetMap <TrackerRole>().GetBoundRoleByDevice(serialNumber);
            }

            // if failed, try to bind tracker from tracker number
            if (trackerNumber > 0 && trackerNumber < Enum.GetNames(typeof(TrackerRole)).Length)
            {
                role = (TrackerRole)trackerNumber;
            }

            // check repeted role
            if (role != TrackerRole.Invalid)
            {
                for (int i = 0; i < connectionDataList.Count; i++)
                {
                    if (connectionDataList[i].trackerRole == role)
                    {
                        role = TrackerRole.Invalid;
                        break;
                    }
                }
            }

            return(role);
        }
        public TrackerRole GetPlayerTrackerRole(int id)
        {
            TrackerRole result = TrackerRole.Invalid;

            ConnectionData data;

            if (connectionDataMap.TryGetValue(id, out data))
            {
                result = data.trackerRole;
            }

            return(result);
        }
        //-----------------------------------------------------------------------------
        // tracker assignment
        //-----------------------------------------------------------------------------

        internal void OnTrakcerResitryRequest(NetworkMessage netMsg)
        {
            // parse message
            ViveTrackerRegistryRequestMsg msg = netMsg.ReadMessage <ViveTrackerRegistryRequestMsg>();

            // check connectivity
            int            connId   = netMsg.conn.connectionId;
            ConnectionData connData = GetConnectionData(connId);

            if (connData == null)
            {
                return;
            }

            // if need to assign a tracker to client
            string trackerPoseObjName = "";

            if (msg.needTrackerPose)
            {
                TrackerRole trackerRole = TryToBindTracker(msg.serialNumber, msg.trackerNumber);

                // if failed, tell client to disconnect
                if (trackerRole == TrackerRole.Invalid)
                {
                    ViveShare_Log.LogError(logLevel, "[ViveShare_Server] Failed to assign tracker for client (" + netMsg.conn.connectionId + ")");
                    netMsg.conn.Disconnect();
                    return;
                }
                // if succeeded, save assigned role and initialize sync pose object
                else
                {
                    ViveShare_Log.LogInfo(logLevel, "[ViveShare_Server] Assign tracker (" + trackerRole.ToString() + ") to client (" + netMsg.conn.connectionId + ")");

                    connData.trackerRole = trackerRole;
                    trackerPoseObjName   = trackerRole.ToString();

                    // create pose tracker and attach sync param object to it
                    GameObject      trackerObj  = new GameObject(trackerPoseObjName);
                    VivePoseTracker poseTracker = trackerObj.AddComponent <VivePoseTracker>();
                    poseTracker.viveRole.SetEx <TrackerRole>(trackerRole);

                    ViveShare_SyncIdentity syncId = trackerObj.AddComponent <ViveShare_SyncIdentity>();
                    syncId.id           = trackerPoseObjName;
                    syncId.hasAuthority = true;
                    syncId.Register();

                    trackerObj.AddComponent <ViveShare_SyncPose>();

                    if (cameraRig != null)
                    {
                        trackerObj.transform.parent = cameraRig.transform;
                    }

                    connData.trackerObject = trackerObj;
                }
            }

            // return "finished" message
            ViveTrackerRegistryReturnMsg returnMsg = new ViveTrackerRegistryReturnMsg();

            returnMsg.syncObjName = trackerPoseObjName;
            returnMsg.clientId    = connId;

            NetworkServer.SendToClient(netMsg.conn.connectionId, ViveShareMsgType.TrackerRegistryFinished, returnMsg);

            // invoke callbacks -- we only think the connection is established at this moment
            ViveShare_Event.server_clientConnectedEvent.Invoke(netMsg.conn.connectionId);
        }