Thrift架构~windows下安装和Hello World及编码引起的荒唐

编码难点

利用VS建立三个thrift文件后,在拓展编写翻译时出现了难题,最后找到答案,原来是编码难题,最终选取记事本把编码被为ANSI就足以健康编写翻译了。

图片 1

Hello world程序代码:

namespace csharp HelloThriftspace

exception Xception {
  1: i32 errorCode,
  2: string message
}

service HelloThrift{
  void HelloWorld() throws (1:Xception ex)
}

编写翻译代码:

thrift --gen csharp hellothrift.thrift 

结果:namespace表示文件夹的所在地,方法,结构,枚举都是以文件情势存在的

图片 2

 

生成的C#代码:

Xception.cs

图片 3图片 4

namespace HelloThriftspace
{

  #if !SILVERLIGHT
  [Serializable]
  #endif
  public partial class Xception : TException, TBase
  {
    private int _errorCode;
    private string _message;

    public int ErrorCode
    {
      get
      {
        return _errorCode;
      }
      set
      {
        __isset.errorCode = true;
        this._errorCode = value;
      }
    }

    public string Message
    {
      get
      {
        return _message;
      }
      set
      {
        __isset.message = true;
        this._message = value;
      }
    }


    public Isset __isset;
    #if !SILVERLIGHT
    [Serializable]
    #endif
    public struct Isset {
      public bool errorCode;
      public bool message;
    }

    public Xception() {
    }

    public void Read (TProtocol iprot)
    {
      TField field;
      iprot.ReadStructBegin();
      while (true)
      {
        field = iprot.ReadFieldBegin();
        if (field.Type == TType.Stop) { 
          break;
        }
        switch (field.ID)
        {
          case 1:
            if (field.Type == TType.I32) {
              ErrorCode = iprot.ReadI32();
            } else { 
              TProtocolUtil.Skip(iprot, field.Type);
            }
            break;
          case 2:
            if (field.Type == TType.String) {
              Message = iprot.ReadString();
            } else { 
              TProtocolUtil.Skip(iprot, field.Type);
            }
            break;
          default: 
            TProtocolUtil.Skip(iprot, field.Type);
            break;
        }
        iprot.ReadFieldEnd();
      }
      iprot.ReadStructEnd();
    }

    public void Write(TProtocol oprot) {
      TStruct struc = new TStruct("Xception");
      oprot.WriteStructBegin(struc);
      TField field = new TField();
      if (__isset.errorCode) {
        field.Name = "errorCode";
        field.Type = TType.I32;
        field.ID = 1;
        oprot.WriteFieldBegin(field);
        oprot.WriteI32(ErrorCode);
        oprot.WriteFieldEnd();
      }
      if (Message != null && __isset.message) {
        field.Name = "message";
        field.Type = TType.String;
        field.ID = 2;
        oprot.WriteFieldBegin(field);
        oprot.WriteString(Message);
        oprot.WriteFieldEnd();
      }
      oprot.WriteFieldStop();
      oprot.WriteStructEnd();
    }

    public override string ToString() {
      StringBuilder sb = new StringBuilder("Xception(");
      sb.Append("ErrorCode: ");
      sb.Append(ErrorCode);
      sb.Append(",Message: ");
      sb.Append(Message);
      sb.Append(")");
      return sb.ToString();
    }

  }

}

View Code

HelloThrift.cs

图片 5图片 6

namespace HelloThriftspace
{
  public partial class HelloThrift {
    public interface Iface {
      void HelloWorld();
      #if SILVERLIGHT
      IAsyncResult Begin_HelloWorld(AsyncCallback callback, object state);
      void End_HelloWorld(IAsyncResult asyncResult);
      #endif
    }

    public class Client : IDisposable, Iface {
      public Client(TProtocol prot) : this(prot, prot)
      {
      }

      public Client(TProtocol iprot, TProtocol oprot)
      {
        iprot_ = iprot;
        oprot_ = oprot;
      }

      protected TProtocol iprot_;
      protected TProtocol oprot_;
      protected int seqid_;

      public TProtocol InputProtocol
      {
        get { return iprot_; }
      }
      public TProtocol OutputProtocol
      {
        get { return oprot_; }
      }


      #region " IDisposable Support "
      private bool _IsDisposed;

      // IDisposable
      public void Dispose()
      {
        Dispose(true);
      }


      protected virtual void Dispose(bool disposing)
      {
        if (!_IsDisposed)
        {
          if (disposing)
          {
            if (iprot_ != null)
            {
              ((IDisposable)iprot_).Dispose();
            }
            if (oprot_ != null)
            {
              ((IDisposable)oprot_).Dispose();
            }
          }
        }
        _IsDisposed = true;
      }
      #endregion



