Example #1
0
    // Update is called once per frame.
    void Update()
    {
        if (!_isStarted)
        {
            return;
        }

        // If we have a connected client, presumably the client wants to send some meshes.
        if (ClientConnected)
        {
            if (Global.clientStatus == Global.netStatus.NotConnected)
            {
                Global.clientStatus = Global.netStatus.Ready;
            }

            // Get the clients stream.
            NetworkStream stream = networkClient.GetStream();

            // Make sure there is data in the stream.
            if (stream.DataAvailable)
            {
                // The first 4 bytes will be the size of the data containing the mesh(es).
                int datasize = ReadInt(stream);

                // Allocate a buffer to hold the data.
                byte[] dataBuffer = new byte[datasize];

                // Read the data.
                // The data can come in chunks.
                int readsize = 0;

                while (readsize != datasize)
                {
                    readsize += stream.Read(dataBuffer, readsize, datasize - readsize);
                }

                if (readsize != datasize)
                {
                    Debug.Log("reading data failed: " + readsize + " != " + datasize);
                }

                //print out the size of the packet for debugging purposes
                //UnityEngine.Debug.Log("size of packet: " + dataBuffer.Length);


                // DONE READING IN THE DATA AT THIS POINT --
                // NOW DO STUFF WITH THE DATA...
                //
                //dataBuffer variable holds the data to decode from byte[]
                interpretIncomingPackage(dataBuffer, datasize);

                // Finally disconnect.
                ClientConnected = false;
                networkClient.Close();

                // And wait for the next connection asynchronously.
                AsyncCallback callback = new AsyncCallback(OnClientConnect);
                networkListener.BeginAcceptTcpClient(callback, this);
            }
        }

        if (Input.GetKeyDown("s")) // Save
        {
            MeshSaver.Save("MeshSave", globalMeshes);
        }
        else if (Input.GetKeyDown("l")) // Load
        {
            globalMeshes = (List <Mesh>)MeshSaver.Load("MeshSave");
            for (int index = 0; index < globalMeshes.Count; index++)
            {
                GameObject surface = AddSurfaceObject(globalMeshes[index], string.Format("Beamed-{0}", surfaceObjects.Count), transform);
                surface.transform.parent = SpatialMappingManager.Instance.transform;
                surface.GetComponent <MeshRenderer>().enabled           = true;
                surface.GetComponent <MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
            }
        }

        // *******
        // The rest of the Update() look handles recieving data from the
        //  Non-Hololens clients. Both Hololens and Non-Hololens clients
        //  messages go through interpretIncomingPackage()
        // *******
        byte[] recBuffer = new byte[bufferSize];

        NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);

        switch (recData)
        {
        case NetworkEventType.Nothing:
            break;

        case NetworkEventType.ConnectEvent:
        {
            Global.connectionIDs.Add(connectionId);
            Debug.Log(String.Format("Connection to host {0}, connection {1}", recHostId, connectionId));
            break;
        }

        case NetworkEventType.DataEvent:
        {
            // Strip out the sent message size
            int messageSize = BitConverter.ToInt32(recBuffer, 0);
            // Create an array of that size
            byte[] messageData = new byte[messageSize - 4];
            // Copy the data we have into said array
            System.Buffer.BlockCopy(recBuffer, 4, messageData, 0, dataSize - 4);

            // While we haven't recieved all data..
            int givenDataSize = dataSize;
            while (givenDataSize < messageSize)
            {
                // Recieve more, put it into the messageData array, add to our size, repeat..
                NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
                System.Buffer.BlockCopy(recBuffer, 0, messageData, givenDataSize, dataSize);
                givenDataSize += dataSize;
            }

            if (messageSize < givenDataSize)
            {
                Debug.LogError("Recieved more bytes than sent by client! Recieved " + givenDataSize + " bytes, expected " + messageSize + " bytes");
            }
            //Debug.Log(String.Format("Received event and Sent message: host {0}, connection {1}, message length {2}", recHostId, connectionId, messageData.Length));

            // Now, send that data along to the interpret function
            interpretIncomingPackage(messageData, messageData.Length);

            // From here, forward the message to all other clients (incl. Hololens)?
            Global.AddForwardMessage(messageData, connectionId);
        }
        break;

        case NetworkEventType.DisconnectEvent:
        {
            Debug.Log(String.Format("Disconnect from host {0}, connection {1}", recHostId, connectionId));
            Global.connectionIDs.Remove(connectionId);

            // Delete the Vive avatar for whichever Vive machine is currently disconnecting
            for (int i = 0; i < ViveMachines.Count; i++)
            {
                if (ViveMachines[i].connectionID == connectionId)
                {
                    Global.DeleteObject(ViveMachines[i].headsetID);
                    Global.DeleteObject(ViveMachines[i].leftControllerID);
                    Global.DeleteObject(ViveMachines[i].rightControllerID);
                    ViveMachines.RemoveAt(i);
                    break;
                }
            }

            break;
        }
        }
    }