Jump to content
aehimself

TSslHttpServer in console application

Recommended Posts

Hello,
I'm sorry if this topic was discussed several times, I was able to find only a REALLY low amount of information and none of them seemed to work.
I need to create a TSslHttpServer (in runtime) and serve the client requests. Everything went fine in my test app so I started to port it to it's final state but it refused to work. Port is opened but no events are being fired. Since I already met this with the standard TClientSocket / TServerSocket so I quickly put my message pump generator in the Repeat...Until Terminated cycle in my main thread.
No joy, so I started to investigate. I already found that I should do something with the NOFORMS directive but I was unable to make it work. Result is always the same: connection to the opened port is possible, but no events are fired in my Delphi app, nor the connection responds.

- I added the NOFORMS directive to the ICS install package and rebuilt all
- I added the NOFORMS directive to my app and rebuilt all
- I added the {$DEFINE NOFORMS} to my app's dpr
- tried enabling or disabling the MultiThreaded property
- Tried moving to my messagepump to SslHttpServer.OnMessagePump
- Tried SslHttpServer.ProcessMessages, .MessagePump, .MessageLoop

I also mixed the above, meaning tried each combination of each message processor method with each directive.

 

TMyThread = Class(TThread)
strict private
 myhttpsrv: TSslHttpServer;
[...]

Constructor TMyThread.Create;
Begin
 myhttpsrv := TSSlHttpServer.Create(nil);
 myhttpsrv.OnClientConnect := WebServerClientConnect;
 myhttpsrv.OnMessagePump := WebServerMessagePump;
[...]

Procedure TMyThread.Execute;
Begin
 Repeat
  If Not _httpsrv.ListenAllOK Then _httpsrv.Start;
//  myhttpsrv.ProcessMessages;
//  myhttpsrv.MessageLoop;
//  myhttpsrv.MessagePump;
 Until Terminated;
[...]

Procedure TMyThread.WebServerClientConnect(Sender: TObject);
Begin
 WriteLn('Client connected...');
[...]

Procedure TMyThread.WebServerMessagePump(Sender: TObject);
Begin
If PeekMessage(msg, 0, 0, 0, 0) Then Begin
 GetMessage(msg, 0, 0, 0);
 TranslateMessage(msg);
 DispatchMessage(msg);
End;
[...]

 

What I am doing wrong? Any ideas on how I can make it work?
Thanks!

Share this post


Link to post

As far as I understand the code you show, you thread's Execute method doesn't have the message pump. So no event is triggered and nothing works.

 

Another error in your code is that you create the HTTP component within the thread's Create method, which is executed in the context of the main thread! You must create the component in the context of the thread, that is at the beginning of the Execute method. Alternatively, you may call the ThreadAttach method to attaché the component created in another context to the thread's context. First solution is better. Also destruction must be done at the end of the Execute procedure.

 

There are several multithreaded samples delivered with ICS. You can follow client or server sample, as far as multithreading is concerned, it makes no difference.

 

Regards.

 

Share this post


Link to post

Hello,

Thanks for the insanely fast reply, I truly appreciate it!

All the code in my threads Execute method was uncommented once (I just commented them out when they did not bring the desired result) so my message pump was indeed in place.

 

The only thing I did not consider is that the component is taking the caller thread as it's parent, and not where it is actually located! When I create the component in Execute it starts to work!!! Thank you very much, I would have never guessed this.

 

Due to the nature of how I see code - is there a way to keep the creation in the OnCreate, and destriction in the Destroy method of the thread? I do not wish to create and assign a Client thread to each connection (Win2000 and Win2003 has really bad thread handling and I would like to stay compatible!) and a simple myhttpsrv.ThreadAttach in the beginning of the Execute does not bring joy.

 

If not - I'll just stick to this solution.

 

Thank you again!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×