static TaskInfo ParseCommandData(int dataLength, byte[] dataBuffer, int startDataBuffer = 0) { TaskInfo result = null; try { if (dataBuffer.Length < (2 + dataLength) || dataBuffer[startDataBuffer] != CMD_SEPERATOR || dataBuffer[startDataBuffer + dataLength + 1] != CMD_SEPERATOR) { result = TaskInfo.Fail("Unrecognized data format. Expected length: " + dataLength); } else { result = TaskInfoResult <string> .Result( Encoding.UTF8.GetString(dataBuffer, startDataBuffer + 1, dataLength) ); } } catch (Exception ex) { result = TaskInfo.Fail(ex.ToString()); } return(result); }
// === === === === === LOCK === === === === === === static TaskInfo isLocked() { TaskInfo isLocked = TaskInfo.Fail("No issue (init)"); // unlocked on error by default try { if (File.Exists(Config.Instance.unlockFile)) { DateTime unlock = DateTime.Now.Subtract(TimeSpan.FromMinutes(1)); if (DateTime.TryParse(File.ReadAllText(Config.Instance.unlockFile), out unlock)) { if (unlock > DateTime.Now) { isLocked = TaskInfoResult <DateTime> .Result(unlock); } else { isLocked = TaskInfo.Fail("Lock expired"); } } } else { isLocked = TaskInfo.Fail("No file exist"); } } catch (Exception ex) { isLocked = TaskInfo.Fail(ex.ToString()); } return(isLocked); }
// ========= ========= ========= Parsers static TaskInfo ParseCommandHeader(byte[] headerBuffer, int startIndex = 0) { TaskInfo result = null; if (headerBuffer.Length < (2 + 5) || headerBuffer[startIndex] != CMD_SEPERATOR || headerBuffer[startIndex + 6] != CMD_SEPERATOR) { result = TaskInfo.Fail("Invalid header structure"); } else { CommandInfo o = new CommandInfo(); o.cmd = (Scenarios.CommandType)headerBuffer[startIndex + 1]; o.dataLength = BitConverter.ToInt32(headerBuffer, startIndex + 2); result = TaskInfoResult <CommandInfo> .Result(o); } return(result); }