コード例 #1
0
ファイル: BlobInterface.cs プロジェクト: melnx/Bermuda
        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;
            }
        }
コード例 #2
0
            /// <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, "'"))));
                }
            }
コード例 #3
0
ファイル: Publisher.cs プロジェクト: ByboyCn/SharpOTP
            /// <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);
            }
コード例 #4
0
ファイル: InMemoryChunk.cs プロジェクト: melnx/Bermuda
        public void PersistData()
        {
            var binary = ThriftMarshaller.Serialize <ThriftMentionChunk>(new ThriftMentionChunk {
                Mentions = Data
            });

            File.WriteAllBytes(Path, binary);
            NeedsPersistance = false;
        }
コード例 #5
0
        /// <summary>
        /// Called each time a message arrives for this consumer.
        /// </summary>
        /// <param name="consumerTag"></param>
        /// <param name="deliveryTag"></param>
        /// <param name="redelivered"></param>
        /// <param name="exchange"></param>
        /// <param name="routingKey"></param>
        /// <param name="properties"></param>
        /// <param name="body"></param>
        public override void HandleBasicDeliver(string consumerTag,
                                                ulong deliveryTag,
                                                bool redelivered,
                                                string exchange,
                                                string routingKey,
                                                IBasicProperties properties,
                                                byte[] body)
        {
            Messaging.Message message = null;
            try { message = ThriftMarshaller.Deserialize <Messaging.Message>(body); }
            catch (Exception ex) { Trace.TraceError(ex.ToString()); return; }

            message.CreatedTick = DateTimeSlim.UtcNow.Ticks;
            this._callback(message);
        }
コード例 #6
0
             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())));
                }
            }
コード例 #7
0
ファイル: BlobInterface.cs プロジェクト: melnx/Bermuda
 void LoadFromFile()
 {
     if (!File.Exists(PersistPath))
     {
         Data = new List <ThriftMention>();
     }
     else
     {
         try
         {
             var binary = File.ReadAllBytes(PersistPath);
             var set    = ThriftMarshaller.Deserialize <ThriftMentionChunk>(binary);
             Data = set.Mentions ?? new List <ThriftMention>();
         }
         catch
         {
             Trace.WriteLine("FAILED TO LOAD FILE:" + PersistPath);
         }
     }
 }
コード例 #8
0
             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())));
                }
            }
コード例 #9
0
 List <ThriftMention> DeserializeMentions(byte[] data)
 {
     return(ThriftMarshaller.Deserialize <ThriftMentionChunk>(data).Mentions);
 }
コード例 #10
0
 byte[] SerializeMentions(List <ThriftMention> mentions)
 {
     return(ThriftMarshaller.Serialize <ThriftMentionChunk>(new ThriftMentionChunk {
         Mentions = mentions
     }));
 }
コード例 #11
0
        //#####################################################

        List <ThriftDatapoint> DeserializeDatapoints(byte[] data)
        {
            return(ThriftMarshaller.Deserialize <ThriftDatapointChunk>(data).Datapoints);
        }
コード例 #12
0
             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);
            }