Ejemplo n.º 1
0
        //-------------------------------------------------
        #region virtual Methods Region
        public virtual StrongString GetForServer()
        {
            StrongString myString =
                CustomName + InCharSeparator +                      // index : 0
                HeroID + InCharSeparator +                          // index : 1
                Level.ToString() + InCharSeparator +                // index : 2
                Power.GetForServer() + InCharSeparator +            // index : 3
                SkillPoint.GetForServer() + InCharSeparator +       // index : 4
                Stars.ToString() + InCharSeparator +                // index : 5
                HeroSkill.GetForServer() + InCharSeparator +        // index : 6
                HP.GetForServer() + InCharSeparator +               // index : 7
                ATK.GetForServer() + InCharSeparator +              // index : 8
                INT.GetForServer() + InCharSeparator +              // index : 9
                DEF.GetForServer() + InCharSeparator +              // index : 10
                RES.GetForServer() + InCharSeparator +              // index : 11
                SPD.GetForServer() + InCharSeparator +              // index : 12
                PEN.GetForServer() + InCharSeparator +              // index : 13
                Block.GetForServer() + InCharSeparator +            // index : 14
                HeroCurrentExp.GetForServer() + InCharSeparator +   // index : 15
                ServerIndex.ToString() + InCharSeparator;           // index : 16

            return(myString);
        }
Ejemplo n.º 2
0
        private static void Main(string[] args)
        {
            var httpListener = new HttpListener();
            var addrs        = httpListener.Prefixes;
            var port         = 23333;

            foreach (var arg in args)
            {
                if (int.TryParse(arg, out var newPort))
                {
                    if (newPort < 1024)
                    {
                        Console.WriteLine($"端口 {newPort} 太小,不被使用");
                    }
                    else if (newPort >= 65535)
                    {
                        Console.WriteLine($"端口 {newPort} 太大,不被使用");
                    }
                    else
                    {
                        port = newPort;
                    }
                }
                else
                {
                    addrs.Add($"http://{arg}:{port}{ServerPrefix}");
                    addrs.Add($"http://{arg}:{port}{ClientPrefix}");
                }
            }
            if (addrs.Count <= 0)
            {
                addrs.Add($"http://localhost:{port}{ServerPrefix}");
                addrs.Add($"http://localhost:{port}{ClientPrefix}");
            }
            try {
                Console.WriteLine("监听列表:");
                foreach (var addr in addrs)
                {
                    Console.WriteLine(addr);
                }
                httpListener.Start();
                Console.WriteLine("成功开始监听各入口");
            } catch (Exception e) {
                echo(e);
                Console.WriteLine("(非 localhost 的监听可能需要管理员权限)");
                return;
            }
            for (HttpListenerResponse response = null; ; response = null)
            {
                try {
                    var context = httpListener.GetContext();
                    response = context.Response;
                    var    url = context.Request.Url.LocalPath;
                    string ID  = null;
                    if (url.StartsWith(ServerPrefix))
                    {
                        lock (SyncObj) {
                            if (ServerIndex.ContainsKey(ID = url.Substring(ServerPrefix.Length)))
                            {
                                response.StatusCode        = 409;
                                response.StatusDescription = "ID conflict";
                                response.Close();
                                continue;
                            }
                        }
                    }
                    else if (url.StartsWith(ClientPrefix))
                    {
                        lock (SyncObj) {
                            if (!ServerIndex.ContainsKey(ID = url.Substring(ClientPrefix.Length)))
                            {
                                response.StatusCode        = 404;
                                response.StatusDescription = "The Server does Not Exist";
                                response.Close();
                                continue;
                            }
                        }
                    }
                    else
                    {
                        response.StatusCode = 400;
                        response.Close();
                        continue;
                    }
                    var ska = context.AcceptWebSocketAsync(null);
                    if (!ska.Wait(10000))
                    {
                        response.Close();
                    }
                    else if (url.StartsWith(ServerPrefix))
                    {
                        StartServer(response, ska.Result.WebSocket, ID);
                    }
                    else if (url.StartsWith(ClientPrefix))
                    {
                        StartClient(response, ska.Result.WebSocket, ID);
                    }
                    else
                    {
                        response.Close();
                    }
                } catch (Exception e) {
                    echo(e);
                    if (response != null)
                    {
                        try { response.StatusCode = 500; response.Close(); } catch (Exception e2) { echo(e2); }
                    }
                    Thread.Sleep(333);
                    GC.Collect();
                }
            }
        }