コード例 #1
0
        void OnSocketReceive(IAsyncResult ar)
        {
            Socket client = (Socket)ar.AsyncState;
            //client.SendBufferSize=10000000;
            int bytes = client.EndReceive(ar);

            client.BeginReceive(socketBuffer, 0, socketBuffer.Length, SocketFlags.None, OnSocketReceive, client);
            //IPAddress ipAd = IPAddress.Parse("127.0.0.1");
            //	myList=new TcpListener(IPAddress.Any,port);
            //	listenThread = new Thread(new ThreadStart(startListening));
            //	listenThread.Start();
            //}
            //private void startListening(){
            //if(myList!=null)
            //myList.Start();


            //Console.WriteLine("BansheRemoteListener will listen on port " + port.ToString());
            //while(true){
            //if(myList!=null)
            //client=myList.AcceptSocket();
            //client.Receive(socketBuffer,socketBuffer.Length,SocketFlags.None);
            string text = Encoding.UTF8.GetString(socketBuffer, 0, bytes);
            string sep  = "/";

            string[] remoteMessage = text.Split('/');
            string   action        = remoteMessage[0];
            string   variable      = remoteMessage[1];

            if (action.Equals("play"))
            {
                variable = variable.Replace('*', '/');
            }
            //Console.WriteLine("BansheeRemoteListener: " + action+ " " + variable);
            Banshee.Collection.TrackInfo currTrack = ServiceManager.PlayerEngine.CurrentTrack;
            string replyText = "";
            ushort currVol;
            ushort volStep   = 10;
            bool   replyReq  = false;
            string home      = Environment.GetEnvironmentVariable("HOME");
            string coverPath = "";

            //string dbPath=home+"/.config/banshee-1/banshee.db";
            if (currTrack != null && currTrack.ArtworkId != null)
            {
                coverPath = home + "/.cache/media-art/" + currTrack.ArtworkId.ToString() + ".jpg";
            }

            switch (action)
            {
            case "coverImage":                                  /*request*/
                byte[] coverImage = File.ReadAllBytes(coverPath);
                client.Send(coverImage);
                replyReq = true;
                break;

            /*case "syncCount":
             *      int count = System.IO.File.ReadAllBytes(dbPath).Length;
             *      client.Send(System.Text.Encoding.UTF8.GetBytes(count.ToString()));
             *      replyReq=true;
             *      break;*/

            /*case "sync":			//request
             *      //Console.WriteLine("About to Read the File");
             *      byte[] db = File.ReadAllBytes(dbPath);
             *      //String s = Convert.ToString(db[8152063],16).PadLeft(2,'0');
             *      //Console.WriteLine("Byte Read   "+s);
             *      byte[] chunk;
             *      int remainingBytes=db.Length;
             *      int offset=0;
             *      int chunksize = 1024;
             *      //int rep = 0;
             *      //String hash="";// = System.Convert.ToBase64String
             *      //int numPieces = (int)Math.Ceiling((double)db.Length/2048);
             *      //Console.WriteLine("Number of bytes "+db.Length);
             *      //client.Send(System.Text.Encoding.UTF8.GetBytes(db.Length.ToString()));
             *      while(remainingBytes>0){
             *              if(remainingBytes>=chunksize){
             *                      chunk = new byte[chunksize];
             *                      Array.Copy(db,offset,chunk,0,chunksize);
             *                      //if(rep==0){
             *                      //	hash = System.Convert.ToBase64String(chunk,0,chunksize);
             *                              //Console.WriteLine("Hash is : "+hash);
             *                              //client.Close();
             *                              //client=myList.AcceptSocket();
             *                              //int bytesT = client.Receive(socketBuffer,socketBuffer.Length,SocketFlags.None);
             *                              //string textT = Encoding.UTF8.GetString(socketBuffer,0,bytesT);
             *                              //client=myList.AcceptSocket();
             *                              //Console.WriteLine("Received Hash is : "+textT);
             *
             *                      //}
             *                      offset+=chunksize;
             *                      remainingBytes=remainingBytes-chunksize;
             *              }
             *              else{
             *                      chunk = new byte[remainingBytes];
             *                      Array.Copy(db,offset,chunk,0,remainingBytes);
             *                      //hash = System.Convert.ToBase64String(chunk,0,chunksize);
             *                      remainingBytes=0;
             *              }
             *              client.Send(chunk);
             *              //rep++;
             *      }
             *      //client.Send(db);
             *      replyReq = true;
             *      break;*/

            /*case "sync":			//request
             *      //Console.WriteLine("About to Read the File");
             *      byte[] db = File.ReadAllBytes(dbPath);
             *      int byteLength = db.Length;
             *      int offset = Convert.ToInt32(variable);
             *      int chunksize = 1024;
             *      byte[] chunk;
             *      //if(offset+chunksize>byteLength){
             *      //	chunk=new byte[byteLength-offset];
             *      //	Array.Copy(db,offset,chunk,0,(byteLength-offset));
             *      //}
             *      //else{
             *              chunk = new byte[chunksize];
             *              Array.Copy(db,0,chunk,0,chunksize);
             *      //}
             *      client.Send(chunk);
             *      Console.WriteLine("Sent from offset "+variable);
             *      //String s = Convert.ToString(db[8152063],16).PadLeft(2,'0');
             *      //Console.WriteLine("Byte Read   "+s);
             *
             *      //int remainingBytes=db.Length;
             *      //int offset=0;
             *      //int chunksize = 1024;
             *      //int rep = 0;
             *      //String hash="";// = System.Convert.ToBase64String
             *      //int numPieces = (int)Math.Ceiling((double)db.Length/2048);
             *      //Console.WriteLine("Number of bytes "+db.Length);
             *      //client.Send(System.Text.Encoding.UTF8.GetBytes(db.Length.ToString()));
             *      //while(remainingBytes>0){
             *      //	if(remainingBytes>=chunksize){
             *      //		chunk = new byte[chunksize];
             *      //		Array.Copy(db,offset,chunk,0,chunksize);
             *                      //if(rep==0){
             *                      //	hash = System.Convert.ToBase64String(chunk,0,chunksize);
             *                              //Console.WriteLine("Hash is : "+hash);
             *                              //client.Close();
             *                              //client=myList.AcceptSocket();
             *                              //int bytesT = client.Receive(socketBuffer,socketBuffer.Length,SocketFlags.None);
             *                              //string textT = Encoding.UTF8.GetString(socketBuffer,0,bytesT);
             *                              //client=myList.AcceptSocket();
             *                              //Console.WriteLine("Received Hash is : "+textT);
             *
             *                      //}
             *      //		offset+=chunksize;
             *      //		remainingBytes=remainingBytes-chunksize;
             *      //	}
             *      //	else{
             *      //		chunk = new byte[remainingBytes];
             *      //		Array.Copy(db,offset,chunk,0,remainingBytes);
             *                      //hash = System.Convert.ToBase64String(chunk,0,chunksize);
             *      //		remainingBytes=0;
             *      //	}
             *              //client.Send(chunk);
             *              //rep++;
             *      //}
             *      //client.Send(db);
             *      replyReq = true;
             *      break;*/

            case "coverExists":                                 /*request*/
                replyText = coverExists(coverPath);
                replyReq  = true;
                break;

            case "playPause":                                   /*command*/
                ServiceManager.PlayerEngine.TogglePlaying();
                replyReq = true;
                break;

            case "next":                                        /*command*/
                ServiceManager.PlaybackController.Next();
                replyReq = true;
                break;

            case "prev":                                        /*command*/
                ServiceManager.PlaybackController.Previous();
                replyReq = true;
                break;

            case "play":
                var source = ServiceManager.SourceManager.ActiveSource as ITrackModelSource;
                if (source != null)
                {
                    var countSongs             = source.Count;
                    UnknownTrackInfo track     = new UnknownTrackInfo(new SafeUri(variable));
                    TrackInfo        trackTemp = null;
                    for (int i = 0; i < countSongs; i++)
                    {
                        trackTemp = source.TrackModel [i];
                        if (trackTemp.TrackEqual(track))
                        {
                            break;
                        }
                    }
                    if (trackTemp != null)
                    {
                        ServiceManager.PlayerEngine.OpenPlay(trackTemp);
                    }
                }
                replyReq = true;
                break;

            case "volumeDown":                                  /*command*/
                currVol = ServiceManager.PlayerEngine.Volume;
                //ServiceManager.PlayerEngine.Open(new SafeUri("hello"));
                if (currVol < 10)
                {
                    ServiceManager.PlayerEngine.Volume = 0;
                }
                else
                {
                    ServiceManager.PlayerEngine.Volume = (ushort)(currVol - volStep);
                }
                replyReq = true;
                break;

            case "volumeUp":                                    /*command*/
                currVol = ServiceManager.PlayerEngine.Volume;
                if (currVol > 90)
                {
                    ServiceManager.PlayerEngine.Volume = 100;
                }
                else
                {
                    ServiceManager.PlayerEngine.Volume = (ushort)(currVol + volStep);
                }
                replyReq = true;
                break;

            case "mute":                                        /*command*/
                currVol = ServiceManager.PlayerEngine.Volume;
                if (currVol > 0)
                {
                    volume = currVol;
                    ServiceManager.PlayerEngine.Volume = 0;
                }
                else
                {
                    ServiceManager.PlayerEngine.Volume = volume;
                }
                replyReq = true;
                break;

            case "status":                                      /*request*/
                replyText = ServiceManager.PlayerEngine.CurrentState.ToString().ToLower();
                Console.WriteLine("Status " + replyText);
                replyReq = true;
                break;

            case "album":                                       /*request*/
                replyText = currTrack.DisplayAlbumTitle;
                replyReq  = true;
                break;

            case "artist":                                      /*request*/
                replyText = currTrack.DisplayArtistName;
                replyReq  = true;
                break;

            case "title":                                       /*request*/
                replyText = currTrack.DisplayTrackTitle;
                replyReq  = true;
                break;

            case "trackCurrentTime":                    /*request*/
                replyText = (ServiceManager.PlayerEngine.Position / 1000).ToString();
                replyReq  = true;
                break;

            case "trackTotalTime":                      /*request*/
                replyText = currTrack.Duration.ToString();
                replyReq  = true;
                break;

            case "seek":                                        /*command*/
                ServiceManager.PlayerEngine.Position = UInt32.Parse(variable) * 1000;
                replyReq = true;
                break;

            case "shuffle":                                     /*command*/ /*request*/
                if (ServiceManager.PlaybackController.ShuffleMode.ToString() == "off")
                {
                    ServiceManager.PlaybackController.ShuffleMode = "song";
                    replyText = "song";
                }
                else if (ServiceManager.PlaybackController.ShuffleMode.ToString() == "song")
                {
                    //ServiceManager.PlaybackController.ShuffleMode = PlaybackShuffleMode.Linear ;
                    ServiceManager.PlaybackController.ShuffleMode = "artist";
                    replyText = "Artist";
                }
                else if (ServiceManager.PlaybackController.ShuffleMode.ToString() == "artist")
                {
                    //ServiceManager.PlaybackController.ShuffleMode = PlaybackShuffleMode.Linear ;
                    ServiceManager.PlaybackController.ShuffleMode = "album";
                    replyText = "Album";
                }
                else if (ServiceManager.PlaybackController.ShuffleMode.ToString() == "album")
                {
                    //ServiceManager.PlaybackController.ShuffleMode = PlaybackShuffleMode.Linear ;
                    ServiceManager.PlaybackController.ShuffleMode = "rating";
                    replyText = "Rating";
                }
                else if (ServiceManager.PlaybackController.ShuffleMode.ToString() == "rating")
                {
                    //ServiceManager.PlaybackController.ShuffleMode = PlaybackShuffleMode.Linear ;
                    ServiceManager.PlaybackController.ShuffleMode = "score";
                    replyText = "Score";
                }
                else
                {
                    ServiceManager.PlaybackController.ShuffleMode = "off";
                    replyText = "off";
                }
                //replyText="off";
                //replyText = ServiceManager.PlaybackController.ShuffleMode.ToString();
                //Console.WriteLine(ServiceManager.PlaybackController.ShuffleMode.ToString());
                replyReq = true;
                break;

            case "repeat":                                      /*command*/ /*request*/
                if (ServiceManager.PlaybackController.RepeatMode == PlaybackRepeatMode.None)
                {
                    ServiceManager.PlaybackController.RepeatMode = PlaybackRepeatMode.RepeatAll;
                    replyText = "all";
                }
                else if (ServiceManager.PlaybackController.RepeatMode == PlaybackRepeatMode.RepeatAll)
                {
                    ServiceManager.PlaybackController.RepeatMode = PlaybackRepeatMode.RepeatSingle;
                    replyText = "single";
                }
                else
                {
                    ServiceManager.PlaybackController.RepeatMode = PlaybackRepeatMode.None;
                    replyText = "off";
                }
                //replyText = ServiceManager.PlaybackController.RepeatMode.ToString();
                //Console.WriteLine(replyText);
                replyReq = true;
                break;

            case "all":                                                 /*request*/
                replyText  = ServiceManager.PlayerEngine.CurrentState.ToString().ToLower() + sep;
                replyText += currTrack.DisplayAlbumTitle.Replace('/', '\\') + sep;
                replyText += currTrack.DisplayArtistName.Replace('/', '\\') + sep;
                replyText += currTrack.DisplayTrackTitle.Replace('/', '\\') + sep;
                replyText += ((uint)(ServiceManager.PlayerEngine.Position / 1000)).ToString() + sep;
                replyText += ((uint)(currTrack.Duration.TotalSeconds)).ToString() + sep;
                replyText += coverExists(coverPath);
                replyReq   = true;
                break;

            case "test":
                replyText = "";
                replyReq  = true;
                break;

            default:
                replyText = "";
                replyReq  = false;
                break;
            }

            byte[] messageByte = System.Text.Encoding.UTF8.GetBytes(replyText);

            if (replyReq)
            {
                reply(client, messageByte);
                //Console.WriteLine(System.Text.Encoding.UTF8.GetString(messageByte));
                //}

                client.Close();
            }
        }
