Example #1
0
    // Update is called once per frame
    private void Update()
    {
        if (_hasReceivedModel)
        {
            // check for command
            VoxelCommand cmd;
            if (Client.TryReceiveTcp <VoxelCommand>(out cmd))
            {
                cmd.Apply(_voxelController);
            }
        }
        else
        {
            // check for model
            SerializedVoxelData data;
            _hasReceivedModel = Client.TryReceiveTcp <SerializedVoxelData>(out data);

            if (_hasReceivedModel)
            {
                // render for first time
                VoxelData voxelData = VoxelSerializer.DeserializeVoxelData(data);
                _voxelController.ChangeData(voxelData);
            }
        }
    }
Example #2
0
    private static void HandleTcpClient(Socket clientTcpSocket)
    {
        // receive UDP port
        int clientUdpPort;

        if (!Protocol.Receive(clientTcpSocket, out clientUdpPort))
        {
            CleanupClient(clientTcpSocket);
            return;
        }
        string clientHost = ((IPEndPoint)clientTcpSocket.RemoteEndPoint).Address.ToString();

        _logger.Log(string.Format("received UDP client at {0}:{1}", clientHost, clientUdpPort));

        lock (_clientUdpAddresses) {
            // add new UDP address to list
            _clientUdpAddresses.Add(new Address(clientHost, clientUdpPort));
        }

        lock (_coarseLock) {
            // send model
            _logger.Log("sending model to client...");
            Protocol.Send(clientTcpSocket, VoxelSerializer.SerializeVoxelData(_voxelData));
            _logger.Log("...sent model to client");

            // add client to sockets
            _clientSockets.Add(clientTcpSocket);
        }

        while (true)
        {
            // deserialize message to command
            object obj;
            if (!Protocol.Receive(clientTcpSocket, out obj))
            {
                CleanupClient(clientTcpSocket);
                return;
            }
            _logger.Log("TCP received object from client");

            lock (_coarseLock) {
                // apply command to model
                if (obj.GetType() == typeof(VoxelCommand))
                {
                    ((VoxelCommand)obj).Apply(_voxelData);
                }

                // broadcast command to other clients
                _logger.Log("TCP broadcasting object to clients...");
                foreach (Socket socket in _clientSockets)
                {
                    Protocol.Send(socket, obj);
                }
                _logger.Log("TCP ...broadcasted object to clients");
            }
        }
    }
Example #3
0
 public void SaveToFile(string filepath)
 {
     if (filepath.EndsWith(".obj", System.StringComparison.InvariantCultureIgnoreCase))
     {
         VoxelSerializer.VoxelMeshToObjFile(filepath, _voxelRenderer.MeshFilter);
     }
     else
     {
         VoxelSerializer.VoxelDataToFile(filepath, _voxelData);
     }
 }
Example #4
0
    private void Awake()
    {
        ServerButton.onClick.AddListener(() =>
        {
            // try to start the server
            VoxelData voxelData = null;
            try {
                voxelData = VoxelSerializer.VoxelDataFromFile(LoadPath);
            } catch (Exception) {
                voxelData = null;
            }
            if (voxelData == null)
            {
                voxelData = new VoxelData();
            }
            try {
                Server.Start(Port, Port, Config.SERVER_LOG_FILE, voxelData);
            } catch (Exception e) {
                WriteToErrorBox(e.ToString());
            }

            // this thread does nothing now...
        });

        ClientButton.onClick.AddListener(() =>
        {
            // try to start the client
            try {
                Client.Start(IpAddress, Port, Port, Config.CLIENT_LOG_FILE);
            } catch (Exception e) {
                WriteToErrorBox(e.ToString());
                return;
            }

            // load editor if client successfully starts
            Application.LoadLevel(1);
        });

        ExitButton.onClick.AddListener(Application.Quit);
    }
Example #5
0
 /// <summary>
 /// Only load from file before the scene is live (before the user can edit the model)
 /// </summary>
 public void LoadFromFile(string filepath)
 {
     _voxelData = VoxelSerializer.VoxelDataFromFile(filepath);
     _voxelRenderer.RenderMesh(_voxelData);
 }