      #if SILVERLIGHT
      public IAsyncResult Begin_HelloWorld(AsyncCallback callback, object state)
      {
        return send_HelloWorld(callback, state);
      }

      public void End_HelloWorld(IAsyncResult asyncResult)
      {
        oprot_.Transport.EndFlush(asyncResult);
        recv_HelloWorld();
      }

      #endif

      public void HelloWorld()
      {
        #if !SILVERLIGHT
        send_HelloWorld();
        recv_HelloWorld();

        #else
        var asyncResult = Begin_HelloWorld(null, null);
        End_HelloWorld(asyncResult);

        #endif
      }
      #if SILVERLIGHT
      public IAsyncResult send_HelloWorld(AsyncCallback callback, object state)
      #else
      public void send_HelloWorld()
      #endif
      {
        oprot_.WriteMessageBegin(new TMessage("HelloWorld", TMessageType.Call, seqid_));
        HelloWorld_args args = new HelloWorld_args();
        args.Write(oprot_);
        oprot_.WriteMessageEnd();
        #if SILVERLIGHT
        return oprot_.Transport.BeginFlush(callback, state);
        #else
        oprot_.Transport.Flush();
        #endif
      }

      public void recv_HelloWorld()
      {
        TMessage msg = iprot_.ReadMessageBegin();
        if (msg.Type == TMessageType.Exception) {
          TApplicationException x = TApplicationException.Read(iprot_);
          iprot_.ReadMessageEnd();
          throw x;
        }
        HelloWorld_result result = new HelloWorld_result();
        result.Read(iprot_);
        iprot_.ReadMessageEnd();
        if (result.__isset.ex) {
          throw result.Ex;
        }
        return;
      }

    }
    public class Processor : TProcessor {
      public Processor(Iface iface)
      {
        iface_ = iface;
        processMap_["HelloWorld"] = HelloWorld_Process;
      }

      protected delegate void ProcessFunction(int seqid, TProtocol iprot, TProtocol oprot);
      private Iface iface_;
      protected Dictionary<string, ProcessFunction> processMap_ = new Dictionary<string, ProcessFunction>();

      public bool Process(TProtocol iprot, TProtocol oprot)
      {
        try
        {
          TMessage msg = iprot.ReadMessageBegin();
          ProcessFunction fn;
          processMap_.TryGetValue(msg.Name, out fn);
          if (fn == null) {
            TProtocolUtil.Skip(iprot, TType.Struct);
            iprot.ReadMessageEnd();
            TApplicationException x = new TApplicationException (TApplicationException.ExceptionType.UnknownMethod, "Invalid method name: '" + msg.Name + "'");
            oprot.WriteMessageBegin(new TMessage(msg.Name, TMessageType.Exception, msg.SeqID));
            x.Write(oprot);
            oprot.WriteMessageEnd();
            oprot.Transport.Flush();
            return true;
          }
          fn(msg.SeqID, iprot, oprot);
        }
        catch (IOException)
        {
          return false;
        }
        return true;
      }

      public void HelloWorld_Process(int seqid, TProtocol iprot, TProtocol oprot)
      {
        HelloWorld_args args = new HelloWorld_args();
        args.Read(iprot);
        iprot.ReadMessageEnd();
        HelloWorld_result result = new HelloWorld_result();
        try {
          iface_.HelloWorld();
        } catch (Xception ex) {
          result.Ex = ex;
        }
        oprot.WriteMessageBegin(new TMessage("HelloWorld", TMessageType.Reply, seqid)); 
        result.Write(oprot);
        oprot.WriteMessageEnd();
        oprot.Transport.Flush();
      }

    }


    #if !SILVERLIGHT
    [Serializable]
    #endif
    public partial class HelloWorld_args : TBase
    {

      public HelloWorld_args() {
      }

      public void Read (TProtocol iprot)
      {
        TField field;
        iprot.ReadStructBegin();
        while (true)
        {
          field = iprot.ReadFieldBegin();
          if (field.Type == TType.Stop) { 
            break;
          }
          switch (field.ID)
          {
            default: 
              TProtocolUtil.Skip(iprot, field.Type);
              break;
          }
          iprot.ReadFieldEnd();
        }
        iprot.ReadStructEnd();
      }

      public void Write(TProtocol oprot) {
        TStruct struc = new TStruct("HelloWorld_args");
        oprot.WriteStructBegin(struc);
        oprot.WriteFieldStop();
        oprot.WriteStructEnd();
      }

