Exemple #1
0
        /// <summary>
        /// Async callback function to be invoked once the connection is established.
        /// </summary>
        /// <param name='ar'>
        /// Async result <see cref="IAsyncResult"/>
        /// </param>
        private void ConnectCallback(System.IAsyncResult ar)
        {
            try
            {
                // Retrieve the socket from the state object.
                _clientSocket = (Socket)ar.AsyncState;

                System.Console.WriteLine(OCLogSymbol.CONNECTION + "Retrieved socket from the state object...");
                // Complete the connection.

                _clientSocket.EndConnect(ar);

                System.Console.WriteLine(OCLogSymbol.CONNECTION + "Connection complete...");

                _isEstablished = true;

                _connectionState = ConnectionState.Connected;

                UnityEngine.Debug.Log(OCLogSymbol.CLEARED + "Socket connected to router.");

                //TODO [UNTESTED]: Does this resolve hanging? Yes, but it also prevents sending, lol.
                //Can we embed it in a loop with a wait?
                //_clientSocket.Blocking = false;

                // Can't write to the console here, it causes one of these:
                //			CompareBaseObjectsInternal  can only be called from the main thread.
                //			Constructors and field initializers will be executed from the loading thread when loading a scene.
                //			Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.


                //			try {
                //				OpenCog.Utility.Console.Console console = OpenCog.Utility.Console.Console.Instance;
                //				console.AddConsoleEntry("Socket connected to Embodiment Router...Logging in...", "Unity World", OpenCog.Utility.Console.Console.ConsoleEntry.Type.COMMAND);
                //			} catch (System.Exception ex) {
                //
                //			}

                LoginRouter();
            }
            catch (System.Exception e)
            {
                UnityEngine.Debug.Log(e.ToString());
            }
        }
Exemple #2
0
        /// <summary>
        /// Async callback function to be invoked once the connection is established.
        /// </summary>
        /// <param name='ar'>
        /// Async result <see cref="IAsyncResult"/>
        /// </param>
        private void ConnectCallback(System.IAsyncResult ar)
        {
            try
            {
                // Retrieve the socket from the state object.
                _clientSocket = (Socket)ar.AsyncState;

                UnityEngine.Debug.Log("Retrieved socket from the state object...");
                // Complete the connection.

                _clientSocket.EndConnect(ar);

                UnityEngine.Debug.Log("Connection complete...");

                _isEstablished = true;

                _connectionState = ConnectionState.Connected;

                UnityEngine.Debug.Log("Socket connected to router.");

                // Can't write to the console here, it causes one of these:
//			CompareBaseObjectsInternal  can only be called from the main thread.
//			Constructors and field initializers will be executed from the loading thread when loading a scene.
//			Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.


//			try {
//				OpenCog.Utility.Console.Console console = OpenCog.Utility.Console.Console.Instance;
//				console.AddConsoleEntry("Socket connected to Embodiment Router...Logging in...", "Unity World", OpenCog.Utility.Console.Console.ConsoleEntry.Type.COMMAND);
//			} catch (System.Exception ex) {
//
//			}

                LoginRouter();
            }
            catch (System.Exception e)
            {
                UnityEngine.Debug.Log(e.ToString());
            }
        }
