Example #1
0
        private void OnSGameConnected(NetPeer peer)
        {
            var newNodeInfoWaiter = new MessageWaiter <SShared.Messages.NodeConfig>(_busMaster, peer).Wait;

            newNodeInfoWaiter.Wait();
            var newNodeInfo = newNodeInfoWaiter.Result;

            var newNode = _routingTable.AddSGameNode(peer, newNodeInfo.BusAddress, newNodeInfo.BusPort, newNodeInfo.ApiUrl);

            Console.Error.WriteLine(">>> SGame node {0} (API: {1}) connected at {2} <<<", peer.EndPoint, newNode.ApiUrl, newNode.Path());

            // IMPORTANT: Send the whole network topology (as of now) to the new node (so that it can build a routing table for itself)
            foreach (var treeNode in _routingTable.RootNode.Traverse().Cast <ArbiterTreeNode>())
            {
                if (treeNode.Peer == peer)
                {
                    continue;
                }

                var otherNodeConfig = new SShared.Messages.NodeConfig()
                {
                    BusAddress = treeNode.BusAddress,
                    BusPort    = treeNode.BusPort,
                    Bounds     = treeNode.Bounds,
                    Path       = treeNode.Path(),
                    ApiUrl     = treeNode.ApiUrl,
                };
                _busMaster.SendMessage(otherNodeConfig, peer, DeliveryMethod.ReliableOrdered);
            }

            // IMPORTANT: Broadcast that the new node is online and where it is (incl. the new node itself to tell it its path)
            var newNodeConfig = new SShared.Messages.NodeConfig()
            {
                BusAddress = newNode.BusAddress,
                BusPort    = newNode.BusPort,
                Bounds     = newNode.Bounds,
                Path       = newNode.Path(),
                ApiUrl     = newNode.ApiUrl,
            };

            _busMaster.BroadcastMessage(newNodeConfig, DeliveryMethod.ReliableOrdered);
        }