Re: [twsocket] TSslWSocket in a non-blocking multithreaded server
Hi Arno Once again many thanks for the quick reply. I have now implemented a working multithreaded test (server) application which uses both the TWSocket and the TSslWSocket in a non-blocking manner. The "regular" WSocket is used to accept incoming sessions, which are then carried on in a new client thread in which the sockethandle is "dupped" onto a TSslWSocket for further processing. I just have to be sure that I understood your reference to f_ERR_remove_state(unsigned ThreadID). Should I call this function just before the Thread's execute method returns, sending the threads ThreadID as param ? I have provided my sample code, for the benefit of others who had the same initial problem as me. If you can spare the time, I would be grateful if you could please verify that my use of the TSslWSocket is correct ? Excerpt of my sample code is listed below: Best regards Kurt ***Main Thread*** __fastcall TForm2::TForm2(TComponent* Owner) : TForm(Owner) { ... // Enable safe OpenSSL Multithreading before creating any ICS SSL sockets m_pSslDynamicLock = new TSslDynamicLock(NULL); m_pSslDynamicLock->Enabled = true; // Shared (thread-safe) SSL context m_pSslContext = new TSslContext(NULL); m_pSslContext->SslCertFile = "c:\\MyCertificate.pem"; m_pSslContext->SslPassPhrase= "password"; m_pSslContext->SslPrivKeyFile = "c:\\MyKey.pem"; m_pSslContext->SslCAFile= "c:\\root.cer"; m_pSslContext->SslCAPath= ""; m_pSslContext->SslVerifyPeer= false;// No client certificate verification // Listen for incoming connections m_pServerSocket = new TWSocket(NULL); m_pServerSocket->OnSessionAvailable = ServerSocketSessionAvailable; m_pServerSocket->Addr = "0.0.0.0"; m_pServerSocket->Port = "443"; m_pServerSocket->MultiThreaded = false;// We're in mainthread and should be fine. m_pServerSocket->Listen(); } //-- - void __fastcall TForm2::ServerSocketSessionAvailable(TObject *Sender, WORD ErrCode) { try { // Accept incoming connection request unsigned int nSocketHandle = m_pServerSocket-> Accept(); // Do we have a valid Socket handle ? if( nSocketHandle > 0 && nSocketHandle < INVALID_SOCKET - 1 ) { // Thread initially created as suspended TSslClientThread* pClientThread = new TSslClientThread( m_hVclComponentCreationMutex, m_pSslContext, nSocketHandle ); pClientThread->OnTerminate = OnSslClientThreadTerminate; pClientThread->OnDebug = OnSslClientThreadDebug; // Add reference to thread, to keep track of session count m_SessionList.Add( pClientThread ); // Resume Thread pClientThread->Resume(); } } catch(Exception& ex) { DebugLog("TForm2::ServerSocketSessionAvailable, exception: " + ex.Message); } UpdateSessionCount(); } //-- - void __fastcall TForm2::OnSslClientThreadTerminate(TObject* Sender) { if( Sender != NULL && Sender->ClassNameIs("TSslClientThread") ) { TSslClientThread* pThread = (TSslClientThread*)Sender; // Remove thread from list of active sessions int nIndex = m_SessionList.IndexOf( pThread ); // Valid index ? if( nIndex >= 0 && nIndex < m_SessionList.Count ) { m_SessionList.Remove( nIndex );// Only remove reference, not deleting obj. } UpdateSessionCount(); } } //-- - ***Session Thread*** __fastcall TSslClientThread::TSslClientThread(HANDLE hMutex, TSslContext* pSslContext, unsigned int nSocketHandle ) : TThread(true)// create suspended { OnDebug = NULL; m_hVclComponentCreationMutex = hMutex; m_pSslContext = pSslContext; m_nSocketHandle = nSocketHandle; FreeOnTerminate = true; } //-- - void __fastcall TSslClientThread::Execute() { // Init members // using a shared Mutex to avoid errors with components calling AllocateHwnd/DeallocateHwnd, which are not thread safe WaitForSingleObject( m_hVclComponentCreationMutex, INFINITE ); Init();
Re: [twsocket] New SVN URLs. Please switch your work copies!
>Note that the old icsv7 branch doesn't get any more >updates, updates go to the trunk from now. What's the difference between old and new v7 ? -- Anton -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be
Re: [twsocket] New SVN URLs. Please switch your work copies!
Anton S. wrote: >> Note that the old icsv7 branch doesn't get any more >> updates, updates go to the trunk from now. > > What's the difference between old and new v7 ? It supports RAD Studio XE. The new POP3 client might break existing code, read the comments in OverbyteIcsPop3Prot.pas. I suggest you read the change log with a SVN client, unfortunately the change log published on the wiki page doesn't list merged revisions. -- Arno Garrels -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be
Re: [twsocket] TSslWSocket in a non-blocking multithreaded server
Kurt Andersen wrote: > I have now implemented a working multithreaded test (server) > application which uses both the TWSocket and the TSslWSocket in a > non-blocking manner. Just a side note to your design "One Thread Per TWSocket Instance", it's not optimal IMO. TWSocket objects allocate at least one shared, hidden window per thread which is waste of resources and decreases the maximum possible number of concurrent client connections. TWSocket actually works non-blocking it uses the non-blocking winsock API. What is blocking the socket I/O is lengthy tasks running in the same thread context. > I just have to be sure that I understood your reference to > f_ERR_remove_state(unsigned ThreadID). > Should I call this function just before the Thread's execute method > returns, sending the threads ThreadID as param ? Yes, it is a cleanup function. -- Arno Garrels -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be
Re: [twsocket] TSslWSocket in a non-blocking multithreaded server
Hello, AFAIU, you are suggesting multiple-clients/thread design. We have a source code example of that (C++) at, http://www.fastream.com/webstresstester.php HTH, SZ On Wed, Sep 1, 2010 at 12:30 PM, Arno Garrels wrote: > Kurt Andersen wrote: > > > I have now implemented a working multithreaded test (server) > > application which uses both the TWSocket and the TSslWSocket in a > > non-blocking manner. > > Just a side note to your design "One Thread Per TWSocket Instance", > it's not optimal IMO. TWSocket objects allocate at least one shared, > hidden window per thread which is waste of resources and decreases > the maximum possible number of concurrent client connections. > TWSocket actually works non-blocking it uses the non-blocking > winsock API. What is blocking the socket I/O is lengthy tasks > running in the same thread context. > > > I just have to be sure that I understood your reference to > > f_ERR_remove_state(unsigned ThreadID). > > Should I call this function just before the Thread's execute method > > returns, sending the threads ThreadID as param ? > > Yes, it is a cleanup function. > > -- > Arno Garrels > -- > To unsubscribe or change your settings for TWSocket mailing list > please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket > Visit our website at http://www.overbyte.be > -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be
Re: [twsocket] TSslWSocket in a non-blocking multithreaded server
Arno, So what you're suggesting is dropping the threads, if I only have event-based and non lengthy message handling in my OnDataAvailable event - thus giving my server app more headroom for concurrent sessions ? On a side note, I have been hammering away on my Server app using a (homemade) multithreaded SSL client, and sometimes I get a ErrCode = 1 in the OnHandShakeDone event. Do you have any idea as to what might be causing this ? Is there any way to (re)Start the handshake after the error occurred, without closing the connection first ? Best regards Kurt -Oprindelig meddelelse- Fra: twsocket-boun...@elists.org [mailto:twsocket-boun...@elists.org] På vegne af Arno Garrels Sendt: 1. september 2010 11:31 Til: ICS support mailing Emne: Re: [twsocket] TSslWSocket in a non-blocking multithreaded server Kurt Andersen wrote: > I have now implemented a working multithreaded test (server) > application which uses both the TWSocket and the TSslWSocket in a > non-blocking manner. Just a side note to your design "One Thread Per TWSocket Instance", it's not optimal IMO. TWSocket objects allocate at least one shared, hidden window per thread which is waste of resources and decreases the maximum possible number of concurrent client connections. TWSocket actually works non-blocking it uses the non-blocking winsock API. What is blocking the socket I/O is lengthy tasks running in the same thread context. > I just have to be sure that I understood your reference to > f_ERR_remove_state(unsigned ThreadID). > Should I call this function just before the Thread's execute method > returns, sending the threads ThreadID as param ? Yes, it is a cleanup function. -- Arno Garrels -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be
Re: [twsocket] TSslWSocket in a non-blocking multithreaded server
Hi Thanks for the example. My application is a server using SSL encryption to transport a custom protocol, where as your example is a "load-test client" as far as I can tell. However the idea of having multiple clients grouped together on a smaller number of separate (server) threads would minimize my resource consumption. But as far as I understand Arno, he would recommend not going multithreaded unless absolutely necessary e.g. lengthy operation which blocks socket I/O. Perhaps a combination of my previous code and a fixed number of server threads in a "thread pool" could be applied. When an incoming session is available I could switch between the available server threads e.g. serverthread[index++]->handleRequest(...) if( index >= MaxServerThreads ) { Index = 0; } Best regards Kurt -Oprindelig meddelelse- Fra: twsocket-boun...@elists.org [mailto:twsocket-boun...@elists.org] På vegne af Fastream Technologies Sendt: 1. september 2010 11:37 Til: ICS support mailing Emne: Re: [twsocket] TSslWSocket in a non-blocking multithreaded server Hello, AFAIU, you are suggesting multiple-clients/thread design. We have a source code example of that (C++) at, http://www.fastream.com/webstresstester.php HTH, SZ On Wed, Sep 1, 2010 at 12:30 PM, Arno Garrels wrote: > Kurt Andersen wrote: > > > I have now implemented a working multithreaded test (server) > > application which uses both the TWSocket and the TSslWSocket in a > > non-blocking manner. > > Just a side note to your design "One Thread Per TWSocket Instance", > it's not optimal IMO. TWSocket objects allocate at least one shared, > hidden window per thread which is waste of resources and decreases > the maximum possible number of concurrent client connections. > TWSocket actually works non-blocking it uses the non-blocking > winsock API. What is blocking the socket I/O is lengthy tasks > running in the same thread context. > > > I just have to be sure that I understood your reference to > > f_ERR_remove_state(unsigned ThreadID). > > Should I call this function just before the Thread's execute method > > returns, sending the threads ThreadID as param ? > > Yes, it is a cleanup function. > > -- > Arno Garrels > -- > To unsubscribe or change your settings for TWSocket mailing list > please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket > Visit our website at http://www.overbyte.be > -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be
Re: [twsocket] TSslWSocket in a non-blocking multithreaded server
Kurt Andersen wrote: > But as far as I understand Arno, he would recommend not going > multithreaded unless absolutely necessary > e.g. lengthy operation which blocks socket I/O. Yes, I prefer this, it's easier to write and to debug unless there's no need to utililize multiple CPUs for the socket I/O. However I'd move lengthy blocking tasks such as database queries into worker threads (thread pool with a request queue preferably). > > Perhaps a combination of my previous code and a fixed number of > server threads in a "thread pool" could be applied. > When an incoming session is available I could switch between the > available server threads One thread per CPU was OK if you want the socket I/O benefit from multiple CPUs, however lengthy blocking code executed in the same thread context would still block some socket I/O, so moving your lengthy blocking code into worker threads was required anyway. Your current design might not be optimal, however it might be sufficient depending on your needs, it's easier to write anyway. -- Arno Garrels -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be
Re: [twsocket] TSslWSocket in a non-blocking multithreaded server
Kurt Andersen wrote: > On a side note, I have been hammering away on my Server app using a > (homemade) multithreaded SSL client, and sometimes > I get a ErrCode = 1 in the OnHandShakeDone event. > > Do you have any idea as to what might be causing this ? It can be anything. Specific SSL error codes are currently not passed to the OnHandShakeDone event. Logging with TIcsLogger is the only way to find the reason for such errors. > > Is there any way to (re)Start the handshake after the error occurred, > without closing the connection first ? I do not think so, the connection is closed on any handshake error AFAIR. -- Arno Garrels > > Best regards > Kurt > > > > -Oprindelig meddelelse- > Fra: twsocket-boun...@elists.org [mailto:twsocket-boun...@elists.org] > På vegne af Arno Garrels > Sendt: 1. september 2010 11:31 > Til: ICS support mailing > Emne: Re: [twsocket] TSslWSocket in a non-blocking multithreaded > server > > Kurt Andersen wrote: > >> I have now implemented a working multithreaded test (server) >> application which uses both the TWSocket and the TSslWSocket in a >> non-blocking manner. > > Just a side note to your design "One Thread Per TWSocket Instance", > it's not optimal IMO. TWSocket objects allocate at least one shared, > hidden window per thread which is waste of resources and decreases > the maximum possible number of concurrent client connections. > TWSocket actually works non-blocking it uses the non-blocking > winsock API. What is blocking the socket I/O is lengthy tasks > running in the same thread context. > >> I just have to be sure that I understood your reference to >> f_ERR_remove_state(unsigned ThreadID). >> Should I call this function just before the Thread's execute method >> returns, sending the threads ThreadID as param ? > > Yes, it is a cleanup function. > > -- > Arno Garrels > -- > To unsubscribe or change your settings for TWSocket mailing list > please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket > Visit our website at http://www.overbyte.be -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be
Re: [twsocket] New SVN URLs. Please switch your work copies!
> It supports RAD Studio XE. > The new POP3 client might break existing code, read the > comments in OverbyteIcsPop3Prot.pas. So the updated POP3 is now in main trunk, not anymore on that other separate URL? What about those EXPERIMENTAL timeout and throttling... can we count on these to stay or is it more likely they will be removed from code? I think they are very useful things to have handy as you always need such things in nearly any app. -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be
Re: [twsocket] New SVN URLs. Please switch your work copies!
Zvone wrote: >> It supports RAD Studio XE. >> The new POP3 client might break existing code, read the >> comments in OverbyteIcsPop3Prot.pas. > > So the updated POP3 is now in main trunk, not anymore on that other > separate URL? Yes, with one reverted change, event OnDisplay uses "String" again. > What about those EXPERIMENTAL timeout and throttling... can we count > on these to stay or is it more likely they will be removed from code? Unless it turns out as totally crappy implementation I think they will not be removed. I use them in two simple client applications and they seem to work fine, AFAIK Angus also uses the throttle in production since quite a while, don't know if server or client. > I think they are very useful things to have handy as you always need > such things in nearly any app. That's true, they have been requested by users many many times in this list. Please test them very hard and report how it works for you. -- Arno Garrels -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be
Re: [twsocket] New SVN URLs. Please switch your work copies!
> AFAIK Angus also uses the throttle in > production since quite a while, don't know if server or client. I added EXPERIMENTAL_THROTTLE to the ICS FTP server component to slow down downloads on LANs, much easier to test clients that way. I thought you had added EXPERIMENTAL_THROTTLE to the ICS FTP client to replace the existing UseBandwidthControl implementation but it does not seem to be in the latest v7, it must have been a test version. Angus -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be
Re: [twsocket] New SVN URLs. Please switch your work copies!
Angus Robertson - Magenta Systems Ltd wrote: >> AFAIK Angus also uses the throttle in >> production since quite a while, don't know if server or client. > > I added EXPERIMENTAL_THROTTLE to the ICS FTP server component to slow > down downloads on LANs, much easier to test clients that way. > > I thought you had added EXPERIMENTAL_THROTTLE to the ICS FTP client to > replace the existing UseBandwidthControl implementation but it does > not seem to be in the latest v7, it must have been a test version. Yes, it is still a private copy but it works . -- Arno Garrels -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be
Re: [twsocket] New SVN URLs. Please switch your work copies!
>I suggest you read the change log with a SVN client, I've read it just now, and all it said was about XE support :) I'll seek changes with WinMerge. -- Anton -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be
Re: [twsocket] New SVN URLs. Please switch your work copies!
Anton S. wrote: >> I suggest you read the change log with a SVN client, > > I've read it just now, and all it said was about XE support :) In the TortoiseSVN log view you have to tick checkbox "Include merged revisions" to see all changes. -- Arno Garrels -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be