Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: talking to foo_controlserver (Read 3711 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

talking to foo_controlserver

I'm trying to write an app using the compact .NET framework to remote control foobar2000 using the foo_controlserver plugin.

It works fine as far as controlling the current song and parsing the results...

However, that only works if I read one line at a time from the output.  Whenever I try to read multiple lines (for example, getting the playlist or searching the playlist) I am having trouble with the communications to the server.

doing streamreader.readline() works fine for one line instances....since I know exactly how many lines I"m getting...well basically when to end the read

however, since foo_controlserver doesn't close the connection, and getting search results and the playlist means I have an unknowable amount of lines coming to me I don't know how to gracefully be done reading....

I've tried doing something like

Code: [Select]
while(sr.Peek() > 0)
{
  sr.Read(someCharArray,0,someCharArray.Length);
  outputStringBuilder.Append(someCharArray);
}

sr.DiscardBufferedData();


but I end up getting duplicate data...I'm always off by one read.  I don't think that DiscardBufferedData() is doing anything on the NetworkStream ...

any ideas?  here's the current code I'm using...which works fine for single lines

Code: [Select]
  public static bool InitializeConnection()
 {
     bool inited = false;
     try
     {
   IPEndPoint localEP = new IPEndPoint(Dns.Resolve(Dns.GetHostName()).AddressList[0],0);
   IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(ipAddress),3333);
   client = new TcpClient(localEP);
   //client.LingerState =
   client.Connect(remoteEP);
   inited = true;
     }
     catch
     {
   
     }
     return inited;
     
 }  

 public static string SendCmd(string cmd)
 {
     cmd = cmd + "\n";
     StreamWriter sw = new StreamWriter(client.GetStream());
     StreamReader sr = new StreamReader(client.GetStream());
     sr.DiscardBufferedData();
     sw.Write(cmd);
     sw.Flush();
     string output = sr.ReadLine();
     return output;
 }
Facts do not cease to exist just because they are ignored.
Aldous Huxley

talking to foo_controlserver

Reply #1
Sorry, I realized after the fact that this was in the wrong forum.


Anyhow, I've got it figured out....had to use sockets to do what I wanted (well, may have been possible with TcpClient, but I don't konw).

Anyhow, here's the code if anyone wants to re-use it

Code: [Select]
  public string SendCmd(string cmd)
 {
     cmd = cmd + "\n";
     StringBuilder outputBuilder = new StringBuilder("");
     byte[] inputData = Encoding.ASCII.GetBytes(cmd);
     s.Send(inputData);
     s.Poll(250000,SelectMode.SelectRead);
     while(s.Available > 0)
     {
   byte[] outputData = new byte[s.Available];
   int received = s.Receive(outputData,0,outputData.Length,SocketFlags.None);
       outputBuilder.Append(Encoding.ASCII.GetString(outputData,0,received));
   s.Poll(100000,SelectMode.SelectRead);
     }
     return outputBuilder.ToString();
 }


 public bool InitializeConnection()
 {
     bool inited = false;
     
     try
     {
   if(s != null)
   {
       s.Close();
   }
   s = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
   IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(textBox2.Text),3333);
   s.Connect(remoteEP);      
   byte[] outputData = new byte[s.Available];
   s.Receive(outputData);
   inited = true;
     }
     catch(Exception ex)
     {
   MessageBox.Show(ex.ToString());
     }
     return inited;
     
 }  



If anyone's interested in the app, just let me know and I'll post it here.

It has the following functions:

-> Play, Pause, Stop, Random, Previous, Next

-> Change playback mode

-> Load playlist, view by artist  (slow for big playlists..on my ipaq anyways)

->  Search playlist (much faster, reccommended for large playlists)


It requires foo_controlserver which is really what does all the work.  Very cool plugin.
Facts do not cease to exist just because they are ignored.
Aldous Huxley

talking to foo_controlserver

Reply #2
I tried writting the exact same application...
Learning C# and writting this thing i wanted soooo bad.. at the same time. I stopped right after i made Play/Pause, Next, Previous, Stop buttons and a console for input and output. I was planning on making a systray control thing and playlist search etc... but i don't have too much free time

I want this baaaad cuz i use Winamp at work, with a remote TCP plugin thingie because i'm the only one with speakers and a large drive to host the music, but 3 people in the office that want to be able to control the player... And i want to switch to foobar (wich i use at home since the early beta stages)...

Anyhow... i'd love to try your little app if i may?


 

talking to foo_controlserver

Reply #4
If you wanted to convert it to a winforms app (non-compact framework) then I believe pretty much all the code you should be able to c/p into a winforms project and make it compile.  if anything only minor tweaks would be needed but I doubt even that.

I mean, the GUI could use redesigning if it's going to be full fledged windows app...but I believe since the compact framework is a pure subset of the main winforms classes that it should work fine.
Facts do not cease to exist just because they are ignored.
Aldous Huxley