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 #2
0
        public IEnumerator Listen()
        {
            UnityEngine.Debug.Log("OCServerListener::Listen has a networkelement with GUID " + _networkElement.VerificationGuid);

            try
            {
                if (_listener == null)
                {
                    _listener = new TcpListener(_networkElement.IP, _networkElement.Port);

                    _listener.Start();

                    UnityEngine.Debug.Log("Now listening on " + _networkElement.IP + ":" + _networkElement.Port + "...");

                    OpenCog.Utility.Console.Console console = OpenCog.Utility.Console.Console.Instance;
                    console.AddConsoleEntry("Listening for connection callback...", "Unity World", OpenCog.Utility.Console.Console.ConsoleEntry.Type.RESULT);
                }
            }
            catch (SocketException se)
            {
                UnityEngine.Debug.Log("Whoops, something went wrong making a TCPListener: " + se.Message);
                //OCLogger.Error(se.Message);
                yield break;
            }

            while (!_shouldStop)
            {
                if (!_listener.Pending())
                {
                    UnityEngine.Debug.Log(System.DateTime.Now.ToString("HH:mm:ss.fff") + ": Nope, not pending...");
                    if (_shouldStop)
                    {
                        UnityEngine.Debug.Log("Which is funny, because IT SHOULDN'T BE HERE BECAUSE _shouldStop IS TRUE!!");
                    }
                    // If listener is not pending, sleep for a while to relax the CPU.
                    yield return(new UnityEngine.WaitForSeconds(0.5f));
                }
                else
                {
                    UnityEngine.Debug.Log("Yep, pending!");

//				try
//				{
                    UnityEngine.Debug.Log("Accepting socket from listener...");

                    _workSocket = _listener.AcceptSocket();

                    UnityEngine.Debug.Log("Socket accepted...");

                    OpenCog.Utility.Console.Console console = OpenCog.Utility.Console.Console.Instance;
                    console.AddConsoleEntry("Callback received, initializing MessageHandler...", "Unity World", OpenCog.Utility.Console.Console.ConsoleEntry.Type.RESULT);

                    _isReady = true;

                    _shouldStop = true;

                    UnityEngine.Debug.Log("_shouldStop is now TRUE!");

                    new OldMessageHandler(OCNetworkElement.Instance, _workSocket).start();

                    console.AddConsoleEntry("MessageHandler online, ready to receive messages!", "Unity World", OpenCog.Utility.Console.Console.ConsoleEntry.Type.RESULT);

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

//					UnityEngine.Debug.Log ("Ok, I'm going to make a new MessageHandler and call StartProcessing now...");
//
//					if (_messageHandler == null)
//						_messageHandler = OCMessageHandler.Instance;
//
//					if (_messageHandler == null)
//						UnityEngine.Debug.Log ("No handler?? I just made it!!");
//
//					_messageHandler.UpdateMessagesSync(workSocket);
                    //_messageHandler.UpdateMessages(workSocket);

//					UnityEngine.Debug.Log ("Well...did anything happen?");
//				}
//				catch( SocketException se )
//				{
//					//OCLogger.Error(se.Message);
//						UnityEngine.Debug.Log (se.Message);
//				}
                }
            }
        }
Exemple #3
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?");
            }
        }