fixed segfault during shutdown

This commit is contained in:
badaix 2016-05-01 13:22:34 +02:00
parent 1c3fd27256
commit 679fbaaca0

View file

@ -106,9 +106,12 @@ void StreamServer::onDisconnect(StreamSession* streamSession)
clientInfo->connected = false; clientInfo->connected = false;
gettimeofday(&clientInfo->lastSeen, NULL); gettimeofday(&clientInfo->lastSeen, NULL);
Config::instance().save(); Config::instance().save();
if (controlServer_ != nullptr)
{
json notification = JsonNotification::getJson("Client.OnDisconnect", clientInfo->toJson()); json notification = JsonNotification::getJson("Client.OnDisconnect", clientInfo->toJson());
controlServer_->send(notification.dump()); controlServer_->send(notification.dump());
} }
}
void StreamServer::onMessageReceived(ControlSession* controlSession, const std::string& message) void StreamServer::onMessageReceived(ControlSession* controlSession, const std::string& message)
@ -357,32 +360,58 @@ void StreamServer::handleAccept(socket_ptr socket)
void StreamServer::start() void StreamServer::start()
{ {
// throw SnapException("good"); try
{
controlServer_.reset(new ControlServer(io_service_, settings_.controlPort, this)); controlServer_.reset(new ControlServer(io_service_, settings_.controlPort, this));
controlServer_->start(); controlServer_->start();
streamManager_.reset(new StreamManager(this, settings_.sampleFormat, settings_.codec, settings_.streamReadMs)); streamManager_.reset(new StreamManager(this, settings_.sampleFormat, settings_.codec, settings_.streamReadMs));
// throw SnapException("xxx");
//TODO: check uniqueness of the stream //TODO: check uniqueness of the stream
for (auto& streamUri: settings_.pcmStreams) for (const auto& streamUri: settings_.pcmStreams)
logO << "Stream: " << streamManager_->addStream(streamUri)->getUri().toJson() << "\n"; {
// throw SnapException("bad"); PcmStream* stream = streamManager_->addStream(streamUri);
if (stream != NULL)
logO << "Stream: " << stream->getUri().toJson() << "\n";
}
streamManager_->start(); streamManager_->start();
acceptor_ = make_shared<tcp::acceptor>(*io_service_, tcp::endpoint(tcp::v4(), settings_.port)); acceptor_ = make_shared<tcp::acceptor>(*io_service_, tcp::endpoint(tcp::v4(), settings_.port));
startAccept(); startAccept();
} }
catch (const std::exception&)
{
stop();
throw;
}
}
void StreamServer::stop() void StreamServer::stop()
{
if (controlServer_)
{ {
controlServer_->stop(); controlServer_->stop();
controlServer_ = nullptr;
}
if (acceptor_)
{
acceptor_->cancel(); acceptor_->cancel();
acceptor_ = nullptr;
}
if (streamManager_)
{
streamManager_->stop(); streamManager_->stop();
streamManager_ = nullptr;
}
// std::lock_guard<std::mutex> mlock(sessionsMutex_); // std::lock_guard<std::mutex> mlock(sessionsMutex_);
for (auto session: sessions_)//it = sessions_.begin(); it != sessions_.end(); ++it) for (auto session: sessions_)//it = sessions_.begin(); it != sessions_.end(); ++it)
{
if (session)
session->stop(); session->stop();
} }
}