added: print socket errors on the display

This commit is contained in:
Bob van Loosen 2012-12-23 15:11:16 +01:00
parent 69bd0ba7ab
commit 7cf7af80b1
2 changed files with 20 additions and 18 deletions

View file

@ -27,17 +27,11 @@ void CMpdClient::Process()
if (!m_socket.IsOpen()) if (!m_socket.IsOpen())
{ {
if (!OpenSocket()) if (!OpenSocket())
{
ClearCurrentSong();
continue; continue;
}
} }
if (!GetCurrentSong()) if (!GetCurrentSong())
{
ClearCurrentSong();
m_socket.Close(); m_socket.Close();
}
USleep(1000000); USleep(1000000);
} }
@ -50,6 +44,7 @@ bool CMpdClient::OpenSocket()
if (returnv != SUCCESS) if (returnv != SUCCESS)
{ {
SetSockError();
LogError("Connecting to %s:%i, %s", m_address.c_str(), m_port, m_socket.GetError().c_str()); LogError("Connecting to %s:%i, %s", m_address.c_str(), m_port, m_socket.GetError().c_str());
m_socket.Close(); m_socket.Close();
@ -61,6 +56,7 @@ bool CMpdClient::OpenSocket()
else else
{ {
Log("Connected to %s:%i", m_address.c_str(), m_port); Log("Connected to %s:%i", m_address.c_str(), m_port);
SetCurrentSong("Connected to " + m_address + " " + ToString(m_port));
return true; return true;
} }
} }
@ -71,6 +67,7 @@ bool CMpdClient::GetCurrentSong()
data.SetData("currentsong\n"); data.SetData("currentsong\n");
if (m_socket.Write(data) != SUCCESS) if (m_socket.Write(data) != SUCCESS)
{ {
SetSockError();
LogError("Writing socket: %s", m_socket.GetError().c_str()); LogError("Writing socket: %s", m_socket.GetError().c_str());
return false; return false;
} }
@ -83,6 +80,7 @@ bool CMpdClient::GetCurrentSong()
{ {
if (m_socket.Read(data) != SUCCESS) if (m_socket.Read(data) != SUCCESS)
{ {
SetSockError();
LogError("Reading socket: %s", m_socket.GetError().c_str()); LogError("Reading socket: %s", m_socket.GetError().c_str());
return false; return false;
} }
@ -107,32 +105,35 @@ bool CMpdClient::GetCurrentSong()
if (!artist.empty() && !title.empty()) if (!artist.empty() && !title.empty())
{ {
string song = artist + " - " + title; string song = artist + " - " + title;
CLock lock(m_condition); SetCurrentSong(song);
if (song != m_currentsong)
{
m_currentsong = song;
m_songchanged = true;
lock.Leave();
Log("Song changed to \"%s\"", m_currentsong.c_str());
}
return true; return true;
} }
} }
} }
SetCurrentSong("Unable to get song info");
return false; return false;
} }
void CMpdClient::ClearCurrentSong() void CMpdClient::SetCurrentSong(const std::string& song)
{ {
CLock lock(m_condition); CLock lock(m_condition);
if (!m_currentsong.empty()) if (song != m_currentsong)
{ {
m_currentsong.clear(); m_currentsong = song;
m_songchanged = true; m_songchanged = true;
lock.Leave();
Log("Song changed to \"%s\"", song.c_str());
} }
} }
void CMpdClient::SetSockError()
{
string error = m_address + " " + ToString(m_port) + " " + m_socket.GetError();
SetCurrentSong(error);
}
bool CMpdClient::CurrentSong(std::string& song) bool CMpdClient::CurrentSong(std::string& song)
{ {
CLock lock(m_condition); CLock lock(m_condition);

View file

@ -20,7 +20,8 @@ class CMpdClient : public CThread
private: private:
bool OpenSocket(); bool OpenSocket();
bool GetCurrentSong(); bool GetCurrentSong();
void ClearCurrentSong(); void SetCurrentSong(const std::string& song);
void SetSockError();
int m_port; int m_port;
std::string m_address; std::string m_address;