Author: jwilcox
Date: 2006-03-12 12:29:02 -0500 (Sun, 12 Mar 2006)
New Revision: 57873

Modified:
   trunk/daap-sharp/ChangeLog
   trunk/daap-sharp/src/ContentFetcher.cs
   trunk/daap-sharp/src/Database.cs
   trunk/daap-sharp/src/Server.cs
Log:
2006-03-12  James Willcox  <[EMAIL PROTECTED]>

        * src/Server.cs: support range requests for seeking, and limit the
        number of database revisions kept for creating deltas.
        * src/Client.cs: support seeking (range requests)



Modified: trunk/daap-sharp/ChangeLog
===================================================================
--- trunk/daap-sharp/ChangeLog  2006-03-12 17:01:29 UTC (rev 57872)
+++ trunk/daap-sharp/ChangeLog  2006-03-12 17:29:02 UTC (rev 57873)
@@ -1,3 +1,9 @@
+2006-03-12  James Willcox  <[EMAIL PROTECTED]>
+
+       * src/Server.cs: support range requests for seeking, and limit the
+       number of database revisions kept for creating deltas.
+       * src/Client.cs: support seeking (range requests)
+
 2006-03-11  James Willcox  <[EMAIL PROTECTED]>
 
        * src/Mono.Zeroconf/*: updated

Modified: trunk/daap-sharp/src/ContentFetcher.cs
===================================================================
--- trunk/daap-sharp/src/ContentFetcher.cs      2006-03-12 17:01:29 UTC (rev 
57872)
+++ trunk/daap-sharp/src/ContentFetcher.cs      2006-03-12 17:29:02 UTC (rev 
57873)
@@ -81,7 +81,7 @@
         public byte[] Fetch (string path, string query, WebHeaderCollection 
extraHeaders,
                              int requestId) {
 
-            HttpWebResponse response = FetchResponse (path, query, 
extraHeaders, requestId, false);
+            HttpWebResponse response = FetchResponse (path, -1, query, 
extraHeaders, requestId, false);
             responses.Add (response);
 
             MemoryStream data = new MemoryStream ();
@@ -110,14 +110,14 @@
         }
 
         public HttpWebResponse FetchResponse (string path, string query, 
WebHeaderCollection headers) {
-            return FetchResponse (path, query, headers, ++requestId, false);
+            return FetchResponse (path, -1, query, headers, ++requestId, 
false);
         }
 
-        public HttpWebResponse FetchFile (string path) {
-            return FetchResponse (path, null, null, ++requestId, true);
+        public HttpWebResponse FetchFile (string path, long offset) {
+            return FetchResponse (path, offset, null, null, ++requestId, true);
         }
 
-        public HttpWebResponse FetchResponse (string path, string query,
+        public HttpWebResponse FetchResponse (string path, long offset, string 
query,
                                               WebHeaderCollection extraHeaders,
                                               int requestId, bool 
disableKeepalive) {
             UriBuilder builder = new UriBuilder ("http", address.ToString ());
@@ -134,8 +134,13 @@
             request.PreAuthenticate = true;
             request.Timeout = System.Threading.Timeout.Infinite;
             request.Headers.Add ("Accept-Encoding", "gzip");
+
+            if (offset > 0) {
+                request.AddRange ("bytes", (int) offset);
+            }
+            
             request.ServicePoint.MaxIdleTime = 
System.Threading.Timeout.Infinite;
-
+            
             if (extraHeaders != null)
                 request.Headers = extraHeaders;
 

Modified: trunk/daap-sharp/src/Database.cs
===================================================================
--- trunk/daap-sharp/src/Database.cs    2006-03-12 17:01:29 UTC (rev 57872)
+++ trunk/daap-sharp/src/Database.cs    2006-03-12 17:29:02 UTC (rev 57873)
@@ -358,11 +358,20 @@
         }
 
         private HttpWebResponse FetchSong (Song song) {
-            return client.Fetcher.FetchFile (String.Format 
("/databases/{0}/items/{1}.{2}", id, song.Id, song.Format));
+            return FetchSong (song, -1);
         }
+
+        private HttpWebResponse FetchSong (Song song, long offset) {
+            return client.Fetcher.FetchFile (String.Format 
("/databases/{0}/items/{1}.{2}", id, song.Id, song.Format),
+                                             offset);
+        }
+
+        public Stream StreamSong (Song song, out long length) {
+            return StreamSong (song, -1, out length);
+        }
         
-        public Stream StreamSong (Song song, out long length) {
-            HttpWebResponse response = FetchSong (song);
+        public Stream StreamSong (Song song, long offset, out long length) {
+            HttpWebResponse response = FetchSong (song, offset);
             length = response.ContentLength;
             return response.GetResponseStream ();
         }
@@ -370,18 +379,18 @@
         public void DownloadSong (Song song, string dest) {
 
             HttpWebResponse response = FetchSong (song);
-            
             BinaryWriter writer = new BinaryWriter (File.Open (dest, 
FileMode.Create));
 
             try {
-                using (BinaryReader reader = new BinaryReader 
(response.GetResponseStream ())) {
+                long len;
+                using (BinaryReader reader = new BinaryReader (StreamSong 
(song, out len))) {
                     int count = 0;
                     byte[] buf = new byte[ChunkLength];
                     
                     do {
                         count = reader.Read (buf, 0, ChunkLength);
                         writer.Write (buf, 0, count);
-                    } while (count == ChunkLength);
+                    } while (count != 0);
                 }
             } finally {
                 writer.Close ();

Modified: trunk/daap-sharp/src/Server.cs
===================================================================
--- trunk/daap-sharp/src/Server.cs      2006-03-12 17:01:29 UTC (rev 57872)
+++ trunk/daap-sharp/src/Server.cs      2006-03-12 17:29:02 UTC (rev 57873)
@@ -36,7 +36,7 @@
 
 namespace DAAP {
 
-    internal delegate bool WebHandler (Socket client, string path, 
NameValueCollection query);
+    internal delegate bool WebHandler (Socket client, string path, 
NameValueCollection query, int range);
 
     internal class WebServer {
 
@@ -135,23 +135,45 @@
             }
         }
 
-        public void WriteResponseFile (Socket client, string file) {
+        public void WriteResponseFile (Socket client, string file, long 
offset) {
             FileInfo info = new FileInfo (file);
 
-            WriteResponseStream (client, info.Open (FileMode.Open, 
FileAccess.Read), info.Length);
+            FileStream stream = info.Open (FileMode.Open, FileAccess.Read);
+            WriteResponseStream (client, stream, info.Length, offset);
         }
 
         public void WriteResponseStream (Socket client, Stream response, long 
len) {
+            WriteResponseStream (client, response, len, -1);
+        }
+
+        public void WriteResponseStream (Socket client, Stream response, long 
len, long offset) {
             using (BinaryWriter writer = new BinaryWriter (new NetworkStream 
(client, false))) {
-                
-                writer.Write (Encoding.UTF8.GetBytes ("HTTP/1.1 200 OK\r\n"));
+
+                if (offset > 0) {
+                    writer.Write (Encoding.UTF8.GetBytes ("HTTP/1.1 206 
Partial Content\r\n"));
+                    writer.Write (Encoding.UTF8.GetBytes (String.Format 
("Content-Range: bytes {0}-{1}/{2}\r\n",
+                                                                         
offset, len, len + 1)));
+                    writer.Write (Encoding.UTF8.GetBytes ("Accept-Range: 
bytes\r\n"));
+                    len = len - offset;
+                } else {
+                    writer.Write (Encoding.UTF8.GetBytes ("HTTP/1.1 200 
OK\r\n"));
+                }
+
                 writer.Write (Encoding.UTF8.GetBytes (String.Format 
("Content-Length: {0}\r\n", len)));
                 writer.Write (Encoding.UTF8.GetBytes ("\r\n"));
 
                 using (BinaryReader reader = new BinaryReader (response)) {
+                    if (offset > 0) {
+                        reader.BaseStream.Seek (offset, SeekOrigin.Begin);
+                    }
+
                     long count = 0;
                     while (count < len) {
                         byte[] buf = reader.ReadBytes (Math.Min (ChunkLength, 
(int) len - (int) count));
+                        if (buf.Length == 0) {
+                            break;
+                        }
+                        
                         writer.Write (buf);
                         count += buf.Length;
                     }
@@ -203,11 +225,12 @@
                 string line = null;
                 string user = null;
                 string password = null;
+                int range = -1;
                 
                 // read the rest of the request
                 do {
                     line = reader.ReadLine ();
-                    
+
                     if (line == "Connection: close") {
                         ret = false;
                     } else if (line != null && line.StartsWith 
("Authorization: Basic")) {
@@ -221,6 +244,21 @@
                         string[] splitUserPass = userpass.Split (new char[] 
{':'}, 2);
                         user = splitUserPass[0];
                         password = splitUserPass[1];
+                    } else if (line != null && line.StartsWith ("Range: ")) {
+                        // we currently expect 'Range: bytes=<offset>-'
+                        string[] splitLine = line.Split ('=');
+
+                        if (splitLine.Length != 2)
+                            continue;
+
+                        string rangestr = splitLine[1];
+                        if (!rangestr.EndsWith ("-"))
+                            continue;
+
+                        try {
+                            range = Int32.Parse (rangestr.Substring (0, 
rangestr.Length - 1));
+                        } catch (FormatException) {
+                        }
                     }
                 } while (line != String.Empty && line != null);
                 
@@ -256,7 +294,7 @@
                             return true;
                         }
 
-                        return handler (client, uri.AbsolutePath, query);
+                        return handler (client, uri.AbsolutePath, query, 
range);
                     } catch (IOException e) {
                         ret = false;
                     } catch (Exception e) {
@@ -304,13 +342,32 @@
 
         private Hashtable revisions = new Hashtable ();
         private int current = 1;
+        private int limit = 3;
 
         public int Current {
             get { return current; }
         }
+
+        public int HistoryLimit {
+            get { return limit; }
+            set { limit = value; }
+        }
         
         public void AddRevision (Database[] databases) {
             revisions[++current] = databases;
+
+            if (revisions.Keys.Count > limit) {
+                // remove the oldest
+
+                int oldest = current;
+                foreach (int rev in revisions.Keys) {
+                    if (rev < oldest) {
+                        oldest = rev;
+                    }
+                }
+
+                RemoveRevision (oldest);
+            }
         }
 
         public void RemoveRevision (int rev) {
@@ -572,7 +629,7 @@
         }
 #endif
 
-        internal bool OnHandleRequest (Socket client, string path, 
NameValueCollection query) {
+        internal bool OnHandleRequest (Socket client, string path, 
NameValueCollection query, int range) {
 
             int session = 0;
             if (query["session-id"] != null) {
@@ -659,7 +716,7 @@
 
                 try {
                     if (song.FileName != null) {
-                        ws.WriteResponseFile (client, song.FileName);
+                        ws.WriteResponseFile (client, song.FileName, range);
                     } else if (db.Client != null) {
                         long songLength = 0;
                         Stream songStream = db.StreamSong (song, out 
songLength);

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to