public ThriftMethodHandler(ThriftMethodMetadata methodMetadata, ThriftCodecManager codecManager) { this.QualifiedName = methodMetadata.QualifiedName; this.Name = methodMetadata.Name; this._invokeAsynchronously = methodMetadata.IsAsync; this._oneway = !_invokeAsynchronously && methodMetadata.IsOneWay; ParameterHandler[] parameters = new ParameterHandler[methodMetadata.Parameters.Count()]; foreach (var fieldMetadata in methodMetadata.Parameters) { ThriftParameterInjection parameter = (ThriftParameterInjection)fieldMetadata.Injections.First(); ParameterHandler handler = new ParameterHandler( fieldMetadata.Id, fieldMetadata.Name, codecManager.GetCodec(fieldMetadata.ThriftType)); parameters[parameter.ParameterIndex] = handler; } this._parameterCodecs = parameters; var builder = ImmutableDictionary.CreateBuilder <short, IThriftCodec>(); foreach (var entry in methodMetadata.Exceptions) { builder.Add(entry.Key, codecManager.GetCodec(entry.Value)); } _exceptionCodecs = builder.ToImmutableDictionary(); // get the thrift codec for the return value _successCodec = codecManager.GetCodec(methodMetadata.ReturnType); }
public void DeclaredUserException(Exception t, IThriftCodec exceptionCodec) { foreach (var entry in contexts) { entry.Key.DeclaredUserException(entry.Value, methodName, t, exceptionCodec); } }
public SetThriftCodec(ThriftType type, IThriftCodec <T> elementCodec) { Guard.ArgumentNotNull(type, nameof(type)); Guard.ArgumentNotNull(elementCodec, nameof(elementCodec)); this._type = type; this._elementCodec = elementCodec; }
private Object[] ReadArguments(TProtocol inProtocol) { try { int numArgs = _method.GetParameters().Length; Object[] args = new Object[numArgs]; TProtocolReader reader = new TProtocolReader(inProtocol); // Map incoming arguments from the ID passed in on the wire to the position in the // java argument list we expect to see a parameter with that ID. reader.ReadStructBegin(); while (reader.NextField()) { short fieldId = reader.GetFieldId(); IThriftCodec codec = null; if (!_parameterCodecs.TryGetValue(fieldId, out codec)) { // unknown field reader.SkipFieldData(); } else { // Map the incoming arguments to an array of arguments ordered as the java // code for the handler method expects to see them args[_thriftParameterIdToCSharpArgument[fieldId]] = reader.ReadField(codec); } } reader.ReadStructEnd(); // Walk through our list of expected parameters and if no incoming parameters were // mapped to a particular expected parameter, fill the expected parameter slow with // the default for the parameter type. int argumentPosition = 0; foreach (ThriftFieldMetadata argument in _parameters) { if (args[argumentPosition] == null) { Type argumentType = argument.ThriftType.CSharpType; if (!argumentType.Equals(typeof(void))) { args[argumentPosition] = ThriftyUtilities.GetDefaultValue(argumentType); } } argumentPosition++; } return(args); } catch (TProtocolException e) { // TProtocolException is the only recoverable exception // Other exceptions may have left the input stream in corrupted state so we must // tear down the socket. throw new TApplicationException(TApplicationException.ExceptionType.ProtocolError, e.Message); } }
public CoercionThriftCodec(IThriftCodec <T> codec, TypeCoercion typeCoercion) { Guard.ArgumentNotNull(codec, nameof(codec)); Guard.ArgumentNotNull(typeCoercion, nameof(typeCoercion)); this._codec = codec; this._typeCoercion = typeCoercion; this.Type = typeCoercion.ThriftType; }
public ListThriftCodec(ThriftType type, IThriftCodec <T> elementCodec) { Guard.ArgumentNotNull(type, nameof(type)); Guard.ArgumentNotNull(elementCodec, nameof(elementCodec)); this.type = type; this.elementCodec = elementCodec; this._isArray = type.CSharpType.IsArray; }
public MapThriftCodec(ThriftType type, IThriftCodec <K> keyCodec, IThriftCodec <V> valueCodec) { Guard.ArgumentNotNull(type, nameof(type)); Guard.ArgumentNotNull(keyCodec, nameof(keyCodec)); Guard.ArgumentNotNull(valueCodec, nameof(valueCodec)); this._thriftType = type; this._keyCodec = keyCodec; this._valueCodec = valueCodec; }
public void WriteField(String name, short id, IThriftCodec codec, Object value) { if (value == null) { return; } Protocol.WriteFieldBegin(new TField(name, codec.Type.ProtocolType.ToTType(), id)); codec.WriteObject(value, Protocol); Protocol.WriteFieldEnd(); }
public void WriteMapField <K, V>(String name, short id, IThriftCodec <IDictionary <K, V> > codec, IDictionary <K, V> map) { if (map == null) { return; } Protocol.WriteFieldBegin(new TField(name, TType.Map, id)); codec.Write(map, Protocol); Protocol.WriteFieldEnd(); }
public void WriteListField <E>(String name, short id, IThriftCodec <IList <E> > codec, IList <E> list) { if (list == null) { return; } Protocol.WriteFieldBegin(new TField(name, TType.List, id)); codec.Write(list, Protocol); Protocol.WriteFieldEnd(); }
public void WriteSetField <E>(String name, short id, IThriftCodec <ISet <E> > codec, ISet <E> set) { if (set == null) { return; } Protocol.WriteFieldBegin(new TField(name, TType.Set, id)); codec.Write(set, Protocol); Protocol.WriteFieldEnd(); }
public void WriteStructField <T>(String name, short id, IThriftCodec <T> codec, T fieldValue) { if (fieldValue == null) { return; } Protocol.WriteFieldBegin(new TField(name, TType.Struct, id)); codec.Write(fieldValue, Protocol); Protocol.WriteFieldEnd(); }
public T ReadStructField <T>(IThriftCodec <T> codec) { if (!CheckReadState(TType.Struct)) { return(default(T)); } currentField = null; T fieldValue = codec.Read(this.Protocol); Protocol.ReadFieldEnd(); return(fieldValue); }
public IEnumerable <E> ReadSetField <E>(IThriftCodec <HashSet <E> > setCodec) { if (!CheckReadState(TType.Set)) { return(null); } currentField = null; IEnumerable <E> fieldValue = setCodec.Read(this.Protocol); Protocol.ReadFieldEnd(); return(fieldValue); }
public List <E> ReadListField <E>(IThriftCodec <List <E> > listCodec) { if (!CheckReadState(TType.List)) { return(null); } currentField = null; List <E> read = listCodec.Read(this.Protocol); Protocol.ReadFieldEnd(); return(read); }
public IDictionary <K, V> ReadMapField <K, V>(IThriftCodec <Dictionary <K, V> > mapCodec) { if (!CheckReadState(TType.Map)) { return(null); } currentField = null; IDictionary <K, V> fieldValue = mapCodec.Read(this.Protocol); this.Protocol.ReadFieldEnd(); return(fieldValue); }
private IThriftCodec <T> GetCodecFromCache() { IThriftCodec <T> codec = codecManager.GetCachedCodecIfPresent <T>(); if (codec == null) { throw new ThriftyException( "Tried to encodec/decode using a DelegateCodec before the target codec was " + "built (likely a bug in recursive type support)"); } return(codec); }
public void WriteEnumField <T>(String name, short id, IThriftCodec <T> codec, T enumValue) where T : struct { if (enumValue.Equals(default(T))) { return; } Protocol.WriteFieldBegin(new TField(name, TType.I32, id)); codec.Write(enumValue, Protocol); Protocol.WriteFieldEnd(); }
public Object ReadField(IThriftCodec codec) { if (!CheckReadState(codec.Type.ProtocolType.ToTType())) { return(null); } currentField = null; Object fieldValue = codec.ReadObject(Protocol); Protocol.ReadFieldEnd(); return(fieldValue); }
public ThriftMethodProcessor( Func <IRequestContext, Object> service, String serviceName, ThriftMethodMetadata methodMetadata, ThriftCodecManager codecManager, ILoggerFactory loggerFactory = null) { Guard.ArgumentNotNull(service, nameof(service)); _logger = loggerFactory?.CreateLogger <ThriftMethodProcessor>() ?? (ILogger)NullLogger.Instance; this._serviceFactory = service; this.ServiceName = serviceName; this.Name = methodMetadata.Name; this.QualifiedName = methodMetadata.QualifiedName; this._resultStructName = $"{this.Name}_result"; this._method = methodMetadata.Method; this._oneway = methodMetadata.IsOneWay && !methodMetadata.IsAsync; this._parameters = methodMetadata.Parameters; var builder = ImmutableDictionary.CreateBuilder <short, IThriftCodec>(); foreach (ThriftFieldMetadata fieldMetadata in methodMetadata.Parameters) { builder.Add(fieldMetadata.Id, codecManager.GetCodec(fieldMetadata.ThriftType)); } this._parameterCodecs = builder.ToImmutableDictionary(); // Build a mapping from thrift parameter ID to a position in the formal argument list var parameterOrderingBuilder = ImmutableDictionary.CreateBuilder <short, short>(); short argumentPosition = 0; foreach (ThriftFieldMetadata fieldMetadata in methodMetadata.Parameters) { parameterOrderingBuilder.Add(fieldMetadata.Id, argumentPosition++); } _thriftParameterIdToCSharpArgument = parameterOrderingBuilder.ToImmutableDictionary(); var exceptions = ImmutableDictionary.CreateBuilder <Type, ExceptionProcessor>(); foreach (var entry in methodMetadata.Exceptions) { ExceptionProcessor processor = new ExceptionProcessor(entry.Key, codecManager.GetCodec(entry.Value)); exceptions.Add(entry.Value.CSharpType, processor); } this._exceptionCodecs = exceptions.ToImmutableDictionary(); this._successCodec = codecManager.GetCodec(methodMetadata.ReturnType); }
public void WriteSet <T>(IThriftCodec <T> elementCodec, ISet <T> set) { if (set == null) { return; } Protocol.WriteSetBegin(new TSet(elementCodec.Type.ProtocolType.ToTType(), set.Count)); foreach (T element in set) { elementCodec.Write(element, Protocol); } Protocol.WriteSetEnd(); }
public void WriteList <T>(IThriftCodec <T> elementCodec, IEnumerable <T> list) { if (list == null) { return; } Protocol.WriteListBegin(new TList(elementCodec.Type.ProtocolType.ToTType(), list.Count())); foreach (T element in list) { elementCodec.Write(element, Protocol); } Protocol.WriteListEnd(); }
private Object ReadResponse(TProtocol inProtocol) { TProtocolReader reader = new TProtocolReader(inProtocol); reader.ReadStructBegin(); Object results = null; Exception exception = null; while (reader.NextField()) { if (reader.GetFieldId() == 0) { results = reader.ReadField(_successCodec); } else { IThriftCodec exceptionCodec = null; if (_exceptionCodecs.TryGetValue(reader.GetFieldId(), out exceptionCodec)) { exception = (Exception)reader.ReadField(exceptionCodec); } else { reader.SkipFieldData(); } } } reader.ReadStructEnd(); inProtocol.ReadMessageEnd(); if (exception != null) { throw exception; } if (_successCodec.Type == ThriftType.Void) { // TODO: check for non-null return from a void function? return(null); } if (results == null) { throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, $"{this.QualifiedName} failed: unknown result"); } return(results); }
public void WriteMap <K, V>(IThriftCodec <K> keyCodec, IThriftCodec <V> valueCodec, IDictionary <K, V> map) { if (map == null) { return; } Protocol.WriteMapBegin(new TMap(keyCodec.Type.ProtocolType.ToTType(), valueCodec.Type.ProtocolType.ToTType(), map.Count)); foreach (var entry in map) { keyCodec.Write(entry.Key, Protocol); valueCodec.Write(entry.Value, Protocol); } Protocol.WriteMapEnd(); }
private void WriteResponse(TProtocol outProtocol, int sequenceId, TMessageType responseType, String responseFieldName, short responseFieldId, IThriftCodec responseCodec, Object result) { outProtocol.WriteMessageBegin(new TMessage(this.QualifiedName, responseType, sequenceId)); TProtocolWriter writer = new TProtocolWriter(outProtocol); writer.WriteStructBegin(_resultStructName); writer.WriteField(responseFieldName, (short)responseFieldId, responseCodec, result); writer.WriteStructEnd(); outProtocol.WriteMessageEnd(); outProtocol.Transport.Flush(); }
public IList <E> ReadList <E>(IThriftCodec <E> elementCodec) { TList tList = Protocol.ReadListBegin(); IList <E> list = new List <E>(); for (int i = 0; i < tList.Count; i++) { try { E element = elementCodec.Read(Protocol); list.Add(element); } catch (Exception e) { // continue e.ThrowIfNecessary(); } } Protocol.ReadListEnd(); return(list); }
public ISet <E> ReadSet <E>(IThriftCodec <E> elementCodec) { TSet tSet = Protocol.ReadSetBegin(); ISet <E> set = new HashSet <E>(); for (int i = 0; i < tSet.Count; i++) { try { E element = elementCodec.Read(Protocol); set.Add(element); } catch (ArgumentException e) { // continue e.ThrowIfNecessary(); } } Protocol.ReadSetEnd(); return(set); }
public T ReadEnumField <T>(IThriftCodec <T> enumCodec) where T : struct { if (!CheckReadState(TType.I32)) { return(default(T)); } currentField = null; T fieldValue = default(T); try { fieldValue = enumCodec.Read(Protocol); } catch (ArgumentException) { // return null } Protocol.ReadFieldEnd(); return(fieldValue); }
public IDictionary <K, V> ReadMap <K, V>(IThriftCodec <K> keyCodec, IThriftCodec <V> valueCodec) { TMap tMap = Protocol.ReadMapBegin(); Dictionary <K, V> map = new Dictionary <K, V>(); for (int i = 0; i < tMap.Count; i++) { try { K key = keyCodec.Read(this.Protocol); V value = valueCodec.Read(this.Protocol); map[key] = value; } catch (Exception e) { // continue e.ThrowIfNecessary(); } } Protocol.ReadMapEnd(); return(map); }
public virtual void DeclaredUserException(Object o, String methodName, Exception t, IThriftCodec exceptionCodec) { }