Ejemplo n.º 1
0
        /// <summary>
        /// Sends the data over the network.
        /// </summary>
        /// <param name="dataBufferToSend">The data buffer to send.</param>
        private void SendDataOverNetwork(byte[] dataBufferToSend)
        {
            if (Sending)
            {
                // This shouldn't happen, but just in case.
                Debug.Log("one at a time please");
                return;
            }

            // Track that we are sending a data buffer.
            Sending = true;

            // Set the next buffer to send when the connection is made.
            nextDataBufferToSend = dataBufferToSend;

            // Setup a connection to the server.
            HostName networkHost = new HostName(ServerIP.Trim());

            networkConnection = new StreamSocket();

            // Connections are asynchronous.
            // !!! NOTE These do not arrive on the main Unity Thread. Most Unity operations will throw in the callback !!!
            IAsyncAction outstandingAction   = networkConnection.ConnectAsync(networkHost, ConnectionPort.ToString());
            AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(NetworkConnectedHandler);

            outstandingAction.Completed = aach;
        }
Ejemplo n.º 2
0
    public void UWPSend(string p_stringToSend)
    {
        stringToSend = p_stringToSend;
        // By default 'HostNameForConnect' is disabled and host name validation is not required. When enabling the
        // text box validating the host name is required since it was received from an untrusted source
        // (user input). The host name is validated by catching ArgumentExceptions thrown by the HostName
        // constructor for invalid input.
        // Note that when enabling the text box users may provide names for hosts on the Internet that require the
        // "Internet (Client)" capability.
        HostName hostName;

        try
        {
            hostName = new HostName(strhostname);
        }
        catch (ArgumentException)
        {
            Debug.Log("Hostname Error");
            return;
        }

        socket2 = new StreamSocket();
        try
        {
            // Connections are asynchronous.
            // !!! NOTE These do not arrive on the main Unity Thread. Most Unity operations will throw in the callback !!!
            IAsyncAction outstandingAction   = socket2.ConnectAsync(hostName, strport);
            AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(NetworkConnectedHandler);
            outstandingAction.Completed = aach;
        }
        catch (Exception exception)
        {
            Debug.Log("Connect Async exception");
        }
    }
