# HG changeset patch # User Sebastien Jodogne # Date 1444131369 -7200 # Node ID 4113a9a668b112cc394489888990804e39653051 # Parent 15acdb19d520cab98f2d83bd21e714c2f04e466c refactoring diff -r 15acdb19d520 -r 4113a9a668b1 OrthancServer/DicomProtocol/DicomServer.cpp --- a/OrthancServer/DicomProtocol/DicomServer.cpp Tue Oct 06 13:31:40 2015 +0200 +++ b/OrthancServer/DicomProtocol/DicomServer.cpp Tue Oct 06 13:36:09 2015 +0200 @@ -58,38 +58,34 @@ }; - void DicomServer::ServerThread(DicomServer* server) + void DicomServer::ServerThread(DicomServer* server, + T_ASC_Network *network) { - /* initialize network, i.e. create an instance of T_ASC_Network*. */ - T_ASC_Network *net; - OFCondition cond = ASC_initializeNetwork - (NET_ACCEPTOR, OFstatic_cast(int, server->port_), /*opt_acse_timeout*/ 30, &net); - if (cond.bad()) - { - LOG(ERROR) << "cannot create network: " << cond.text(); - throw OrthancException(ErrorCode_DicomPortInUse); - } - LOG(INFO) << "DICOM server started"; - server->started_ = true; - while (server->continue_) { /* receive an association and acknowledge or reject it. If the association was */ /* acknowledged, offer corresponding services and invoke one or more if required. */ - std::auto_ptr dispatcher(Internals::AcceptAssociation(*server, net)); + std::auto_ptr dispatcher(Internals::AcceptAssociation(*server, network)); - if (dispatcher.get() != NULL) + try { - if (server->isThreaded_) + if (dispatcher.get() != NULL) { - server->bagOfDispatchers_.Add(dispatcher.release()); + if (server->isThreaded_) + { + server->bagOfDispatchers_.Add(dispatcher.release()); + } + else + { + IRunnableBySteps::RunUntilDone(*dispatcher); + } } - else - { - IRunnableBySteps::RunUntilDone(*dispatcher); - } + } + catch (OrthancException& e) + { + LOG(ERROR) << "Exception in the DICOM server thread: " << e.What(); } } @@ -102,12 +98,12 @@ /* drop the network, i.e. free memory of T_ASC_Network* structure. This call */ /* is the counterpart of ASC_initializeNetwork(...) which was called above. */ - cond = ASC_dropNetwork(&net); + OFCondition cond = ASC_dropNetwork(&network); if (cond.bad()) { LOG(ERROR) << "Error while dropping the network: " << cond.text(); } - } + } DicomServer::DicomServer() : @@ -122,8 +118,7 @@ checkCalledAet_ = true; clientTimeout_ = 30; isThreaded_ = true; - continue_ = false; - started_ = false; + continue_ = true; } DicomServer::~DicomServer() @@ -308,14 +303,19 @@ void DicomServer::Start() { Stop(); - continue_ = true; - started_ = false; - pimpl_->thread_ = boost::thread(ServerThread, this); - while (!started_) + /* initialize network, i.e. create an instance of T_ASC_Network*. */ + T_ASC_Network *network; + OFCondition cond = ASC_initializeNetwork + (NET_ACCEPTOR, OFstatic_cast(int, port_), /*opt_acse_timeout*/ 30, &network); + if (cond.bad()) { - Toolbox::USleep(50000); // Wait 50ms + LOG(ERROR) << "cannot create network: " << cond.text(); + throw OrthancException(ErrorCode_DicomPortInUse); } + + continue_ = true; + pimpl_->thread_ = boost::thread(ServerThread, this, network); } diff -r 15acdb19d520 -r 4113a9a668b1 OrthancServer/DicomProtocol/DicomServer.h --- a/OrthancServer/DicomProtocol/DicomServer.h Tue Oct 06 13:31:40 2015 +0200 +++ b/OrthancServer/DicomProtocol/DicomServer.h Tue Oct 06 13:36:09 2015 +0200 @@ -41,6 +41,8 @@ #include #include +struct T_ASC_Network; + namespace Orthanc { class DicomServer : public boost::noncopyable @@ -63,7 +65,8 @@ BagOfRunnablesBySteps bagOfDispatchers_; // This is used iff the server is threaded - static void ServerThread(DicomServer* server); + static void ServerThread(DicomServer* server, + T_ASC_Network *net); public: DicomServer();