ServerMain.cpp is considerably different to the non service samples. The main for our service is boiler-plate code which is implemented by the library and we use a macro to instantiate this code and configure it.
#include "JetByteTools\Admin\Admin.h" #include "Service.h" #pragma hdrstop #include "JetByteTools\ServiceTools\ServiceMain.h" /////////////////////////////////////////////////////////////////////////////// // Implement our main() /////////////////////////////////////////////////////////////////////////////// IMPLEMENT_SERVICE_MAIN(_T("Echo Server Service"), CService, _T("EchoServerService.log"));
class CService : public JetByteTools::Service::CService { public : CService(); virtual ~CService(); private : // Implement CService interface virtual void OnServiceInstalled(); virtual void OnServiceRemoved(); virtual void InitialiseService( JetByteTools::Service::INotifyProgress ¬ify); virtual void ContinueService( JetByteTools::Service::INotifyProgress ¬ify); virtual void PauseService( JetByteTools::Service::INotifyProgress ¬ify); virtual void StopService( JetByteTools::Service::INotifyProgress ¬ify); virtual void UninitialiseService( JetByteTools::Service::INotifyProgress ¬ify); virtual void OnException( const JetByteTools::Win32::CException &e); virtual void OnException( const JetByteTools::Win32::CSEHException &e); virtual void OnException();
void CService::InitialiseService( INotifyProgress ¬ify) { notify.Notify(1000); m_pLockFactory = new CSharedCriticalSection(47); m_pPool = new CIOPool(0); // number of threads (0 = 2 x processors) m_pPool->Start(); notify.Notify(1000); m_pSocketAllocator = new CSequencedStreamSocketAllocator( *m_pLockFactory, 10); // max number of sockets to keep in the pool m_pBufferAllocator = new CBufferAllocator( 1024, // buffer size 10); // max number of buffers to keep in the pool notify.Notify(1000); const CAddressIPv4 address( INADDR_ANY, // address to listen on 5050); // port to listen on const size_t listenBacklog = 5; m_pLimiter = new CConnectionLimiter(CConnectionLimiter::NoLimit); m_pServer = new CSocketServer( "Welcome to echo server\r\n", address, listenBacklog, *m_pPool, *m_pSocketAllocator, *m_pBufferAllocator, *m_pLimiter); notify.Notify(5000); m_pServer->Start(); notify.Notify(1000); }
void CService::UninitialiseService( INotifyProgress ¬ify) { notify.Notify(5000); m_pServer->WaitForShutdownToComplete(); notify.Notify(5000); m_pPool->WaitForShutdownToComplete(); notify.Notify(5000); m_pBufferAllocator->Flush(); notify.Notify(5000); m_pSocketAllocator->ReleaseSockets(); delete m_pServer; m_pServer = 0; delete m_pLimiter; m_pLimiter = 0; delete m_pPool; m_pPool = 0; delete m_pBufferAllocator; m_pBufferAllocator = 0; delete m_pSocketAllocator; m_pSocketAllocator = 0; delete m_pLockFactory; m_pLockFactory = 0; }
true to a constructor parameter which specified if "multiple instances" are supported. Multiple instances are a powerful feature of the JetByte Service Tools Library that allow you to install a service multiple times, each with a unique instance name. You can then run each service independently and when they start they have access to the instance name that they're running under. This allows you to install and run a service configured in multiple ways or providing access to different servers on different ports, or whatever. See the Echo Server Multi Instance Service example for more details.
1.5.3