Пример #1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            Platform.Init(this, savedInstanceState);

            SetContentView(Resource.Layout.Main);

            var sendButton = FindViewById<Button>(Resource.Id.send_button);
            MessageTextView = FindViewById<EditText>(Resource.Id.message_text_view);
            var messagesRecyclerView = FindViewById<RecyclerView>(Resource.Id.messages_recycler_view);

            Messages = new ObservableCollection<string>();
            messagesRecyclerView.SetLayoutManager(new LinearLayoutManager(this));
            messagesRecyclerView.SetAdapter(new MessagesAdapter(messagesRecyclerView, Messages));

            sendButton.Click += OnSend;

            NetworkComms.AppendGlobalIncomingPacketHandler<string>("Chat", HandleChatConnection);
            Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, 49999));

            PeerDiscoveryService = new PeerDiscoveryService(new UdpBroadcastService());

            PeerDiscoveryService.StartListening(OnPeerDiscovered);
            PeerDiscoveryService.StartSending(DeviceInfo.Name);

            Timer.Elapsed += CheckExpiredNodes;
            Timer.Start();
        }
Пример #2
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            UIKeyboard.Notifications.ObserveDidShow(OnKeyboardDidShow);
            UIKeyboard.Notifications.ObserveWillHide(OnKeyboardWillHide);

            _messageTextField.Delegate = this;

            _tableView.RowHeight          = UITableView.AutomaticDimension;
            _tableView.EstimatedRowHeight = UITableView.AutomaticDimension;

            _tableView.RegisterNibForCellReuse(MessageCell.Nib, MessageCell.Key);
            _tableView.Source = new MessagesTableSource(_tableView, Messages);
            _tableView.ReloadData();

            NetworkComms.AppendGlobalIncomingPacketHandler <string>("Chat", HandleChatConnection);
            Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, 49999));

            PeerDiscoveryService = new PeerDiscoveryService(new UdpBroadcastService());

            PeerDiscoveryService.StartListening(OnPeerDiscovered);
            PeerDiscoveryService.StartSending(DeviceInfo.Name);

            Timer.Elapsed += CheckExpiredNodes;
            Timer.Start();
        }
Пример #3
0
        // Figure out what IP Address to listen on
        internal void SetHostIPInformation()
        {
            _networkStatusMessage = "";

            if (GameConfig.LocalIPAddress.Length != 0)
            {
                _hostIP = IPAddress.Parse(GameConfig.LocalIPAddress.Trim());
                if (_hostIP.ToString() == "0.0.0.0")
                {
                    Debug.Assert(_networkStatusMessage.Length == 0);
                    _networkStatusMessage =
                        "You have not entered a valid ipAddress in the localIPAddress tag of the Userconfig.xml file.\nUse the format: <localIPAddress>X.X.X.X</localIPAddress>.";
                }
                return;
            }

            var peerService = new PeerDiscoveryService
            {
                Url     = (GameConfig.WebRoot + "/discovery/discoverydb.asmx"),
                Timeout = NetworkTimeoutMsec
            };

            string address;
            var    isVisibleNetworkAddress  = false;
            var    contactedDiscoveryServer = false;

            try
            {
                address = peerService.ValidatePeer();
                contactedDiscoveryServer = true;
            }
            catch (Exception e)
            {
                Trace.WriteLine("HANDLED EXCEPTION: There was a problem accessing the Peer Discovery Web Service. " + e);
                Debug.Assert(_networkStatusMessage.Length == 0);
                _networkStatusMessage =
                    "The .NET Terrarium is unable to connect to the Terrarium server. \nThis means you won't receive any creatures from other peers.\nYou'll need to restart the Terrarium when it is accessible again to receive creatures.";
                address = "127.0.0.1";
            }

            if (contactedDiscoveryServer)
            {
                isVisibleNetworkAddress = ValidatePeer(address);
            }

            if (isVisibleNetworkAddress)
            {
                _hostIP = IPAddress.Parse(address);
            }
            else
            {
                if (contactedDiscoveryServer)
                {
                    Debug.Assert(_networkStatusMessage.Length == 0);
                    _networkStatusMessage = NetworkBehindNatMessage;
                    IPHostEntry he = null;
                    try
                    {
                        he = Dns.GetHostEntry(Dns.GetHostName());
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine("There was a problem resolving the hostname for this peer: " + address + " : " +
                                        ex);
                    }

                    _hostIP = he != null ? he.AddressList[0] : IPAddress.Parse("127.0.0.1");
                }
            }

            Trace.WriteLine("The IP address used for listening is " + _hostIP);
        }