      public override string ToString() {
        StringBuilder sb = new StringBuilder("HelloWorld_args(");
        sb.Append(")");
        return sb.ToString();
      }

    }


    #if !SILVERLIGHT
    [Serializable]
    #endif
    public partial class HelloWorld_result : TBase
    {
      private Xception _ex;

      public Xception Ex
      {
        get
        {
          return _ex;
        }
        set
        {
          __isset.ex = true;
          this._ex = value;
        }
      }


      public Isset __isset;
      #if !SILVERLIGHT
      [Serializable]
      #endif
      public struct Isset {
        public bool ex;
      }

      public HelloWorld_result() {
      }

      public void Read (TProtocol iprot)
      {
        TField field;
        iprot.ReadStructBegin();
        while (true)
        {
          field = iprot.ReadFieldBegin();
          if (field.Type == TType.Stop) { 
            break;
          }
          switch (field.ID)
          {
            case 1:
              if (field.Type == TType.Struct) {
                Ex = new Xception();
                Ex.Read(iprot);
              } else { 
                TProtocolUtil.Skip(iprot, field.Type);
              }
              break;
            default: 
              TProtocolUtil.Skip(iprot, field.Type);
              break;
          }
          iprot.ReadFieldEnd();
        }
        iprot.ReadStructEnd();
      }

      public void Write(TProtocol oprot) {
        TStruct struc = new TStruct("HelloWorld_result");
        oprot.WriteStructBegin(struc);
        TField field = new TField();

        if (this.__isset.ex) {
          if (Ex != null) {
            field.Name = "Ex";
            field.Type = TType.Struct;
            field.ID = 1;
            oprot.WriteFieldBegin(field);
            Ex.Write(oprot);
            oprot.WriteFieldEnd();
          }
        }
        oprot.WriteFieldStop();
        oprot.WriteStructEnd();
      }

      public override string ToString() {
        StringBuilder sb = new StringBuilder("HelloWorld_result(");
        sb.Append("Ex: ");
        sb.Append(Ex== null ? "<null>" : Ex.ToString());
        sb.Append(")");
        return sb.ToString();
      }

    }

  }
}

View Code

觉得生成的代码依然比较劳碌的,希望thrift在随后的成品中,对那块化解的好一些,呵呵。

下一讲大家将展开客户端与服务器环境的搭建。

 

 

 

上面看一下windows下的安装与利用。

Thrift近期最高0.9.1,地址:http://archive.apache.org/dist/thrift/

注意,大家要把exe和tar文件都下载下来,exe用来编写翻译你的thrift中间语言,而tar解压后,大家可以看看csharp,php,java,js等种种开发语言的实例代码,对我们很有帮衬的,下载最新版

下载之后,我们把exe文件可以放在C盘,建个Thrift目录,把它放入,然后可以配备一下环境变量,如图:

图片 7

下一场,大家就能够拓展thrift中间语言的支出了,之所以说它是当中语言,是因为它不是终极我们要运用的,而急需将它进行编写翻译之后,才生成大家的目的语言,就像是C语言,它在编写翻译时也是生成obj目的语言,然后再三遍编写翻译最后生成exe文件,它们的道理是同样的,

我们的thrift语言,通过thrift程序可以变动三种编制程序语言的源代码。

多年来起来专业接触Thrift框架结构,很牛B的技艺,它被apache收纳了,属于开源中的一员,呵呵。

概念:

Thrift源于威名昭著的facebook之手,在二〇〇七年facebook提交Apache基金会将Thrift作为2个开源项目,对于当下
的facebook来说成立thrift是为了解决facebook系统中各系统间大数据量的传
输通信以及系统里面语言环境分化须求跨平台的特性。所以thrift能够支撑多样程序语言,例如: 
C++, C#, Cocoa, Erlang, Haskell, Java, Ocami, Perl, PHP, Python, Ruby,
Smalltalk.
在三种差异的言语之间通讯thrift能够看做二进制的高品质的简报中间件,协理数据(对象)连串化和六体系型的昂CoraPC服务。Thrift适用于程序对程
序静态的数据交流,须要先鲜明好她的数据结构,他是完全静态化的,当数据结构产生变化时,必须另行编排IDL文件,代码生成,再编写翻译载入的流程,跟其他IDL工具相比较能够说是是Thrift的缺点,Thrift适用于搭建大型数据调换及储存的通用工具,对于大型系统中的内部数据传输相对于JSON和
xml无论在品质、传输大小上有显著的优势。