private void IncomingFunction()
        {
            try
            {
                InitializeClient(ref _incomingClient);

                GsService.AddServer(_localEP);

                while (_state == 0)
                {
                    try
                    {
                        IPEndPoint remoteEP = null;
                        byte[]     buffer   = _incomingClient.Receive(ref remoteEP);

#if VERBOSE
                        Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + " [UdpClient] " + Encoding.ASCII.GetString(buffer, 0, buffer.Length));
#endif

                        if (Utils.EqualsTo(buffer, buffer.Length, _data[0]))
                        {
                            buffer = remoteEP.Address.GetAddressBytes();

                            Contract.Assert(buffer.Length == 4);

                            long   address = BitConverter.ToUInt32(buffer, 0);
                            int    port    = remoteEP.Port;
                            byte[] msg     = _server.Status;

                            Enqueue(address, port, msg);
                        }
                        else if (Utils.StartsWith(buffer, buffer.Length, _data[1]))
                        {
                            string id = Encoding.UTF8.GetString(buffer, _data[1].Length, buffer.Length - _data[1].Length);

                            if (int.TryParse(id, NumberStyles.None, CultureInfo.InvariantCulture, out int clientId))
                            {
                                _server.CheckClient(clientId);
                            }
                        }
                    }
                    catch (Exception)
                    { }
                }
            }
            catch (Exception)
            { }
            finally
            {
                Close();

                GsService.TryRemoveServer(_localEP);
            }
        }
Example #2
0
        private void UdpFunction()
        {
            IPEndPoint localEP = null;
            UdpClient  client  = null;

            try
            {
                localEP = new IPEndPoint(_localIP, CentralSwitchPort);
                client  = new UdpClient(localEP);

                // adds the server profile and sets its default status message

                GsService.AddServer(localEP);

                byte[] serverStatus = Encoding.UTF8.GetBytes("\\gamename\\sfc2op\\gamever\\1.6\\location\\0\\serverver\\2.5.6.4\\validclientver\\2.5.6.4\\hostname\\Standard\\hostport\\" + CentralSwitchPort + "\\mapname\\StandardMap.mvm StandardMap.mvm StandardMap.mvm\\gametype\\Man-in-the-middle\\maxnumplayers\\3000\\numplayers\\0\\maxnumloggedonplayers\\64\\numloggedonplayers\\0\\gamemode\\Open\\racelist\\0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 \\password\\\\final\\\\queryid\\1.1");

                // starts receiving

                Dictionary <string, string> d = new Dictionary <string, string>();

                while (true)
                {
                    try
                    {
                        IPEndPoint dataEP = null;

                        byte[] datagram = client.Receive(ref dataEP);

                        Utils.GetArguments(datagram, datagram.Length, ref d);

                        if (d.ContainsKey("status"))
                        {
                            client.Send(serverStatus, serverStatus.Length, dataEP);
                        }

                        d.Clear();
                    }
                    catch (Exception)
                    { }
                }
            }
            catch (Exception)
            { }
            finally
            {
                GsService.TryRemoveServer(localEP);

                client?.Close();
            }
        }