Skip to content

Commit

Permalink
fix some bugs
Browse files Browse the repository at this point in the history
不再将返回数据转换为utf8编码
修正FileBuffer LRU队列可能出现查询结果为空导致程序错误
  • Loading branch information
macworld committed Apr 23, 2014
1 parent 05c0f93 commit 7477069
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 77 deletions.
114 changes: 55 additions & 59 deletions FileManager/FileBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ public static FileBuffer GetInstance()
/// <returns></returns>
private bool SaveFile(Byte[] fileByteStream, string url)
{
Logger log = new Logger("AppLogger");
long fileLength = fileByteStream.Length;
if (totalFreeSpace < fileLength)
{
if (fileLength > bufferSize)
{
//Logger.GetLogger().Info("The file is to big to buffer it");
log.Warn("File: " + url + " is too big to store in buffer!");
return false;
}
//Logger.GetLogger().Info("There is not enough space in the fileBuffer.We'll try to release some older file");
while (totalFreeSpace < fileLength)
{
if (!ReleaseFileLRU(fileLength)) // while there is no file to realse ,return false
Expand All @@ -157,6 +157,7 @@ private bool SaveFile(Byte[] fileByteStream, string url)
{
if (fileLengthDictionary.ContainsKey(url) || totalFreeSpace < fileLength)//avoid the error causing by other saving action
{
//log.Warn("Warning: File "+url+"is");
return false;
}
LruList.AddFirst(url);
Expand Down Expand Up @@ -203,9 +204,8 @@ private bool SaveFile(Byte[] fileByteStream, string url)
}
urlToPageDic.Add(url, urlPages);//added to the dictionnary which is used as a page table
fileLengthDictionary.Add(url, fileLength);//add to the dictionnary when it has been saved in the memory
Logger log = new Logger("AppLogger");
log.Debug("Add file: " + url + " in buffer");
}
log.Debug("Add file: " + url + " in buffer");
}
return true;
}
Expand All @@ -225,7 +225,7 @@ private bool ReleaseFileLRU(long fileLength)
else
{
Logger log = new Logger("AppLogger");
log.Warn("There is no file to release");
log.Error("There is no file to release");
return false;
}
}
Expand All @@ -237,8 +237,8 @@ private bool ReleaseFileLRU(long fileLength)
private bool RemoveFileInBuffer(string url)
{
Logger log = new Logger("AppLogger");
log.Debug("File changed, remove file data in buffer: "+ url);
// lock (LockRDConflict)//avoid the conflict when read the file which has been deleted
log.Debug("File changed, remove file data in buffer: " + url);
// lock (LockRDConflict)//avoid the conflict when read the file which has been deleted
RWLock.AcquireWriterLock(FileManager.Properties.FileManagerSettings.Default.LockTimeOut);
try
{
Expand All @@ -262,7 +262,7 @@ private bool RemoveFileInBuffer(string url)
{
RWLock.ReleaseWriterLock();
}
return true;
return true;
}
/// <summary>
/// used to avoid too much delete caused by multithreading
Expand All @@ -272,31 +272,26 @@ private bool RemoveFileInBuffer(string url)
/// <returns></returns>
private bool RemoveFileInBuffer(string url, long fileLength)
{
// lock (LockRDConflict)//avoid the conflict when read the file which has been deleted
// lock (LockRDConflict)//avoid the conflict when read the file which has been deleted
RWLock.AcquireWriterLock(FileManager.Properties.FileManagerSettings.Default.LockTimeOut);
try
if (totalFreeSpace > fileLength | !LruList.Remove(url))//when the url is not in the LruList
{
if (totalFreeSpace > fileLength | !LruList.Remove(url))//when the url is not in the LruList
{
return false;
}
string pageString = urlToPageDic[url];
urlToPageDic.Remove(url);
fileLengthDictionary.Remove(url);
string[] pageNumStr = pageString.Split(',');
int pageNumInt = 0;
for (int i = 0; i < pageNumStr.Length; ++i)
{
pageNumInt = Convert.ToInt32(pageNumStr[i]);
freePageStack.Push(pageNumInt);//release the buffer
totalFreeSpace += pageSize; //get the new size of FreeSpace
}
return false;
}
finally
string pageString = urlToPageDic[url];
urlToPageDic.Remove(url);
fileLengthDictionary.Remove(url);
string[] pageNumStr = pageString.Split(',');
int pageNumInt = 0;
for (int i = 0; i < pageNumStr.Length; ++i)
{
RWLock.ReleaseWriterLock();
pageNumInt = Convert.ToInt32(pageNumStr[i]);
freePageStack.Push(pageNumInt);//release the buffer
totalFreeSpace += pageSize; //get the new size of FreeSpace
}


RWLock.ReleaseWriterLock();

return true;
}

Expand All @@ -323,47 +318,48 @@ public Byte[] readFile(string url, ref int statusCode)
if (fileLengthDictionary.ContainsKey(url))//if the file was exsited in filebuffer
{
RWLock.AcquireReaderLock(FileManager.Properties.FileManagerSettings.Default.LockTimeOut);
try
//lock (LockRDConflict)
if (!fileLengthDictionary.ContainsKey(url))//when the delete part has delete the file in the buffer,
//due to the wait the last contain is not effective now,we need to make sure if the file exist or not again
{
if (!fileLengthDictionary.ContainsKey(url))//when the delete part has delete the file in the buffer,
//due to the wait the last contain is not effective now,we need to make sure if the file exist or not again
Byte[] readBuffer = FileSystem.GetInstance().readFile(url);
SaveFile(readBuffer, url);//save the file to fileBuffer in memory
statusCode = 200;
return readBuffer;
}
long fileLength = fileLengthDictionary[url];
Byte[] fileByteStream = new Byte[fileLength];
string pageString = urlToPageDic[url];
string[] pageNumStr = pageString.Split(',');
int pageNumInt = 0;
int fileOffset = 0;
for (int i = 0; i < pageNumStr.Length; ++i)
{
pageNumInt = Convert.ToInt32(pageNumStr[i]);
if (fileLength - fileOffset >= pageSize)
{
Byte[] readBuffer = FileSystem.GetInstance().readFile(url);
SaveFile(readBuffer, url);//save the file to fileBuffer in memory
statusCode = 200;
return readBuffer;
System.Buffer.BlockCopy(memoryBuffer, pageNumInt * pageSize, fileByteStream, (int)fileOffset, pageSize);
}
long fileLength = fileLengthDictionary[url];
Byte[] fileByteStream = new Byte[fileLength];
string pageString = urlToPageDic[url];
string[] pageNumStr = pageString.Split(',');
int pageNumInt = 0;
int fileOffset = 0;
for (int i = 0; i < pageNumStr.Length; ++i)
else
{
pageNumInt = Convert.ToInt32(pageNumStr[i]);
if (fileLength - fileOffset >= pageSize)
{
System.Buffer.BlockCopy(memoryBuffer, pageNumInt * pageSize, fileByteStream, (int)fileOffset, pageSize);
}
else
{
System.Buffer.BlockCopy(memoryBuffer, pageNumInt * pageSize, fileByteStream, (int)fileOffset, (int)(fileLength - fileOffset));
}
fileOffset += pageSize;
System.Buffer.BlockCopy(memoryBuffer, pageNumInt * pageSize, fileByteStream, (int)fileOffset, (int)(fileLength - fileOffset));
}
//to realize LRU,the file which was read need to be put into the head of the list
LinkedListNode<string> readNode = LruList.Find(url);
fileOffset += pageSize;
}
//to realize LRU,the file which was read need to be put into the head of the list
LinkedListNode<string> readNode = LruList.Find(url);
if (readNode != null)
{
LruList.Remove(url);
LruList.AddFirst(readNode);
statusCode = 200;
return fileByteStream;
}
finally
else
{
RWLock.ReleaseReaderLock();
LruList.AddFirst(url);

}
statusCode = 200;
RWLock.ReleaseReaderLock();
return fileByteStream;
}
else //when the file is not in fileBuffer,read the file from local fileSystem
{
Expand Down
7 changes: 2 additions & 5 deletions HttpParser/HttpProtocolParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,11 @@ public Byte[] GetWrappedResponse(Byte[] data)
if (this.statusCode == 200)
{
stringBuilder.Append("HTTP/1.1 200 OK\r\nDate: ");
stringBuilder.Append(dateTime.GetDateTimeFormats('r')[0].ToString());
stringBuilder.Append(dateTime.GetDateTimeFormats('r')[0]);
stringBuilder.Append("\r\n");
stringBuilder.Append("Server: ezHttp\r\n");
stringBuilder.Append("Connection: keepalive\r\n");
if(GetResourceUrl().EndsWith(".svg"))
stringBuilder.Append("Content-Type: image/svg+xml" + "; charset=utf8\r\n");
else
stringBuilder.Append("Content-Type: "+ TypeConverrter.GetType(GetResourceUrl()) + "; charset=utf8\r\n");
stringBuilder.Append("Content-Type: "+ TypeConverrter.GetType(GetResourceUrl()) + "\r\n");

stringBuilder.Append("Content-Length: ");
stringBuilder.Append(data.Length);
Expand Down
9 changes: 0 additions & 9 deletions ezHttp/BizLogic/MainLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ internal static bool StartService()
return true;
}

internal static void RestartService()
{
socketServer.Start();
fileBuffer.Run();
}

internal static void StopService()
{
socketServer.Stop();
Expand All @@ -48,13 +42,10 @@ internal static void StopService()
static void OnReceivedHttpReq(object sender, AsyncUserToken token, byte[] data)
{


data = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"), Encoding.UTF8, data);
int statusCode = 0;
token.HttpParser.SetRawData(data);
if (token.HttpParser.IsHttpRequest())
{
//Console.WriteLine("\t\tresource url: " + token.HttpParser.GetResourceUrl());
Byte[] sendData = FileManager.FileBuffer.GetInstance().readFile(token.HttpParser.GetResourceUrl(), ref statusCode);
token.HttpParser.SetStatusCode(statusCode);
token.Socket.Send(token.HttpParser.GetWrappedResponse(sendData));
Expand Down
6 changes: 3 additions & 3 deletions ezHttp/BizLogic/SettingsLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private bool DetectPortNum(string ListenPort)
{
if (Convert.ToInt32(ListenPort) < 1024 || Convert.ToInt32(ListenPort) > 65535)
{
text_remind.Text = "The value of ListenPort Should between 1023 and 65536(not include).";
text_remind.Text = "The value of ListenPort Should between 1023 and 65535.";
text_remind.Foreground = new SolidColorBrush(remind_error_color);
SetUnChanged();
return false;
Expand All @@ -195,9 +195,9 @@ private bool DetectHomeDic(string ServerDirectory,string HomeDic)

private bool DetectMaxConnection(string MaxConnection)
{
if (Convert.ToInt32(MaxConnection) < 100)
if (Convert.ToInt32(MaxConnection) < 32)
{
text_remind.Text = "The Max Connection should be bigger than 100.";
text_remind.Text = "The Max Connection should be bigger than 32.";
text_remind.Foreground = new SolidColorBrush(remind_error_color);
SetUnChanged();
return false;
Expand Down
2 changes: 1 addition & 1 deletion ezHttp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void UpdateStateText(string state, SolidColorBrush color)
StatusText.Foreground = color;
StatusText.Text = state;
showState.Begin();
fadeCount = 6;
fadeCount = 8;
}

private void ButtonTurnGreen()
Expand Down

0 comments on commit 7477069

Please sign in to comment.