示例#1
0
文件: Program.cs 项目: maestrodk/OST
        void Init()
        {
            InitInterfaces();
            InitSockets();

            bcast = new BroadcastHandler <ProxyPeerInfo>()
            {
                AddMessage = AddMessageDebug
            };
            myPid          = System.Diagnostics.Process.GetCurrentProcess().Id;
            remotePeers    = new Dictionary <IPEndPoint, DateTime>();
            curLocalIP     = null;
            curLocalIPLast = DateTime.MinValue;

            AppDomain.CurrentDomain.ProcessExit += async(object sender, EventArgs e) => { await Done(); };
            Console.CancelKeyPress += async(object sender, ConsoleCancelEventArgs e) => { await Done(); };

            {
                if (config.TryGetValue("isServer", out object isServer) && (bool)isServer)
                {
                    AddMessage("Signing on tracker at " + config["trackerBaseUrl"]);

                    TrackerPost("?online=true", new MJDict {
                        { "name", config["serverName"] },
                        { "notes", config["notes"] },
                        { "numPlayers", 0 },
                        { "maxNumPlayers", 0 },
                        { "map", "" },
                        { "mode", "" }
                    });
                }
            }
        }
示例#2
0
        void Init()
        {
            InitInterfaces();
            InitSockets();

            bcast = new BroadcastHandler <ProxyPeerInfo>()
            {
                AddMessage = AddMessageDebug
            };
            remotePeers    = new Dictionary <IPEndPoint, DateTime>();
            curLocalIP     = null;
            curLocalIPLast = DateTime.MinValue;
        }
示例#3
0
文件: Program.cs 项目: maestrodk/OST
        void ProcessLocalPacket(IPEndPoint endPoint, byte[] packet)
        {
            if (!LocalIPSet.Contains(endPoint.Address)) // v0.2 policy: only accept local packets, better for multiple olproxies on LAN
            {
                return;
            }

            var pktPid = BroadcastHandler <ProxyPeerInfo> .GetPacketPID(packet);

            if (bcast.activeFakePids.Contains(pktPid)) // ignore packets sent by ourself
            {
                return;
            }

            // v0.2 policy: only accept packets from single local ip, allow ip change if idle for 10 seconds
            var now     = DateTime.UtcNow;
            var timeout = now.AddSeconds(-10);

            if (curLocalIPLast < timeout)
            {
                if (debug && !endPoint.Address.Equals(curLocalIP))
                {
                    AddMessage("Using local address " + endPoint.Address);
                }
                curLocalIP = endPoint.Address;
            }
            curLocalIPLast = now;

            if (!endPoint.Address.Equals(curLocalIP))
            {
                return;
            }

            //if (LocalIPSet.Contains(endPoint.Address)) // treat all local ips as same
            //    endPoint.Address = FirstLocalIP;

            var msgStr = bcast.HandlePacket(endPoint, packet, out bool isNew, out BroadcastPeer <ProxyPeerInfo> peer);

            if (msgStr == null) // message not yet complete / invalid
            {
                return;
            }

            var msg = ParseMessage(msgStr);

            if (msg.IsRequest)
            {
                var adr = FindPasswordAddress(msg.Password, out string hostname);
                if (adr == null)
                {
                    return;
                }
                var destEndPoint = new IPEndPoint(adr, remotePort);
                if (isNew)
                {
                    AddMessage(debug ? pktPid + " " + isNew + " Sending match " + msg.ticketType +
                               " " + msg.ticket +
                               " to server " + hostname :
                               "Sending " + (msg.HasPrivateMatchData ? "create" : "join") + " match " + msg.ticketType +
                               (msg.HasPrivateMatchData ? " (" + new MatchInfo(msgStr) + ")" : "") +
                               " to " + hostname);
                }
                else
                {
                    spinner.Spin();
                }
                bcast.Send(msgStr, remoteSocket.Client, destEndPoint, pktPid, isNew);
                return;
            }
            else if (msg.IsMatch && isNew)
            {
                if (config.TryGetValue("isServer", out object isServer) && (bool)isServer)
                {
                    var matchInfo = new MatchInfo(msgStr);
                    if (playerCount != matchInfo.PlayerCount)
                    {
                        playerCount = matchInfo.PlayerCount;

                        AddMessage("Updating tracker at " + config["trackerBaseUrl"] + " with player count of " + matchInfo.PlayerCount + ".");

                        TrackerPost("", new MJDict {
                            { "numPlayers", matchInfo.PlayerCount }
                        });
                    }
                }
            }

            if (!remotePeers.Any())
            {
                return;
            }

            var dels = new List <IPEndPoint>();

            foreach (var peerEP in remotePeers.Keys)
            {
                if (remotePeers[peerEP] < timeout)
                {
                    dels.Add(peerEP);
                }
            }
            foreach (var ep in dels)
            {
                remotePeers.Remove(ep);
            }

            if (!remotePeers.Any())
            {
                return;
            }

            //if (msgStr == "")
            //    bcast.peers.Remove(new BroadcastPeerId() { source = endPoint, pid = BroadcastHandler.GetPacketPID(packet) });

            if (isNew)
            {
                AddMessage(debug ? pktPid + " Sending match " +
                           (msgStr == "" ? "done" : msg.ticketType + " " + msg.ticket) +
                           " to clients " + String.Join(", ", remotePeers.Keys.Select(x => x.Address.ToString())) :
                           "Sending " + (msgStr != "" ? (msg.HasPrivateMatchData ? "create " : "join ") : "") +
                           "match " + (msgStr == "" ? "done" : msg.ticketType) +
                           (msg.HasPrivateMatchData || msg.ticketType == "match" ? " (" + new MatchInfo(msgStr) + ")": ""));
            }
            else
            {
                spinner.Spin();
            }
            Debug.WriteLine((msgStr == "" ? "done" : msg.ticketType) + " to " + String.Join(", ", remotePeers.Keys.Select(x => x.ToString())));
            bcast.SendMulti(msgStr, remoteSocket.Client, remotePeers.Keys, pktPid, isNew);
        }