Exemplo n.º 1
0
        public void Reload()
        {
            ImagesSource imgSource = new ImagesSource(moment.Media.FindAll((obj) => { return(obj.Type == "Image"); }));

            this.imagesCollectionView.Source = imgSource;
            imgSource.MediaSelected         += (media) =>
            {
                MediaViewController viewController = new MediaViewController();
                viewController.Config(media, this.moment);
                this.NavigationController.PushViewController(viewController, true);
            };

            ImagesSource videoSource = new ImagesSource(moment.Media.FindAll((obj) => { return(obj.Type == "Video"); }));

            this.videosCollectionView.Source = videoSource;
            videoSource.MediaSelected       += (media) =>
            {
                MediaViewController viewController = new MediaViewController();
                viewController.Config(media, this.moment);
                this.NavigationController.PushViewController(viewController, true);
            };

            this.mapView.RemoveAnnotations(this.mapView.Annotations);
            this.mapView.AddAnnotation(new MKPointAnnotation()
            {
                Coordinate = new CoreLocation.CLLocationCoordinate2D(this.moment.Location.Lat, this.moment.Location.Lng)
            });
            mapView.SetCenterCoordinate(new CoreLocation.CLLocationCoordinate2D(this.moment.Location.Lat, this.moment.Location.Lng), true);
            mapView.Camera.Altitude = 500;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Each client connection will be served by this thread.
        /// </summary>
        /// <param name="client"></param>
        private void ClientThread(object client)
        {
            var socket = client as Socket;

            Debug.WriteLine(string.Format("New client from {0}", socket.RemoteEndPoint.ToString()));

            lock (_clients)
                _clients.Add(socket);
            try
            {
                using (var wr = new StreamJPGWriter(new NetworkStream(socket, true)))
                {
                    wr.WriteHeader();
                    foreach (var imgStream in ImagesSource.GenerateMemoryStream())
                    {
                        if (Interval > 0)
                        {
                            Thread.Sleep(Interval);
                        }

                        wr.Write(imgStream);
                    }
                }
            }
            catch { }
            finally
            {
                lock (_clients)
                    _clients.Remove(socket);
            }
        }
        public void Stop()
        {
            if (IsRunning)
            {
                try
                {
                    _Thread.Join();
                    _Thread.Abort();
                }
                finally
                {
                    lock (_Clients)
                    {
                        foreach (var s in _Clients)
                        {
                            try
                            {
                                s.Close();
                            }
                            catch { }
                        }
                        _Clients.Clear();
                    }

                    lock (ImagesSource)
                        ImagesSource.Clear();

                    _Thread = null;
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        ///     Each client connection will be served by this thread.
        /// </summary>
        /// <param name="client"></param>
        private void ClientThread(object client)
        {
            var socket = (Socket)client;

            WriteToLogbook(string.Format("New client from: {0}", socket.RemoteEndPoint));

            lock (_clients)
            {
                _clients.Add(socket);
            }

            try
            {
                using (var mjpegWriter = new MjpegWriter(new NetworkStream(socket, true)))
                {
                    // Writes the response header to the client.
                    mjpegWriter.WriteHeader();

                    // Streams the images from the source to the client.
                    foreach (var imageStream in ImagesSource.Streams())
                    {
                        if (Interval > 0)
                        {
                            Thread.Sleep(Interval);
                        }

                        mjpegWriter.Write(imageStream);
                    }
                }
            }
            catch (Exception exception)
            {
                WriteToLogbook(exception.Message);
            }
            finally
            {
                lock (_clients)
                {
                    _clients.Remove(socket);
                }
            }
        }
Exemplo n.º 5
0
        private void ClientThread(object client)
        {
            if (_stopListeningTokenSource.IsCancellationRequested)
            {
                return;
            }

            Socket socket = (Socket)client;

            lock (_clients)
                _clients.Add(socket);

            try
            {
                using (MjpegWriter writer = new MjpegWriter(new NetworkStream(socket, true)))
                {
                    writer.WriteHeader();

                    while (!_stopListeningTokenSource.IsCancellationRequested)
                    {
                        MemoryStream ms = new MemoryStream();

                        foreach (var imgStream in ImagesSource.Streams())
                        {
                            Thread.Sleep(_interval);
                            writer.Write(imgStream);
                        }
                    }
                }
            }
            catch
            {
                // Shit happens...
                // Client probably disconnected.
            }
            finally
            {
                lock (_clients)
                    _clients.Remove(socket);
            }
        }
        /// <summary>
        /// Each client connection will be served by this thread.
        /// </summary>
        /// <param name="client"></param>
        private void ClientThread(object camobject)
        {
            ClientData clientdata = (ClientData)camobject;

            Socket socket = clientdata.client;

            string apAdress = socket.RemoteEndPoint.ToString();

            Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss - ") + TAG + string.Format(".ClientThread.Connect({0})", apAdress), "WEB INFO");

            lock (_Clients)
                _Clients.Add(socket);

            try
            {
                if (callback != null)
                {
                    callback.OnClientConnect(clientdata.chanel, clientdata.stream);
                }

                using (MjpegWriter wr = new MjpegWriter(new NetworkStream(socket, true)))
                {
                    // Writes the response header to the client.
                    wr.WriteHeader();

                    // Streams the images from the source to the client.
                    foreach (var imgStream in Dvr.Streams(ImagesSource[socket]))
                    {
                        if (Interval > 0)
                        {
                            Thread.Sleep(Interval);
                        }

                        wr.Write(imgStream);
                    }
                }
            }
            catch (IOException e)
            {
                Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss - ") + TAG + ".ClientThread.IOException(" + e.Message + ")", "WEB ERROR");
            }
            catch (Exception e)
            {
                Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss - ") + TAG + ".ClientThread.Exception(" + e.Message + ")", "WEB ERROR");
            }
            finally
            {
                if (callback != null)
                {
                    callback.OnClientDisconnect(clientdata.chanel);
                }

                Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss - ") + TAG + string.Format(".ClientThread.Disconnect({0})", apAdress), "WEB INFO");

                socket.Close();

                lock (_Clients)
                    _Clients.Remove(socket);

                lock (ImagesSource)
                    ImagesSource.Remove(socket);
            }

            Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss - ") + TAG + ".ClientThread", "STOP THREAD");
        }
        /// <summary>
        /// This the main thread of the server that serves all the new
        /// connections from clients.
        /// </summary>
        /// <param name="state"></param>
        private void ServerThread(object state)
        {
            Socket Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            Server.Bind(new IPEndPoint(IPAddress.Any, (int)state));
            Server.Listen(50);

            Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss - ") + TAG + string.Format(".ServerThread({0})", state), "WEB INFO");

            foreach (Socket client in Server.IncommingConnectoins())
            {
                try
                {
                    byte[] bytes = new byte[1500];
                    client.Receive(bytes);
                    var    str  = Encoding.Default.GetString(bytes);
                    String path = GetLine(str, 1);
                    String host = GetLine(str, 2);

                    if (path.IndexOf("GET /favicon.ico", StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        client.Close();
                    }
                    else if (path.IndexOf("GET /index.html", StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        using (Stream stream = new NetworkStream(client, true))
                        {
                            string        adress = host.ToLower().Replace("host: ", "");
                            string        html   = @"<html><head></head><body>Life on http://" + adress + "/?chanel=X<br /> Shots on http://" + adress + "/?chanel_shot=X </body></html>";
                            StringBuilder sb     = new StringBuilder();
                            sb.AppendLine("HTTP/1.1 200 OK");
                            sb.AppendLine("content-type: text/html");
                            sb.AppendLine("connection: keep-alive");
                            sb.AppendLine("content-length: " + html.Length.ToString());
                            sb.AppendLine();
                            sb.AppendLine(html);

                            byte[] data_byte = Encoding.ASCII.GetBytes(sb.ToString());
                            stream.Write(data_byte, 0, data_byte.Length);
                        }

                        client.Close();
                    }
                    else if (path.IndexOf("GET /?chanel=", StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        string chanel   = "0";
                        string streamid = "1";

                        string get = path.Split(new char[] { ' ' })[1];
                        get = get.Substring(2);
                        string[] paramArr = get.Split(new char[] { '&' });

                        foreach (string param in paramArr)
                        {
                            string[] paramsplit = param.Split(new char[] { '=' });
                            if (paramsplit[0] == "chanel")
                            {
                                chanel = paramsplit[1];
                            }
                            if (paramsplit[0] == "stream")
                            {
                                streamid = paramsplit[1];
                            }
                        }

                        //string param = "?chanel=";
                        //string chanel = path.Substring(path.IndexOf(param, StringComparison.CurrentCultureIgnoreCase) + param.Length, path.IndexOf(" ", path.IndexOf(param, StringComparison.CurrentCultureIgnoreCase)) - (path.IndexOf(param, StringComparison.CurrentCultureIgnoreCase) + param.Length));

                        lock (ImagesSource)
                            ImagesSource.Add(client, Dvr.Snapshots(chanel, imageData));
                        ClientData clientdata = new ClientData();
                        clientdata.client = client;
                        clientdata.chanel = Int32.Parse(chanel);
                        clientdata.stream = Int32.Parse(streamid);
                        ThreadPool.QueueUserWorkItem(new WaitCallback(ClientThread), clientdata);
                    }
                    else if (path.IndexOf("GET /?chanel_shot=", StringComparison.CurrentCultureIgnoreCase) == 0)
                    {
                        Stream stream     = new NetworkStream(client, true);
                        string chanel     = "0";
                        string streamid   = "0";
                        bool   chanelopen = false;
                        try
                        {
                            string get = path.Split(new char[] { ' ' })[1];
                            get = get.Substring(2);
                            string[] paramArr = get.Split(new char[] { '&' });

                            foreach (string param in paramArr)
                            {
                                string[] paramsplit = param.Split(new char[] { '=' });
                                if (paramsplit[0] == "chanel_shot")
                                {
                                    chanel = paramsplit[1];
                                }
                                if (paramsplit[0] == "stream")
                                {
                                    streamid = paramsplit[1];
                                }
                            }

                            if (callback != null)
                            {
                                callback.OnClientConnect(Int32.Parse(chanel), Int32.Parse(streamid));
                                chanelopen = true;
                            }
                            int counter = 0;
                            while (imageData.ContainsKey(chanel) != true && counter < 1000)
                            {
                                counter++;
                                Thread.Sleep(10);
                            }
                            if (counter == 1000)
                            {
                                client.Close();
                            }
                            else
                            {
                                Image        image = Image.FromStream(new MemoryStream(imageData[chanel]));
                                MemoryStream ms    = new MemoryStream();
                                image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                                StringBuilder sb = new StringBuilder();
                                sb.AppendLine("HTTP/1.1 200 OK");
                                sb.AppendLine("Connection: keep-alive");
                                sb.AppendLine("Content-Type: image/jpeg");
                                sb.AppendLine("Content-Length: " + ms.Length.ToString());
                                sb.AppendLine();
                                byte[] data_byte = Encoding.ASCII.GetBytes(sb.ToString());

                                stream.Write(data_byte, 0, data_byte.Length);
                                ms.WriteTo(stream);

                                stream.Flush();
                            }
                            if (callback != null && chanelopen)
                            {
                                callback.OnClientDisconnect(Int32.Parse(chanel));
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss - ") + TAG + ".ServerThread.Exception(" + e.Message + ")", "WEB ERROR");
                        }
                        finally
                        {
                            Thread.Sleep(100);
                            client.Close();
                        }
                    }
                    else
                    {
                        using (Stream stream = new NetworkStream(client, true))
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.AppendLine("HTTP/1.1 301 Moved Permanently");
                            sb.AppendLine("Location: /index.html");
                            sb.AppendLine();

                            byte[] data_byte = Encoding.ASCII.GetBytes(sb.ToString());
                            stream.Write(data_byte, 0, data_byte.Length);
                        }

                        client.Close();
                    }
                }
                catch (SocketException e)
                {
                    Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss - ") + TAG + ".ServerThread.SocketException(" + e.Message + ")", "WEB ERROR");
                    Debug.WriteLine(e.ToString());
                }
                catch (Exception e)
                {
                    Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss - ") + TAG + ".ServerThread.Exception(" + e.Message + ")", "WEB ERROR");
                    Debug.WriteLine(e.ToString());
                }
            }

            Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss - ") + TAG + ".ServerThread", "STOP THREAD");

            Stop();
        }