/// <summary> /// process /// </summary> /// <param name="payload"></param> /// <param name="callback"></param> public void Process(byte[] payload, Action <byte[]> callback) { var iproto = ThriftMarshaller.GetBinaryProtocol(payload); TMessage message; try { message = iproto.ReadMessageBegin(); } catch (System.Exception) { iproto.Transport.Close(); return; } ProcessHandle handle = null; if (this.processMap_.TryGetValue(message.Name, out handle)) { handle(message, iproto, callback); } else { iproto.Transport.Close(); callback(ThriftMarshaller.Serialize(new TMessage(message.Name, TMessageType.Exception, message.SeqID), new TApplicationException(TApplicationException.ExceptionType.UnknownMethod, string.Concat("Invalid method name: '", message.Name, "'")))); } }
public void PersistDataToLocalFile() { try { var binary = ThriftMarshaller.Serialize <ThriftMentionChunk>(new ThriftMentionChunk { Mentions = Data }); string domainPath = Path.Combine(StorageAccount, Domain); if (!Directory.Exists(StorageAccount)) { Directory.CreateDirectory(StorageAccount); } if (!Directory.Exists(domainPath)) { Directory.CreateDirectory(domainPath); } File.WriteAllBytes(PersistPath, binary); } catch { Trace.WriteLine("FAILED TO PERSIST DATA:" + PersistPath); } finally { NeedsPersistance = false; } }
/// <summary> /// publish message /// </summary> /// <param name="message"></param> /// <returns></returns> public async Task <bool> HandleCall(Messaging.Message message) { if (this._channel == null || this._channel.IsClosed) { try { this._channel = await this._publisher.CreateChannel(); } catch (Exception ex) { Trace.TraceError(ex.ToString()); } if (this._channel == null || this._channel.IsClosed) { throw new ApplicationException("rabbitMQ channel unavailable"); } } var ticksNow = DateTimeSlim.UtcNow.Ticks; message.MillisecondsTimeout = message.MillisecondsTimeout - Math.Max(0, (int)(ticksNow - message.CreatedTick) / 10000); if (message.MillisecondsTimeout <= 0) { switch (message.Action) { case Messaging.Actions.Request: throw new TimeoutException("publish request timeout"); case Messaging.Actions.Response: throw new TimeoutException("publish response timeout"); default: throw new TimeoutException(); } } this._channel.BasicPublish(this._exchange, message.To, null, ThriftMarshaller.Serialize(message)); return(true); }
public void PersistData() { var binary = ThriftMarshaller.Serialize <ThriftMentionChunk>(new ThriftMentionChunk { Mentions = Data }); File.WriteAllBytes(Path, binary); NeedsPersistance = false; }
private void GetUser_Process(TMessage message, TProtocol iproto, Action <byte[]> callback) { var args = new Service1.GetUser_args(); try { args.Read(iproto); } catch (System.Exception ex) { iproto.Transport.Close(); callback(ThriftMarshaller.Serialize(new TMessage(message.Name, TMessageType.Exception, message.SeqID), new TApplicationException(TApplicationException.ExceptionType.Unknown, ex.Message))); return; } iproto.Transport.Close(); int seqID = message.SeqID; try { this._face.GetUser(args.UserId, (result) => { callback(ThriftMarshaller.Serialize(new TMessage("GetUser", TMessageType.Reply, seqID), new Service1.GetUser_result { Success = result })); }); } catch (System.Exception ex) { if (ex is Example.Service.Thrift.IllegalityUserIdException) { callback(ThriftMarshaller.Serialize(new TMessage("GetUser", TMessageType.Reply, seqID), new Service1.GetUser_result { Ex = ex as Example.Service.Thrift.IllegalityUserIdException })); return; } callback(ThriftMarshaller.Serialize(new TMessage(message.Name, TMessageType.Exception, message.SeqID), new TApplicationException(TApplicationException.ExceptionType.Unknown, ex.ToString()))); } }
private void Sum_Process(TMessage message, TProtocol iproto, Action <byte[]> callback) { var args = new Service1.Sum_args(); try { args.Read(iproto); } catch (System.Exception ex) { iproto.Transport.Close(); callback(ThriftMarshaller.Serialize(new TMessage(message.Name, TMessageType.Exception, message.SeqID), new TApplicationException(TApplicationException.ExceptionType.Unknown, ex.Message))); return; } iproto.Transport.Close(); int seqID = message.SeqID; try { this._face.Sum(args.X, args.Y, (result) => { callback(ThriftMarshaller.Serialize(new TMessage("Sum", TMessageType.Reply, seqID), new Service1.Sum_result { Success = result })); }); } catch (System.Exception ex) { callback(ThriftMarshaller.Serialize(new TMessage(message.Name, TMessageType.Exception, message.SeqID), new TApplicationException(TApplicationException.ExceptionType.Unknown, ex.ToString()))); } }
byte[] SerializeMentions(List <ThriftMention> mentions) { return(ThriftMarshaller.Serialize <ThriftMentionChunk>(new ThriftMentionChunk { Mentions = mentions })); }
public Task <System.Int32> Sum(System.Int32 x, System.Int32 y, object asyncState = null) { var taskSource_ = new TaskCompletionSource <System.Int32>(asyncState); //构建请求发送buffer int seqID_ = this.client_.NextRequestSeqID(); var sendBuffer_ = ThriftMarshaller.Serialize(new TMessage("Sum", TMessageType.Call, seqID_), new Service1.Sum_args() { X = x, Y = y }); //开始异步发送 this.client_.Send("Example.Service.Thrift.Service1+Iface", "Sum", seqID_, sendBuffer_, (ex_) => { //处理异常回调 taskSource_.SetException(ex_); }, (payload_) => { if (payload_ == null || payload_.Length == 0) { taskSource_.SetException(new TApplicationException( TApplicationException.ExceptionType.MissingResult, "Sum failed: Did not receive any data.")); return; } TMessage recvMsg_; TApplicationException exServer_ = null; Service1.Sum_result result_ = null; var oproto_ = ThriftMarshaller.GetBinaryProtocol(payload_); try { //read TMessage recvMsg_ = oproto_.ReadMessageBegin(); //read server return exception if (recvMsg_.Type == TMessageType.Exception) { exServer_ = TApplicationException.Read(oproto_); } else { //read result result_ = new Service1.Sum_result(); result_.Read(oproto_); } } catch (System.Exception ex_) { oproto_.Transport.Close(); taskSource_.SetException(ex_); return; } oproto_.Transport.Close(); if (exServer_ != null) { taskSource_.SetException(exServer_); } else { if (result_.__isset.success) { taskSource_.SetResult(result_.Success); return; } taskSource_.SetException(new TApplicationException( TApplicationException.ExceptionType.MissingResult, "Sum failed: unknown result")); } }); return(taskSource_.Task); }