예제 #1
0
        // Thread entry point
        public void WorkerThread()
        {
            byte[] buffer = new byte[bufSize];                  // buffer to read stream

            while (true)
            {
                // reset reload event
                reloadEvent.Reset();

                HttpWebRequest req = null;
                WebResponse    resp = null;
                Stream         stream = null;
                byte[]         delimiter = null;
                byte[]         delimiter2 = null;
                byte[]         boundary = null;
                int            boundaryLen, delimiterLen = 0, delimiter2Len = 0;
                int            read, todo = 0, total = 0, pos = 0, align = 1;
                int            start = 0, stop = 0;

                // align
                //  1 = searching for image start
                //  2 = searching for image end
                try
                {
                    // create request
                    req = (HttpWebRequest)WebRequest.Create(source);
                    // set login and password
                    if ((login != null) && (password != null) && (login != ""))
                    {
                        req.Credentials = new NetworkCredential(login, password);
                    }
                    // set connection group name
                    if (useSeparateConnectionGroup)
                    {
                        req.ConnectionGroupName = GetHashCode().ToString();
                    }
                    // get response
                    resp = req.GetResponse();

                    // check content type
                    string ct = resp.ContentType;
                    if (ct.IndexOf("multipart/x-mixed-replace") == -1)
                    {
                        throw new ApplicationException("Invalid URL");
                    }

                    // get boundary
                    ASCIIEncoding encoding = new ASCIIEncoding();
                    boundary    = encoding.GetBytes(ct.Substring(ct.IndexOf("boundary=", 0) + 9));
                    boundaryLen = boundary.Length;

                    // get response stream
                    stream = resp.GetResponseStream();

                    // loop
                    while ((!stopEvent.WaitOne(0, true)) && (!reloadEvent.WaitOne(0, true)))
                    {
                        // check total read
                        if (total > bufSize - readSize)
                        {
                            System.Diagnostics.Debug.WriteLine("flushing");
                            total = pos = todo = 0;
                        }

                        // read next portion from stream
                        if ((read = stream.Read(buffer, total, readSize)) == 0)
                        {
                            throw new ApplicationException();
                        }

                        total += read;
                        todo  += read;

                        // increment received bytes counter
                        bytesReceived += read;

                        // does we know the delimiter ?
                        if (delimiter == null)
                        {
                            // find boundary
                            pos = ByteArrayUtils.Find(buffer, boundary, pos, todo);

                            if (pos == -1)
                            {
                                // was not found
                                todo = boundaryLen - 1;
                                pos  = total - todo;
                                continue;
                            }

                            todo = total - pos;

                            if (todo < 2)
                            {
                                continue;
                            }

                            // check new line delimiter type
                            if (buffer[pos + boundaryLen] == 10)
                            {
                                delimiterLen = 2;
                                delimiter    = new byte[2] {
                                    10, 10
                                };
                                delimiter2Len = 1;
                                delimiter2    = new byte[1] {
                                    10
                                };
                            }
                            else
                            {
                                delimiterLen = 4;
                                delimiter    = new byte[4] {
                                    13, 10, 13, 10
                                };
                                delimiter2Len = 2;
                                delimiter2    = new byte[2] {
                                    13, 10
                                };
                            }

                            pos += boundaryLen + delimiter2Len;
                            todo = total - pos;
                        }

                        // boundary aligned

                        /*						if ((align == 0) && (todo >= boundaryLen))
                         *                                              {
                         *                                                      if (ByteArrayUtils.Compare(buffer, boundary, pos))
                         *                                                      {
                         *                                                              // boundary located
                         *                                                              align = 1;
                         *                                                              ttodo -= boundaryLen;
                         *                                                              pos += boundaryLen;
                         *                                                      }
                         *                                                      else
                         *                                                              align = 2;
                         *                                              }*/

                        // search for image
                        if (align == 1)
                        {
                            start = ByteArrayUtils.Find(buffer, delimiter, pos, todo);
                            if (start != -1)
                            {
                                // found delimiter
                                start += delimiterLen;
                                pos    = start;
                                todo   = total - pos;
                                align  = 2;
                            }
                            else
                            {
                                // delimiter not found
                                todo = delimiterLen - 1;
                                pos  = total - todo;
                            }
                        }

                        // search for image end
                        while ((align == 2) && (todo >= boundaryLen))
                        {
                            stop = ByteArrayUtils.Find(buffer, boundary, pos, todo);
                            if (stop != -1)
                            {
                                pos  = stop;
                                todo = total - pos;

                                // increment frames counter
                                framesReceived++;

                                // image at stop
                                if (NewFrame != null)
                                {
                                    Bitmap bmp = (Bitmap)Bitmap.FromStream(new MemoryStream(buffer, start, stop - start));
                                    // notify client
                                    NewFrame(this, new CameraEventArgs(bmp));
                                    // release the image
                                    bmp.Dispose();
                                    bmp = null;
                                }
//								System.Diagnostics.Debug.WriteLine("found image end, size = " + (stop - start));

                                // shift array
                                pos  = stop + boundaryLen;
                                todo = total - pos;
                                Array.Copy(buffer, pos, buffer, 0, todo);

                                total = todo;
                                pos   = 0;
                                align = 1;
                            }
                            else
                            {
                                // delimiter not found
                                todo = boundaryLen - 1;
                                pos  = total - todo;
                            }
                        }
                    }
                }
                catch (WebException ex)
                {
                    System.Diagnostics.Debug.WriteLine("=============: " + ex.Message);
                    // wait for a while before the next try
                    Thread.Sleep(250);
                }
                catch (ApplicationException ex)
                {
                    System.Diagnostics.Debug.WriteLine("=============: " + ex.Message);
                    // wait for a while before the next try
                    Thread.Sleep(250);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("=============: " + ex.Message);
                }
                finally
                {
                    // abort request
                    if (req != null)
                    {
                        req.Abort();
                        req = null;
                    }
                    // close response stream
                    if (stream != null)
                    {
                        stream.Close();
                        stream = null;
                    }
                    // close response
                    if (resp != null)
                    {
                        resp.Close();
                        resp = null;
                    }
                }

                // need to stop ?
                if (stopEvent.WaitOne(0, true))
                {
                    break;
                }
            }
        }
