Article View: borland.public.cppbuilder.internet.socket
Article #3383Simple e-mail server
From: Vladimir Stefanovic
Date: Mon, 26 Sep 2005 11:47
Date: Mon, 26 Sep 2005 11:47
133 lines
4308 bytes
4308 bytes
Hi,
I would like to make a *simple* e-mail server, and I have
a couple of dilemmas which are mainly related to stable
functioning of the whole system.
By that I mean to be sure that my threads, critical sections
and lists are coordinating properly, without collisions and
bottle necks.
My imaginary e-mail server (Service Application + TServerSocket)
should:
* receive requests from clients by ServerSocketGetThread(),
ie, the threads should just make Add()s to the TStringList
* process the TStringList (send emails) and Delete()
processed Items
QUESTION 1:
Is try/catch needed at all in case of spawning the thread in
a service (to prevent the service from shuting down because of
unhandled exception)?
void __fastcall TEmailService::ServerSocketGetThread(TObject *Sender,
TServerClientWinSocket *ClientSocket,
TServerClientThread *&SocketThread)
{
try
{
SocketThread = new TEmailServerThread( false, ClientSocket );
}
catch ( const Exception &e )
{
// ... just swallow not to stop the service ...
}
}
QUESTION 2:
Should I place Enter/Leave/CriticalSection here or in the
body of AddRequestToSharedStringList(), like I already did,
and is there any difference?
void __fastcall TEmailServerThread::ClientExecute()
{
// ...
AddRequestToSharedStringList( /* ARGS */ );
// ...
}
QUESTION 3:
Can I trust to '__finally' or it's better to use RAII?
void __fastcall TEmailServerThread::AddRequestToSharedStringList( /* ARGS */ )
{
try
{
EnterCriticalSection( &EmlSrvCriticalSection );
try
{
MyStringList->Add ( /* ARGS */ );
}
catch ( const Exception &e )
{
// ...
}
}
__finally
{
LeaveCriticalSection( &EmlSrvCriticalSection );
}
}
QUESTION 4:
Where should I place the sending code? Is the ServiceExecute()
appropriate because if bottle neck?
void __fastcall TEmailService::ServiceExecute(TService *Sender)
{
while ( !Terminated )
{
if ( !SendingInProgress )
{
try
{
EnterCriticalSection( &EmlSrvCriticalSection );
SendingInProgress = true;
SendEmailsFromList(); // send the *whole* list
}
__finally
{
SendingInProgress = false;
LeaveCriticalSection( &EmlSrvCriticalSection );
}
}
// This prevents Windows Task Manager to report 100% CPU usage
Sleep ( 500 );
ServiceThread->ProcessRequests( false );
}
}
QUESTION 5:
In the code above, I send all the list within a CriticalSection.
Is it smarter to spawn the CriticalSection in the body of
SendEmailsFromList() and spaw it for *every* single item to be sent,
and so leave ServerSocketGetThread() to 'breathe' ?
The reason why I wanted first to store the requests in the list and
not to send them immedatelly is because I wanted:
* to keep the threads as simple as they can be
* that one request (protocol) can hold more than one e-mail request
to be sent, like |email1|email2|email3|...
* the system to be flexible if I decide to make SMS server, which will
not be as fast as sending emails.
QUESTION 6:
If all this is a bad idea can you please suggest me something better
to start with?
Best regards,
Vladimir Stefanovic
Technical Details
Message-ID:
<4337dfe1$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 11508e,97b4b095375844bd,start X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Vladimir Stefanovic" <antivari@po.sbb.co.yu> Sender: "Vladimir Stefanovic" <antivari@po.sbb.co.yu> Reply-To: "Vladimir Stefanovic" <antivari@po.sbb.co.yu> Subject: Simple e-mail server Newsgroups: borland.public.cppbuilder.internet.socket X-User-Info: 80.93.241.226 X-Forwarded-For: 128.128.129.165 NNTP-Posting-Host: 207.105.83.66 Message-ID: <4337dfe1$1@newsgroups.borland.com> Date: 26 Sep 2005 04:47:45 -0700 X-Trace: newsgroups.borland.com 1127735265 207.105.83.66 (26 Sep 2005 04:47:45 -0700) Lines: 134 Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail