一.如果你对Thrift不够了解,请先看这篇入门文章
http://www.cnblogs.com/hanmos/archive/2011/09/15/2177891.html
二.以下是说明如何基于Thrift协议,开发一个C#的客户端,用以发布C#的跨语言服务(封装原生的thrift0.9)
1.服务的发布类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Thrift;
using Thrift.Protocol;
using System.IO;
using System.Reflection;
using System.Dynamic;
using saf.cshap.client;
using Thrift.Collections;
namespace com.jd.saf
{
public class SafProcesser : TProcessor
{
public dynamic service;
public bool Process(TProtocol iprot, TProtocol oprot)
{
try
{
TMessage msg = iprot.ReadMessageBegin();
///反射,真正方法的调用
dynamic[] args = readPara(iprot);
dynamic result = service.GetType().InvokeMember(msg.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, Type.DefaultBinder, service, args);
iprot.ReadMessageEnd();
///方法的执行结果返回给client
oprot.WriteMessageBegin(new TMessage(msg.Name, TMessageType.Reply, msg.SeqID));
Write(oprot, result, msg.Name);
oprot.WriteMessageEnd();
oprot.Transport.Flush();
}
catch (Exception)
{
return false;
}
return true;
}
/// <summary>
/// 读取iport中的方法参数
/// </summary>
/// <returns></returns>
private dynamic[] readPara(TProtocol iprot)
{
iprot.ReadStructBegin();
List<dynamic> paraList = new List<dynamic>();
bool flag = true;
while (flag)
{
try
{
paraList.Add(readField(iprot, TType.Stop));
}
catch
{
flag = false;
}
}
iprot.ReadStructEnd();
return paraList.ToArray();
}
private dynamic readField(TProtocol iprot, TType type)
{
type = (type == TType.Stop ? iprot.ReadFieldBegin().Type : type);
dynamic para = null;
switch (type)
{
case TType.Stop: throw new Exception();
case TType.String: para = iprot.ReadString(); break;
case TType.Bool: para = iprot.ReadBool(); break;
case TType.Byte: para = iprot.ReadByte(); break;
case TType.Double: para = iprot.ReadDouble(); break;
case TType.I16: para = iprot.ReadI16(); break;
case TType.I32: para = iprot.ReadI32(); break;
case TType.I64: para = iprot.ReadI64(); break;
case TType.List:
TList list = iprot.ReadListBegin();
//List<dynamic> pList = new List<dynamic>();
//for (int i = 0; i < list.Count; i++)
//{
// pList.Add(readField(iprot, list.ElementType));
//}
//para = pList;
para = readTList(list, iprot);
iprot.ReadListEnd();
break;
case TType.Set:
TSet set = iprot.ReadSetBegin();
//THashSet<object> pSet = new THashSet<object>();
//for (int i = 0; i < set.Count; i++)
//{
// pSet.Add(readField(iprot, set.ElementType));
//}
para = readTSet(set, iprot);
iprot.ReadSetEnd();
break;
case TType.Map:
TMap map = iprot.ReadMapBegin();
Dictionary<dynamic, dynamic> pmap = new Dictionary<dynamic, dynamic>();
for (int i = 0; i < map.Count; i++)
{
var key = readField(iprot, map.KeyType);
var value = readField(iprot, map.ValueType);
pmap[key] = value;
}
para = pmap;
iprot.ReadMapEnd();
break;
//case TType.Struct :
//TStruct st = iprot.ReadStructBegin();
//反射生成实例,反射调用方法
//para = st.GetType();
// iprot.ReadStructEnd();
// break;
default:
TProtocolUtil.Skip(iprot, type); break;
}
iprot.ReadFieldEnd();
return para;
}
private dynamic readTList(TList list, TProtocol iprot)
{
TType type = list.ElementType;
dynamic para = null;
switch (type)
{
case TType.String:
para = new List<string>();
for (int i = 0; i < list.Count; i++)
{
para.Add(readField(iprot, list.ElementType));
}
break;
case TType.I32:
para = new List<int>();
for (int i = 0; i < list.Count; i++)
{
para.Add(readField(iprot, list.ElementType));
}
break;
case TType.I16:
para = new List<short>();
for (int i = 0; i < list.Count; i++)
{
para.Add(readField(iprot, list.ElementType));
}
break;
case TType.I64:
para = new List<long>();
for (int i = 0; i < list.Count; i++)
{
para.Add(readField(iprot, list.ElementType));
}
break;
case TType.Bool:
para = new List<bool>();
for (int i = 0; i < list.Count; i++)
{
para.Add(readField(iprot, list.ElementType));
}
break;
case TType.Byte:
para = new List<byte>();
for (int i = 0; i < list.Count; i++)
{
para.Add(readField(iprot, list.ElementType));
}
break;
case TType.Double:
para = new List<double>();
for (int i = 0; i < list.Count; i++)
{
para.Add(readField(iprot, list.ElementType));
}
break;
}
return para;
}
private dynamic readTSet(TSet set, TProtocol iprot)
{
TType type = set.ElementType;
dynamic para = null;
switch (type)
{
case TType.String:
para = new THashSet<string>();
for (int i = 0; i < set.Count; i++)
{
para.Add(readField(iprot, set.ElementType));
}
break;
case TType.I32:
para = new THashSet<int>();
for (int i = 0; i < set.Count; i++)
{
para.Add(readField(iprot, set.ElementType));
}
break;
case TType.I16:
para = new THashSet<short>();
for (int i = 0; i < set.Count; i++)
{
para.Add(readField(iprot, set.ElementType));
}
break;
case TType.I64:
para = new THashSet<long>();
for (int i = 0; i < set.Count; i++)
{
para.Add(readField(iprot, set.ElementType));
}
break;
case TType.Bool:
para = new THashSet<bool>();
for (int i = 0; i < set.Count; i++)
{
para.Add(readField(iprot, set.ElementType));
}
break;
case TType.Byte:
para = new THashSet<byte>();
for (int i = 0; i < set.Count; i++)
{
para.Add(readField(iprot, set.ElementType));
}
break;
case TType.Double:
para = new THashSet<double>();
for (int i = 0; i < set.Count; i++)
{
para.Add(readField(iprot, set.ElementType));
}
break;
}
return para;
}
/// <summary>
/// 发消息给客户端
/// </summary>
/// <param name="oprot"></param>
/// <param name="result"></param>
/// <param name="msgName"></param>
private void Write(TProtocol oprot, dynamic result, string msgName)
{
TStruct struc = new TStruct(msgName + "_result");
oprot.WriteStructBegin(struc);
if (result != null)
{
TField field = new TField();
field.Name = "Success";
field.ID = 0;
write(field, result, oprot);
}
oprot.WriteFieldStop();
oprot.WriteStructEnd();
}
private void write(TField field, object result, TProtocol oprot)
{
if (result is System.String)
{
field.Type = TType.String;
oprot.WriteFieldBegin(field);
oprot.WriteString((string)result);
oprot.WriteFieldEnd();
}
else if (result is System.Boolean)
{
field.Type = TType.Bool;
oprot.WriteFieldBegin(field);
oprot.WriteBool((bool)result);
oprot.WriteFieldEnd();
}
else if (result is System.Int32)
{
field.Type = TType.I32;
oprot.WriteFieldBegin(field);
oprot.WriteI32((int)result);
oprot.WriteFieldEnd();
}
else if (result is System.Int16)
{
field.Type = TType.I16;
oprot.WriteFieldBegin(field);
oprot.WriteI16((short)result);
oprot.WriteFieldEnd();
}
else if (result is System.Int64)
{
field.Type = TType.I64;
oprot.WriteFieldBegin(field);
oprot.WriteI64((long)result);
oprot.WriteFieldEnd();
}
else if (result is System.Byte)
{
field.Type = TType.Byte;
oprot.WriteFieldBegin(field);
oprot.WriteByte((byte)result);
oprot.WriteFieldEnd();
}
else if (result is System.Double)
{
field.Type = TType.Double;
oprot.WriteFieldBegin(field);
oprot.WriteDouble((double)result);
oprot.WriteFieldEnd();
}
else if(result.GetType().Name.Contains("List"))
{
field.Type = TType.List;
oprot.WriteFieldBegin(field);
writeList(field, result, oprot);
oprot.WriteFieldEnd();
}
else if (result.GetType().Name.Contains("Set"))
{
field.Type = TType.Set;
oprot.WriteFieldBegin(field);
writeSet(field, result, oprot);
oprot.WriteFieldEnd();
}
else if (result.GetType().Name.Contains("Dictionary"))
{
field.Type = TType.Map;
oprot.WriteFieldBegin(field);
writeMap(field, result, oprot);
oprot.WriteFieldEnd();
}
}
private void writeList(TField field, object result, TProtocol oprot)
{
if (result is System.Collections.Generic.List<string>)
{
List<string> list = (List<string>)result;
oprot.WriteListBegin(new TList(TType.String, list.Count));
foreach (string el in list)
{
oprot.WriteString(el);
}
oprot.WriteListEnd();
}
else if (result is System.Collections.Generic.List<int>)
{
List<int> list = (List<int>)result;
oprot.WriteListBegin(new TList(TType.I32, list.Count));
foreach (int el in list)
{
oprot.WriteI32(el);
}
oprot.WriteListEnd();
}
else if (result is System.Collections.Generic.List<long>)
{
List<long> list = (List<long>)result;
oprot.WriteListBegin(new TList(TType.I64, list.Count));
foreach (long el in list)
{
oprot.WriteI64(el);
}
oprot.WriteListEnd();
}
else if (result is System.Collections.Generic.List<short>)
{
List<short> list = (List<short>)result;
oprot.WriteListBegin(new TList(TType.I16, list.Count));
foreach (short el in list)
{
oprot.WriteI16(el);
}
oprot.WriteListEnd();
}
else if (result is System.Collections.Generic.List<bool>)
{
List<bool> list = (List<bool>)result;
oprot.WriteListBegin(new TList(TType.Bool, list.Count));
foreach (bool el in list)
{
oprot.WriteBool(el);
}
oprot.WriteListEnd();
}
else if (result is System.Collections.Generic.List<byte>)
{
List<byte> list = (List<byte>)result;
oprot.WriteListBegin(new TList(TType.Byte, list.Count));
foreach (byte el in list)
{
oprot.WriteByte(el);
}
oprot.WriteListEnd();
}
else if (result is System.Collections.Generic.List<double>)
{
List<double> list = (List<double>)result;
oprot.WriteListBegin(new TList(TType.Double, list.Count));
foreach (double el in list)
{
oprot.WriteDouble(el);
}
oprot.WriteListEnd();
}
}
private void writeSet(TField field, object result, TProtocol oprot)
{
if (result is THashSet<string>)
{
THashSet<string> set = (THashSet<string>)result;
oprot.WriteSetBegin(new TSet(TType.String, set.Count));
foreach (string el in set)
{
oprot.WriteString(el);
}
oprot.WriteSetEnd();
}
else if (result is THashSet<int>)
{
THashSet<int> set = (THashSet<int>)result;
oprot.WriteSetBegin(new TSet(TType.I32, set.Count));
foreach (int el in set)
{
oprot.WriteI32(el);
}
oprot.WriteSetEnd();
}
else if (result is THashSet<long>)
{
THashSet<long> set = (THashSet<long>)result;
oprot.WriteSetBegin(new TSet(TType.I64, set.Count));
foreach (long el in set)
{
oprot.WriteI64(el);
}
oprot.WriteSetEnd();
}
else if (result is THashSet<short>)
{
THashSet<short> set = (THashSet<short>)result;
oprot.WriteSetBegin(new TSet(TType.I16, set.Count));
foreach (short el in set)
{
oprot.WriteI16(el);
}
oprot.WriteSetEnd();
}
else if (result is THashSet<bool>)
{
THashSet<bool> set = (THashSet<bool>)result;
oprot.WriteSetBegin(new TSet(TType.Bool, set.Count));
foreach (bool el in set)
{
oprot.WriteBool(el);
}
oprot.WriteSetEnd();
}
else if (result is THashSet<byte>)
{
THashSet<byte> set = (THashSet<byte>)result;
oprot.WriteSetBegin(new TSet(TType.Byte, set.Count));
foreach (byte el in set)
{
oprot.WriteByte(el);
}
oprot.WriteSetEnd();
}
else if (result is THashSet<double>)
{
THashSet<double> set = (THashSet<double>)result;
oprot.WriteSetBegin(new TSet(TType.Double, set.Count));
foreach (double el in set)
{
oprot.WriteDouble(el);
}
oprot.WriteSetEnd();
}
}
private void writeMap(TField field, object result, TProtocol oprot)
{
if (result is Dictionary<string, string>)
{
Dictionary<string, string> map = (Dictionary<string, string>)result;
oprot.WriteMapBegin(new TMap(TType.String, TType.String, map.Count));
foreach (string key in map.Keys)
{
oprot.WriteString(key);
oprot.WriteString(map[key]);
}
oprot.WriteMapEnd();
}
if (result is Dictionary<string, int>)
{
Dictionary<string, int> map = (Dictionary<string, int>)result;
oprot.WriteMapBegin(new TMap(TType.String, TType.I32, map.Count));
foreach (string key in map.Keys)
{
oprot.WriteString(key);
oprot.WriteI32(map[key]);
}
oprot.WriteMapEnd();
}
if (result is Dictionary<string, long>)
{
Dictionary<string, long> map = (Dictionary<string, long>)result;
oprot.WriteMapBegin(new TMap(TType.String, TType.I64, map.Count));
foreach (string key in map.Keys)
{
oprot.WriteString(key);
oprot.WriteI64(map[key]);
}
oprot.WriteMapEnd();
}
if (result is Dictionary<string, short>)
{
Dictionary<string, short> map = (Dictionary<string, short>)result;
oprot.WriteMapBegin(new TMap(TType.String, TType.I16, map.Count));
foreach (string key in map.Keys)
{
oprot.WriteString(key);
oprot.WriteI16(map[key]);
}
oprot.WriteMapEnd();
}
if (result is Dictionary<string, byte>)
{
Dictionary<string, byte> map = (Dictionary<string, byte>)result;
oprot.WriteMapBegin(new TMap(TType.String, TType.Byte, map.Count));
foreach (string key in map.Keys)
{
oprot.WriteString(key);
oprot.WriteByte(map[key]);
}
oprot.WriteMapEnd();
}
if (result is Dictionary<string, bool>)
{
Dictionary<string, bool> map = (Dictionary<string, bool>)result;
oprot.WriteMapBegin(new TMap(TType.String, TType.Bool, map.Count));
foreach (string key in map.Keys)
{
oprot.WriteString(key);
oprot.WriteBool(map[key]);
}
oprot.WriteMapEnd();
}
if (result is Dictionary<string, double>)
{
Dictionary<string, double> map = (Dictionary<string, double>)result;
oprot.WriteMapBegin(new TMap(TType.String, TType.Double, map.Count));
foreach (string key in map.Keys)
{
oprot.WriteString(key);
oprot.WriteDouble(map[key]);
}
oprot.WriteMapEnd();
}
}
}
}
2.引用远程服务类:
using System.Collections.Generic;
using Thrift.Collections;
using Thrift.Protocol;
using Thrift;
using System;
namespace saf.cshap.client
{
public class ServiceGet
{
public ServiceGet(TProtocol prot)
: this(prot, prot)
{
}
public ServiceGet(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_; }
}
public dynamic callService(string method, dynamic[] para)
{
Write(method, para);
return Read(method);
}
protected void Write(string method, dynamic[] para)
{
oprot_.WriteMessageBegin(new TMessage(method, TMessageType.Call, seqid_));
Write(method, para, oprot_);
oprot_.WriteMessageEnd();
oprot_.Transport.Flush();
}
protected void Write(string method, dynamic[] para, TProtocol oprot)
{
TStruct struc = new TStruct(method + "_args");
oprot.WriteStructBegin(struc);
if (para != null && para.Length > 0)
{
TField field = new TField();
field.Name = "para";
field.ID = 1;
for (int i = 0; i < para.Length; i++)
{
writeField(field, para[i], oprot);
}
}
oprot.WriteFieldStop();
oprot.WriteStructEnd();
}
protected void writeField(TField field, dynamic para, TProtocol oprot)
{
if (para is System.String)
{
field.Type = TType.String;
oprot.WriteFieldBegin(field);
oprot.WriteString(para);
oprot.WriteFieldEnd();
}
else if (para is System.Boolean)
{
field.Type = TType.Bool;
oprot.WriteFieldBegin(field);
oprot.WriteBool(para);
oprot.WriteFieldEnd();
}
else if (para is System.Int32)
{
field.Type = TType.I32;
oprot.WriteFieldBegin(field);
oprot.WriteI32(para);
oprot.WriteFieldEnd();
}
else if (para is System.Int16)
{
field.Type = TType.I16;
oprot.WriteFieldBegin(field);
oprot.WriteI16(para);
oprot.WriteFieldEnd();
}
else if (para is System.Int64)
{
field.Type = TType.I64;
oprot.WriteFieldBegin(field);
oprot.WriteI64(para);
oprot.WriteFieldEnd();
}
else if (para is System.Byte)
{
field.Type = TType.Byte;
oprot.WriteFieldBegin(field);
oprot.WriteByte(para);
oprot.WriteFieldEnd();
}
else if (para is System.Double)
{
field.Type = TType.Double;
oprot.WriteFieldBegin(field);
oprot.WriteDouble(para);
oprot.WriteFieldEnd();
}
else if (para.GetType().Name.Contains("List"))
{
field.Type = TType.List;
oprot.WriteFieldBegin(field);
writeList(field, para, oprot);
oprot.WriteFieldEnd();
}
else if (para.GetType().Name.Contains("Set"))
{
field.Type = TType.Set;
oprot.WriteFieldBegin(field);
writeSet(field, para, oprot);
oprot.WriteFieldEnd();
}
else if (para.GetType().Name.Contains("Dictionary"))
{
field.Type = TType.Map;
oprot.WriteFieldBegin(field);
writeMap(field, para,oprot);
oprot.WriteFieldEnd();
}
}
protected void writeList(TField field, dynamic para, TProtocol oprot)
{
if (para is System.Collections.Generic.List<string>)
{
oprot.WriteListBegin(new TList(TType.String, para.Count));
foreach (string el in para)
{
oprot.WriteString(el);
}
oprot.WriteListEnd();
}
else if (para is System.Collections.Generic.List<int>)
{
oprot.WriteListBegin(new TList(TType.I32, para.Count));
foreach (int el in para)
{
oprot.WriteI32(el);
}
oprot.WriteListEnd();
}
else if (para is System.Collections.Generic.List<long>)
{
oprot.WriteListBegin(new TList(TType.I64, para.Count));
foreach (long el in para)
{
oprot.WriteI64(el);
}
oprot.WriteListEnd();
}
else if (para is System.Collections.Generic.List<short>)
{
oprot.WriteListBegin(new TList(TType.I16, para.Count));
foreach (short el in para)
{
oprot.WriteI16(el);
}
oprot.WriteListEnd();
}
else if (para is System.Collections.Generic.List<bool>)
{
oprot.WriteListBegin(new TList(TType.Bool, para.Count));
foreach (bool el in para)
{
oprot.WriteBool(el);
}
oprot.WriteListEnd();
}
else if (para is System.Collections.Generic.List<byte>)
{
oprot.WriteListBegin(new TList(TType.Byte, para.Count));
foreach (byte el in para)
{
oprot.WriteByte(el);
}
oprot.WriteListEnd();
}
else if (para is System.Collections.Generic.List<double>)
{
oprot.WriteListBegin(new TList(TType.Double, para.Count));
foreach (double el in para)
{
oprot.WriteDouble(el);
}
oprot.WriteListEnd();
}
}
protected void writeSet(TField field, dynamic para, TProtocol oprot)
{
if (para is THashSet<string>)
{
oprot.WriteSetBegin(new TSet(TType.String, para.Count));
foreach (string el in para)
{
oprot.WriteString(el);
}
oprot.WriteSetEnd();
}
else if (para is THashSet<int>)
{
oprot.WriteSetBegin(new TSet(TType.I32, para.Count));
foreach (int el in para)
{
oprot.WriteI32(el);
}
oprot.WriteSetEnd();
}
else if (para is THashSet<long>)
{
oprot.WriteSetBegin(new TSet(TType.I64, para.Count));
foreach (long el in para)
{
oprot.WriteI64(el);
}
oprot.WriteSetEnd();
}
else if (para is THashSet<short>)
{
oprot.WriteSetBegin(new TSet(TType.I16, para.Count));
foreach (short el in para)
{
oprot.WriteI16(el);
}
oprot.WriteSetEnd();
}
else if (para is THashSet<bool>)
{
oprot.WriteSetBegin(new TSet(TType.Bool, para.Count));
foreach (bool el in para)
{
oprot.WriteBool(el);
}
oprot.WriteSetEnd();
}
else if (para is THashSet<byte>)
{
oprot.WriteSetBegin(new TSet(TType.Byte, para.Count));
foreach (byte el in para)
{
oprot.WriteByte(el);
}
oprot.WriteSetEnd();
}
else if (para is THashSet<double>)
{
oprot.WriteSetBegin(new TSet(TType.Double, para.Count));
foreach (double el in para)
{
oprot.WriteDouble(el);
}
oprot.WriteSetEnd();
}
}
protected void writeMap(TField field, dynamic para, TProtocol oprot)
{
if (para is Dictionary<string, string>)
{
oprot.WriteMapBegin(new TMap(TType.String, TType.String, para.Count));
foreach (string key in para.Keys)
{
oprot.WriteString(key);
oprot.WriteString(para[key]);
}
oprot.WriteMapEnd();
}
if (para is Dictionary<string, int>)
{
oprot.WriteMapBegin(new TMap(TType.String, TType.I32, para.Count));
foreach (string key in para.Keys)
{
oprot.WriteString(key);
oprot.WriteI32(para[key]);
}
oprot.WriteMapEnd();
}
if (para is Dictionary<string, long>)
{
oprot.WriteMapBegin(new TMap(TType.String, TType.I64, para.Count));
foreach (string key in para.Keys)
{
oprot.WriteString(key);
oprot.WriteI64(para[key]);
}
oprot.WriteMapEnd();
}
if (para is Dictionary<string, short>)
{
oprot.WriteMapBegin(new TMap(TType.String, TType.I16, para.Count));
foreach (string key in para.Keys)
{
oprot.WriteString(key);
oprot.WriteI16(para[key]);
}
oprot.WriteMapEnd();
}
if (para is Dictionary<string, byte>)
{
oprot.WriteMapBegin(new TMap(TType.String, TType.Byte, para.Count));
foreach (string key in para.Keys)
{
oprot.WriteString(key);
oprot.WriteByte(para[key]);
}
oprot.WriteMapEnd();
}
if (para is Dictionary<string, bool>)
{
oprot.WriteMapBegin(new TMap(TType.String, TType.Bool, para.Count));
foreach (string key in para.Keys)
{
oprot.WriteString(key);
oprot.WriteBool(para[key]);
}
oprot.WriteMapEnd();
}
if (para is Dictionary<string, double>)
{
oprot.WriteMapBegin(new TMap(TType.String, TType.Double, para.Count));
foreach (string key in para.Keys)
{
oprot.WriteString(key);
oprot.WriteDouble(para[key]);
}
oprot.WriteMapEnd();
}
}
protected dynamic Read(string method)
{
TMessage msg = iprot_.ReadMessageBegin();
if (msg.Type == TMessageType.Exception)
{
TApplicationException x = TApplicationException.Read(iprot_);
iprot_.ReadMessageEnd();
throw x;
}
dynamic result = null;
bool flag = true;
while (flag)
{
try { result = readField(TType.Stop, iprot_); }
catch
{
flag = false;
}
}
iprot_.ReadMessageEnd();
if (result != null)
{
return result;
}
throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, method + " failed: result = null!");
}
protected dynamic readField(TType type, TProtocol iprot)
{
iprot.ReadStructBegin();
dynamic para = null;
TField field;
field = iprot.ReadFieldBegin();
if (type == TType.Stop)
{
type = field.Type;
switch (type)
{
case TType.Stop: throw new Exception();
case TType.String: para = iprot.ReadString(); break;
case TType.Bool: para = iprot.ReadBool(); break;
case TType.Byte: para = iprot.ReadByte(); break;
case TType.Double: para = iprot.ReadDouble(); break;
case TType.I16: para = iprot.ReadI16(); break;
case TType.I32: para = iprot.ReadI32(); break;
case TType.I64: para = iprot.ReadI64(); break;
case TType.List:
TList list = iprot.ReadListBegin();
para = readTList(list, iprot);
iprot.ReadListEnd();
break;
case TType.Set:
TSet set = iprot.ReadSetBegin();
para = readTSet(set, iprot);
iprot.ReadSetEnd();
break;
case TType.Map:
TMap map = iprot.ReadMapBegin();
Dictionary<dynamic, dynamic> pmap = new Dictionary<dynamic, dynamic>();
for (int i = 0; i < map.Count; i++)
{
var key = readField(map.KeyType, iprot);
var value = readField(map.ValueType, iprot);
pmap[key] = value;
}
para = pmap;
iprot.ReadMapEnd();
break;
default:
TProtocolUtil.Skip(iprot, type); break;
}
iprot.ReadFieldEnd();
}
iprot.ReadStructEnd();
return para;
}
protected dynamic readTList(TList list, TProtocol iprot)
{
TType type = list.ElementType;
dynamic para = null;
switch (type)
{
case TType.String:
para = new List<string>();
for (int i = 0; i < list.Count; i++)
{
para.Add(readField(list.ElementType, iprot));
}
break;
case TType.I32:
para = new List<int>();
for (int i = 0; i < list.Count; i++)
{
para.Add(readField(list.ElementType, iprot));
}
break;
case TType.I16:
para = new List<short>();
for (int i = 0; i < list.Count; i++)
{
para.Add(readField(list.ElementType, iprot));
}
break;
case TType.I64:
para = new List<long>();
for (int i = 0; i < list.Count; i++)
{
para.Add(readField(list.ElementType, iprot));
}
break;
case TType.Bool:
para = new List<bool>();
for (int i = 0; i < list.Count; i++)
{
para.Add(readField(list.ElementType, iprot));
}
break;
case TType.Byte:
para = new List<byte>();
for (int i = 0; i < list.Count; i++)
{
para.Add(readField(list.ElementType, iprot));
}
break;
case TType.Double:
para = new List<double>();
for (int i = 0; i < list.Count; i++)
{
para.Add(readField(list.ElementType, iprot));
}
break;
}
return para;
}
protected dynamic readTSet(TSet set, TProtocol iprot)
{
TType type = set.ElementType;
dynamic para = null;
switch (type)
{
case TType.String:
para = new THashSet<string>();
for (int i = 0; i < set.Count; i++)
{
para.Add(readField(set.ElementType, iprot));
}
break;
case TType.I32:
para = new THashSet<int>();
for (int i = 0; i < set.Count; i++)
{
para.Add(readField(set.ElementType, iprot));
}
break;
case TType.I16:
para = new THashSet<short>();
for (int i = 0; i < set.Count; i++)
{
para.Add(readField(set.ElementType, iprot));
}
break;
case TType.I64:
para = new THashSet<long>();
for (int i = 0; i < set.Count; i++)
{
para.Add(readField(set.ElementType, iprot));
}
break;
case TType.Bool:
para = new THashSet<bool>();
for (int i = 0; i < set.Count; i++)
{
para.Add(readField(set.ElementType, iprot));
}
break;
case TType.Byte:
para = new THashSet<byte>();
for (int i = 0; i < set.Count; i++)
{
para.Add(readField(set.ElementType, iprot));
}
break;
case TType.Double:
para = new THashSet<double>();
for (int i = 0; i < set.Count; i++)
{
para.Add(readField(set.ElementType, iprot));
}
break;
}
return para;
}
}
}
3.测试类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Thrift.Transport;
using Thrift.Protocol;
using com.jd.saf;
using System.Collections;
using Thrift;
using Thrift.Server;
using System.IO;
using saf.cshap.client;
namespace com.jd.saf
{
class MainClass
{
static void Main(string[] args)
{
/// new Client().readPropertiesMap("D:/新版saf/C++/ConsoleApplication1/ConsoleApplication1/log4j.properties");
/// new Client().testDNS("erpcd.360buy.com");
/// string[] ips = DNSUtils.Hostname2iplist("www.360buy.com");
/// foreach (string ip in ips)
/// {
/// Console.WriteLine(ip);
/// }
// Console.WriteLine(DNSUtils.Hostname2ip("www.localhost.com"));//www.12306.cn
/// Console.ReadKey();
/// new MainClass().thriftServer();
///MainClass.showMethods();
// MainClass.testReflectMethods();
// testSplit();
//testThriftClient();
testSafClient();
}
static void testSplit() {
string ss = "www.localhost.com,www.12306.cn";
foreach(string s in ss.Split(','))
{
Console.WriteLine(s);
}
Console.ReadKey();
}
public static void testThriftClient()
{
TTransport transport = new TSocket("localhost", 7911);
TProtocol protocol = new TBinaryProtocol(transport);
Hello.Client client = new Hello.Client(protocol);
transport.Open();
string val = client.helloString("C# client");
Console.WriteLine(val);
try
{
client.helloNull();
}
catch (Exception e)
{
Console.WriteLine("正常的异常" + e.Message);
}
int i = 8;
i = client.helloInt(i);
Console.WriteLine("i=" + i);
bool b = client.helloBoolean(true);
Console.WriteLine("b=" + b);
transport.Close();
///Console.ReadLine();
Console.ReadKey();
}
public void readProperties(String path)
{
PropertyFileOperator opt = new PropertyFileOperator(path);
string value = opt.GetPropertiesText("key1");
opt.Close();
Console.WriteLine("value = " + value);
Console.ReadKey();
}
public void readPropertiesMap(String path)
{
PropertyFileOperator opt = new PropertyFileOperator(path);
Hashtable table = opt.GetProperties();
foreach(DictionaryEntry de in table)
{
Console.WriteLine(de.Key);///de.Key对应于keyvalue键值对key
Console.WriteLine(de.Value);///de.Key对应于keyvalue键值对value
}
opt.Close();
Console.ReadKey();
}
public void testDNS(String host)
{
ResloveDNS dns = new ResloveDNS();
dns.Resolve(host);
///Console.WriteLine(dns.IPLength);
///Console.WriteLine(dns.);
///Console.ReadKey();
}
public void thriftServer()
{
TServerSocket transport = new TServerSocket(7911,0,false);
/// TTransport tt = new TFramedTransport(new TSocket("localhost", 10005));
/// TBinaryProtocol tp = new TBinaryProtocol(tt);
Hello.Processor processor = new Hello.Processor(new HelloImpl());
TServer server = new TSimpleServer(processor, transport);
Console.WriteLine("Starting server on port 7911 ...");
server.Serve();
}
public static void showMethods()
{
SafProcesser pro = new SafProcesser();
pro.service = new MyHelloImpl();
System.Reflection.MethodInfo[] methods = pro.service.GetType().GetMethods();
foreach (System.Reflection.MethodInfo m in methods)
{
Console.WriteLine("m.Name = " + m.Name);
}
Console.ReadKey();
}
public static void testReflectMethods()
{
TServerSocket transport = new TServerSocket(7911, 0, false);
SafProcesser processor = new SafProcesser();
///用户自己的服务类
processor.service = new MyHelloImpl();
TServer server = new TSimpleServer(processor, transport);
Console.WriteLine("Starting server on port 7911 ...");
server.Serve();
}
public static void testSafClient()
{
TTransport transport = new TSocket("localhost", 7911);
TProtocol protocol = new TBinaryProtocol(transport);
ServiceGet clientService = new ServiceGet(protocol);
transport.Open();
int start = DateTime.Now.Millisecond;
Console.WriteLine("start = " + DateTime.Now);
try
{
string s = clientService.callService("helloNull", null);
Console.WriteLine(s);
}
catch (Exception e)
{
Console.WriteLine("正常的异常" + e.Message);
}
var v = clientService.callService("helloString", new dynamic[] { "C# client" });
Console.WriteLine(v);
int i = 8;
i = clientService.callService("helloInt", new dynamic[] { i });
Console.WriteLine("i=" + i);
bool b = clientService.callService("helloBoolean", new dynamic[] { true }); ;
Console.WriteLine("b=" + b);
Console.WriteLine("end = " + DateTime.Now);
Console.WriteLine("持续时间: " + (DateTime.Now.Millisecond - start) + "毫秒");
transport.Close();
Console.ReadKey();
}
}
}
相关推荐
一个简单的demo,关于使用thrift进行跨语言服务间的通信
Thrift RPC客户端的服务化框架代码,
Thrift Java 服务器 客户端通信,网上看到的,觉得不错就分享出来供大家一起共同分享学习。
使用wireshark抓取thrift协议接口调用
基于Thrift的Golang与c#程序互相访问
Nifty是facebook公司开源的,基于netty的thrift服务端和客户端实现。 然后使用此包就可以快速发布出基于netty的高效的服务端和客户端代码。 示例: public void startServer() { // Create the handler ...
thrift在windows下生成C#代码
本例改编自Apache Thrift教程: ... ... 原教程使用的是c++ server和php client. 我用c++和c#分别实现了client和server,他们之间可以用任意组合互相调用,没有太复杂的功能,非常利于初学Thrift.
精心整理,零基础开始学习,步骤详细!环境搭建,demo案例,楼主耗费两天奉献!
一个支持thrift协议的客户端工程,支持负载均衡、支持zookeeper service 节点自动发现、failover、异步连接池、异步调用模式等。 jdk version:jdk1.7+ QQ:814912127 QQ群:413012474 EMAIL: build service to client ...
flume通过thrift协议收集日志-Python。博客地址:http://blog.csdn.net/wangshuwei5/article/details/48902757
Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。接下来通过本文给大家介绍Java thrift服务器和客户端创建实例代码,需要的朋友参考下吧
thrift官方代码+与dubbo集成支持原生thrift协议
Thrift初探:简单实现C#通讯服务程序
分析Thrift的结构动机是为了实现服务端能取到客户端的IP,因此需要对它的结构、调用流程有些了解。另外,请注意本文针对的是TNonblockingServer,不包含TThreadPoolServer、TThreadedServer和TSimpleServer。 ...
一种基于Thrift的日志收集分析系统_周康.pdf
内容是Thrift访问cassandra数据库,主程序中有多种写入的方式,其中批量写入cassandra数据库的批量写入方法最快能达到1秒1W条数据的写入速度。
HARPC(High Availability RPC)是基于Thrift的跨语言、高可用的RPC框架。具备高性能、高可用、轻量级等特点 * 跨语言通信 * 方便的使Java、Python、C++三种程序可以相互通信 * 负载均衡和容灾处理 * 方便的实现...
编译后的thrift客户端,已经经过公司师父同意分享。 1. cp /Users/dxm/Desktop/thrift /usr/local/bin/ 2. echo $PATH 3. thrift 4. chmod +x /usr/local/bin/thrift 5. thrift 6. thrift -version
基于Thrift框架RPC的研究与实现_田翠珍.pdf