NextPVR Forums
  • ______
  • Home
  • New Posts
  • Wiki
  • Members
  • Help
  • Search
  • Register
  • Login
  • Home
  • Wiki
  • Members
  • Help
  • Search
NextPVR Forums Public Developers v
« Previous 1 … 10 11 12 13 14 … 93 Next »
Handling simultaneous HTTP requests

 
  • 0 Vote(s) - 0 Average
Handling simultaneous HTTP requests
bgowland
Offline

Posting Freak

West Yorkshire, UK
Posts: 4,583
Threads: 384
Joined: Dec 2004
#1
2012-01-27, 08:35 PM
For the nDroidService I have a thread which uses the HttpListener class in synchronous mode. I create the listener instance and then use...
Code:
listener.GetContext();
...which blocks the thread until a request comes in. I then process the request and respond within the same thread and loop back to that line to listen/block again.

I want to expand the code to just simply take a request and put it out to a different thread to handle in its own time rather than process in a synchronous manner. I've looked at the BeginGetContext method of HttpListener but I can't get my head around how to use it and I'm not sure it's what I want anyway.

Anybody have any ideas for handling multiple HTTP connections/requests/responses simultaneously?
whurlston
Offline

Posting Freak

Posts: 7,885
Threads: 102
Joined: Nov 2006
#2
2012-01-27, 08:46 PM
Have you looked at http://msdn.microsoft.com/en-us/library/...ntext.aspx ?
bgowland
Offline

Posting Freak

West Yorkshire, UK
Posts: 4,583
Threads: 384
Joined: Dec 2004
#3
2012-01-27, 09:06 PM
whurlston Wrote:Have you looked at http://msdn.microsoft.com/en-us/library/...ntext.aspx ?
Yes thanks, in fact I keep coming back to that page with the numerous searches I've done today. It explains how to handle a single request/response asynchronously but I don't get how to handle multiple async requests with that API. I'm possibly just missing the point somewhere - perhaps the programmer part of my brain just isn't working today.
whurlston
Offline

Posting Freak

Posts: 7,885
Threads: 102
Joined: Nov 2006
#4
2012-01-27, 10:56 PM
The example is a console app so it exits after it gets a single request and responds to it. In a windows form (or service) where you are not needing to end the application and return to the command line, you would simply remove the following two lines from the main code block:
Code:
result.AsyncWaitHandle.WaitOne();
    listener.Close();
It should then respond to any requests until the listener is closed. You would close the listener when closing the form or stopping the service.

I can do a quick example for you later tonight. Do you want a service, NPVR plugin or some other example?
whurlston
Offline

Posting Freak

Posts: 7,885
Threads: 102
Joined: Nov 2006
#5
2012-01-27, 11:14 PM (This post was last modified: 2012-01-27, 11:19 PM by whurlston.)
I actually found a pretty good example: http://www.west-wind.com/weblog/posts/20...es-of-code

Edit: and another: http://www.dreamincode.net/forums/topic/...threading/

Apparently, you have to start another instance of IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); after you finish handling one.
bgowland
Offline

Posting Freak

West Yorkshire, UK
Posts: 4,583
Threads: 384
Joined: Dec 2004
#6
2012-01-28, 03:13 AM
whurlston Wrote:I actually found a pretty good example: http://www.west-wind.com/weblog/posts/20...es-of-code
Thanks for taking the time to help - I just seemed to be going around in circles doing searches. That example goes a bit further to help understand it than the MSDN code snippets - I'll give it a go.

Cheers,
Brian
Ommina
Offline

Senior Member

Posts: 330
Threads: 39
Joined: Feb 2006
#7
2012-01-28, 08:49 AM
whurlston Wrote:Apparently, you have to start another instance of IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); after you finish handling one.

The route I take:

Create the HttpListener
Add prefixes
Spin up a thread using QueueUserWorkItem, passing to the target method the listener object.

Inside the target method:

.Start() the listener
Start a loop on a class variable (_KeepRunning or whatever)
Call .BeginGetContext, giving it method DoStuff
.WaitOne on the IAsynchResult returned
Back to start of loop

Do your actual stuff in the DoStuff.

Keep in mind that both the original target method and DoStuff will be on new threads, so all the cross-thread considerations need to be, uh, considered.

So you have your original method that starts the ball rolling. It, in turn, start methods B on a new thread. Method B contains a loop that repeatedly calls BeginGetContext after .WaitOne returns. Method C does stuff.

And I really hope that I didn't just confuse things even more. . .
bgowland
Offline

Posting Freak

West Yorkshire, UK
Posts: 4,583
Threads: 384
Joined: Dec 2004
#8
2012-01-28, 09:23 AM
Ommina Wrote:The route I take:

Spin up a thread using QueueUserWorkItem, passing to the target method the listener object.
Interesting. I also came across this in my searches but haven't tried it yet. Thanks.
bgowland
Offline

Posting Freak

West Yorkshire, UK
Posts: 4,583
Threads: 384
Joined: Dec 2004
#9
2012-01-29, 12:51 AM
Thanks both for the suggestions.

I persevered with the west-wind.com example linked by whurlston and now have it working. Had a few problems along the way and it still needs some tweaks but my code is pretty close to what I want now. Big Grin

Cheers,
Brian
« Next Oldest | Next Newest »

Users browsing this thread: 2 Guest(s)



Possibly Related Threads…
Thread Author Replies Views Last Post
  Touch and http audio url's mvallevand 0 1,413 2013-11-03, 07:05 PM
Last Post: mvallevand
  NewStyleButtonListPlugin - handling keypresses psycik 3 1,591 2012-12-14, 07:17 AM
Last Post: whurlston
  A few requests imilne 10 3,880 2011-08-07, 07:53 AM
Last Post: imilne
  NPVR HTTP streaming bug? tmrt 3 1,849 2010-12-28, 11:48 PM
Last Post: tmrt
  Handling registry redirection in .NET bgowland 2 1,832 2008-11-26, 10:40 AM
Last Post: bgowland
  http://www.mtvmusic.com/ ??? herrmannj 6 4,146 2008-10-30, 05:27 PM
Last Post: InVermont
  Unhandled exception handling in GB-PVR ubu 7 2,310 2007-02-18, 02:54 AM
Last Post: sub
  "Skin Engine" requests MixMan 0 1,551 2006-05-13, 06:30 PM
Last Post: MixMan
  Tricks for handling number keys? (C#) bgowland 1 1,280 2005-11-14, 07:31 PM
Last Post: psycik
  My Pictures Feature Requests reven 26 6,660 2005-07-09, 05:35 AM
Last Post: reven

  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Designed by D&D, modified by NextPVR - Powered by MyBB

Linear Mode
Threaded Mode