المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Threaded Proxy?



C# Programming
11-13-2009, 09:40 AM
Hi,
Thanks for reading this!
For the people who never read any of my posts I am trying to write a proxy server in c#.
I am trying to "keep it simple" and stay away from asynchronous requests, but is not possible to totally avoid threading and I think I am encountering some multi threading problems.

The idea is to have a listener socket, binded to a certain prefix. Whatever requests I get there, I append them the correct credentials and send them straight to the (atuthenticated) server on the other side; then I grabb the bytes of the response and show them in the browser. For this I am using the HttpListenerContext class.


public void Start()
{
try
{
m_listener.Start();
while (true)
{
try
{
HttpListenerContext request = m_listener.GetContext();
ThreadPool.QueueUserWorkItem(ProcessRequest, request);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
System.Diagnostics.Debug.WriteLine("Listening socket on port " + m_port);
}


The function ProcessRequest does the actual job that I wanted. (note that it does it in an asynchronous way!)

private void ProcessRequest(object listenerContext)
{
try
{
var context = (HttpListenerContext)listenerContext;

try
{
// READ ADDRESS HERE...
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

Stream instream = context.Request.InputStream;
Stream outstream = response.GetResponseStream();

//TRANSFER DATA FROM REQUEST STREAM TO RESPONSE STREAM
// Close streams
instream.Close();
outstream.Close();
context.Response.OutputStream.Close();
}
catch (Exception ex)
{

}
}
catch
{
Restart();
}
}


Normally, everything goes ok; however, whenever I have a client that does a lot of requests very quickly (almost parallely) there is a problem in line writing the output stream (with the bytes that I just grabbed from the server) and an error is thrown:

An operation was attempted on a nonexistent network connection

This is misterious; is the network connection referring to the incoming stream (the server that I am comunicating with)? if its there is no apparent problem, because I checked and the connection is still on... or is this connection referring to the output stream? if it is, there is also no apparent problem cause I checked it with the functon CanWrite and it returns true, so it is looks in good health;
If anybody has any "light" to to bring into this question I would really appreciate it, cause I googled it to exhaustion and did not come to any conclusions http://www.barakasoft.com/script/Forums/Images/smiley_confused.gif

Anyway, moving on: I caught the error and figure out that I need to somehow close the streams that are opened; I did not find a clean easy way to do it (cause I cannot access the streams anymore), so the "brute-force" approach was to close the actual listener and start it again (so that I can again listen for connections);


private void Restart()
{
Stop();
Start();
}


Most of the times, this is ok, but sometimes, when I enter the Start() function, there is a problem with this line:


HttpListenerContext request = m_listener.GetContext();


and an exception is thrown:

The I/O operation has been aborted because of either a thread exit or an application request


and then I am in a bit of a mess.
Apart from trying to solve this particular errors that cause my application to fail requests once in a while, I am looking at the bigger picture and wondering if the issues I am encountering are from not approaching properly the situation; am I having simultaneous access to the code and no handling properly the multithreading (I actually tried a lock() of the critical section without any improvement...) ? do I need multithreading here? or is it something else?
As I said, any help here would be much appreciated! Thanks in advance http://www.barakasoft.com/script/Forums/Images/thumbs_up.gif