public virtual string PrintHeader() { Func <string, string> escape = null; if (Delimiter.Contains(",")) { escape = value => (value.Contains("\"") || value.Contains(",")) ? $"\"{(value.Replace("\"", "\"\""))}\"" : value; } return(string.Join(Delimiter, GetHeaderAry().Select(x => escape?.Invoke(x) ?? x).ToArray())); }
public void Send(byte[] buffer) { Helpers.DebugInfo("#{0}: Sending {1} bytes.", Id, buffer.Length); if (Mode != MessageMode.Raw && buffer.Length > MaxMessageSize) { throw new ArgumentException("Message is larger than MaxMessageSize."); } switch (Mode) { case MessageMode.Raw: _socket.Send(buffer); break; case MessageMode.DelimiterBound: if (Delimiter == null || Delimiter.Length == 0) { throw new NotSupportedException("Delimiter must be at least one byte for delimited messages."); } if (Delimiter.Contains(Escapecode)) { throw new NotSupportedException("The escape code can not be part of the message delimiter."); } var encodedBuffer = appendEscapeCodes(buffer); _socket.Send(encodedBuffer); _socket.Send(Delimiter); break; case MessageMode.PrefixedLength: var sizeOfMessage = BitConverter.GetBytes(buffer.Length); _socket.Send(sizeOfMessage); _socket.Send(buffer); break; case MessageMode.FixedLength: if (buffer.Length != MaxMessageSize) { throw new ArgumentException(string.Format("Message is {0} bytes but expected {1} bytes.", buffer.Length, MaxMessageSize)); } _socket.Send(buffer); break; default: throw new NotSupportedException(); } }
private void ValidateChar(char src, string name) { if (src == ChoCharEx.NUL) { throw new ChoRecordConfigurationException("Invalid 'NUL' {0} specified.".FormatString(name)); } if (Delimiter.Contains(src)) { throw new ChoRecordConfigurationException("{2} [{0}] can't be one of Delimiter characters [{1}]".FormatString(FileHeaderConfiguration.FillChar, Delimiter, name)); } if (EOLDelimiter.Contains(src)) { throw new ChoRecordConfigurationException("{2} [{0}] can't be one of EOLDelimiter characters [{1}]".FormatString(src, EOLDelimiter, name)); } if ((from comm in Comments where comm.Contains(src.ToString()) select comm).Any()) { throw new ChoRecordConfigurationException("One of the Comments contains {0}. Not allowed.".FormatString(name)); } }
public override void Validate(object state) { base.Validate(state); if (Delimiter.IsNull()) { throw new ChoRecordConfigurationException("Delimiter can't be null or whitespace."); } if (Delimiter == EOLDelimiter) { throw new ChoRecordConfigurationException("Delimiter [{0}] can't be same as EODDelimiter [{1}]".FormatString(Delimiter, EOLDelimiter)); } if (Delimiter.Contains(QuoteChar)) { throw new ChoRecordConfigurationException("QuoteChar [{0}] can't be one of Delimiter characters [{1}]".FormatString(QuoteChar, Delimiter)); } if (Comments != null && Comments.Contains(Delimiter)) { throw new ChoRecordConfigurationException("One of the Comments contains Delimiter. Not allowed."); } //Validate Header if (FileHeaderConfiguration != null) { if (FileHeaderConfiguration.FillChar != null) { if (FileHeaderConfiguration.FillChar.Value == ChoCharEx.NUL) { throw new ChoRecordConfigurationException("Invalid '{0}' FillChar specified.".FormatString(FileHeaderConfiguration.FillChar)); } if (Delimiter.Contains(FileHeaderConfiguration.FillChar.Value)) { throw new ChoRecordConfigurationException("FillChar [{0}] can't be one of Delimiter characters [{1}]".FormatString(FileHeaderConfiguration.FillChar, Delimiter)); } if (EOLDelimiter.Contains(FileHeaderConfiguration.FillChar.Value)) { throw new ChoRecordConfigurationException("FillChar [{0}] can't be one of EOLDelimiter characters [{1}]".FormatString(FileHeaderConfiguration.FillChar.Value, EOLDelimiter)); } if ((from comm in Comments where comm.Contains(FileHeaderConfiguration.FillChar.Value.ToString()) select comm).Any()) { throw new ChoRecordConfigurationException("One of the Comments contains FillChar. Not allowed."); } } } string[] headers = state as string[]; if (AutoDiscoverColumns && CSVRecordFieldConfigurations.Count == 0) { if (headers != null) { int index = 0; CSVRecordFieldConfigurations = (from header in headers select new ChoCSVRecordFieldConfiguration(header, ++index)).ToList(); } else { MapRecordFields(RecordType); } } else { int maxFieldPos = CSVRecordFieldConfigurations.Max(r => r.FieldPosition); foreach (var fieldConfig in CSVRecordFieldConfigurations) { if (fieldConfig.FieldPosition > 0) { continue; } fieldConfig.FieldPosition = ++maxFieldPos; } } if (CSVRecordFieldConfigurations.Count > 0) { MaxFieldPosition = CSVRecordFieldConfigurations.Max(r => r.FieldPosition); } else { throw new ChoRecordConfigurationException("No record fields specified."); } //Validate each record field foreach (var fieldConfig in CSVRecordFieldConfigurations) { fieldConfig.Validate(this); } //Check if any field has 0 if (CSVRecordFieldConfigurations.Where(i => i.FieldPosition <= 0).Count() > 0) { throw new ChoRecordConfigurationException("Some fields contain invalid field position. All field positions must be > 0."); } //Check field position for duplicate int[] dupPositions = CSVRecordFieldConfigurations.GroupBy(i => i.FieldPosition) .Where(g => g.Count() > 1) .Select(g => g.Key).ToArray(); if (dupPositions.Length > 0) { throw new ChoRecordConfigurationException("Duplicate field position(s) [Index: {0}] found.".FormatString(String.Join(",", dupPositions))); } if (!FileHeaderConfiguration.HasHeaderRecord) { } else { //Check if any field has empty names if (CSVRecordFieldConfigurations.Where(i => i.FieldName.IsNullOrWhiteSpace()).Count() > 0) { throw new ChoRecordConfigurationException("Some fields has empty field name specified."); } //Check field names for duplicate string[] dupFields = CSVRecordFieldConfigurations.GroupBy(i => i.FieldName, FileHeaderConfiguration.StringComparer) .Where(g => g.Count() > 1) .Select(g => g.Key).ToArray(); if (dupFields.Length > 0) { throw new ChoRecordConfigurationException("Duplicate field name(s) [Name: {0}] found.".FormatString(String.Join(",", dupFields))); } } RecordFieldConfigurationsDict = CSVRecordFieldConfigurations.OrderBy(i => i.FieldPosition).Where(i => !i.Name.IsNullOrWhiteSpace()).ToDictionary(i => i.Name, FileHeaderConfiguration.StringComparer); RecordFieldConfigurationsDict2 = CSVRecordFieldConfigurations.OrderBy(i => i.FieldPosition).Where(i => !i.FieldName.IsNullOrWhiteSpace()).ToDictionary(i => i.FieldName, FileHeaderConfiguration.StringComparer); AlternativeKeys = RecordFieldConfigurationsDict2.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Name, FileHeaderConfiguration.StringComparer); FCArray = RecordFieldConfigurationsDict.ToArray(); LoadNCacheMembers(CSVRecordFieldConfigurations); }
public override void Validate(object state) { base.Validate(state); if (Delimiter.IsNull()) { throw new ChoRecordConfigurationException("Delimiter can't be null or whitespace."); } if (Delimiter == EOLDelimiter) { throw new ChoRecordConfigurationException("Delimiter [{0}] can't be same as EODDelimiter [{1}]".FormatString(Delimiter, EOLDelimiter)); } if (Delimiter.Contains(QuoteChar)) { throw new ChoRecordConfigurationException("QuoteChar [{0}] can't be one of Delimiter characters [{1}]".FormatString(QuoteChar, Delimiter)); } if (Comments != null && Comments.Contains(Delimiter)) { throw new ChoRecordConfigurationException("One of the Comments contains Delimiter. Not allowed."); } //Validate Header if (FileHeaderConfiguration != null) { if (FileHeaderConfiguration.FillChar != null) { ValidateChar(FileHeaderConfiguration.FillChar.Value, nameof(FileHeaderConfiguration.FillChar)); } } string[] headers = state as string[]; if (AutoDiscoverColumns && CSVRecordFieldConfigurations.Count == 0) { if (headers != null && IsDynamicObject) { int index = 0; CSVRecordFieldConfigurations = (from header in headers select new ChoCSVRecordFieldConfiguration(header, ++index)).ToList(); } else { MapRecordFields(RecordType); } } else { int maxFieldPos = CSVRecordFieldConfigurations.Max(r => r.FieldPosition); foreach (var fieldConfig in CSVRecordFieldConfigurations) { if (fieldConfig.FieldPosition > 0) { continue; } fieldConfig.FieldPosition = ++maxFieldPos; } } if (CSVRecordFieldConfigurations.Count > 0) { MaxFieldPosition = CSVRecordFieldConfigurations.Max(r => r.FieldPosition); } else { throw new ChoRecordConfigurationException("No record fields specified."); } //Validate each record field foreach (var fieldConfig in CSVRecordFieldConfigurations) { fieldConfig.Validate(this); } //Check if any field has 0 if (CSVRecordFieldConfigurations.Where(i => i.FieldPosition <= 0).Count() > 0) { throw new ChoRecordConfigurationException("Some fields contain invalid field position. All field positions must be > 0."); } //Check field position for duplicate int[] dupPositions = CSVRecordFieldConfigurations.GroupBy(i => i.FieldPosition) .Where(g => g.Count() > 1) .Select(g => g.Key).ToArray(); if (dupPositions.Length > 0) { throw new ChoRecordConfigurationException("Duplicate field position(s) [Index: {0}] found.".FormatString(String.Join(",", dupPositions))); } if (!FileHeaderConfiguration.HasHeaderRecord) { } else { //Check if any field has empty names if (CSVRecordFieldConfigurations.Where(i => i.FieldName.IsNullOrWhiteSpace()).Count() > 0) { throw new ChoRecordConfigurationException("Some fields has empty field name specified."); } //Check field names for duplicate string[] dupFields = CSVRecordFieldConfigurations.GroupBy(i => i.FieldName, FileHeaderConfiguration.StringComparer) .Where(g => g.Count() > 1) .Select(g => g.Key).ToArray(); if (dupFields.Length > 0) { throw new ChoRecordConfigurationException("Duplicate field name(s) [Name: {0}] found.".FormatString(String.Join(",", dupFields))); } } PIDict = new Dictionary <string, System.Reflection.PropertyInfo>(); PDDict = new Dictionary <string, PropertyDescriptor>(); foreach (var fc in CSVRecordFieldConfigurations) { if (fc.PropertyDescriptor == null) { fc.PropertyDescriptor = ChoTypeDescriptor.GetProperties(RecordType).Where(pd => pd.Name == fc.Name).FirstOrDefault(); } if (fc.PropertyDescriptor == null) { continue; } PIDict.Add(fc.PropertyDescriptor.Name, fc.PropertyDescriptor.ComponentType.GetProperty(fc.PropertyDescriptor.Name)); PDDict.Add(fc.PropertyDescriptor.Name, fc.PropertyDescriptor); } RecordFieldConfigurationsDict = CSVRecordFieldConfigurations.OrderBy(i => i.FieldPosition).Where(i => !i.Name.IsNullOrWhiteSpace()).ToDictionary(i => i.Name, FileHeaderConfiguration.StringComparer); RecordFieldConfigurationsDict2 = CSVRecordFieldConfigurations.OrderBy(i => i.FieldPosition).Where(i => !i.FieldName.IsNullOrWhiteSpace()).ToDictionary(i => i.FieldName, FileHeaderConfiguration.StringComparer); if (IsDynamicObject) { AlternativeKeys = RecordFieldConfigurationsDict2.ToDictionary(kvp => kvp.Key, kvp => { if (kvp.Key == kvp.Value.Name) { return(kvp.Value.Name.ToValidVariableName()); } else { return(kvp.Value.Name); } }, FileHeaderConfiguration.StringComparer); } else { AlternativeKeys = RecordFieldConfigurationsDict2.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Name, FileHeaderConfiguration.StringComparer); } FCArray = RecordFieldConfigurationsDict.ToArray(); LoadNCacheMembers(CSVRecordFieldConfigurations); if (Sanitize) { ValidateChar(InjectionEscapeChar, nameof(InjectionEscapeChar)); foreach (char injectionChar in InjectionChars) { ValidateChar(injectionChar, nameof(InjectionChars)); if (injectionChar.ToString().IsAlphaNumeric()) { throw new ChoRecordConfigurationException("Invalid '{0}' injection char specified.".FormatString(injectionChar)); } } } if (RecordTypeConfiguration != null) { if (RecordSelector == null && RecordTypeCodeExtractor == null) { } } }
public byte[] ReceiveMessage() { switch (Mode) { case MessageMode.DelimiterBound: { if (Delimiter == null || Delimiter.Length == 0) { throw new NotSupportedException("Delimiter must be at least one byte for delimited messages."); } if (Delimiter.Contains(Escapecode)) { throw new NotSupportedException("The escape code can not be part of the message delimiter."); } var buffer = new byte[0].AsEnumerable(); var inEscapeMode = false; do { var readBuffer = readSocketUntil(Delimiter, MaxMessageSize); if (readBuffer == null) { if (Socket.Available >= MaxMessageSize) { throw new InvalidOperationException("Message is larger than max allowed message size."); } return(null); } var escapedBuffer = removeEscapeCodes(readBuffer, ref inEscapeMode); buffer = buffer.Concat(escapedBuffer); } while (buffer.Skip(buffer.Count() - Delimiter.Length).SequenceEqual(Delimiter)); return(buffer.ToArray()); } case MessageMode.FixedLength: { if (Socket.Available < this.MaxMessageSize) { return(null); } var buffer = Receive(this.MaxMessageSize); if (buffer.Length != this.MaxMessageSize) { throw new ApplicationException(string.Format("Expected message of size {0} but got {1} bytes.", MaxMessageSize, buffer.Length)); } return(buffer); } case MessageMode.PrefixedLength: { var messageSizeBuffer = Receive(4, SocketFlags.Peek); if (messageSizeBuffer.Length < 4) { return(null); } var messageSize = BitConverter.ToInt32(messageSizeBuffer, 0); if (messageSize > MaxMessageSize) { throw new InvalidOperationException("Message is larger than max allowed message size."); } if (messageSize > Socket.Available - 4) { return(null); } Receive(4); //Read out the length prefix var buffer = Receive(messageSize); return(buffer); } case MessageMode.Raw: throw new InvalidOperationException("You must first select a message mode."); default: throw new NotSupportedException(); } }
public override void Validate() { if (RecordFieldConfigurations.Count > 0) { MaxFieldPosition = RecordFieldConfigurations.Max(r => r.FieldPosition); } else { throw new ChoRecordConfigurationException("No record fields specified."); } RecordFieldConfigurationsDict = RecordFieldConfigurations.Where(i => !i.Name.IsNullOrWhiteSpace()).ToDictionary(i => i.Name); if (Delimiter.IsNullOrWhiteSpace()) { Delimiter = Culture.TextInfo.ListSeparator; if (Delimiter.IsNullOrWhiteSpace()) { Delimiter = ","; } } base.Validate(); if (Delimiter.IsNullOrWhiteSpace()) { throw new ChoRecordConfigurationException("Delimiter can't be null or whitespace."); } if (Delimiter == EOLDelimiter) { throw new ChoRecordConfigurationException("Delimiter [{0}] can't be same as EODDelimiter [{1}]".FormatString(Delimiter, EOLDelimiter)); } if (Delimiter.Contains(QuoteChar)) { throw new ChoRecordConfigurationException("QuoteChar [{0}] can't be one of Delimiter characters [{1}]".FormatString(QuoteChar, Delimiter)); } if (Comments.Contains(Delimiter)) { throw new ChoRecordConfigurationException("One of the Comments contains Delimiter. Not allowed."); } //Validate Header if (CSVFileHeaderConfiguration != null) { CSVFileHeaderConfiguration.Validate(this); } //Validate each record field foreach (var fieldConfig in RecordFieldConfigurations) { fieldConfig.Validate(this); } if (!CSVFileHeaderConfiguration.HasHeaderRecord) { //Check if any field has 0 if (RecordFieldConfigurations.Where(i => i.FieldPosition <= 0).Count() > 0) { throw new ChoRecordConfigurationException("Some fields contain invalid field position. All field positions must be > 0."); } //Check field position for duplicate int[] dupPositions = RecordFieldConfigurations.GroupBy(i => i.FieldPosition) .Where(g => g.Count() > 1) .Select(g => g.Key).ToArray(); if (dupPositions.Length > 0) { throw new ChoRecordConfigurationException("Duplicate field positions [Index: {0}] specified to record fields.".FormatString(String.Join(",", dupPositions))); } } else { //Check if any field has empty names if (RecordFieldConfigurations.Where(i => i.FieldName.IsNullOrWhiteSpace()).Count() > 0) { throw new ChoRecordConfigurationException("Some fields has empty field name specified."); } //Check field names for duplicate string[] dupFields = RecordFieldConfigurations.GroupBy(i => i.FieldName, CSVFileHeaderConfiguration.StringComparer) .Where(g => g.Count() > 1) .Select(g => g.Key).ToArray(); if (dupFields.Length > 0) { throw new ChoRecordConfigurationException("Duplicate field names [Name: {0}] specified to record fields.".FormatString(String.Join(",", dupFields))); } } }
public override void Validate(object state) { base.Validate(state); if (Delimiter.IsNullOrWhiteSpace()) throw new ChoRecordConfigurationException("Delimiter can't be null or whitespace."); if (Delimiter == EOLDelimiter) throw new ChoRecordConfigurationException("Delimiter [{0}] can't be same as EODDelimiter [{1}]".FormatString(Delimiter, EOLDelimiter)); if (Delimiter.Contains(QuoteChar)) throw new ChoRecordConfigurationException("QuoteChar [{0}] can't be one of Delimiter characters [{1}]".FormatString(QuoteChar, Delimiter)); if (Comments.Contains(Delimiter)) throw new ChoRecordConfigurationException("One of the Comments contains Delimiter. Not allowed."); //Validate Header if (CSVFileHeaderConfiguration != null) CSVFileHeaderConfiguration.Validate(this); string[] headers = state as string[]; if (AutoDiscoverColumns && RecordFieldConfigurations.Count == 0 && headers != null) { int index = 0; RecordFieldConfigurations = (from header in headers select new ChoCSVRecordFieldConfiguration(header, ++index)).ToList(); } if (RecordFieldConfigurations.Count > 0) MaxFieldPosition = RecordFieldConfigurations.Max(r => r.FieldPosition); else throw new ChoRecordConfigurationException("No record fields specified."); RecordFieldConfigurationsDict = RecordFieldConfigurations.Where(i => !i.Name.IsNullOrWhiteSpace()).ToDictionary(i => i.Name); //Validate each record field foreach (var fieldConfig in RecordFieldConfigurations) fieldConfig.Validate(this); if (!CSVFileHeaderConfiguration.HasHeaderRecord) { //Check if any field has 0 if (RecordFieldConfigurations.Where(i => i.FieldPosition <= 0).Count() > 0) throw new ChoRecordConfigurationException("Some fields contain invalid field position. All field positions must be > 0."); //Check field position for duplicate int[] dupPositions = RecordFieldConfigurations.GroupBy(i => i.FieldPosition) .Where(g => g.Count() > 1) .Select(g => g.Key).ToArray(); if (dupPositions.Length > 0) throw new ChoRecordConfigurationException("Duplicate field positions [Index: {0}] specified to record fields.".FormatString(String.Join(",", dupPositions))); } else { //Check if any field has empty names if (RecordFieldConfigurations.Where(i => i.FieldName.IsNullOrWhiteSpace()).Count() > 0) throw new ChoRecordConfigurationException("Some fields has empty field name specified."); //Check field names for duplicate string[] dupFields = RecordFieldConfigurations.GroupBy(i => i.FieldName, CSVFileHeaderConfiguration.StringComparer) .Where(g => g.Count() > 1) .Select(g => g.Key).ToArray(); if (dupFields.Length > 0) throw new ChoRecordConfigurationException("Duplicate field names [Name: {0}] specified to record fields.".FormatString(String.Join(",", dupFields))); } }