|
Hello everyone,
Here is my code for an HttpService. My question is, if there is no coming client request, both (1) and (2) wil be blocked and when there is a coming request,
1. the the callback function provided as the 1st parameter of the BeginGetContext function will be called;
2. and also for the handle wait on (result.AsyncWaitHandle.WaitOne()) will be signalled.
Are there any sequences for (1) and (2)? i.e. for asynchronous call, signal is set before callback is called or vice versa?
public class TestHttpServer
{
private int _Port = 0;
private HttpListener _Server = new HttpListener();
private Service1 _manager;
public TestHttpServer (Service1 manager)
{
_manager = manager;
}
public int ListenPort
{
get
{
return _Port;
}
set
{
_Port = value;
}
}
public void StartListen()
{
try
{
IAsyncResult result;
_Server.Prefixes.Add(String.Format("http://+:{0}/", 9099));
_Server.Start();
while (true)
{
result = _Server.BeginGetContext(new AsyncCallback(this.HttpCallback), _Server);
result.AsyncWaitHandle.WaitOne();
}
}
catch (Exception ex)
{
throw ex;
}
}
public void Stop(bool isTerminate)
{
_Server.Stop();
}
private void HttpCallback(IAsyncResult result)
{
HttpListenerContext context = _Server.EndGetContext(result);
HandleRequest(context);
}
private void HandleRequest(HttpListenerContext context)
{
string matchUrl = context.Request.Url.AbsolutePath.Trim().ToLower();
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.Close();
}
}
thanks in advance,
George
|
|
|
|
|
This is the old code what we have discussed. Are looking to write a HTTPServer ? If I am getting time, I will probably write a simple one and send you at the end of the day.
|
|
|
|
|
Yes, N a v a n e e t h! I have discussed similar code with you before and learned a lot from you. I feel asynchronous call is easy to write, but there are many things internal to discover so that we can use it more efficiently.
For this question, this is what I have further studied about the issue of dependency/sequence of callback and signal set of wait handle.
Let me know if you have got any further ideas?
regards,
George
|
|
|
|
|
This might sound like a silly question, but will it?
|
|
|
|
|
erm.... no
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Yes; the guy in the next cubicle may come smash it.
|
|
|
|
|
|
Eventually yes, but the other components will have already failed by that point so it's irrelevant.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
It will if you sit near me!
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Hey guys got a problem here
I create a datatable manually and bind in to the gridview. It can be done
But here the issue how to create a hyperlink column manually in datatable
and a button column in datatable
any advise or guide
i will seriously deeply apperciate the help
Thanks a million Bros and Sis
KaKaShi HaTaKe
|
|
|
|
|
Hi All,
I've having some problems using a Binary Reader to find a chunk of format information in a WAV file. Basically everything is working fine with my reader using the standard data chunks, thje problem starts when I parse the file for the actual "data" chunk. The WAV standard is a pretty loose file format and allows for all manner of chunks inbetween the basic information and where the actual WAVE data begins. Each chunk has an ID which is either "RIFF", "fmt ", other random ones, then there is a "data" chunk located at a variable location. The code below is shocking, I know hence why i'm here, it works for some files but its also horribly inefficient and is in no way reliable. Can someone enlighten me as to a sure fire way of getting to this "data" chunk?
private void LocateDataChunk(BinaryReader bReader)
{
int _pos = 0
for (int i = _pos; i < (int)riffChunk.dwFileLength; i++)
{
bReader.BaseStream.Position = i;
try
{
dataChunk.sChunkID = new string(bReader.ReadChars(4));
if (dataChunk.sChunkID == "data")
{
dataChunk.dwChunkSize = bReader.ReadUInt32();
return;
}
}
catch (Exception ex)
{
}
}
}
riffChunk.dwFileLength is the uint byte count of the file.
Any help on this would be great.
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
I don't think the code you posted is that bad. You could improve it a little by keeping track of the previous characters so you don't need to reset the position. I did some unscientific tests using a 20mb MemoryStream with the data part at the end.
Original 1x
Keep track of previous characters using an array like a circular queue. ~1.8x speedup
Same as above but treating characters as bytes. ~8x speedup to original
Read whole file into byte[]. ~20x speedup
long pos = 0;
byte[] previous = bReader.ReadBytes(4);
for (long i = bReader.BaseStream.Position; i < length; i++)
{
byte current = bReader.ReadByte();
previous[pos++ % 4] = current;
if (previous[pos % 4] == 'd' && previous[(pos + 1) % 4] == 'a' && previous[(pos + 2) % 4] == 't' && previous[(pos + 3) % 4] == 'a')
{
dataChunk.sChunkID = "data";
dataChunk.dwChunkSize = bReader.ReadUInt32();
return;
}
}
It would seem the text encoding has the biggest impact, I couldn't find many wav files to test so I don't know if my code behaves any better than yours does, of the ones I tested the actual speed impact was negligible maybe something else was causing the slowdown?
Check the files that aren't working in a hex editor (in VS Open File > Open Dropdown > Binary Editor) and make sure you take care between int/unit/long for positions, file lengths e.t.c and the endianess of what your reading.
Think you've already seen this article.
|
|
|
|
|
Thanks for this dave, i'll try your version later. The problem I'm having is that On some files it was taking an extraordinary amount of time to find the "data" string. I was kind of hoping there was some other way to quickly scan to this chunk and get the position rather than try{}catch{} at every position. Reading the file into memory isn't an option as some of the files could be 100mb+ wav files ...
Anyway, i'll try your version later and see how it performs. Really appreciate that. Thank you!
Cheers,
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Why on earth are you reading the file four characters at a time, looking for an identifier? The file is organised into chunks, where the four character identifier is followed by an int containing the size of the chunk. Just read the chunk size and use that to skip to the next chunk, instead of reading throught the chunk four characters at a time.
Besides, there is no guarantee that the chunk size is a multiple of four, so using that method you may just as well miss the next identifier.
Also, you are catching exceptions and just ignoring them, you should never do that. If you re-throw the exception, you will probably find that when the method takes long time to run, you have missed the data identifier so that you are reading the entire file, and then continue to try to read way past the end of the file, as your loop runs for four times the size of the file.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hi Guffa,
This is the first time I've ever attempted dealing with files like this. I'm a C# newbie (as you can tell) your points about this are sound (scuse the pun) and make complete sense.
If you have the time is there any chance you could provide a code example? I'm going to go and have a play with this idea and see what I can do.
Much appreciated.
Cheers,
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Hmm ... it doesn't appear to be as simple as you suggest.
I have pieced together some code that performs as you suggest and i'm testing it with some wav files I have here ... after the initial "RIFF" chunk and the "fmt " chunk I have a file here with a "fact" chunk
public class factChunk
{
public string sChunkID;
public uint dwChunkSize;
public uint dwNumSamples;
}
this particular file reports its factChunk ChunkSize as 4 ...
Looking at this file in Riffpad the chunks are detailed as
RIFF = len 611548 offset=0
fmt = len 18 offset 20
fact = len 4 offset 46
smpl = len 54 offset 58
data = len 611348 offset 120
INFO = len 80 offset 611480
INAM = len 17 offset 611488
IPRD = len 15 offset 611514
ISFT = len 17 offset 611538
I don't see how to make these correlate to positions leading on from each other? Can anyone help?
I'm reading in the RIFF and fmt chunks as they are standard to all wav files, then i'm kicking off a method that attempts to find the other chunks, but its completely failing to do so based on the reported chunk sizes.
public void FindNextChunk(BinaryReader bReader, long offset)
{
_startPos = bReader.BaseStream.Position;
genchunk.ChunkID = new string(bReader.ReadChars(4));
genchunk.ChunkSize = bReader.ReadUInt32();
FindNextChunk(bReader, _startPos + (long)genchunk.ChunkSize);
}
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
The chunk size is not including the identifier and chunk size, so you would have to add 8 to your calculation.
Or simply go from the current position:
bReader.BaseStream.Seek((long)genchunk.ChunkSize, SeekOrigin.Current);
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Brilliant!! Thanks for this Guffa, much appreciated!
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Hi,
I created a Form with a menu bar with a few menu itens pre-made. I've setted the Modifiers to "protected" (or even to public) but when I create an inherited form from this one, the VS (2005) Designer doesn't allow me to add menu itens. Is there any way to make it work?
Thanks,
Dirso
|
|
|
|
|
You remembered that there's "Menu Items", and a "Menu Strip" right?
The menu strip needs to be set to protected or public.
Cheers,
|
|
|
|
|
That' what I did. I can't understand why that "lock" symbol keeps showing in the designer (and all its properties are disabled in the properties window.
Thanks,
Dirso
|
|
|
|
|
Hmmm works fine for me.
Care to post some code?
|
|
|
|
|
I uploaded the solution source code (VS2005) to my website - since I didn't know what code to post - it's only 27k. I hope you can help me.
http://www.jrsoftwares.com.br/JRFW.zip
Thanks,
Dirso
|
|
|
|
|
This worked fine for me:
<br />
<br />
mmMain.Items.Add(new ToolStripMenuItem("Testing"));<br />
<br />
I can't spot why it's breaking on your end.
|
|
|
|
|
Hi,
Not breaking, it works for me too. My question is about the designer: why can't I edit the control at design time?
Thanks,
Dirso
|
|
|
|