From 136807bc478fb8ce675b3b8456efaea2716379eb Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Sun, 12 Mar 2006 20:33:04 +0000 Subject: [PATCH] Connection failure detection --- command.cc | 21 ++++++++----- i18n.cc | 1 + player.cc | 22 ++++++++++++++ remote.cc | 5 ++++ remote.h | 1 + tcp.cc | 2 +- timers.cc | 2 +- vdr.cc | 79 ++++++++++++++++++++++++++----------------------- vepgsettimer.cc | 5 ++++ vepgsettimer.h | 1 + vlivebanner.cc | 5 ++-- vvideolive.cc | 2 +- vwelcome.cc | 12 ++++++++ 13 files changed, 108 insertions(+), 50 deletions(-) diff --git a/command.cc b/command.cc index a65538d..286e1bd 100644 --- a/command.cc +++ b/command.cc @@ -337,6 +337,7 @@ void Command::doFromTheTop(bool which) connLost->draw(); viewman->add(connLost); viewman->updateView(connLost); + remote->clearBuffer(); } else { @@ -618,18 +619,24 @@ void Command::doJustConnected(VConnect* vconnect) vdr->configSave("General", "Last Power State", "On"); + // Make sure connection didn't die + if (!vdr->isConnected()) + { + Command::getInstance()->connectionLost(); + } + else + { + viewman->removeView(vi); - viewman->removeView(vi); - - VWelcome* vw = new VWelcome(); - vw->draw(); - viewman->add(vw); - viewman->updateView(vw); + VWelcome* vw = new VWelcome(); + vw->draw(); + viewman->add(vw); + viewman->updateView(vw); // Enter pre-keys here // handleCommand(Remote::THREE); // handleCommand(Remote::DOWN); // handleCommand(Remote::OK); // handleCommand(Remote::PLAY); - + } } diff --git a/i18n.cc b/i18n.cc index 4c04d83..28904a0 100644 --- a/i18n.cc +++ b/i18n.cc @@ -38,6 +38,7 @@ int I18n::initialize(void) { LanguageID = 0; } + delete[] lang; } else { diff --git a/player.cc b/player.cc index d8dc92e..a48f557 100644 --- a/player.cc +++ b/player.cc @@ -211,6 +211,8 @@ void Player::stop() if (!initted) return; if (!playing) return; + logger->log("Player", Log::DEBUG, "Stop called", streamLength); + if (ffwd || fbwd) { ffwd = false; @@ -526,6 +528,14 @@ void Player::threadMethod() if (isRecording && ((lastRescan + 60) < time(NULL))) { streamLength = vdr->rescanRecording(); + if (!vdr->isConnected()) + { + Message* m = new Message(); + m->message = Message::CONNECTION_LOST; + m->to = this; + commandMessageQueue->postMessage(m); + return; + } logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", streamLength); lastRescan = time(NULL); setEndTS(); @@ -559,6 +569,15 @@ void Player::threadMethod() } threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead); + if (!vdr->isConnected()) + { + Message* m = new Message(); + m->message = Message::CONNECTION_LOST; + m->to = this; + commandMessageQueue->postMessage(m); + return; + } + if (!threadBuffer) break; if (startup) @@ -619,6 +638,9 @@ void Player::threadMethod() // end of recording logger->log("Player", Log::DEBUG, "Recording playback ends"); + + threadCheckExit(); + Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex if (streamLength) m->message = Message::STOP_PLAYBACK; // recording else m->message = Message::STREAM_END; // live diff --git a/remote.cc b/remote.cc index a0ed018..ce04e10 100644 --- a/remote.cc +++ b/remote.cc @@ -145,3 +145,8 @@ UCHAR Remote::getButtonPress(int waitType) } return NA_UNKNOWN; } + +void Remote::clearBuffer() +{ + while(getButtonPress(3) != NA_NONE); +} diff --git a/remote.h b/remote.h index 799f62f..edba87a 100644 --- a/remote.h +++ b/remote.h @@ -40,6 +40,7 @@ class Remote int getDevice(); void setRemoteType(UCHAR type); UCHAR getButtonPress(int how); + void clearBuffer(); // Not buttons const static UCHAR NA_NONE = 98; diff --git a/tcp.cc b/tcp.cc index 07276ab..6f68d4f 100644 --- a/tcp.cc +++ b/tcp.cc @@ -249,7 +249,7 @@ int TCP::readData(UCHAR* buffer, int totalBytes) { FD_ZERO(&readSet); FD_SET(sock, &readSet); - timeout.tv_sec = 30; + timeout.tv_sec = 20; timeout.tv_usec = 0; success = select(sock + 1, &readSet, NULL, NULL, passToSelect); if (success < 1) diff --git a/timers.cc b/timers.cc index e530759..26624b0 100755 --- a/timers.cc +++ b/timers.cc @@ -271,7 +271,7 @@ void Timers::threadMethod() // timer ran out -//## Log::getInstance()->log("Timers", Log::DEBUG, "Timer firing for client %p ref %i", nextTimer->client, nextTimer->clientReference); + Log::getInstance()->log("Timers", Log::DEBUG, "Timer firing for client %p ref %i", nextTimer->client, nextTimer->clientReference); // send this timer to the timer receiver, via the command message queue // so that the gui mutex is locked when it happens diff --git a/vdr.cc b/vdr.cc index 0eee6ea..10e6d67 100644 --- a/vdr.cc +++ b/vdr.cc @@ -154,7 +154,7 @@ void VDR::disconnect() void VDR::setReceiveWindow(size_t size) { - tcp->setReceiveWindow(size); + if (connected) tcp->setReceiveWindow(size); } /////////////////////////////////////////////////////// @@ -226,8 +226,6 @@ long VDR::extractLONG() int VDR::doLogin() { - if (!connected) return 0; - UCHAR buffer[14]; *(unsigned long*)&buffer[0] = htonl(10); @@ -235,8 +233,9 @@ int VDR::doLogin() tcp->getMAC((char*)&buffer[8]); - pthread_mutex_lock(&mutex); + if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + int a = tcp->sendPacket(buffer, 14); if (a != 14) { @@ -298,14 +297,14 @@ int VDR::doLogin() Directory* VDR::getRecordingsList() { - if (!connected) return 0; - UCHAR buffer[8]; *(unsigned long*)&buffer[0] = htonl(4); *(unsigned long*)&buffer[4] = htonl(VDR_GETRECORDINGLIST); pthread_mutex_lock(&mutex); + if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + int a = tcp->sendPacket(buffer, 8); if (a != 8) { @@ -391,8 +390,6 @@ Directory* VDR::getRecordingsList() int VDR::deleteRecording(char* fileName) { - if (!connected) return 0; - unsigned long totalLength = 8 + strlen(fileName) + 1; UCHAR buffer[totalLength]; @@ -401,6 +398,8 @@ int VDR::deleteRecording(char* fileName) strcpy((char*)&buffer[8], fileName); pthread_mutex_lock(&mutex); + if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + unsigned int a = tcp->sendPacket(buffer, totalLength); if (a != totalLength) { @@ -424,8 +423,6 @@ int VDR::deleteRecording(char* fileName) char* VDR::getRecordingSummary(char* fileName) { - if (!connected) return 0; - unsigned long totalLength = 8 + strlen(fileName) + 1; UCHAR buffer[totalLength]; @@ -434,6 +431,8 @@ char* VDR::getRecordingSummary(char* fileName) strcpy((char*)&buffer[8], fileName); pthread_mutex_lock(&mutex); + if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + unsigned int a = tcp->sendPacket(buffer, totalLength); if (a != totalLength) { @@ -456,14 +455,14 @@ char* VDR::getRecordingSummary(char* fileName) ChannelList* VDR::getChannelsList(ULONG type) { - if (!connected) return 0; - UCHAR buffer[8]; *(unsigned long*)&buffer[0] = htonl(4); *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELLIST); pthread_mutex_lock(&mutex); + if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + int a = tcp->sendPacket(buffer, 8); if (a != 8) { @@ -508,8 +507,6 @@ ChannelList* VDR::getChannelsList(ULONG type) int VDR::streamChannel(ULONG number) { - if (!connected) return 0; - UCHAR buffer[12]; *(unsigned long*)&buffer[0] = htonl(8); @@ -517,6 +514,8 @@ int VDR::streamChannel(ULONG number) *(unsigned long*)&buffer[8] = htonl(number); pthread_mutex_lock(&mutex); + if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + int a = tcp->sendPacket(buffer, 12); if (a != 12) @@ -541,14 +540,14 @@ int VDR::streamChannel(ULONG number) int VDR::stopStreaming() { - if (!connected) return 0; - UCHAR buffer[8]; *(unsigned long*)&buffer[0] = htonl(4); *(unsigned long*)&buffer[4] = htonl(VDR_STOPSTREAMING); pthread_mutex_lock(&mutex); + if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + int a = tcp->sendPacket(buffer, 8); if (a != 8) @@ -573,8 +572,6 @@ int VDR::stopStreaming() UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived) { - if (!connected) return 0; - UCHAR buffer[20]; *(unsigned long*)&buffer[0] = htonl(16); @@ -583,6 +580,8 @@ UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived) *(unsigned long*)&buffer[16] = htonl(maxAmount); pthread_mutex_lock(&mutex); + if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + int a = tcp->sendPacket(buffer, 20); if (a != 20) { @@ -597,6 +596,14 @@ UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived) return NULL; } + if (serverError()) + { + Log::getInstance()->log("VDR", Log::DEBUG, "Detected getblock 0"); + freePacket(); + pthread_mutex_unlock(&mutex); + return NULL; + } + UCHAR* toReturn = packet; *amountReceived = packetLength; // Manually clean up instead of running freePacket to keep the block @@ -610,8 +617,6 @@ UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived) ULLONG VDR::streamRecording(Recording* rec) { - if (!connected) return 0; - unsigned long totalLength = 8 + strlen(rec->fileName) + 1; UCHAR buffer[totalLength]; @@ -620,6 +625,8 @@ ULLONG VDR::streamRecording(Recording* rec) strcpy((char*)&buffer[8], rec->fileName); pthread_mutex_lock(&mutex); + if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + unsigned int a = tcp->sendPacket(buffer, totalLength); if (a != totalLength) { @@ -645,8 +652,6 @@ ULLONG VDR::streamRecording(Recording* rec) ULLONG VDR::rescanRecording() { - if (!connected) return 0; - unsigned long totalLength = 8; UCHAR buffer[totalLength]; @@ -654,6 +659,8 @@ ULLONG VDR::rescanRecording() *(unsigned long*)&buffer[4] = htonl(VDR_RESCANRECORDING); pthread_mutex_lock(&mutex); + if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + unsigned int a = tcp->sendPacket(buffer, totalLength); if (a != totalLength) { @@ -679,8 +686,6 @@ ULLONG VDR::rescanRecording() ULLONG VDR::positionFromFrameNumber(ULONG frameNumber) { - if (!connected) return 0; - unsigned long totalLength = 12; UCHAR buffer[totalLength]; @@ -689,6 +694,8 @@ ULLONG VDR::positionFromFrameNumber(ULONG frameNumber) *(unsigned long*)&buffer[8] = htonl(frameNumber); pthread_mutex_lock(&mutex); + if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + unsigned int a = tcp->sendPacket(buffer, totalLength); if (a != totalLength) { @@ -722,8 +729,6 @@ EventList* VDR::getChannelSchedule(ULONG number) EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration) { // retrieve event list (vector of events) from vdr within filter window. duration is in seconds - if (!connected) return 0; - UCHAR buffer[20]; *(unsigned long*)&buffer[0] = htonl(16); @@ -733,6 +738,8 @@ EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration) *(unsigned long*)&buffer[16] = htonl(duration); pthread_mutex_lock(&mutex); + if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + int a = tcp->sendPacket(buffer, 20); if (a != 20) @@ -748,10 +755,10 @@ EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration) return NULL; } + // received a ulong(0) - schedules error in the plugin if (serverError()) { freePacket(); - disconnect(); pthread_mutex_unlock(&mutex); return NULL; } @@ -810,8 +817,6 @@ ULLONG VDR::getResumePoint(char* fileName) int VDR::configSave(char* section, char* key, const char* value) { - if (!connected) return 0; - ULONG totalLength = 8 + strlen(section) + strlen(key) + strlen(value) + 3; // 8 for headers, 3 for nulls UCHAR buffer[totalLength]; @@ -826,6 +831,8 @@ int VDR::configSave(char* section, char* key, const char* value) strcpy((char*)&buffer[position], value); pthread_mutex_lock(&mutex); + if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + unsigned int a = tcp->sendPacket(buffer, totalLength); if (a != totalLength) { @@ -849,8 +856,6 @@ int VDR::configSave(char* section, char* key, const char* value) char* VDR::configLoad(char* section, char* key) { - if (!connected) return 0; - ULONG totalLength = 8 + strlen(section) + strlen(key) + 2; // 8 for headers, 2 for nulls UCHAR buffer[totalLength]; @@ -863,6 +868,8 @@ char* VDR::configLoad(char* section, char* key) strcpy((char*)&buffer[position], key); pthread_mutex_lock(&mutex); + if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + unsigned int a = tcp->sendPacket(buffer, totalLength); if (a != totalLength) { @@ -880,21 +887,19 @@ char* VDR::configLoad(char* section, char* key) freePacket(); pthread_mutex_unlock(&mutex); - printf("%p %s\n", toReturn, toReturn); - return toReturn; } RecTimerList* VDR::getRecTimersList() { - if (!connected) return NULL; - UCHAR buffer[8]; *(unsigned long*)&buffer[0] = htonl(4); *(unsigned long*)&buffer[4] = htonl(VDR_GETTIMERS); pthread_mutex_lock(&mutex); + if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + int a = tcp->sendPacket(buffer, 8); if (a != 8) { @@ -954,8 +959,6 @@ RecTimerList* VDR::getRecTimersList() ULONG VDR::setEventTimer(char* timerString) { - if (!connected) return false; - unsigned long totalLength = 8 + strlen(timerString) + 1; UCHAR buffer[totalLength]; @@ -964,6 +967,8 @@ ULONG VDR::setEventTimer(char* timerString) strcpy((char*)&buffer[8], timerString); pthread_mutex_lock(&mutex); + if (!connected) { pthread_mutex_unlock(&mutex); return 0; } + unsigned int a = tcp->sendPacket(buffer, totalLength); if (a != totalLength) { diff --git a/vepgsettimer.cc b/vepgsettimer.cc index 7fdf590..a185e6e 100644 --- a/vepgsettimer.cc +++ b/vepgsettimer.cc @@ -228,6 +228,11 @@ void VEpgSetTimer::doit() ULONG ret = vdr->setEventTimer(timerString); delete[] timerString; + if (!vdr->isConnected()) + { + Command::getInstance()->connectionLost(); + } + if (ret == 0) logger->log("VEPGST", Log::DEBUG, "Success"); else if (ret == 1) logger->log("VEPGST", Log::DEBUG, "Fail: Timer already set for this event"); else if (ret == 2) logger->log("VEPGST", Log::DEBUG, "Fail: General failure setting timer"); diff --git a/vepgsettimer.h b/vepgsettimer.h index 01f05d4..bd13583 100644 --- a/vepgsettimer.h +++ b/vepgsettimer.h @@ -33,6 +33,7 @@ #include "wbutton.h" #include "vinfo.h" #include "message.h" +#include "command.h" class VEpgSetTimer : public View { diff --git a/vlivebanner.cc b/vlivebanner.cc index 1ca5a9f..6e53cf9 100644 --- a/vlivebanner.cc +++ b/vlivebanner.cc @@ -96,7 +96,9 @@ void VLiveBanner::setChannel(Channel* tChannel) snprintf(ttitleText, 99, "%03lu - %s", currentChannel->number, currentChannel->name); setTitleText(ttitleText); + Log::getInstance()->log("Banner", Log::DEBUG, "Start get ch"); eventList = VDR::getInstance()->getChannelSchedule(currentChannel->number); + Log::getInstance()->log("Banner", Log::DEBUG, "End get ch"); if (!eventList) { @@ -104,10 +106,7 @@ void VLiveBanner::setChannel(Channel* tChannel) } else { - // sort the list - Log::getInstance()->log("Banner", Log::DEBUG, "Start sort"); sort(eventList->begin(), eventList->end(), EventSorter()); - Log::getInstance()->log("Banner", Log::DEBUG, "End sort"); char tempString[300]; char tempString2[300]; diff --git a/vvideolive.cc b/vvideolive.cc index b669598..0b23376 100644 --- a/vvideolive.cc +++ b/vvideolive.cc @@ -213,7 +213,7 @@ void VVideoLive::channelChange(UCHAR changeType, UINT newData) void VVideoLive::streamEnd() { Log::getInstance()->log("VVideoLive", Log::DEBUG, "streamEnd"); - stop(); + stop(1); showUnavailable(1); } diff --git a/vwelcome.cc b/vwelcome.cc index 7f20431..fb3f849 100644 --- a/vwelcome.cc +++ b/vwelcome.cc @@ -223,6 +223,10 @@ void VWelcome::doChannelsList() viewman->add(vchan); viewman->updateView(vchan); } + else + { + Command::getInstance()->connectionLost(); + } } void VWelcome::doRadioList() @@ -238,6 +242,10 @@ void VWelcome::doRadioList() viewman->add(vchan); viewman->updateView(vchan); } + else + { + Command::getInstance()->connectionLost(); + } } void VWelcome::doRecordingsList() @@ -287,6 +295,10 @@ void VWelcome::doTimersList() viewman->add(vtl); viewman->updateView(vtl); } + else + { + Command::getInstance()->connectionLost(); + } } void VWelcome::doOptions() -- 2.39.2