public void Run() { try { _fs = File.OpenRead(_fileName); } catch { EndRequest(_req.Socket, 500, "can't open file."); _req.SaveToPoll(); _req = null; return; } string extention = CommonUtil.GetFileExtention(_fileName); string mimeType = HttpMimeTypeManage.GetHttpMimeType(extention); Tuple <int, int> tuple = _req.IfRange(); int num; int num2; if (tuple == null) { byte[] headDomain = ResultFactory.BuildSuccessResult(mimeType, _fileSize, "", CommonUtil.GetFileTime(_modifyTime), _req.IsKeepAlive(), false); byte[] body = new byte[32768]; _fs.Read(body, 0, body.Length); _sendedSize += body.Length; _offsetSize -= body.Length; _req.Socket.WriteForPost(headDomain, body, new Action <OwinSocket, int, Exception, object>(CompilteCallback), null); return; } //续传 GetNextSize(tuple, checked ((int)_fileSize), out num, out num2); _sendedSize = num; _offsetSize = num2; string s = ResultFactory.BuildResult(206, mimeType, "", _sendedSize, _offsetSize, _fileSize, false, _req.IsKeepAlive()); byte[] array3 = (_offsetSize > 32768L) ? new byte[32768] : new byte[_offsetSize]; _fs.Seek(_sendedSize, SeekOrigin.Begin); _fs.Read(array3, (int)_sendedSize, array3.Length); _sendedSize += array3.Length; _offsetSize -= array3.Length; _req.Socket.WriteForPost(Encoding.ASCII.GetBytes(s), array3, new Action <OwinSocket, int, Exception, object>(CompilteCallback), null); }
private static void SendFile(RequestData req, string file, long mtime, long fsize) { byte[] array = null; try { array = File.ReadAllBytes(file); } catch { EndRequest(req.Socket, 500, "Can't Open File."); req.SaveToPoll(); req = null; return; } string fileExtention = CommonUtil.GetFileExtention(file); string mimeType = HttpMimeTypeManage.GetHttpMimeType(fileExtention); Tuple <int, int> tuple = req.IfRange(); if (tuple == null) { byte[] array2 = ResultFactory.BuildSuccessResult(mimeType, fsize, CacheManager.FileLastTimeAndFileLengthString(file, fsize, mtime), CommonUtil.GetFileTime(mtime), req.IsKeepAlive(), false); req.Socket.WriteForPost(array2, array, new Action <OwinSocket, int, Exception, object>(SendCompleteCallback), req); CacheManager.Save(req.RequestUrl, file, array, req.SafeRequestUrl); return; } int num3 = 0; int num4 = checked ((int)fsize); GetNextSize(tuple, fsize, out num3, out num4); byte[] array3 = new byte[num4]; Buffer.BlockCopy(array, num3, array3, 0, num4); //206的续传回应 string s = ResultFactory.BuildResult(206, mimeType, "", num3, checked (num3 + num4), fsize, false, req.IsKeepAlive()); byte[] bytes = Encoding.ASCII.GetBytes(s); req.Socket.WriteForPost(bytes, array3, new Action <OwinSocket, int, Exception, object>(SendCompleteCallback), req); }
public static void Save(string key, string path, byte[] array, string safeRequestUrl) { if (Interlocked.CompareExchange(ref _addingLockTag, 1, 0) != 0) { return; } if (HasCached(key)) { Interlocked.Exchange(ref _addingLockTag, 0); return; } if (!_mainLock.TryEnterWriteLock(500)) { Interlocked.Exchange(ref _addingLockTag, 0); return; } try { if (_nowUsedMemory <= BuffMaxMemory) { if (_arrayCacheTable.Count <= BuffMaxFiles) { long lastWriteTime = 0; long fileLength = 0; if (CommonUtil.GetFileLastWriteTimeAndLength(path, ref lastWriteTime, ref fileLength)) { string cacheKey = FileLastTimeAndFileLengthString(path, fileLength, lastWriteTime); if (array == null) { array = File.ReadAllBytes(path); } Cache cache = new Cache(); cache.FileBytes = array; string safePath = CommonUtil.GetFileExtention(path); if (!string.IsNullOrEmpty(safePath)) { safePath = safePath.ToLower(); } if (array.Length >= MinFileSize_GZIP && IsInzipFileExtList(safePath)) { try { cache.FileBytesCompress = CommonUtil.Compress(array); } catch { cache.FileBytesCompress = null; } } cache.LastWriteTime = CommonUtil.GetFileTime(lastWriteTime); cache.LastWriteTimeLong = lastWriteTime; cache.Times = ActionQueue.LongTimes / 1000L; cache.Path = path; cache.Key = cacheKey; cache.HttpMimeType = HttpMimeTypeManage.GetHttpMimeType(safePath); cache.RequestUrl = safeRequestUrl; _arrayCacheTable.Add(key, cache); _nowUsedMemory += array.Length; } } } } catch { //ignore } finally { _mainLock.ExitWriteLock(); Interlocked.Exchange(ref _addingLockTag, 0); } }