示例#1
0
        public async Task <Stream> GetMap(string maptype, string x, string y, string z)
        {
            WriteFileHelper.AddList("reqnum");
            //if (Redisbool == "true" || Redisbool == "TRUE")
            //{
            //    return await GeteMapService(maptype, x, y, z);
            //}
            //else
            //{
            //    return GeteMapServer(maptype, x, y, z);
            //}

            return(GeteMapServer(maptype, x, y, z));
        }
示例#2
0
        /// <summary>
        /// 带缓存功能的地图服务
        /// </summary>
        /// <param name="maptype">地图类型</param>
        /// <param name="x">行号</param>
        /// <param name="y">列号</param>
        /// <param name="z">地图等级</param>
        /// <returns></returns>
        public async Task <Stream> GeteMapService(string maptype, string x, string y, string z)
        {
            if (string.IsNullOrEmpty(maptype))
            {
                maptype = Defaultmaptype;
            }
            string key    = maptype + "-" + JsonHelper.GetValueByKey(maptype).version + "-" + z + "-" + x + "-" + y;
            string base64 = (await SeRedisHelper.GetAsync(key)).ToString();

            if (!string.IsNullOrEmpty(base64))
            {
                try
                {
                    byte[] bytes = Convert.FromBase64String(base64);
                    Stream strem = new MemoryStream(bytes);
                    WriteFileHelper.AddList("redsuc");
                    return(strem);
                }
                catch (Exception e)
                {
                    string base404 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEAAQMAAABmvDolAAAAA1BMVEX8+fKNaX6qAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAH0lEQVRo3u3BAQ0AAADCoPdPbQ43oAAAAAAAAAAAvg0hAAABfxmcpwAAAABJRU5ErkJggg==";
                    WriteFileHelper.AddList("rederr");
                    Log4Helper.WriteLog("redis读取错误!");
                    Log4Helper.WriteLog(e);
                    byte[] bytes = Convert.FromBase64String(base404);
                    Stream strem = new MemoryStream(bytes);
                    return(strem);
                }
            }
            else
            {
                string        filepath   = GetMapDir(maptype, x, y, z);
                string        folderpath = CreateMapDir(maptype, x, y, z);
                DirectoryInfo dir        = new DirectoryInfo(folderpath);
                if (!dir.Exists)
                {
                    try
                    {
                        dir.Create();
                    }
                    catch (Exception e)
                    {
                        Log4Helper.WriteLog("文件夹创建失败!");
                        Log4Helper.WriteLog(e);
                    }
                }
                if (File.Exists(filepath))
                {
                    try
                    {
                        FileStream fs = new FileStream(filepath, FileMode.Open,
                                                       FileAccess.Read, FileShare.Read);
                        WriteFileHelper.AddList("folsuc");
                        return(fs);
                    }
                    catch (Exception e)
                    {
                        string base404 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEAAQMAAABmvDolAAAAA1BMVEX8+fKNaX6qAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAH0lEQVRo3u3BAQ0AAADCoPdPbQ43oAAAAAAAAAAAvg0hAAABfxmcpwAAAABJRU5ErkJggg==";

                        Log4Helper.WriteLog("文件夹读取失败!");
                        Log4Helper.WriteLog(e);
                        byte[] bytes = Convert.FromBase64String(base404);
                        Stream strem = new MemoryStream(bytes);
                        WriteFileHelper.AddList("folerr");
                        return(strem);
                    }
                }
                else
                {
                    string         url     = GetMapUrl(maptype, x, y, z);
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.Timeout   = 60000;
                    request.KeepAlive = false;
                    request.Method    = "GET";
                    request.ServicePoint.ConnectionLimit = 512;
                    request.UserAgent         = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; //
                    request.AllowAutoRedirect = true;                                                                    //是否允许302
                    HttpWebResponse response = null;
                    Stream          stream   = null;
                    try
                    {
                        response = request.GetResponse() as HttpWebResponse;
                        stream   = response.GetResponseStream();
                        byte[] bytes = StreamToBytes(stream);
                        WriteFileHelper.AddQueue(maptype, x, y, z, bytes);
                        WriteFileHelper.AddList("websuc");
                        Stream str = new MemoryStream(bytes);
                        return(str);
                    }
                    catch (Exception e)
                    {
                        string base404 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEAAQMAAABmvDolAAAAA1BMVEX8+fKNaX6qAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAH0lEQVRo3u3BAQ0AAADCoPdPbQ43oAAAAAAAAAAAvg0hAAABfxmcpwAAAABJRU5ErkJggg==";

                        Log4Helper.WriteLog("网络请求失败!");
                        Log4Helper.WriteLog(e);
                        byte[] bytes = Convert.FromBase64String(base404);
                        Stream strem = new MemoryStream(bytes);
                        WriteFileHelper.AddList("weberr");
                        return(strem);
                    }
                    finally
                    {
                        request.Abort();
                        if (response != null)
                        {
                            response.Close();
                        }
                        if (stream != null)
                        {
                            stream.Close();
                        }
                    }
                }
            }
        }