예제 #2
0
 public void WorkerThread()
 {
     byte[] buffer = new byte[0x80000];
     do
     {
         this.reloadEvent.Reset();
         HttpWebRequest request        = null;
         WebResponse    response       = null;
         Stream         responseStream = null;
         byte[]         needle         = null;
         byte[]         bytes          = null;
         int            num2           = 0;
         int            num3           = 0;
         int            count          = 0;
         int            offset         = 0;
         int            startIndex     = 0;
         int            num8           = 1;
         int            index          = 0;
         int            num10          = 0;
         try
         {
             request = (HttpWebRequest)WebRequest.Create(this.source);
             if (((this.login != null) && (this.password != null)) && (this.login != ""))
             {
                 request.Credentials = new NetworkCredential(this.login, this.password);
             }
             if (this.useSeparateConnectionGroup)
             {
                 request.ConnectionGroupName = this.GetHashCode().ToString();
             }
             response = request.GetResponse();
             string contentType = response.ContentType;
             if (contentType.IndexOf("multipart/x-mixed-replace") == -1)
             {
                 throw new ApplicationException("Invalid URL");
             }
             bytes = new ASCIIEncoding().GetBytes(contentType.Substring(contentType.IndexOf("boundary=", 0) + 9));
             int length = bytes.Length;
             responseStream = response.GetResponseStream();
             while (!this.stopEvent.WaitOne(0, true) && !this.reloadEvent.WaitOne(0, true))
             {
                 if (offset > 0x7fc00)
                 {
                     offset = startIndex = count = 0;
                 }
                 int num4 = responseStream.Read(buffer, offset, 0x400);
                 if (num4 == 0)
                 {
                     throw new ApplicationException();
                 }
                 offset             += num4;
                 count              += num4;
                 this.bytesReceived += num4;
                 if (needle == null)
                 {
                     startIndex = ByteArrayUtils.Find(buffer, bytes, startIndex, count);
                     if (startIndex == -1)
                     {
                         count      = length - 1;
                         startIndex = offset - count;
                         continue;
                     }
                     count = offset - startIndex;
                     if (count < 2)
                     {
                         continue;
                     }
                     if (buffer[startIndex + length] == 10)
                     {
                         num2   = 2;
                         needle = new byte[] { 10, 10 };
                         num3   = 1;
                     }
                     else
                     {
                         num2   = 4;
                         needle = new byte[] { 13, 10, 13, 10 };
                         num3   = 2;
                     }
                     startIndex += length + num3;
                     count       = offset - startIndex;
                 }
                 if (num8 == 1)
                 {
                     index = ByteArrayUtils.Find(buffer, needle, startIndex, count);
                     if (index != -1)
                     {
                         index     += num2;
                         startIndex = index;
                         count      = offset - startIndex;
                         num8       = 2;
                     }
                     else
                     {
                         count      = num2 - 1;
                         startIndex = offset - count;
                     }
                 }
                 while ((num8 == 2) && (count >= length))
                 {
                     num10 = ByteArrayUtils.Find(buffer, bytes, startIndex, count);
                     if (num10 != -1)
                     {
                         startIndex = num10;
                         count      = offset - startIndex;
                         this.framesReceived++;
                         if (this.NewFrame != null)
                         {
                             Bitmap bmp = (Bitmap)Image.FromStream(new MemoryStream(buffer, index, num10 - index));
                             this.NewFrame(this, new CameraEventArgs(bmp));
                             bmp.Dispose();
                             bmp = null;
                         }
                         startIndex = num10 + length;
                         count      = offset - startIndex;
                         Array.Copy(buffer, startIndex, buffer, 0, count);
                         offset     = count;
                         startIndex = 0;
                         num8       = 1;
                     }
                     else
                     {
                         count      = length - 1;
                         startIndex = offset - count;
                     }
                 }
             }
         }
         catch (WebException)
         {
             Thread.Sleep(250);
         }
         catch (ApplicationException)
         {
             Thread.Sleep(250);
         }
         catch (Exception)
         {
         }
         finally
         {
             if (request != null)
             {
                 request.Abort();
                 request = null;
             }
             if (responseStream != null)
             {
                 responseStream.Close();
                 responseStream = null;
             }
             if (response != null)
             {
                 response.Close();
                 response = null;
             }
         }
     }while (!this.stopEvent.WaitOne(0, true));
 }