Пример #4
0
        // Used to find other peers on the network
        // This routine is run on its own thread and just goes through the
        // while loop forever
        internal void AnnounceAndRegisterPeer()
        {
            try
            {
                var peerService = new PeerDiscoveryService
                {
                    Url     = (GameConfig.WebRoot + "/discovery/discoverydb.asmx"),
                    Timeout = NetworkTimeoutMsec
                };

                while (true)
                {
                    // Make sure our bad peer list remains manageable
                    _peerManager.TruncateBadPeerList();

                    // Register with the central server and get peers
                    try
                    {
                        if (GameConfig.UseConfigForDiscovery || ValidatePeer(peerService.ValidatePeer()))
                        {
                            _networkStatusMessage = "";

                            if (_discoveryLed != null)
                            {
                                _discoveryLed.LedState = LedStates.Waiting;
                            }

                            DataSet data;

                            if (GameConfig.UseConfigForDiscovery)
                            {
                                // Get it from the config file
                                _totalPeersOnChannel = GetNumPeersFromConfig(_peerChannel);
                                if (_discoveryLed != null)
                                {
                                    _discoveryLed.LedState = LedStates.Idle;
                                }

                                if (_discoveryLed != null)
                                {
                                    _discoveryLed.LedState = LedStates.Waiting;
                                }

                                data = GetAllPeersFromConfig(_peerChannel);
                                if (_discoveryLed != null)
                                {
                                    _discoveryLed.LedState = LedStates.Idle;
                                }
                            }
                            else
                            {
                                peerService.RegisterMyPeerGetCountAndPeerList(
                                    Assembly.GetExecutingAssembly().GetName().Version.ToString(), _peerChannel,
                                    GameEngine.Current.CurrentVector.State.StateGuid, out data,
                                    out _totalPeersOnChannel);
                            }

                            if (_discoveryLed != null)
                            {
                                _discoveryLed.LedState = LedStates.Idle;
                            }

                            if (data != null)
                            {
                                var peerTable    = data.Tables["Peers"];
                                var newPeersHash = Hashtable.Synchronized(new Hashtable());
                                foreach (DataRow row in peerTable.Rows)
                                {
                                    var ipAddress = (string)row["IPAddress"];
                                    var peerLease = (DateTime)row["Lease"];
                                    if (ipAddress == _hostIP.ToString())
                                    {
                                        continue;
                                    }

                                    // If the bad peer has updated their lease, then they
                                    // are online again, count them as good
                                    if (!_peerManager.ClearBadPeer(ipAddress, peerLease))
                                    {
                                        continue;
                                    }

                                    newPeersHash.Add(ipAddress, new Peer(ipAddress, peerLease));
                                }

                                _peerManager.KnownPeers = newPeersHash;
                            }
                        }
                        else
                        {
                            _networkStatusMessage = NetworkBehindNatMessage;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (_discoveryLed != null)
                        {
                            _discoveryLed.LedState = LedStates.Failed;
                        }

                        ErrorLog.LogHandledException(ex);
                    }

                    // Sleep for a while so we don't flood the network
                    Thread.Sleep(AnnounceAndRegisterPeerWaitMsec);
                }
            }
            catch (ThreadAbortException)
            {
                // percolate out and exit
                Thread.ResetAbort();
            }
            catch (Exception e)
            {
                if (!(e.InnerException is ThreadAbortException))
                {
                    ErrorLog.LogHandledException(e);
                }
            }
        }