コード例 #2
0
        void OnSocketReceive(IAsyncResult ar)
        {
            Socket client = null;
            int    bytes  = 0;

            try
            {
                client = (Socket)ar.AsyncState;
                bytes  = client.EndReceive(ar);

                client.BeginReceive(socketBuffer, 0, socketBuffer.Length, SocketFlags.None, OnSocketReceive, client);


                string   text          = Encoding.UTF8.GetString(socketBuffer, 0, bytes);
                string   sep           = "/";
                string[] remoteMessage = text.Split('/');
                string   action        = remoteMessage[0];
                string   variable      = remoteMessage[1];
                if (action.Equals("play"))
                {
                    variable = variable.Replace('*', '/');
                }

                Banshee.Collection.TrackInfo currTrack = ServiceManager.PlayerEngine.CurrentTrack;
                string replyText = "";
                ushort currVol;
                ushort volStep   = 10;
                bool   replyReq  = false;
                string home      = Environment.GetEnvironmentVariable("HOME");
                string coverPath = "";
                string dbPath    = home + "/.config/banshee-1/banshee.db";
                if (currTrack != null && currTrack.ArtworkId != null)
                {
                    coverPath = home + "/.cache/media-art/" + currTrack.ArtworkId.ToString() + ".jpg";
                }

                switch (action)
                {
                case "coverImage":
                    byte[] coverImage = File.ReadAllBytes(coverPath);
                    client.Send(coverImage);
                    replyReq = true;
                    break;

                case "syncCount":
                    int count = System.IO.File.ReadAllBytes(dbPath).Length;
                    client.Send(System.Text.Encoding.UTF8.GetBytes(count.ToString()));
                    replyReq = true;
                    break;

                case "sync":
                    byte[] db = File.ReadAllBytes(dbPath);
                    client.Send(db);
                    replyReq = true;
                    break;

                case "coverExists":
                    replyText = coverExists(coverPath);
                    replyReq  = true;
                    break;

                case "playPause":
                    ServiceManager.PlayerEngine.TogglePlaying();
                    replyReq = true;
                    break;

                case "next":
                    ServiceManager.PlaybackController.Next();
                    replyReq = true;
                    break;

                case "prev":
                    ServiceManager.PlaybackController.Previous();
                    replyReq = true;
                    break;

                case "play":
                    var source = ServiceManager.SourceManager.MusicLibrary as DatabaseSource;
                    source.FilterQuery = "";
                    if (source != null)
                    {
                        var countSongs = source.Count;

                        UnknownTrackInfo track     = new UnknownTrackInfo(new SafeUri(variable));
                        TrackInfo        trackTemp = null;
                        for (int i = 0; i < countSongs; i++)
                        {
                            trackTemp = source.TrackModel [i];
                            if (trackTemp.TrackEqual(track))
                            {
                                break;
                            }
                        }
                        if (trackTemp != null)
                        {
                            ServiceManager.PlayerEngine.OpenPlay(trackTemp);
                        }
                    }
                    replyReq = true;
                    break;

                case "volumeDown":
                    currVol = ServiceManager.PlayerEngine.Volume;
                    if (currVol < 10)
                    {
                        ServiceManager.PlayerEngine.Volume = 0;
                    }
                    else
                    {
                        ServiceManager.PlayerEngine.Volume = (ushort)(currVol - volStep);
                    }
                    replyReq = true;
                    break;

                case "volumeUp":
                    currVol = ServiceManager.PlayerEngine.Volume;
                    if (currVol > 90)
                    {
                        ServiceManager.PlayerEngine.Volume = 100;
                    }
                    else
                    {
                        ServiceManager.PlayerEngine.Volume = (ushort)(currVol + volStep);
                    }
                    replyReq = true;
                    break;

                case "mute":
                    currVol = ServiceManager.PlayerEngine.Volume;
                    if (currVol > 0)
                    {
                        volume = currVol;
                        ServiceManager.PlayerEngine.Volume = 0;
                    }
                    else
                    {
                        ServiceManager.PlayerEngine.Volume = volume;
                    }
                    replyReq = true;
                    break;

                case "status":
                    replyText = ServiceManager.PlayerEngine.CurrentState.ToString().ToLower();
                    replyReq  = true;
                    break;

                case "album":
                    replyText = currTrack.DisplayAlbumTitle;
                    replyReq  = true;
                    break;

                case "artist":
                    replyText = currTrack.DisplayArtistName;
                    replyReq  = true;
                    break;

                case "title":
                    replyText = currTrack.DisplayTrackTitle;
                    replyReq  = true;
                    break;

                case "trackCurrentTime":
                    replyText = (ServiceManager.PlayerEngine.Position / 1000).ToString();
                    replyReq  = true;
                    break;

                case "trackTotalTime":
                    replyText = currTrack.Duration.ToString();
                    replyReq  = true;
                    break;

                case "seek":
                    ServiceManager.PlayerEngine.Position = UInt32.Parse(variable) * 1000;
                    replyReq = true;
                    break;

                case "shuffle":
                    if (ServiceManager.PlaybackController.ShuffleMode.ToString() == "off")
                    {
                        ServiceManager.PlaybackController.ShuffleMode = "song";
                        replyText = "song";
                    }
                    else if (ServiceManager.PlaybackController.ShuffleMode.ToString() == "song")
                    {
                        ServiceManager.PlaybackController.ShuffleMode = "artist";
                        replyText = "Artist";
                    }
                    else if (ServiceManager.PlaybackController.ShuffleMode.ToString() == "artist")
                    {
                        ServiceManager.PlaybackController.ShuffleMode = "album";
                        replyText = "Album";
                    }
                    else if (ServiceManager.PlaybackController.ShuffleMode.ToString() == "album")
                    {
                        ServiceManager.PlaybackController.ShuffleMode = "rating";
                        replyText = "Rating";
                    }
                    else if (ServiceManager.PlaybackController.ShuffleMode.ToString() == "rating")
                    {
                        ServiceManager.PlaybackController.ShuffleMode = "score";
                        replyText = "Score";
                    }
                    else
                    {
                        ServiceManager.PlaybackController.ShuffleMode = "off";
                        replyText = "off";
                    }
                    replyReq = true;
                    break;

                case "repeat":
                    if (ServiceManager.PlaybackController.RepeatMode == PlaybackRepeatMode.None)
                    {
                        ServiceManager.PlaybackController.RepeatMode = PlaybackRepeatMode.RepeatAll;
                        replyText = "all";
                    }
                    else if (ServiceManager.PlaybackController.RepeatMode == PlaybackRepeatMode.RepeatAll)
                    {
                        ServiceManager.PlaybackController.RepeatMode = PlaybackRepeatMode.RepeatSingle;
                        replyText = "single";
                    }
                    else
                    {
                        ServiceManager.PlaybackController.RepeatMode = PlaybackRepeatMode.None;
                        replyText = "off";
                    }
                    replyReq = true;
                    break;

                case "all":
                    replyText  = ServiceManager.PlayerEngine.CurrentState.ToString().ToLower() + sep;
                    replyText += currTrack.DisplayAlbumTitle.Replace('/', '\\') + sep;
                    replyText += currTrack.DisplayArtistName.Replace('/', '\\') + sep;
                    replyText += currTrack.DisplayTrackTitle.Replace('/', '\\') + sep;
                    replyText += ((uint)(ServiceManager.PlayerEngine.Position / 1000)).ToString() + sep;
                    replyText += ((uint)(currTrack.Duration.TotalSeconds)).ToString() + sep;
                    replyText += coverExists(coverPath);
                    replyReq   = true;
                    break;

                case "test":
                    replyText = "";
                    replyReq  = true;
                    break;

                default:
                    replyText = "";
                    replyReq  = false;
                    break;
                }

                byte[] messageByte = System.Text.Encoding.UTF8.GetBytes(replyText);

                if (replyReq)
                {
                    reply(client, messageByte);
                }
                client.Close();
            }
            catch (Exception)
            {
            }
        }