Ejemplo n.º 1
0
            void OnDestroy()
            {
                if (_cmdServer != null)
                {
                    _cmdServer.Close();
                    _cmdServer = null;
                }

                for (int i = 0; i < _socketConnections.Count; i++)
                {
                    if (_socketConnections[i] != null && _socketConnections[i].IsConnected())
                    {
                        _socketConnections[i].Close();
                        _socketConnections[i] = null;
                    }
                }

                //if (_fusionSocket != null && _fusionSocket.IsConnected()) {
                //    _fusionSocket.Close();
                //    _fusionSocket = null;
                //}

                if (_commanderSocket != null && _commanderSocket.IsConnected())
                {
                    _commanderSocket.Close();
                    _commanderSocket = null;
                }

                if (_ksimSocket != null && _ksimSocket.IsConnected())
                {
                    _ksimSocket.Close();
                    _ksimSocket = null;
                }

                if (_adeSocket != null && _adeSocket.IsConnected())
                {
                    _adeSocket.Close();
                    _adeSocket = null;
                }
            }
Ejemplo n.º 2
0
            void Update()
            {
                //if (_fusionSocket != null) {
                //    if (_fusionSocket.IsConnected()) {
                //        string inputFromFusion = _fusionSocket.GetMessage();
                //        if (inputFromFusion != "") {
                //            Debug.Log(inputFromFusion);
                //            Debug.Log(_fusionSocket.HowManyLeft() + " messages left.");
                //            _fusionSocket.OnFusionReceived(this, new FusionEventArgs(inputFromFusion));
                //        }
                //    }
                //    else {
                //        //SocketConnection _retry = socketConnections.FirstOrDefault(s => s.GetType() == typeof(FusionSocket));
                //        //TryReconnectSocket(_fusionSocket.Address, _fusionSocket.Port, typeof(FusionSocket), ref _retry);
                //        //_fusionSocket.OnConnectionLost(this, null);
                //        string fusionAddress = string.Format("{0}:{1}", _fusionSocket.Address, _fusionSocket.Port);
                //        if (!tryAgain.ContainsKey(fusionAddress)) {
                //            tryAgain.Add(fusionAddress, _fusionSocket.GetType());
                //        }
                //    }
                //}

                if (_cmdServer != null)
                {
                    string inputFromCommander = _cmdServer.GetMessage();
                    if (inputFromCommander != "")
                    {
                        Debug.Log(inputFromCommander);
                        ((InputController)(GameObject.Find("IOController").GetComponent("InputController"))).inputString =
                            inputFromCommander.Trim();
                        ((InputController)(GameObject.Find("IOController").GetComponent("InputController"))).MessageReceived(
                            inputFromCommander.Trim());
                    }
                }

                if (_commanderSocket != null)
                {
                    //            Debug.Log (_commanderClient.IsConnected ());
                    string inputFromCommander = _commanderSocket.GetMessage();
                    if (inputFromCommander != "")
                    {
                        Debug.Log(inputFromCommander);
                        ((InputController)(GameObject.Find("IOController").GetComponent("InputController"))).inputString =
                            inputFromCommander.Trim();
                        ((InputController)(GameObject.Find("IOController").GetComponent("InputController"))).MessageReceived(
                            inputFromCommander.Trim());
                    }
                }

                if (_ksimSocket != null)
                {
                    if (_ksimSocket.IsConnected())
                    {
                    }
                    else
                    {
                        _ksimSocket.OnConnectionLost(this, null);
                        string ksimAddress = string.Format("{0}:{1}", _ksimSocket.Address, _ksimSocket.Port);
                        if (!tryAgainSockets.ContainsKey(ksimAddress))
                        {
                            tryAgainSockets.Add(ksimAddress, _ksimSocket.GetType());
                        }
                    }
                }

                if (_adeSocket != null)
                {
                    if (_adeSocket.IsConnected())
                    {
                    }
                    else
                    {
                        _adeSocket.OnConnectionLost(this, null);
                        string adeAddress = string.Format("{0}:{1}", _adeSocket.Address, _adeSocket.Port);
                        if (!tryAgainSockets.ContainsKey(adeAddress))
                        {
                            tryAgainSockets.Add(adeAddress, _adeSocket.GetType());
                        }
                    }
                }

                if ((retryConnections) && (tryAgainSockets.Keys.Count > 0))
                {
                    foreach (string connectionUrl in tryAgainSockets.Keys)
                    {
                        if (tryAgainSockets[connectionUrl] != null)
                        {
                            SocketConnection socket =
                                _socketConnections.FirstOrDefault(s => s.GetType() == tryAgainSockets[connectionUrl]);
                            if (socket != null)
                            {
                                if (!socket.IsConnected())
                                {
                                    Debug.Log(string.Format("Retrying connection {0}@{1}", tryAgainSockets[connectionUrl],
                                                            connectionUrl));
                                    // try again
                                    try {
                                        string[] url     = connectionUrl.Split(':');
                                        string   address = url[0];
                                        if (address != "")
                                        {
                                            int port = Convert.ToInt32(url[1]);
                                            try {
                                                Type socketType = tryAgainSockets[connectionUrl];
                                                TryReconnectSocket(address, port, socketType, ref socket);
                                            }
                                            catch (Exception e) {
                                                Debug.Log(e.Message);
                                            }
                                        }
                                    }
                                    catch (Exception e) {
                                        Debug.Log(e.Message);
                                    }

                                    if (socket.IsConnected())
                                    {
                                        connected.Add(connectionUrl);
                                    }
                                    else
                                    {
                                        //Debug.Log(string.Format("Connection to {0} is lost!", socket.GetType()));
                                    }

                                    if (tryAgainSockets[connectionUrl] == typeof(FusionSocket))
                                    {
                                        //_fusionSocket = (FusionSocket) socket;
                                        //Debug.Log(_fusionSocket.IsConnected());
                                    }
                                    else if (tryAgainSockets[connectionUrl] == typeof(KSIMSocket))
                                    {
                                        _ksimSocket = (KSIMSocket)socket;

                                        if (_ksimSocket.IsConnected())
                                        {
                                            // register VoxSim
                                            byte[] bytes = BitConverter.GetBytes(1).Concat(new byte[] { 0x02 }).ToArray <byte>();
                                            _ksimSocket.Write(bytes);
                                        }
                                    }
                                    else if (tryAgainSockets[connectionUrl] == typeof(ADESocket))
                                    {
                                        _adeSocket = (ADESocket)socket;
                                        //Debug.Log(_fusionSocket.IsConnected());
                                    }
                                }
                            }
                        }
                    }

                    foreach (string label in connected)
                    {
                        if (tryAgainSockets.ContainsKey(label))
                        {
                            tryAgainSockets.Remove(label);
                        }
                    }

                    connected.Clear();

                    retryConnections = false;
                }
            }