[转载]Thrift的C#入门Demo_图文详解版

[转载]Thrift的C#入门Demo_图文详解版,转自群23152359,原作者为KX

 

002_url.txt

1 http://thrift.apache.org/
View Code

003_class.txt

 1 namespace csharp kx.thrift.demo
 2 
 3 struct KXChildClass
 4 {
 5     1: i32 Value_Int32,
 6     2: string Value_String
 7 }
 8 
 9 struct KXClass
10 {
11     1: double Value_Double,
12     2: list<byte> Value_ByteArray,
13     3: list<KXChildClass> Value_KXChildClassArray
14 }
15 
16 service KXService
17 {
18     void ping(),
19     KXClass GetKXClass( 1: i32 arg_ID, 2: KXClass arg_kxClass )
20 }
View Code

004.txt

名称
Server

位置
C:\temp\ThriftTest\thriftClassX\TestProject\

解决方案名称
Server
View Code

005.txt

1 URL:
2 http://thrift.apache.org/
3 
4 RAR路径:
5 thrift-0.9.1.tar.gz\thrift-0.9.1\lib\csharp\src
6 
7 解压后路径:
8 C:\temp\ThriftTest\thriftClassX\ThriftCSharpLib\src\
View Code

006_gen_bat.txt

1 thrift-0.9.1.exe --gen csharp class.txt
2 pause
View Code

 

008_KXServiceHandle.cs

 1 using kx.thrift.demo;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 
 7 namespace Server
 8 {
 9     public class KXServiceHandle : KXService.Iface
10     {
11 
12         private int _id = 0;
13 
14         public void ping()
15         {
16             Console.WriteLine((++this._id) + " 客户端 ping " + DateTime.Now + "\r\n");
17         }
18 
19         public KXClass GetKXClass(int arg_ID, KXClass arg_kxClass)
20         {
21             Console.WriteLine((++this._id) + " 客户端发来kxClass " + DateTime.Now + ",用断点来查看具体内容。\r\n");
22             KXClass newKXClass = this.BuildKXClass();
23             return newKXClass;
24         }
25 
26         public KXClass BuildKXClass()
27         {
28             Random newRandom = new Random();
29             List<KXChildClass> KXChildClassList = new List<KXChildClass>(10);
30             for (int i = 0; i < 10; i++)
31             {
32                 KXChildClass newChild = new KXChildClass();
33                 newChild.Value_Int32 = newRandom.Next();
34                 newChild.Value_String = "值:" + newChild.Value_Int32;
35                 KXChildClassList.Add(newChild);
36             }
37             //
38             List<sbyte> byteList = new List<sbyte>(10);
39             for (int i = 0; i < 10; i++)
40             {
41                 sbyte currentByte = (sbyte)newRandom.Next(64);
42                 byteList.Add(currentByte);
43             }
44             //
45             KXClass newKXClass = new KXClass();
46             newKXClass.Value_ByteArray = byteList;
47             newKXClass.Value_Double = newRandom.NextDouble();
48             newKXClass.Value_KXChildClassArray = KXChildClassList;
49             //
50             return newKXClass;
51         }
52     }
53 }
View Code

 

009_Program.cs

 1 using kx.thrift.demo;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using Thrift.Protocol;
 7 using Thrift.Transport;
 8 using Thrift.Server;
 9 
10 namespace Server
11 {
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             int serverPort = 60000;
17             TServerTransport serverTransport = new TServerSocket(serverPort, 0, false);
18             KXServiceHandle handle = new KXServiceHandle();
19             KXService.Processor processor = new KXService.Processor(handle);
20             TServer server = new TThreadPoolServer(processor, serverTransport);
21             try
22             {
23                 Console.WriteLine("启动服务于本地端口" + serverPort + "\r\n");
24                 server.Serve();
25             }
26             catch (System.Exception ex)
27             {
28                 Console.WriteLine("启动时出错:" + ex.Message + "\r\n");
29                 Console.ReadLine();
30             }
31             
32         }
33     }
34 }
View Code

 

 

 

 

013_Program.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using Thrift.Transport;
 6 using Thrift.Protocol;
 7 using kx.thrift.demo;
 8 
 9 namespace Client
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             TTransport transport = new TSocket("127.0.0.1", 60000);
16             TProtocol protocol = new TBinaryProtocol(transport);
17             KXService.Client client = new KXService.Client(protocol);
18             //
19             try
20             {
21                 Console.WriteLine("transport open\r\n");
22                 transport.Open();
23             }
24             catch (System.Exception ex)
25             {
26                 Console.WriteLine("在Open时发生错误:" + ex.Message);
27                 Console.ReadLine();
28                 return;
29             }
30             //
31             try
32             {
33                 Console.WriteLine("client: ping\r\n");
34                 client.ping();
35             }
36             catch (System.Exception ex)
37             {
38                 Console.WriteLine("在ping时发生错误:" + ex.Message);
39                 Console.ReadLine();
40                 return;
41             }
42             //
43             try
44             {
45                 Console.WriteLine("client: ping\r\n");
46                 int ID = (new Random()).Next();
47                 KXClass newKXClass = Program.BuildKXClass();
48                 KXClass returnKXClass = client.GetKXClass(ID, newKXClass);
49                 Console.WriteLine("GetKXClass完成,请使用断点来查看返回内容");
50             }
51             catch (System.Exception ex)
52             {
53                 Console.WriteLine("在GetKXClass时发生错误:" + ex.Message);
54                 Console.ReadLine();
55                 return;
56             }
57             Console.WriteLine("结束,按回车键退出");
58             Console.ReadLine();
59         }
60 
61         public static KXClass BuildKXClass()
62         {
63             Random newRandom = new Random();
64             List<KXChildClass> KXChildClassList = new List<KXChildClass>(10);
65             for (int i = 0; i < 10; i++)
66             {
67                 KXChildClass newChild = new KXChildClass();
68                 newChild.Value_Int32 = newRandom.Next();
69                 newChild.Value_String = "值:" + newChild.Value_Int32;
70                 KXChildClassList.Add(newChild);
71             }
72             //
73             List<sbyte> byteList = new List<sbyte>(10);
74             for (int i = 0; i < 10; i++)
75             {
76                 sbyte currentByte = (sbyte)newRandom.Next(64);
77                 byteList.Add(currentByte);
78             }
79             //
80             KXClass newKXClass = new KXClass();
81             newKXClass.Value_ByteArray = byteList;
82             newKXClass.Value_Double = newRandom.NextDouble();
83             newKXClass.Value_KXChildClassArray = KXChildClassList;
84             //
85             return newKXClass;
86         }
87     }
88 }
View Code

posted on 2013-08-23 17:55  xxxteam  阅读(2587)  评论(0编辑  收藏  举报

导航