def playlist_open(self, playlist_id):
     """
     """
     msg = cr.Message()
     msg.type = cr.OPEN_PLAYLIST
     msg.request_open_playlist.playlist_id = playlist_id
     self.send_message(msg)
 def previous(self):
     """
     Sends a "previous" command to the player.
     """
     msg = cr.Message()
     msg.type = cr.PREVIOUS
     self.send_message(msg)
 def next(self):
     """
     Sends a "next" command to the player.
     """
     msg = cr.Message()
     msg.type = cr.NEXT
     self.send_message(msg)
 def playpause(self):
     """
     Sends a "playpause" command to the player.
     """
     msg = cr.Message()
     msg.type = cr.PLAYPAUSE
     self.send_message(msg)
 def stop(self):
     """
     Sends a "play" command to the player.
     """
     msg = cr.Message()
     msg.type = cr.STOP
     self.send_message(msg)
 def play(self):
     """
     Sends a "play" command to the player.
     """
     msg = cr.Message()
     msg.type = cr.PLAY
     self.send_message(msg)
 def change_song(self, playlist_id, song_index):
     """
     """
     msg = cr.Message()
     msg.type = cr.CHANGE_SONG
     msg.request_change_song.playlist_id = playlist_id
     msg.request_change_song.song_index = song_index
     self.send_message(msg)
 def set_volume(self, volume):
     """
     Sets player volume (note, this does not change host computer main volume).
     """
     msg = cr.Message()
     msg.type = cr.SET_VOLUME
     msg.request_set_volume.volume = int(volume)
     self.send_message(msg)
 def insert_urls(self,
                 playlist_id,
                 urls,
                 position=-1,
                 play_now=False,
                 enqueue=False):
     """
     """
     msg = cr.Message()
     msg.type = cr.INSERT_URLS
     msg.request_insert_urls.playlist_id = playlist_id
     msg.request_insert_urls.urls.extend(urls)
     msg.request_insert_urls.position = position
     msg.request_insert_urls.play_now = play_now
     msg.request_insert_urls.enqueue = enqueue
     self.send_message(msg)
    def _connect(self):
        """
        Connects to the server defined in the constructor.
        """

        self.first_data_sent_complete = False
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.connect((self.host, self.port))

        msg = cr.Message()
        msg.type = cr.CONNECT
        msg.request_connect.auth_code = self.auth_code or 0
        msg.request_connect.send_playlist_songs = False
        msg.request_connect.downloader = False

        self.send_message(msg)
    def client_thread(self):

        while not self._terminated:

            try:
                self._connect()
            except Exception as e:
                if self.reconnect:
                    time.sleep(self.RECONNECT_SECONDS)
                else:
                    self._terminated = True
                    raise

            try:
                while self.socket:

                    chunk = self.socket.recv(4)
                    if not chunk: break

                    (msg_length, ) = struct.unpack(">I", chunk)

                    data = bytes()
                    while len(data) < msg_length:
                        chunk = self.socket.recv(
                            min(4096, msg_length - len(data)))
                        if not chunk: break
                        data += chunk

                    if not chunk: break

                    try:
                        msg = cr.Message()
                        msg.ParseFromString(data)

                        self.process_incoming_message(msg)
                        self.on_message(msg)
                    except Exception as e:
                        raise

            except OSError as e:
                self.state = "Disconnected"
            except Exception as e:
                self.state = "Disconnected"

            if not self.reconnect:
                self._terminated = True