Exemple #3
0
        protected System.Collections.IEnumerator Connect()
        {
            if (_connectionState == ConnectionState.Disconnected)
            {
                _connectionState = ConnectionState.Connecting;

                UnityEngine.Debug.Log("_connectionState == Disconnected, connecting...");

                UnityEngine.Debug.Log("NetworkElement.Connect called at " + System.DateTime.Now.ToString("HH:mm:ss.fff"));

                Socket asyncSocket = new
                                     Socket
                                         (AddressFamily.InterNetwork
                                         , SocketType.Stream
                                         , ProtocolType.Tcp
                                         );

                IPEndPoint ipe = new IPEndPoint(_routerIP, _routerPort);

                UnityEngine.Debug.Log("Start Connecting to router on IP " + _routerIP + ":" + _routerPort + "...");

                // I'd kinda like to display this in the console...so people can see how it's connecting.

                OpenCog.Utility.Console.Console console = OpenCog.Utility.Console.Console.Instance;

                if (console == null)
                {
                    UnityEngine.Debug.Log("Nope, grabbing the console didn't work...");
                }
                else
                {
                    //UnityEngine.Debug.Log ("Awesome grabbing the console worked...");

                    console.AddConsoleEntry("Start Connecting to router on IP " + _routerIP + ":" + _routerPort + "...", "Unity World", OpenCog.Utility.Console.Console.ConsoleEntry.Type.RESULT);
                }

                // Start the async connection request.
                System.IAsyncResult ar = asyncSocket
                                         .BeginConnect
                                             (ipe
                                             , new System.AsyncCallback(ConnectCallback)
                                             , asyncSocket
                                             );

                //UnityEngine.Debug.Log ("Error occurs after this...");

                yield return(new UnityEngine.WaitForSeconds(3f));

                //UnityEngine.Debug.Log ("...but before this.");

                int retryTimes = CONNECTION_TIMEOUT;
                while (!ar.IsCompleted)
                {
                    retryTimes--;
                    if (retryTimes == 0)
                    {
                        UnityEngine.Debug.LogWarning("Connection timed out.");
                        yield break;
                    }

                    yield return(new UnityEngine.WaitForSeconds(1.5f));
                }
            }
            else if (_connectionState == ConnectionState.Connecting)
            {
                UnityEngine.Debug.Log("_connectionState == Connecting, not doing anything...");
            }
            else if (_connectionState == ConnectionState.Connected)
            {
                UnityEngine.Debug.Log("_connectionState == Connected, are you a mental?");
            }
        }
Exemple #4
0
        protected System.Collections.IEnumerator Connect()
        {
            if (_connectionState == ConnectionState.Disconnected)
            {
                _connectionState = ConnectionState.Connecting;

                //UnityEngine.Debug.Log ("_connectionState == Disconnected, connecting...");

                UnityEngine.Debug.Log(OCLogSymbol.CONNECTION + "Trying to Connect, current time is " + System.DateTime.Now.ToString("HH:mm:ss.fff"));


                //NOTE: This InterNetwork flag is probably why our game and embodiment machines must be
                //on the same network in order to detect one another.
                //TODO [Task]: if we checked out our IP address ahead of time we could see if it was on
                //the same network as us and throw a message about it.
                Socket asyncSocket = new
                                     Socket
                                         (AddressFamily.InterNetwork
                                         , SocketType.Stream
                                         , ProtocolType.Tcp
                                         );

                IPEndPoint ipe = new IPEndPoint(_routerIP, _routerPort);

                UnityEngine.Debug.Log(OCLogSymbol.CONNECTION + "Start Connecting to router on IP " + _routerIP + ":" + _routerPort + "...");

                // I'd kinda like to display this in the console...so people can see how it's connecting.

                OpenCog.Utility.Console.Console console = OpenCog.Utility.Console.Console.Instance;

                if (console == null)
                {
                    UnityEngine.Debug.LogWarning(OCLogSymbol.WARN + "Nope, grabbing the console didn't work...");
                }
                else
                {
                    //UnityEngine.Debug.Log ("Awesome grabbing the console worked...");

                    console.AddConsoleEntry("Start Connecting to router on IP " + _routerIP + ":" + _routerPort + "...", "Unity World", OpenCog.Utility.Console.Console.ConsoleEntry.Type.RESULT);
                }

                // Start the async connection request.
                System.IAsyncResult ar = asyncSocket
                                         .BeginConnect
                                             (ipe
                                             , new System.AsyncCallback(ConnectCallback)
                                             , asyncSocket
                                             );

                //UnityEngine.Debug.Log ("Error occurs after this...");

                yield return(new UnityEngine.WaitForSeconds(3f));

                //UnityEngine.Debug.Log ("...but before this.");

                int retryTimes = CONNECTION_TIMEOUT;
                while (!ar.IsCompleted)
                {
                    retryTimes--;
                    if (retryTimes == 0)
                    {
                        UnityEngine.Debug.LogError(OCLogSymbol.ERROR + "Connection timed out.");

                        _isEstablished = false;
                        _clientSocket  = null;
                        yield return(false);

                        yield break;
                    }

                    yield return(new UnityEngine.WaitForSeconds(1.5f));
                }
            }
            else if (_connectionState == ConnectionState.Connecting)
            {
                System.Console.WriteLine(OCLogSymbol.CONNECTION + "_connectionState == Connecting, not doing anything...");
            }
            else if (_connectionState == ConnectionState.Connected)
            {
                System.Console.WriteLine(OCLogSymbol.CONNECTION + "_connectionState == Connected, are you a mental?");
            }
        }