private void ServerThreadRoutine(object data) { string defaultTimeOutValuestring = TimeOutValuestring; TvBusinessLayer layer = new TvBusinessLayer(); Setting setting = null; while (PipeServerActive) { try { var sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null); var rule = new PipeAccessRule(sid, PipeAccessRights.ReadWrite,System.Security.AccessControl.AccessControlType.Allow); var sec = new PipeSecurity(); sec.AddAccessRule(rule); #if (MPTV2) string pipeName = "MP2TvWishListPipe"; #else string pipeName = "TvWishListPipe"; #endif pipeServer = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.None, 0, 0, sec); //only 1 thread for pipe int threadId = Thread.CurrentThread.ManagedThreadId; // Wait for a client to connect Logdebug("TvServer: Waiting for client to connect"); PipeServerBusy = false; pipeServer.WaitForConnection(); ServerMessage = string.Empty; Logdebug("Client connected on pipe server thread."); PipeServerBusy = true; ; // Read the request from the client. Once the client has // written to the pipe its security token will be available. //pipeServer.ReadTimeout = 5000; //timeout not supported for async streams StreamString ss = new StreamString(pipeServer); // Verify our identity to the connected client using a // string that the client anticipates. MessageFromClient = ss.ReadString(); //receive message from client first //labelreceivedTextServer.Text = messagefromclient; Logdebug("***** CLIENTMESSAGE=" + MessageFromClient); //******************************* //commandinterpretation //******************************* if (MessageFromClient == PipeCommands.RequestTvVersion.ToString()) { string response = PipeCommands.RequestTvVersion.ToString() + "=" + this.Version; Logdebug("sending response " + response); ss.WriteString(response); } else if (MessageFromClient.StartsWith(PipeCommands.StartEpg.ToString()) == true) { defaultTimeOutValuestring = TimeOutValuestring; string[] tokens = MessageFromClient.Split(':'); if (tokens.Length > 1) { TimeOutValuestring = tokens[1]; Logdebug("Changed TimeOutValuestring=" + TimeOutValuestring); } Logdebug("Starting EPG Search from pipe command"); Thread StartEPGsearchThread = new Thread(StartEPGsearchCommand); StartEPGsearchThread.Start(); while ((ServerMessage.StartsWith(PipeCommands.Ready.ToString())==false) && (ServerMessage.StartsWith(PipeCommands.Error.ToString()) == false)) { ServerTimeOutCounter = new Thread(ServerTimeOutError); ServerTimeOutCounter.Start(); while ((NewServerMessage == false) || (ServerMessage==string.Empty)) { Thread.Sleep(500); Logdebug("waiting for new servermessage (ServerMessage="+ServerMessage); } Logdebug("Sending Servermessage=" + ServerMessage); ss.WriteString(ServerMessage); //send response messages until done ServerTimeOutCounter.Abort(); NewServerMessage = false; } TimeOutValuestring = defaultTimeOutValuestring; } else if (MessageFromClient.StartsWith(PipeCommands.Error_TimeOut.ToString()) == true) { TimeOutValuestring = MessageFromClient.Replace(PipeCommands.Error_TimeOut.ToString(), string.Empty); Logdebug("new TimeOutValuestring="+TimeOutValuestring); ss.WriteString(TimeOutValuestring); } else if (MessageFromClient.StartsWith(PipeCommands.ExportTvWishes.ToString()) == true) { defaultTimeOutValuestring = TimeOutValuestring; string[] tokens = MessageFromClient.Split(':'); if (tokens.Length > 1) { TimeOutValuestring = tokens[1]; Log.Debug("Changed TimeOutValuestring=" + TimeOutValuestring); } if (MessageFromClient.Contains("VIEWONLY=TRUE") == true) { ServerMessage = ExportTvWishes(true); } else //Email & Record Mode { ServerMessage = ExportTvWishes(false); } ss.WriteString(ServerMessage); TimeOutValuestring = defaultTimeOutValuestring; } else if (MessageFromClient.StartsWith(PipeCommands.ImportTvWishes.ToString()) == true) { defaultTimeOutValuestring = TimeOutValuestring; string[] tokens = MessageFromClient.Split(':'); if (tokens.Length > 1) { TimeOutValuestring = tokens[1]; Logdebug("Changed TimeOutValuestring=" + TimeOutValuestring); } if (MessageFromClient.Contains("VIEWONLY=TRUE") == true) { ServerMessage = ImportTvWishes(true); } else //Email & Record Mode { ServerMessage = ImportTvWishes(false); } ss.WriteString(ServerMessage); TimeOutValuestring = defaultTimeOutValuestring; } else if (MessageFromClient.StartsWith(PipeCommands.RemoveSetting.ToString()) == true) { defaultTimeOutValuestring = TimeOutValuestring; string[] tokens = MessageFromClient.Split(':'); if (tokens.Length > 1) { TimeOutValuestring = tokens[1]; Logdebug("Changed TimeOutValuestring=" + TimeOutValuestring); } string tag = tokens[0].Replace(PipeCommands.RemoveSetting.ToString(), string.Empty); setting = layer.GetSetting(tag, string.Empty); if (setting != null) { setting.Remove(); ServerMessage = "Setting " + tag + " removed"; Logdebug("Setting " + tag + " removed"); } else { ServerMessage = "Setting " + tag + " could not be removed"; Logdebug("Eror: Setting " + tag + " could not be removed"); } ss.WriteString(ServerMessage); TimeOutValuestring = defaultTimeOutValuestring; } else if (MessageFromClient.StartsWith(PipeCommands.RemoveRecording.ToString()) == true) {//string command = Main_GUI.PipeCommands.RemoveRecording.ToString() + this.IdRecording.ToString() + ":10"; defaultTimeOutValuestring = TimeOutValuestring; string[] tokens = MessageFromClient.Split(':'); if (tokens.Length > 1) { TimeOutValuestring = tokens[1]; Logdebug("Changed TimeOutValuestring=" + TimeOutValuestring); } string idRecordingString = tokens[0].Replace(PipeCommands.RemoveRecording.ToString(), string.Empty); try { int idRecording = -1; int.TryParse(idRecordingString, out idRecording); Logdebug("idRecording=" + idRecording.ToString()); Recording myrecording = Recording.Retrieve(idRecording); Logdebug("idRecording=" + myrecording.Title.ToString()); myrecording.Delete(); Logdebug("Recording deleted"); ServerMessage = "Recording deleted"; } catch (Exception exc) { Logdebug("no recording found for idRecordingString = " + idRecordingString); Logdebug("exception is " + exc.Message); ServerMessage = "Error: Recording could not be deleted check the tvserver log file"; } ss.WriteString(ServerMessage); TimeOutValuestring = defaultTimeOutValuestring; } else if (MessageFromClient.StartsWith(PipeCommands.RemoveLongSetting.ToString()) == true) { defaultTimeOutValuestring = TimeOutValuestring; string[] tokens = MessageFromClient.Split(':'); if (tokens.Length > 1) { TimeOutValuestring = tokens[1]; Logdebug("Changed TimeOutValuestring=" + TimeOutValuestring); } //processing string mysetting = string.Empty; try { //cleanup work mysetting = tokens[0].Replace(PipeCommands.RemoveLongSetting.ToString(), string.Empty); Log.Debug("mysetting=" + mysetting); for (int i = 1; i < 1000; i++) { setting = layer.GetSetting(mysetting + "_" + i.ToString("D3"), "_DOES_NOT_EXIST_"); Log.Debug("save_longsetting setting=" + setting.Value); if (setting.Value == "_DOES_NOT_EXIST_") { setting.Remove(); break; } else { string value = setting.Value; setting.Remove(); } } ServerMessage = "Long setting could be removed"; } catch (Exception exc) { Logdebug("Longsetting could not be removed for mysetting= " + mysetting); Logdebug("exception is " + exc.Message); ServerMessage = "Error: Long setting could not be removed - check the tvserver log file"; } ss.WriteString(ServerMessage); TimeOutValuestring = defaultTimeOutValuestring; } #if (MPTV2) else if (MessageFromClient.StartsWith(PipeCommands.WriteSetting.ToString()) == true) { string tag = MessageFromClient.Replace(PipeCommands.WriteSetting.ToString(), string.Empty); string[] tags = tag.Split('\n'); ServiceAgents.Instance.SettingServiceAgent.SaveValue(tags[0], tags[1]); ServerMessage = "SUCCESS"; ss.WriteString(ServerMessage); } else if (MessageFromClient.StartsWith(PipeCommands.ReadSetting.ToString()) == true) { string tag = MessageFromClient.Replace(PipeCommands.ReadSetting.ToString(), string.Empty); Log.Debug("tag="+tag); string value = ServiceAgents.Instance.SettingServiceAgent.GetValue(tag, string.Empty); Log.Debug("value=" + value); ServerMessage = value; ss.WriteString(ServerMessage); } else if (MessageFromClient.StartsWith(PipeCommands.ReadAllCards.ToString()) == true) { defaultTimeOutValuestring = TimeOutValuestring; string[] tokens = MessageFromClient.Split(':'); if (tokens.Length > 1) { TimeOutValuestring = tokens[1]; Logdebug("Changed TimeOutValuestring=" + TimeOutValuestring); } ServerMessage = string.Empty; foreach (Card mycard in Card.ListAll()) { ServerMessage += mycard.IdCard.ToString() + "\n" + mycard.Name + "\n"; } //65000 max chars ss.WriteString(ServerMessage); } else if (MessageFromClient.StartsWith(PipeCommands.ReadAllChannelsByGroup.ToString()) == true) { string groupIdString = MessageFromClient.Replace(PipeCommands.ReadAllChannelsByGroup.ToString(), string.Empty); Log.Debug("groupIdString="+groupIdString); int groupId = -1; int.TryParse(groupIdString, out groupId); Log.Debug("groupId=" + groupId.ToString()); ServerMessage = string.Empty; foreach (Channel mychannel in Channel.ListAllByGroup(groupId)) { ServerMessage += mychannel.IdChannel.ToString() + "\n" + mychannel.DisplayName + "\n"; } Log.Debug("Groupchannels=" + ServerMessage); //65000 max chars ss.WriteString(ServerMessage); } else if (MessageFromClient.StartsWith(PipeCommands.ReadAllChannels.ToString()) == true)//must be after ReadAllChannelsByGroup { ServerMessage = string.Empty; foreach (Channel mychannel in Channel.ListAll()) { ServerMessage += mychannel.IdChannel.ToString() + "\n" + mychannel.DisplayName + "\n"; } //65000 max chars ss.WriteString(ServerMessage); } else if (MessageFromClient.StartsWith(PipeCommands.ReadAllRadioChannelsByGroup.ToString()) == true) { string groupIdString = MessageFromClient.Replace(PipeCommands.ReadAllRadioChannelsByGroup.ToString(), string.Empty); Log.Debug("radiogroupIdString=" + groupIdString); int groupId = -1; int.TryParse(groupIdString, out groupId); Log.Debug("radiogroupId=" + groupId.ToString()); ServerMessage = string.Empty; foreach (RadioChannel myradiochannel in RadioChannel.ListAllByGroup(groupId)) { ServerMessage += myradiochannel.Id.ToString() + "\n" + myradiochannel.Name + "\n"; } Log.Debug("radioGroupchannels=" + ServerMessage); //65000 max chars ss.WriteString(ServerMessage); } else if (MessageFromClient.StartsWith(PipeCommands.ReadAllRadioChannels.ToString()) == true)//must be after ReadAllRadioChannelsByGroup { ServerMessage = string.Empty; foreach (RadioChannel myradiochannel in RadioChannel.ListAll()) { ServerMessage += myradiochannel.Id.ToString() + "\n" + myradiochannel.Name + "\n"; } //65000 max chars ss.WriteString(ServerMessage); } else if (MessageFromClient.StartsWith(PipeCommands.ReadAllChannelGroups.ToString()) == true) { ServerMessage = string.Empty; foreach (ChannelGroup mygroup in ChannelGroup.ListAll()) { ServerMessage += mygroup.Id.ToString() + "\n" + mygroup.GroupName + "\n"; } //65000 max chars ss.WriteString(ServerMessage); } else if (MessageFromClient.StartsWith(PipeCommands.ReadAllRadioChannelGroups.ToString()) == true) { ServerMessage = string.Empty; foreach (RadioChannelGroup myradiogroup in RadioChannelGroup.ListAll()) { ServerMessage += myradiogroup.Id.ToString() + "\n" + myradiogroup.GroupName + "\n"; } //65000 max chars ss.WriteString(ServerMessage); } else if (MessageFromClient.StartsWith(PipeCommands.ReadAllRecordings.ToString()) == true) { ServerMessage = string.Empty; foreach (Recording myrecording in Recording.ListAll()) { ServerMessage += myrecording.IdRecording.ToString() + "\n" + myrecording.Title + "\n"+myrecording.FileName + "\n" + myrecording.IdChannel.ToString() + "\n" + myrecording.StartTime.ToString("yyyy-MM-dd_HH:mm", CultureInfo.InvariantCulture) + "\n" + myrecording.EndTime.ToString("yyyy-MM-dd_HH:mm", CultureInfo.InvariantCulture) + "\n"; } //65000 max chars ss.WriteString(ServerMessage); } else if (MessageFromClient.StartsWith(PipeCommands.ReadAllSchedules.ToString()) == true) { ServerMessage = string.Empty; foreach (Schedule myschedule in Schedule.ListAll()) { ServerMessage += myschedule.IdSchedule.ToString() + "\n" + myschedule.ProgramName + "\n" + myschedule.IdChannel.ToString() + "\n" + myschedule.StartTime.ToString("yyyy-MM-dd_HH:mm", CultureInfo.InvariantCulture) + "\n" + myschedule.EndTime.ToString("yyyy-MM-dd_HH:mm", CultureInfo.InvariantCulture) + "\n" + myschedule.ScheduleType.ToString() + "\n" + myschedule.PreRecordInterval.ToString() + "\n" + myschedule.PostRecordInterval.ToString() + "\n" + myschedule.MaxAirings.ToString() + "\n" + myschedule.KeepDate.ToString("yyyy-MM-dd_HH:mm", CultureInfo.InvariantCulture) + "\n" + myschedule.KeepMethod.ToString() + "\n" + myschedule.Priority.ToString() + "\n" + myschedule.PreRecordInterval.ToString() + "\n" + myschedule.Series.ToString() + "\n"; } //65000 max chars ss.WriteString(ServerMessage); } else if (MessageFromClient.StartsWith(PipeCommands.ScheduleDelete.ToString()) == true) { string scheduleIdString = MessageFromClient.Replace(PipeCommands.ScheduleDelete.ToString(), string.Empty); Log.Debug("scheduleIdString=" + scheduleIdString); int scheduleId = -1; int.TryParse(scheduleIdString, out scheduleId); Log.Debug("scheduleId=" + scheduleId.ToString()); Schedule.Delete(scheduleId); //65000 max chars ss.WriteString("Deleted"); } else if (MessageFromClient.StartsWith(PipeCommands.ScheduleNew.ToString()) == true) { string schedule = MessageFromClient.Replace(PipeCommands.ScheduleNew.ToString(), string.Empty); string[] scheduletags = schedule.Split('\n'); int idChannel = -1; int.TryParse(scheduletags[1], out idChannel); DateTime start = DateTime.ParseExact(scheduletags[2], "yyyy-MM-dd_HH:mm", System.Globalization.CultureInfo.InvariantCulture); DateTime end = DateTime.ParseExact(scheduletags[3], "yyyy-MM-dd_HH:mm", System.Globalization.CultureInfo.InvariantCulture); Schedule myschedule = new Schedule(idChannel, scheduletags[0], start, end); myschedule.Persist(); ServerMessage = "SUCCESS"; ss.WriteString(ServerMessage); } #endif else //Unknown command { Logdebug("sending response " + PipeCommands.UnknownCommand.ToString()); ss.WriteString(PipeCommands.UnknownCommand.ToString()); } } // Catch the IOException that is raised if the pipe is broken // or disconnected. catch (IOException e) { Log.Error("ServerThread ERROR: " + e.Message); } catch (Exception e) { Log.Error("ServerThread ERROR: " + e.Message); } if (pipeServer != null) { if (pipeServer.IsConnected) pipeServer.Disconnect(); pipeServer.Close(); pipeServer.Dispose(); pipeServer = null; } Logdebug("Connection closed"); } Logdebug("Pipe Server Thread Completed"); }
private void ClientThreadRoutineRunSingleCommand() { ReceivedMessage = "Error in ClientThreadRoutineRunSingleCommand"; try { Log.Debug("ClientThreadRoutineRunSingleCommand: Connecting to server " + hostName); Log.Debug("pipe=" + pipeName); Log.Debug("hostname=" + hostName); pipeClient = new NamedPipeClientStream(hostName, pipeName, PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation); pipeClient.Connect(); StreamString ss = new StreamString(pipeClient); //Log.Debug("1 ClientMessage=" + ClientMessage); //send clientmessage to server ss.WriteString(ClientMessage); //pipeClient.ReadTimeout = 5000; //timeout not supported for async streams PipeRunThreadTimeOutCounter = new Thread(ClientTimeOutError); PipeRunThreadTimeOutCounter.Start(); ReceivedMessage = ss.ReadString(); PipeRunThreadTimeOutCounter.Abort(); Log.Debug("ClientThreadRoutineRunSingleCommand: ***** SERVERMESSAGE=" + ReceivedMessage); Log.Debug("ClientThreadRoutineRunSingleCommand: closing client pipe - command executed"); } catch (Exception exc) { Log.Debug("ClientThreadRoutineRunSingleCommand: ClientThread() exception=" + exc.Message); } if (pipeClient != null) { pipeClient.Close(); //pipeClient = null; } Log.Debug("ClientThreadRoutineRunSingleCommand: Pipe Client Thread Completed"); //OldConnect = false; return; }
public ReadFileToStream(StreamString str, string filename) { fn = filename; ss = str; }
private void ClientRoutineRunEpg() { bool success = false; for (int i = 1; i <= 3; i++) //three retries for sending command { TIMEOUT = false; try { Log.Debug("ClientRoutineRunEpg: Connecting to server " + hostName); Log.Debug("pipe=" + pipeName); Log.Debug("hostname=" + hostName); pipeClient = new NamedPipeClientStream(hostName, pipeName, PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation); pipeClient.Connect(); StreamString ss = new StreamString(pipeClient); //send clientmessage to server Log.Debug("Writing command " + ClientMessage); ss.WriteString(ClientMessage); ReceivedMessage = "ClientThreadRoutineRunEpg Error"; while ((ReceivedMessage.StartsWith(PipeCommands.Ready.ToString()) == false) && (ReceivedMessage.StartsWith(PipeCommands.Error.ToString()) == false)) { PipeRunThreadTimeOutCounter = new Thread(ClientTimeOutError); PipeRunThreadTimeOutCounter.Start(); ReceivedMessage = ss.ReadString(); PipeRunThreadTimeOutCounter.Abort(); //evaluate response from server string Processedmessage = string.Empty; if (ReceivedMessage.StartsWith(PipeCommands.Ready.ToString()) == true) { Processedmessage = ReceivedMessage.Substring(PipeCommands.Ready.ToString().Length); } else if (ReceivedMessage.StartsWith(PipeCommands.Error.ToString()) == true) { Processedmessage = ReceivedMessage.Substring(PipeCommands.Error.ToString().Length); } else { Processedmessage = ReceivedMessage; } Log.Debug("***** SERVERMESSAGE=" + ReceivedMessage); labelmessage(Processedmessage, PipeCommands.StartEpg); //myTvWishes.StatusLabel(Processedmessage); Log.Debug("***** SERVERMESSAGE=" + ReceivedMessage); } Log.Debug("closing client pipe - command executed"); if (pipeClient != null) { pipeClient.Close(); if (pipeClient != null) pipeClient = null; } success = true; break; } catch (Exception exc) { Log.Debug("Sending tv server command failed in iteration i=" + i.ToString()); Log.Debug("ClientThread() exception=" + exc.Message); Thread.Sleep(2000); if (pipeClient != null) { pipeClient.Close(); if (pipeClient != null) pipeClient = null; } if (TIMEOUT) { TIMEOUT = false; break; } } } if (success == false) { //MessageBox.Show(lng.TranslateString("TvWishList MediaPortal Plugin Does Not Match To TvWishList TV Server Plugin", 1206).Replace("<br>", "\n")); MessageBox.Show(lng.TranslateString("TvWishList MediaPortal Plugin Does Not Match To TvWishList TV Server Plugin", 1206).Replace("<br>", Environment.NewLine),"Error"); // myTvWishes.MyMessageBox(4305, 1206); //TvWishList MediaPortal Plugin Does Not Match To TvWishList TV Server Plugin } Log.Debug("ClientThreadRoutineRunEpg() Thread Completed"); //OldConnect = false; }