/// <summary> /// StartAnalyse /// </summary> /// <param name="metaData"></param> void StartAnalyse(String metaData) { String vsStatus = " ", vsMsg = " "; Command cmd = new Command(CommandType.VS_START_ANALYSE, "VS_START_ANALYSE" + ";" + vsStatus); try { vsMsg = ";StartAnalyse->" + metaData + ";"; vsStatus = "VS_START_ANALYSE" + ";" + vsServer.StartAnalyse(metaData).ToString(); cmd.MetaData = vsStatus; foreach (ClientManager cm in vsClients) cm.SendCommand(cmd); } catch (Exception err) { SendError(CommandType.VS_START_ANALYSE, err.Message + " " + err.Source + " " + err.StackTrace); } }
/// <summary> /// StopEventAlert /// </summary> /// <param name="metaData"></param> void StopEventAlert(String metaData) { String vsStatus = " ", vsMsg = " "; Command cmd = new Command(CommandType.VS_STOP_EVENT_ALERT, "VS_STOP_EVENT_ALERT" + ";" + vsStatus); try { vsMsg = ";StartEventAlert->" + metaData + ";"; vsStatus = "VS_STOP_EVENT_ALERT" + ";" + vsServer.StartEventAlert(metaData).ToString(); cmd.MetaData = vsStatus; foreach (ClientManager cm in vsClients) cm.SendCommand(cmd); } catch (Exception err) { SendError(CommandType.VS_STOP_EVENT_ALERT, err.Message + " " + err.Source + " " + err.StackTrace); } }
/// <summary> /// SendStatus /// </summary> /// <param name="vsStatus"></param> private void SendStatus(string vsStatus) { Command cmd = new Command(CommandType.VS_INFO, vsStatus); foreach (ClientManager cm in vsClients) cm.SendCommand(cmd); }
/// <summary> /// SendError /// </summary> /// <param name="cmdType"></param> /// <param name="msgs"></param> private void SendError(CommandType cmdType, String cmdMsg) { logger.Log(LogLevel.Error, cmdMsg); Console.WriteLine(cmdMsg); Command cmd = new Command(cmdType, cmdMsg); foreach (ClientManager cm in vsClients) cm.SendCommand(cmd); }
/// <summary> /// ListCameraDataAlert /// </summary> /// <param name="metaData"></param> void ListCameraDataAlert(String metaData) { String vsStatus = " "; try { vsStatus = vsServer.ListCameraDataAlert(metaData); Command cmd = new Command(CommandType.VS_LIST_CAMERA_DATA_ALERT_STATUS, "VS_LIST_CAMERA_DATA_ALERT_STATUS" + ";" + vsStatus); foreach (ClientManager cm in vsClients) cm.SendCommand(cmd); } catch (Exception err) { SendError(CommandType.VS_LIST_CAMERA_DATA_ALERT_STATUS, err.Message + " " + err.Source + " " + err.StackTrace); } }
/// <summary> /// ListInfo /// </summary> /// <param name="metaData"></param> void ListInfo(String metaData) { String vsStatus = " "; try { vsStatus = vsServer.ListInfo(); Command cmd = new Command(CommandType.VS_INFO, "VS_INFO" + ";" + vsStatus); foreach (ClientManager cm in vsClients) cm.SendCommand(cmd); } catch (Exception err) { SendError(CommandType.VS_INFO, err.Message + " " + err.Source + " " + err.StackTrace); } }
/// <summary> /// ListCamerasRecord /// </summary> /// <param name="metaData"></param> void ListCamerasRecord() { String vsStatus = " "; try { String[] vsList = vsServer.ListCamerasRecord(); vsStatus = ""; foreach (String str in vsList) vsStatus += (";" + str); Command cmd = new Command(CommandType.VS_LIST_CAMERAS_RECORD_STATUS, "VS_LIST_CAMERAS_RECORD_STATUS" + ";" + vsStatus); foreach (ClientManager cm in vsClients) cm.SendCommand(cmd); } catch (Exception err) { SendError(CommandType.VS_LIST_CAMERAS_RECORD_STATUS, err.Message + " " + err.Source + " " + err.StackTrace); } }
/// <summary> /// Creates an instance of CommandEventArgs class. /// </summary> /// <param name="cmd">The received command.</param> public CommandEventArgs(Command cmd) { this.command = cmd; }
/// <summary> /// Sends a command to the remote client if the connection is alive. /// </summary> /// <param name="cmd">The command to send.</param> public void SendCommand(Command cmd) { if ( this.socket != null && this.socket.Connected ) { BackgroundWorker bwSender = new BackgroundWorker(); bwSender.DoWork += new DoWorkEventHandler(bwSender_DoWork); bwSender.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwSender_RunWorkerCompleted); bwSender.RunWorkerAsync(cmd); } else this.OnCommandFailed(new EventArgs()); }
private bool SendCommandToClient(Command cmd) { StreamWriter streamWriter = new StreamWriter(this.networkStream); if (cmd.MetaData.Length == 0) return false; try { semaphor.WaitOne(); try { // Send command type int intType = (int)cmd.CommandType; String strType = intType.ToString(); streamWriter.WriteLine(strType); streamWriter.Flush(); // Send metadata size String strSize = cmd.MetaData.Length.ToString(); streamWriter.WriteLine(strSize); streamWriter.Flush(); // Send metadata String strMeta = cmd.MetaData; streamWriter.WriteLine(strMeta); streamWriter.Flush(); } catch (Exception ex) { Console.WriteLine("{0}", ex.Message); } semaphor.Release(); return true; } catch { semaphor.Release(); return false; } }
private void StartReceive(object sender , DoWorkEventArgs e) { StreamReader streamReader = new StreamReader(this.networkStream); while ( this.socket.Connected ) { try { // Read the command's Type. String strType = streamReader.ReadLine(); CommandType cmdType = (CommandType)int.Parse(strType); // Read the MetaData size. String strSize = streamReader.ReadLine(); int metaSize = int.Parse(strSize); // Read the MetaData. String strMeta = streamReader.ReadLine(); // create command Command cmd = new Command(cmdType, strMeta); // send command this.OnCommandReceived(new CommandEventArgs(cmd)); /* //Read the command's Type. byte[] buffer = new byte[4]; int readBytes = this.networkStream.Read(buffer, 0, 4); if (readBytes == 0) break; CommandType cmdType = (CommandType)(BitConverter.ToInt32(buffer, 0)); //Read the command's MetaData size. string cmdMetaData = ""; buffer = new byte[4]; readBytes = this.networkStream.Read(buffer, 0, 4); if (readBytes == 0) break; int metaDataSize = BitConverter.ToInt32(buffer, 0); //Read the command's Meta data. buffer = new byte[metaDataSize]; readBytes = this.networkStream.Read(buffer, 0, metaDataSize); if (readBytes == 0) break; cmdMetaData = System.Text.Encoding.Unicode.GetString(buffer); Command cmd = new Command(cmdType, cmdMetaData); this.OnCommandReceived(new CommandEventArgs(cmd)); */ } catch { } } this.OnDisconnected(new ClientEventArgs(this.socket)); this.Disconnect(); }