/// <summary> /// Each client connection will be served by this thread. /// </summary> /// <param name="client"></param> private void ClientThread(object client) { Socket socket = (Socket)client; System.Diagnostics.Debug.WriteLine(string.Format("New client from {0}", socket.RemoteEndPoint.ToString())); lock (_Clients) _Clients.Add(socket); try { 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 Screen.Streams(this.ImagesSource)) { if (this.Interval > 0) { Thread.Sleep(this.Interval); } wr.Write(imgStream); } } } catch { } 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 client) { System.Net.Sockets.Socket socket = (System.Net.Sockets.Socket)client; System.Diagnostics.Debug.WriteLine(string.Format("New client from {0}", socket.RemoteEndPoint.ToString())); lock (_Clients) _Clients.Add(socket); try { while (true) { using (MjpegWriter wr = new MjpegWriter(new System.Net.Sockets.NetworkStream(socket, true))) { // Writes the response header to the client. wr.WriteHeader(); MemoryStream ms; if (ImagesSource.TryDequeue(out ms) == true) { if (ms == null) { continue; } if (this.Interval > 0) { System.Threading.Thread.Sleep(this.Interval); } wr.Write(ms); } } } } catch (System.Exception ex) { System.Console.WriteLine(ex.Message); } 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 client) { System.Net.Sockets.Socket socket = (System.Net.Sockets.Socket)client; System.Diagnostics.Debug.WriteLine(string.Format("New client from {0}", socket.RemoteEndPoint.ToString())); lock (_Clients) _Clients.Add(socket); try { using (MjpegWriter wr = new MjpegWriter(new System.Net.Sockets.NetworkStream(socket, true))) { // Writes the response header to the client. wr.WriteHeader(); while (socket.Connected == true) { foreach (var img in ImagesSource) { if (this.Interval > 0) { System.Threading.Thread.Sleep(this.Interval); } wr.Write(img); } ImagesSource.Clear(); } } } catch (System.Exception ex) { System.Console.WriteLine(ex.Message); } 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 client) { Socket socket = (Socket)client; System.Diagnostics.Debug.WriteLine(string.Format("New client from {0}",socket.RemoteEndPoint.ToString())); lock (_Clients) _Clients.Add(socket); try { 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 Screen.Streams(this.ImagesSource)) { if (this.Interval > 0) Thread.Sleep(this.Interval); wr.Write(imgStream); } } } catch { } 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"); }