public Session(Socket s) { socket = s; userId = ""; sState = SState.State_Init; headerBuff = new byte[HEADER_SIZE]; }
public void Close() { if (socket != null) { socket.Disconnect(false); socket.Close(); } recvBuff.Dispose(); socket = null; userId = ""; sState = SState.State_Init; headerBuff = null; Disconnected = null; DataReceived = null; }
//-------------------------------------------------------------------------Read Formats---------------// /// <summary> /// Converts an advanced substation alpha subtitle into the local subtitle format /// </summary> /// <param name="path">Path to the subtitle to read</param> private void ReadASS(string path) { string subContent = File.ReadAllText(path, EncodingRead); subContent = Regex.Replace(subContent, @"\{[^}]*\}", ""); //Remove all additional styling using (StringReader assFile = new StringReader(subContent)) { string line = ""; SState state = SState.Empty; while ((line = assFile.ReadLine()) != null) //Iterate over string { switch (state) { case SState.Empty: if (line.StartsWith("[Events]")) //The row before all dialog { assFile.ReadLine(); //Skip the format state = SState.Iterating; } break; case SState.Iterating: if (line.StartsWith("Dialogue:")) //As Diaglog starts with this { //Split into 10 Segments //Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text //TODO: does it require them all?, or will the magic 10 be replace based on ^ string[] splitLine = line.Split(CommaArray, 10, StringSplitOptions.None); DateTime beginTime = DateTime.ParseExact(splitLine[1], "H:mm:ss.ff", CultureInfo.InvariantCulture); DateTime endTime = DateTime.ParseExact(splitLine[2], "H:mm:ss.ff", CultureInfo.InvariantCulture); string text = splitLine[9].Replace("\\N", "\n");//Replace \N with actual newlines subTitleLocal.Add(new SubtitleEntry(beginTime, endTime, text)); } break; } } } //Since ass/ssa can be in any order we must do this; //It shouldn't mess up already ordered for the next part subTitleLocal = subTitleLocal.OrderBy(o => o.startTime).ToList(); JoinSameStart(); }
/// <summary> /// Converts a wsrt subtitle into the local subtitle format /// </summary> /// <param name="path">Path to the WSRT file</param> private void ReadWSRT2(string path) { string raw = File.ReadAllText(path, EncodingRead); raw = raw.Replace("\r\n", "\n"); raw = raw.Trim(); var splited = raw.Split(NewLineArray, StringSplitOptions.None).ToList(); DateTime beginTime = new DateTime(); DateTime endTime = new DateTime(); string previous = "<blankstring>"; //Since need to handle first time option and can string subContent = ""; SState ss = SState.Empty; string cleanedString = ""; foreach (string line in splited) { switch (ss) { case (SState.Empty): //First time if (line.Contains("-->")) { string[] time = Regex.Split(line, " *--> *"); DateTime.TryParse(time[0], out beginTime); DateTime.TryParse(time[1].Split(SpaceArray, StringSplitOptions.None)[0], out endTime); //beginTime = DateTime.ParseExact(time[0].Substring(0, 12), "HH:mm:ss.fff", CultureInfo.InvariantCulture); //endTime = DateTime.ParseExact(time[1].Substring(0, 12), "HH:mm:ss.fff", CultureInfo.InvariantCulture); ss = SState.Adding; } break; case (SState.Adding): if (line.Contains("-->")) { //Add cleanedString = subContent.TrimEnd(); cleanedString = Regex.Replace(cleanedString, @"</*[0-9]+>", ""); subTitleLocal.Add(new SubtitleEntry(beginTime, endTime, cleanedString)); //Cleanup for new subContent = ""; previous = "<blankstring>"; //Set date string[] time = Regex.Split(line, " *--> *"); DateTime.TryParse(time[0], out beginTime); DateTime.TryParse(time[1].Split(SpaceArray, StringSplitOptions.None)[0], out endTime); //beginTime = DateTime.ParseExact(time[0].Substring(0, 12), "HH:mm:ss.fff", CultureInfo.InvariantCulture); //endTime = DateTime.ParseExact(time[1].Substring(0, 12), "HH:mm:ss.fff", CultureInfo.InvariantCulture); } else if (previous.Equals("<blankstring>")) { previous = line; } else { subContent += previous + "\n"; previous = line; } break; } } //Add stragggler cleanedString = subContent.TrimEnd(); cleanedString = Regex.Replace(cleanedString, @"</*[0-9]+>", ""); subTitleLocal.Add(new SubtitleEntry(beginTime, endTime, cleanedString)); }
/// <summary> /// Reads a WebVTT to the applications local format /// </summary> /// <param name="path">The path to the subtitle to convert</param> private void ReadWebVTT(string path) { string raw = File.ReadAllText(path, EncodingRead); raw = raw.Replace("\r\n", "\n"); //Replace Windows format raw = raw.Replace("\r", "\n"); //Replace old Mac format (it's in the specs to do so) raw = raw.Trim(); raw = Regex.Replace(raw, @"<v(.*? )(.*?)>", "$2: "); //Replace voice tags with a "Name: " raw = Regex.Replace(raw, @"<[^>]*>", ""); //Remove all anotations var splited = raw.Split(NewLineArray, StringSplitOptions.None).ToList(); SState ss = SState.Empty; //Current state if (!splited[0].StartsWith("WEBVTT")) { return; //Not a valid WebVTT } DateTime beginTime = new DateTime(); DateTime endTime = new DateTime(); string textContent = ""; foreach (string line in splited.Skip(1)) //Iterate line by line { switch (ss) { case (SState.Empty): string linetrim = line.TrimEnd(); if (linetrim.Equals("")) { continue; //Run past newlines } //Style is only allowed to appear before all cues, hence a separate test, //unsure if you can have style and a :: value on same line, so test both anyway if (subTitleLocal.Count == 0 && linetrim.Equals("STYLE") || linetrim.StartsWith("STYLE ")) { ss = SState.Comment; //We want to skip like a note; break; } //WEBVTTComment, or region values we'll just skip if (linetrim.Equals("NOTE") || linetrim.StartsWith("NOTE ") || linetrim.Equals("REGION")) { ss = SState.Comment; break; } if (linetrim.Contains("-->")) { goto case (SState.Timestamp); //As we dont care for Queue ID, test only for timestamp } break; //Must be junk data, spec says to abort, but lets keep going anhyway case (SState.Timestamp): //Split and parse the timestamp string[] time = Regex.Split(line, " *--> *"); //Parse the time, can only be one of 2 option, so try this one first bool tryBegin = DateTime.TryParseExact(time[0], "HH:mm:ss.fff", CultureInfo.InvariantCulture, DateTimeStyles.None, out beginTime); string[] endTimeSplit = time[1].Split(SpaceArray, StringSplitOptions.None); //Timestamp might contain info after it, so remove it bool tryEnd = DateTime.TryParseExact(endTimeSplit[0], "HH:mm:ss.fff", CultureInfo.InvariantCulture, DateTimeStyles.None, out endTime); //If something went wrong, parse it differnetly; if (!tryBegin) { DateTime.TryParseExact(time[0], "mm:ss.fff", CultureInfo.InvariantCulture, DateTimeStyles.None, out beginTime); } if (!tryEnd) { DateTime.TryParseExact(endTimeSplit[0], "mm:ss.fff", CultureInfo.InvariantCulture, DateTimeStyles.None, out endTime); } ss = SState.Iterating; break; case (SState.Iterating): if (line.Equals("")) //Come to the end of the cue block so add it to the sub { string cleanedString = textContent.TrimEnd(); //Remove the additional newline we added subTitleLocal.Add(new SubtitleEntry(beginTime, endTime, cleanedString)); textContent = ""; //Cleanup ss = SState.Empty; } else { textContent += line + "\n"; //Otherwise just add to the cue content; } break; case (SState.Comment): //We dont want notes so lets go here string linetrimc = line.TrimEnd(); if (linetrimc.Equals("")) { ss = SState.Empty; //Reached the end of the comment/style/region; } break; } } if (ss == SState.Iterating) //End of file, add last if we were still going { string cleanedString = textContent.TrimEnd(); //Remove the additional newline we added subTitleLocal.Add(new SubtitleEntry(beginTime, endTime, cleanedString)); } }
SState sState; //Server Session State public void SetSState(SState sState) { this.sState = sState; }