]> git.tdb.fi Git - xinema.git/blobdiff - source/client.cpp
Avoid crashing due to network exceptions
[xinema.git] / source / client.cpp
index 1f2164876024adc75fe4f440aa6f7938ca944dd8..ba0be5b44da51b11af152c59e1c6255c584ac958 100644 (file)
@@ -3,7 +3,6 @@
 #include <msp/strings/format.h>
 #include "client.h"
 #include "xinema.h"
-#include "xinestream.h"
 
 using namespace std;
 using namespace Msp;
@@ -25,7 +24,17 @@ Client::Client(Xinema &x, Net::StreamSocket *s):
 void Client::data_available()
 {
        char rbuf[1024];
-       unsigned len = socket->read(rbuf, sizeof(rbuf));
+       unsigned len;
+       try
+       {
+               len = socket->read(rbuf, sizeof(rbuf));
+       }
+       catch(const std::exception &)
+       {
+               stale = true;
+               return;
+       }
+
        buffer.append(rbuf, len);
 
        string::size_type start = 0;
@@ -55,6 +64,15 @@ void Client::end_of_stream()
        stale = true;
 }
 
+XineStream &Client::get_stream() const
+{
+       XineStream *stream = xinema.get_stream();
+       if(stream)
+               return *stream;
+
+       throw runtime_error("No stream");
+}
+
 void Client::process_command(const string &cmd)
 {
        string::size_type space = cmd.find(' ');
@@ -62,18 +80,35 @@ void Client::process_command(const string &cmd)
        string args;
        if(space!=string::npos)
                args = cmd.substr(space+1);
+
        if(keyword=="list_directory")
                list_directory(args);
        else if(keyword=="play_file")
                xinema.play_file(args);
+       else if(keyword=="play")
+               get_stream().play();
+       else if(keyword=="seek")
+               get_stream().seek(lexical_cast<float>(args)*Time::sec);
+       else if(keyword=="pause")
+               get_stream().pause();
+       else if(keyword=="stop")
+               get_stream().stop();
        else
-               send_reply("error Invalid command");
+               throw runtime_error("Invalid command");
 }
 
 void Client::send_reply(const string &reply)
 {
-       socket->write(reply);
-       socket->put('\n');
+       Msp::MutexLock lock(mutex);
+       try
+       {
+               socket->write(reply);
+               socket->put('\n');
+       }
+       catch(const std::exception &)
+       {
+               stale = true;
+       }
 }
 
 void Client::list_directory(const FS::Path &dn)
@@ -92,16 +127,26 @@ void Client::list_directory(const FS::Path &dn)
 
 void Client::stream_created(XineStream &stream)
 {
+       stream.signal_state_changed.connect(sigc::mem_fun(this, &Client::stream_state_changed));
        stream.signal_title_changed.connect(sigc::mem_fun(this, &Client::stream_title_changed));
        stream.signal_duration_changed.connect(sigc::mem_fun(this, &Client::stream_duration_changed));
        stream.signal_position_changed.connect(sigc::mem_fun(this, &Client::stream_position_changed));
+
+       stream_state_changed(stream.get_state());
+
        string title = stream.get_title();
        if(!title.empty())
                send_reply("title "+title);
+
        if(const Time::TimeDelta &dur = stream.get_duration())
                stream_duration_changed(dur);
 }
 
+void Client::stream_state_changed(XineStream::State state)
+{
+       send_reply(format("state %s", state));
+}
+
 void Client::stream_title_changed(const string &title)
 {
        send_reply("title "+title);