Ejemplo n.º 3
0
    public void Connect(string address)
    {
        host = address;
        //Async connection.
        if (!con && !busy)
        {
            busy = true;
            Debug.Log("connecting");
            Debug.Log(port);
#if UNITY_EDITOR
            runThread = new Thread(Run);
            runThread.Start();
#endif

# if !UNITY_EDITOR

            messageWebSocket = new MessageWebSocket();
            messageWebSocket.Control.MessageType = SocketMessageType.Utf8;
            messageWebSocket.MessageReceived += Win_MessageReceived;

            server = new Uri("ws://" + host + ":" + port.ToString());

            IAsyncAction outstandingAction = messageWebSocket.ConnectAsync(server);
            AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(NetworkConnectedHandler);
            outstandingAction.Completed = aach;

#endif
        }
Ejemplo n.º 4
0
 public static void TrackAsyncAction(IAsyncAction action, AsyncActionCompletedHandler completed)
 {
     DisableView(null);
     action.Completed = (s, e) => DispatcherHelper.BeginInvokeOnUIThread(() =>
     {
         EnableView();
         completed(s, e);
     });
 }
Ejemplo n.º 5
0
 public static void TrackAsyncAction(IAsyncAction action, AsyncActionCompletedHandler completed)
 {
     DisableView(null);
     action.Completed = (s, e) => root.Dispatcher.Begin(() =>
     {
         EnableView();
         completed(s, e);
     });
 }
Ejemplo n.º 6
0
    /// <summary>
    /// Sends the data over the network.
    /// </summary>
    /// <param name="dataBufferToSend">The data buffer to send.</param>
    private void SendDataOverNetwork(byte[] dataBufferToSend)
    {
        if (Sending)
        {
            // This shouldn't happen, but just in case.
            Debug.Log("one at a time please");
            return;
        }

        // Track that we are sending a data buffer.
        Sending = true;

        // Set the next buffer to send when the connection is made.
        nextDataBufferToSend = dataBufferToSend;

        // Setup a connection to the server.
#if !UNITY_EDITOR
        HostName networkHost = new HostName(ServerIP.Trim());
        networkConnection = new StreamSocket();
        // Connections are asynchronous.
        // !!! NOTE These do not arrive on the main Unity Thread. Most Unity operations will throw in the callback !!!
        IAsyncAction outstandingAction   = networkConnection.ConnectAsync(networkHost, ConnectionPort.ToString());
        AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(NetworkConnectedHandler);
        outstandingAction.Completed = aach;
#else
        try
        {
            IPAddress ipAddress = IPAddress.Parse(ServerIP);

            // Convert the string data to byte data using ASCII encoding.

            TcpClient client = new TcpClient();

            client.BeginConnect(ipAddress, ConnectionPort,
                                new AsyncCallback(ConnectCallback), client);
            connectDone.WaitOne(100);

            NetworkStream netStream = client.GetStream();
            // Write how much data we are sending.
            netStream.WriteByte((byte)dataBufferToSend.Length);

            // Then write the data.
            netStream.Write(dataBufferToSend, 0, dataBufferToSend.Length);

            Sending = false;
            // Always disconnect here since we will reconnect when sending the next message
            client.Close();
        }
        catch (Exception e)
        {
            Sending = false;
            Debug.Log(e.ToString());
        }
#endif
    }
Ejemplo n.º 7
0
    //void DataReceivedHandler(IAsyncOperation<uint> operation, AsyncStatus status)
    //{
    //    Debug.Log("DataReceivedHandler Started");
    //    if (status == AsyncStatus.Error)
    //    {
    //        Debug.Log("Connection Lost");
    //        ConnectToServer();
    //    }
    //    else
    //    {
    //        string recv = "";
    //        while (reader.UnconsumedBufferLength > 0)
    //        {
    //            recv = reader.ReadString(reader.UnconsumedBufferLength);
    //            //await reader.LoadAsync(256);
    //        }
    //        Connecting = false;
    //        //Debug.Log("Received String is: " + System.Text.Encoding.ASCII.GetString(data));
    //        Debug.Log("Received String is: " + recv);
    //        DataWriter writer = new DataWriter(networkConnection.OutputStream);
    //        string response = "Data Received with " + recv.Length + "Characters";
    //        //byte[] responseBytes = System.Text.Encoding.ASCII.GetBytes(response);
    //        //writer.WriteInt32(responseBytes.Length);
    //        //writer.WriteBytes(responseBytes);

    //        //DataWriterStoreOperation dwso = writer.StoreAsync();
    //        //dwso.Completed
    //        //networkConnection.Dispose();
    //    }
    //    //Debug.Log("Received String is: " + System.Text.Encoding.ASCII.GetString(data));
    //    //networkConnection.Dispose();
    //}

    void ConnectToServer()
    {
        Connecting = true;
        Debug.Log("try connecting now");
        networkConnection = new StreamSocket();
        HostName     networkHost         = new HostName(this.ServerIP);
        IAsyncAction outstandingAcion    = networkConnection.ConnectAsync(networkHost, this.ConnectionPort.ToString());
        AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(NetworkConnectedHandler);

        outstandingAcion.Completed = aach;
    }
        public override void ReceiveBundles(string networkConfig)
        {
            Debug.Log("Master client has created a listener and would like us to receive bundle");

            holoClient = new StreamSocket();
            string[] networkConfigArray = networkConfig.Split(':');
            connection = holoClient.ConnectAsync(new HostName(networkConfigArray[0]), networkConfigArray[1]);
            var aach = new AsyncActionCompletedHandler(NetworkConnectedHandlerBundles);

            connection.Completed = aach;
        }
Ejemplo n.º 9
0
    public void Connect(string ServerIP, string ServerPort)
    {
        IsConnected = false;
        encoder     = new System.Text.ASCIIEncoding();
        socket      = new StreamSocket();

        socket.Control.QualityOfService = SocketQualityOfService.LowLatency;

        HostName     networkHost         = new HostName(ServerIP.Trim());
        IAsyncAction outstandingAction   = socket.ConnectAsync(networkHost, ServerPort);
        AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(NetworkConnectedHandler);

        outstandingAction.Completed = aach;
    }
Ejemplo n.º 10
0
        public void Start()
        {
            currentReceivedMessage = null;
            // Setup a connection to the server.
            HostName networkHost = new HostName(ServerIP.Trim());

            networkConnection = new StreamSocket();

            // Connections are asynchronous.
            // !!! NOTE These do not arrive on the main Unity Thread. Most Unity operations will throw in the callback !!!
            IAsyncAction outstandingAction   = networkConnection.ConnectAsync(networkHost, ConnectionPort.ToString());
            AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(NetworkConnectedHandler);

            outstandingAction.Completed = aach;
        }
Ejemplo n.º 11
0
    /// <summary>
    /// opens a socket connection as client.
    /// </summary>
    private void OpenConnection()
    {
        // Setup a connection to the server.
        HostName networkHost = new HostName(ServerIP.Trim());

        socketConnection = new StreamSocket();

        Debug.Log("Attempting to connect");

        // Connections are asynchronous. This callback function 'SocketOpenedHandler' will be called when the connection is established
        IAsyncAction outstandingAction   = socketConnection.ConnectAsync(networkHost, ConnectionPort.ToString());
        AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(SocketOpenedHandler);

        outstandingAction.Completed = aach;
    }
Ejemplo n.º 12
0
    public void Connect()
    {
        //Async connection.
#if UNITY_EDITOR
        Thread = new Thread(Run);
        Thread.Start();
#endif

#if !UNITY_EDITOR
        messageWebSocket = new MessageWebSocket();

        server = new Uri("ws://" + host + ":" + port.ToString());

        IAsyncAction outstandingAction   = messageWebSocket.ConnectAsync(server);
        AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(NetworkConnectedHandler);
        outstandingAction.Completed = aach;
#endif
    }
Ejemplo n.º 13
0
    /// <summary>
    /// Connects to the server and requests data.
    /// </summary>
    private void ConnectListener()
    {
        if (waitingForConnection)
        {
            return;
        }

        Debug.Log("Connecting to " + serverIP);
        waitingForConnection = true;
        HostName networkHost = new HostName(serverIP);

        networkConnection = new StreamSocket();

        IAsyncAction outstandingAction   = networkConnection.ConnectAsync(networkHost, SendConnectionPort.ToString());
        AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(RcvNetworkConnectedHandler);

        outstandingAction.Completed = aach;
    }
Ejemplo n.º 14
0
        /**
         * Connect to the remote ros environment.
         */
        public void Connect()
        {
#if UNITY_EDITOR
            _myThread = new System.Threading.Thread(Run);
            _myThread.Start();
#endif

#if !UNITY_EDITOR
            messageWebSocket = new MessageWebSocket();

            server = new Uri(_host + ":" + _port.ToString());

            //set handler for MessageReceived and parse string received from socket
            messageWebSocket.MessageReceived += (sender, e) => this.OnMessage(e.GetDataReader().ReadString(e.GetDataReader().UnconsumedBufferLength));
            IAsyncAction asyncAction = messageWebSocket.ConnectAsync(server);
            AsyncActionCompletedHandler asyncActionCompletedHandler = new AsyncActionCompletedHandler(NetworkConnectedHandler);
            asyncAction.Completed = asyncActionCompletedHandler;
#endif
        }
Ejemplo n.º 15
0
    /// <summary>
    /// Connects to the server and requests data.
    /// </summary>
    private void ConnectListener()
    {
        if (frameReceiverStatus == FrameReceiverStatus.WaitingForConnection ||
            frameReceiverStatus == FrameReceiverStatus.ToDefer ||
            frameReceiverStatus == FrameReceiverStatus.Looping)
        {
            Debug.Log(TAG + ": ConnectListener() not supported at current status");
            return;
        }

        frameReceiverStatus = FrameReceiverStatus.WaitingForConnection;
        Debug.Log(TAG + ": Connecting to " + serverIP);
        HostName networkHost = new HostName(serverIP);

        networkConnection = new StreamSocket();

        IAsyncAction outstandingAction   = networkConnection.ConnectAsync(networkHost, connectionPort.ToString());
        AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(RcvNetworkConnectedHandler);

        outstandingAction.Completed = aach;
    }
Ejemplo n.º 16
0
        /// <summary>
        /// Connects to the server and requests data.
        /// </summary>
        private bool ConnectListener()
        {
            if (waitingForConnection)
            {
                Debug.Log("Not a good time to connect listener");
                return(false);
            }

            Debug.Log("Connecting to " + serverIP);
            waitingForConnection = true;
            HostName networkHost = new HostName(serverIP);

            networkConnection = new StreamSocket();

            IAsyncAction outstandingAction   = networkConnection.ConnectAsync(networkHost, SendConnectionPort.ToString());
            AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(RcvNetworkConnectedHandler);

            outstandingAction.Completed = aach;

            return(true);
        }
Ejemplo n.º 17
0
    // HL-rosbridge connection methods
    public void Connect()
    {
        //Async connection.
        if (!Con && !busy)
        {
            busy = true;
            Debug.Log("Connecting to rosbridge at " + host + ":" + port + "...");


#if !UNITY_EDITOR
            messageWebSocket = new MessageWebSocket();
            messageWebSocket.Control.MessageType = SocketMessageType.Utf8;
            messageWebSocket.MessageReceived    += Win_MessageReceived;

            server = new Uri("ws://" + host + ":" + port.ToString());

            IAsyncAction outstandingAction   = messageWebSocket.ConnectAsync(server);
            AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(NetworkConnectedHandler);
            outstandingAction.Completed = aach;
#endif
        }
    }
Ejemplo n.º 18
0
        /// <summary>
        /// Connects to the server and requests data.
        /// </summary>
        private bool ConnectListener()
        {
            if (waitingForConnection)
            {
                Debug.Log("Not a good time to connect listener");
                return(false);
            }

            Debug.Log("Connecting to " + AnchorOwnerIP);
            waitingForConnection = true;
            HostName networkHost = new HostName(AnchorOwnerIP);

            networkConnection = new StreamSocket();

            //NOTE: This fails to connect when using Unity 2017.3, but works with Unity 2017.4.
            IAsyncAction outstandingAction   = networkConnection.ConnectAsync(networkHost, SendConnectionPort.ToString());
            AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(RcvNetworkConnectedHandler);

            outstandingAction.Completed = aach;

            return(true);
        }
        /// <summary>
        /// Connects to the server and requests data.
        /// </summary>
        private bool ConnectListener()
        {
#if !UNITY_EDITOR && UNITY_WSA && (!ENABLE_IL2CPP && NET_STANDARD_2_0)
            if (waitingForConnection)
            {
                Debug.Log("Not a good time to connect listener");
                return(false);
            }

            waitingForConnection = true;
            Debug.Log("Connecting to " + serverIp);
            HostName networkHost = new HostName(serverIp);
            networkConnection = new StreamSocket();

            IAsyncAction outstandingAction   = networkConnection.ConnectAsync(networkHost, SendConnectionPort.ToString());
            AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(RcvNetworkConnectedHandler);
            outstandingAction.Completed = aach;

            return(true);
#else
            return(false);
#endif
        }