コード例 #1
0
        public byte[] DownloadHttp(string fileName)
        {
            fileName = fileName.Replace(@"\", "/");

            if (fileName != "PList.gz")
            {
                fileName += ".gz";
            }
            byte[] ret = new byte[1];
            try
            {
                MirLog.info("downfile:" + fileName);
                using (MemoryStream mStream = new MemoryStream())
                {
                    using (BinaryWriter gStream = new BinaryWriter(mStream))
                    {
                        HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(Settings.P_Host + fileName);// 打开网络连接


                        Stream ftpStream  = myRequest.GetResponse().GetResponseStream();// 向服务器请求,获得服务器的回应数据流
                        int    bufferSize = 1024;
                        int    readCount;
                        byte[] buffer = new byte[bufferSize];

                        readCount = ftpStream.Read(buffer, 0, bufferSize);
                        while (readCount > 0)
                        {
                            gStream.Write(buffer, 0, readCount);
                            readCount = ftpStream.Read(buffer, 0, bufferSize);
                        }
                        ret = mStream.ToArray();
                        ftpStream.Close();
                    }
                }
                //using (WebClient client = new WebClient())
                //{
                //    client.Credentials = new NetworkCredential(Settings.Login, Settings.Password);
                //   return client.DownloadData(Settings.Host  + fileName);
                //}
            }
            catch
            {
                System.Console.WriteLine("下载文件失败:" + fileName);
            }
            if (ret.Length > 10)
            {
                return(ret);
            }
            return(null);
        }
コード例 #2
0
        //下载文件,返回的是字节数组
        public byte[] Download(string fileName)
        {
            if (Settings.P_Host.IndexOf("http:", StringComparison.CurrentCultureIgnoreCase) != -1)
            {
                return(DownloadHttp(fileName));
            }
            fileName = fileName.Replace(@"\", "/");

            if (fileName != "PList.gz")
            {
                fileName += ".gz";
            }
            byte[] ret = new byte[1];
            try
            {
                MirLog.info("downfile:" + fileName);
                using (MemoryStream mStream = new MemoryStream())
                {
                    using (BinaryWriter gStream = new BinaryWriter(mStream))
                    {
                        FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(Settings.P_Host + fileName));
                        reqFTP.Method     = WebRequestMethods.Ftp.DownloadFile;
                        reqFTP.Timeout    = 5000;
                        reqFTP.UseBinary  = true;
                        reqFTP.UsePassive = false;
                        if (Settings.P_NeedLogin)
                        {
                            reqFTP.Credentials = new NetworkCredential(Settings.P_Login, Settings.P_Password);
                        }

                        FtpWebResponse response   = (FtpWebResponse)reqFTP.GetResponse();
                        Stream         ftpStream  = response.GetResponseStream();
                        int            bufferSize = 2048;
                        int            readCount;
                        byte[]         buffer = new byte[bufferSize];

                        readCount = ftpStream.Read(buffer, 0, bufferSize);
                        while (readCount > 0)
                        {
                            gStream.Write(buffer, 0, readCount);
                            readCount = ftpStream.Read(buffer, 0, bufferSize);
                        }
                        ret = mStream.ToArray();
                        ftpStream.Close();
                        response.Close();
                    }
                }
                //using (WebClient client = new WebClient())
                //{
                //    client.Credentials = new NetworkCredential(Settings.Login, Settings.Password);
                //   return client.DownloadData(Settings.Host  + fileName);
                //}
            }
            catch
            {
                System.Console.WriteLine("下载文件失败:" + fileName);
            }
            if (ret.Length > 10)
            {
                return(ret);
            }
            return(null);
        }
コード例 #3
0
        private static void Main(string[] args)
        {
#if DEBUG
            //Settings.UseTestConfig = true;
#endif

            //MirLog.info("DEBUG:" + Settings.UseTestConfig);
            try
            {
                //加大连接并发数
                System.Net.ServicePointManager.DefaultConnectionLimit = 256;

                if (UpdatePatcher())
                {
                    return;
                }

                if (RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully == true)
                {
                }

                long currExeLen = new FileInfo(Process.GetCurrentProcess().MainModule.FileName).Length;
                //客户端多开限制,只运行开3个客户端
                int       currClient = 0;
                string[]  dsmach     = { "Config", "Data", "DirectX", "Map", "Sound" };
                Process[] ps         = Process.GetProcesses();
                foreach (Process p in ps)
                {
                    try
                    {
                        if (p.MainModule.FileName == null)
                        {
                            continue;
                        }
                        MirLog.info(p.MainModule.FileName);
                        if (new FileInfo(p.MainModule.FileName).Length == currExeLen)
                        {
                            currClient++;
                        }

                        FileInfo        f           = new FileInfo(p.MainModule.FileName);
                        DirectoryInfo[] ds          = f.Directory.GetDirectories();
                        int             dsmachcount = 0;
                        foreach (DirectoryInfo di in ds)
                        {
                            foreach (string dm in dsmach)
                            {
                                if (di.Name.ToLower().Equals(dm.ToLower()))
                                {
                                    dsmachcount++;
                                }
                            }
                        }
                        if (dsmachcount >= 5)
                        {
                            //currClient++;
                        }
                    }
                    catch (Exception e)
                    {
                        MirLog.error(e.Message);
                    }
                }
                if (currClient >= 4)
                {
                    MirLog.info("最多只运行同时打开3个客户端");
                    MessageBox.Show("最多只运行同时打开3个客户端", "提示", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
                    Application.Exit();
                    return;
                }


                Packet.IsServer = false;
                Settings.Load();

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                Application.Run(PForm = new Launcher.AMain());

                //if (Settings.P_Patcher) Application.Run(PForm = new Launcher.AMain());
                //else Application.Run(Form = new CMain());
                //Application.Run(Form = new CMain());
                //Application.Run( new Test());
                Settings.Save();
                CMain.InputKeys.Save();

                if (Restart)
                {
                    Application.Restart();
                }
            }
            catch (Exception ex)
            {
                CMain.SaveError(ex.ToString());
            }
        }