├── obj └── Debug │ ├── TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs │ ├── TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs │ ├── TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs │ ├── Thrift_HelloWorld_CSharp.exe │ ├── Thrift_HelloWorld_CSharp.pdb │ ├── DesignTimeResolveAssemblyReferences.cache │ ├── Thrift_HelloWorld_CSharp.Form1.resources │ ├── DesignTimeResolveAssemblyReferencesInput.cache │ ├── Thrift_HelloWorld_CSharp.Bidirection.Form1.resources │ ├── Thrift_HelloWorld_CSharp.csproj.GenerateResource.Cache │ ├── Thrift_HelloWorld_CSharp.Properties.Resources.resources │ └── Thrift_HelloWorld_CSharp.csproj.FileListAbsolute.txt ├── HelloWorld.thrift ├── bin └── Debug │ ├── Thrift.dll │ ├── Thrift_HelloWorld_CSharp.exe │ ├── Thrift_HelloWorld_CSharp.pdb │ ├── Thrift_HelloWorld_CSharp.vshost.exe │ ├── Thrift_HelloWorld_CSharp.exe.config │ ├── Thrift_HelloWorld_CSharp.vshost.exe.config │ └── Thrift_HelloWorld_CSharp.vshost.exe.manifest ├── README.md ├── packages.config ├── App.config ├── Thrift_HelloWorld_CSharp.csproj.user ├── Properties ├── Settings.settings ├── AssemblyInfo.cs ├── Settings.Designer.cs ├── Resources.Designer.cs └── Resources.resx ├── Program.cs ├── HelloWorldClient.cs ├── HelloWorldServer.cs ├── Form1.cs ├── Bidirection ├── Form1.cs ├── HelloWorldBidirectionClient.cs ├── HelloWorldBidirectionServer.cs ├── Form1.resx ├── HelloWorldBidirectionService.cs └── Form1.Designer.cs ├── Thrift_HelloWorld_CSharp.csproj ├── Form1.resx ├── Form1.Designer.cs └── HelloWorldService.cs /obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /HelloWorld.thrift: -------------------------------------------------------------------------------- 1 | service HelloWorldService{ 2 | void SayHello(1:string msg); 3 | } -------------------------------------------------------------------------------- /bin/Debug/Thrift.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BuildSoftwareBetter/Thrift_HelloWorld_CSharp/HEAD/bin/Debug/Thrift.dll -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Thrift_HelloWorld_CSharp 2 | C#实现的Thrift服务端与客户端 3 | 博客地址 http://blog.csdn.net/lwwl12/article/details/77102209 4 | -------------------------------------------------------------------------------- /bin/Debug/Thrift_HelloWorld_CSharp.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BuildSoftwareBetter/Thrift_HelloWorld_CSharp/HEAD/bin/Debug/Thrift_HelloWorld_CSharp.exe -------------------------------------------------------------------------------- /bin/Debug/Thrift_HelloWorld_CSharp.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BuildSoftwareBetter/Thrift_HelloWorld_CSharp/HEAD/bin/Debug/Thrift_HelloWorld_CSharp.pdb -------------------------------------------------------------------------------- /obj/Debug/Thrift_HelloWorld_CSharp.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BuildSoftwareBetter/Thrift_HelloWorld_CSharp/HEAD/obj/Debug/Thrift_HelloWorld_CSharp.exe -------------------------------------------------------------------------------- /obj/Debug/Thrift_HelloWorld_CSharp.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BuildSoftwareBetter/Thrift_HelloWorld_CSharp/HEAD/obj/Debug/Thrift_HelloWorld_CSharp.pdb -------------------------------------------------------------------------------- /packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /bin/Debug/Thrift_HelloWorld_CSharp.vshost.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BuildSoftwareBetter/Thrift_HelloWorld_CSharp/HEAD/bin/Debug/Thrift_HelloWorld_CSharp.vshost.exe -------------------------------------------------------------------------------- /obj/Debug/DesignTimeResolveAssemblyReferences.cache: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BuildSoftwareBetter/Thrift_HelloWorld_CSharp/HEAD/obj/Debug/DesignTimeResolveAssemblyReferences.cache -------------------------------------------------------------------------------- /obj/Debug/Thrift_HelloWorld_CSharp.Form1.resources: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BuildSoftwareBetter/Thrift_HelloWorld_CSharp/HEAD/obj/Debug/Thrift_HelloWorld_CSharp.Form1.resources -------------------------------------------------------------------------------- /obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BuildSoftwareBetter/Thrift_HelloWorld_CSharp/HEAD/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache -------------------------------------------------------------------------------- /obj/Debug/Thrift_HelloWorld_CSharp.Bidirection.Form1.resources: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BuildSoftwareBetter/Thrift_HelloWorld_CSharp/HEAD/obj/Debug/Thrift_HelloWorld_CSharp.Bidirection.Form1.resources -------------------------------------------------------------------------------- /obj/Debug/Thrift_HelloWorld_CSharp.csproj.GenerateResource.Cache: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BuildSoftwareBetter/Thrift_HelloWorld_CSharp/HEAD/obj/Debug/Thrift_HelloWorld_CSharp.csproj.GenerateResource.Cache -------------------------------------------------------------------------------- /obj/Debug/Thrift_HelloWorld_CSharp.Properties.Resources.resources: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BuildSoftwareBetter/Thrift_HelloWorld_CSharp/HEAD/obj/Debug/Thrift_HelloWorld_CSharp.Properties.Resources.resources -------------------------------------------------------------------------------- /App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /bin/Debug/Thrift_HelloWorld_CSharp.exe.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /bin/Debug/Thrift_HelloWorld_CSharp.vshost.exe.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Thrift_HelloWorld_CSharp.csproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ShowAllFiles 5 | 6 | -------------------------------------------------------------------------------- /Properties/Settings.settings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /bin/Debug/Thrift_HelloWorld_CSharp.vshost.exe.manifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using System.Windows.Forms; 6 | 7 | namespace Thrift_HelloWorld_CSharp 8 | { 9 | static class Program 10 | { 11 | /// 12 | /// 应用程序的主入口点。 13 | /// 14 | [STAThread] 15 | static void Main() 16 | { 17 | Application.EnableVisualStyles(); 18 | Application.SetCompatibleTextRenderingDefault(false); 19 | Application.Run(new Form1()); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /HelloWorldClient.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Thrift; 7 | using Thrift.Protocol; 8 | using Thrift.Transport; 9 | 10 | namespace Thrift_HelloWorld_CSharp 11 | { 12 | public static class HelloWorldClient 13 | { 14 | public static void Say(int port, string msg, string ip = "127.0.0.1") 15 | { 16 | TTransport transport = new TSocket(ip, port); 17 | TProtocol protocol = new TBinaryProtocol(transport); 18 | HelloWorldService.Client client = new HelloWorldService.Client(protocol); 19 | 20 | transport.Open(); 21 | try 22 | { 23 | client.SayHello(msg); 24 | } 25 | catch (TApplicationException x) 26 | { 27 | Console.WriteLine(x.StackTrace); 28 | 29 | } 30 | finally 31 | { 32 | transport.Close(); 33 | } 34 | } 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /HelloWorldServer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Thrift.Protocol; 7 | using Thrift.Server; 8 | using Thrift.Transport; 9 | 10 | namespace Thrift_HelloWorld_CSharp 11 | { 12 | public class HelloWorldServer : HelloWorldService.Iface 13 | { 14 | public static void Run(int port) 15 | { 16 | HelloWorldServer client = new HelloWorldServer(); 17 | HelloWorldService.Processor processor = new global::HelloWorldService.Processor(client); 18 | TServerTransport transport = new TServerSocket(port); 19 | TServer server = new TThreadPoolServer(processor, transport); 20 | 21 | Console.WriteLine("Starting the server..."); 22 | 23 | server.Serve(); 24 | 25 | transport.Close(); 26 | } 27 | 28 | public void SayHello(string msg) 29 | { 30 | Console.WriteLine(string.Format("{0:yyyy/MM/dd hh:mm:ss} {1}", DateTime.Now, msg)); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /obj/Debug/Thrift_HelloWorld_CSharp.csproj.FileListAbsolute.txt: -------------------------------------------------------------------------------- 1 | D:\sample\Thrift_Cpp_CSharp_Demo\Thrift_HelloWorld_CSharp\bin\Debug\Thrift_HelloWorld_CSharp.exe.config 2 | D:\sample\Thrift_Cpp_CSharp_Demo\Thrift_HelloWorld_CSharp\bin\Debug\Thrift_HelloWorld_CSharp.exe 3 | D:\sample\Thrift_Cpp_CSharp_Demo\Thrift_HelloWorld_CSharp\bin\Debug\Thrift_HelloWorld_CSharp.pdb 4 | D:\sample\Thrift_Cpp_CSharp_Demo\Thrift_HelloWorld_CSharp\bin\Debug\Thrift.dll 5 | D:\sample\Thrift_Cpp_CSharp_Demo\Thrift_HelloWorld_CSharp\obj\Debug\Thrift_HelloWorld_CSharp.Form1.resources 6 | D:\sample\Thrift_Cpp_CSharp_Demo\Thrift_HelloWorld_CSharp\obj\Debug\Thrift_HelloWorld_CSharp.Properties.Resources.resources 7 | D:\sample\Thrift_Cpp_CSharp_Demo\Thrift_HelloWorld_CSharp\obj\Debug\Thrift_HelloWorld_CSharp.csproj.GenerateResource.Cache 8 | D:\sample\Thrift_Cpp_CSharp_Demo\Thrift_HelloWorld_CSharp\obj\Debug\Thrift_HelloWorld_CSharp.exe 9 | D:\sample\Thrift_Cpp_CSharp_Demo\Thrift_HelloWorld_CSharp\obj\Debug\Thrift_HelloWorld_CSharp.pdb 10 | D:\sample\Thrift_Cpp_CSharp_Demo\Thrift_HelloWorld_CSharp\obj\Debug\Thrift_HelloWorld_CSharp.Bidirection.Form1.resources 11 | -------------------------------------------------------------------------------- /Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的一般信息由以下 6 | // 控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("Thrift_HelloWorld_CSharp")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("玖嘉久电子科技有限公司")] 12 | [assembly: AssemblyProduct("Thrift_HelloWorld_CSharp")] 13 | [assembly: AssemblyCopyright("Copyright © 玖嘉久电子科技有限公司 2017")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | //将 ComVisible 设置为 false 将使此程序集中的类型 18 | //对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 19 | //请将此类型的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("6914c101-5ef9-4549-b84f-867e8d75da39")] 24 | 25 | // 程序集的版本信息由下列四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, 33 | // 方法是按如下所示使用“*”: : 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace Thrift_HelloWorld_CSharp.Properties 12 | { 13 | 14 | 15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] 17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 18 | { 19 | 20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 21 | 22 | public static Settings Default 23 | { 24 | get 25 | { 26 | return defaultInstance; 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Form1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Data; 5 | using System.Drawing; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading; 9 | using System.Threading.Tasks; 10 | using System.Windows.Forms; 11 | 12 | namespace Thrift_HelloWorld_CSharp 13 | { 14 | public partial class Form1 : Form 15 | { 16 | public Form1() 17 | { 18 | InitializeComponent(); 19 | } 20 | 21 | private void button1_Click(object sender, EventArgs e) 22 | { 23 | Thread t = new Thread(new ThreadStart(StartServer)); 24 | t.Start(); 25 | 26 | button1.Enabled = false; 27 | 28 | //await Task.Run(new Action(() => 29 | // { 30 | // HelloWorldServer.Run(SrvPort); 31 | // } 32 | // )); 33 | } 34 | 35 | private void button2_Click(object sender, EventArgs e) 36 | { 37 | Task.Run(new Action(() => 38 | { 39 | HelloWorldClient.Say(CliPort, Msg); 40 | } 41 | )); 42 | } 43 | 44 | int SrvPort 45 | { 46 | get 47 | { 48 | return Convert.ToInt32(this.txbSrvPort.Text); 49 | } 50 | } 51 | 52 | int CliPort 53 | { 54 | get 55 | { 56 | return Convert.ToInt32(this.txbCliPort.Text); 57 | } 58 | } 59 | 60 | string Msg 61 | { 62 | get 63 | { 64 | return this.txbMsg.Text; 65 | } 66 | } 67 | 68 | void StartServer() 69 | { 70 | HelloWorldServer.Run(SrvPort); 71 | } 72 | 73 | private void button3_Click(object sender, EventArgs e) 74 | { 75 | Bidirection.Form1 form = new Bidirection.Form1(); 76 | form.Show(); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Bidirection/Form1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Data; 5 | using System.Drawing; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading; 9 | using System.Threading.Tasks; 10 | using System.Windows.Forms; 11 | 12 | namespace Thrift_HelloWorld_CSharp.Bidirection 13 | { 14 | public partial class Form1 : Form 15 | { 16 | public Form1() 17 | { 18 | InitializeComponent(); 19 | } 20 | 21 | 22 | int SrvPort 23 | { 24 | get 25 | { 26 | return Convert.ToInt32(this.txbSrvPort.Text); 27 | } 28 | } 29 | 30 | int CliPort 31 | { 32 | get 33 | { 34 | return Convert.ToInt32(this.txbCliPort.Text); 35 | } 36 | } 37 | 38 | string Msg 39 | { 40 | get 41 | { 42 | return this.txbMsg.Text; 43 | } 44 | } 45 | 46 | string SrvMsg 47 | { 48 | get 49 | { 50 | return this.txbSrvMsg.Text; 51 | } 52 | } 53 | 54 | private void button1_Click(object sender, EventArgs e) 55 | { 56 | Thread t = new Thread(new ThreadStart(StartServer)); 57 | t.Start(); 58 | 59 | button1.Enabled = false; 60 | } 61 | 62 | void StartServer() 63 | { 64 | HelloWorldBidirectionServer srv = new Thrift_HelloWorld_CSharp.HelloWorldBidirectionServer(); 65 | srv.Run(SrvPort); 66 | } 67 | 68 | 69 | private void button3_Click(object sender, EventArgs e) 70 | { 71 | HelloWorldBidirectionServer srv = new Thrift_HelloWorld_CSharp.HelloWorldBidirectionServer(); 72 | srv.SayToClient(SrvMsg); 73 | } 74 | 75 | private void button4_Click(object sender, EventArgs e) 76 | { 77 | HelloWorldBidirectionClient client = new HelloWorldBidirectionClient(); 78 | client.ConnectAndListern(CliPort); 79 | } 80 | 81 | private void button2_Click(object sender, EventArgs e) 82 | { 83 | Task.Run(new Action(() => 84 | { 85 | HelloWorldBidirectionClient client = new HelloWorldBidirectionClient(); 86 | client.Say(Msg); 87 | } 88 | )); 89 | 90 | 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /Bidirection/HelloWorldBidirectionClient.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading; 6 | using System.Threading.Tasks; 7 | using Thrift.Protocol; 8 | using Thrift.Transport; 9 | 10 | namespace Thrift_HelloWorld_CSharp 11 | { 12 | public class HelloWorldBidirectionClient 13 | { 14 | static HelloWorldBidirectionService.Client client = null; 15 | public void ConnectAndListern(int port, string ip = "127.0.0.1") 16 | { 17 | //Tsocket: TCP/IP Socket接口 18 | TSocket tSocket = new TSocket(ip, port); 19 | //消息结构协议 20 | TProtocol protocol = new TBinaryProtocol(tSocket); 21 | try 22 | { 23 | if (client == null) 24 | { 25 | client = new global::HelloWorldBidirectionService.Client(protocol); 26 | tSocket.Open();//建立连接 27 | StartListern(tSocket);//启动监听线程 28 | } 29 | //cli.SayHello(msg); 30 | //tSocket.Close(); 31 | //cli.Dispose(); 32 | } 33 | catch (Exception ex) 34 | { 35 | Console.WriteLine(ex.Message); 36 | } 37 | } 38 | 39 | public void Say(string msg) 40 | { 41 | if (client != null) 42 | client.SayHello(msg); 43 | } 44 | 45 | void StartListern(TSocket tSocket) 46 | { 47 | Thread t = new Thread(new ParameterizedThreadStart(Run)); 48 | t.Start(tSocket); 49 | } 50 | 51 | public void Run(object tSocket) 52 | { 53 | HelloWorldBidirectionService.Processor process = new HelloWorldBidirectionService.Processor(new HelloWorldBidirectionFace()); 54 | 55 | try 56 | { 57 | while (process.Process(new TBinaryProtocol((TSocket)tSocket), new TBinaryProtocol((TSocket)tSocket))) 58 | { 59 | Console.WriteLine("消息接收完成,等下一波,阻塞中......"); 60 | } 61 | } 62 | catch (Exception ex) 63 | { 64 | Console.WriteLine("连接断开..." + ex.Message); 65 | } 66 | } 67 | 68 | } 69 | class HelloWorldBidirectionFace : HelloWorldBidirectionService.Iface 70 | { 71 | public void SayHello(string msg) 72 | { 73 | Console.WriteLine(string.Format("{0:yyyy/MM/dd hh:mm:ss} 收到服务端响应消息 {1}", DateTime.Now, msg)); 74 | 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 此代码由工具生成。 4 | // 运行时版本: 4.0.30319.42000 5 | // 6 | // 对此文件的更改可能导致不正确的行为,如果 7 | // 重新生成代码,则所做更改将丢失。 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace Thrift_HelloWorld_CSharp.Properties 12 | { 13 | 14 | 15 | /// 16 | /// 强类型资源类,用于查找本地化字符串等。 17 | /// 18 | // 此类是由 StronglyTypedResourceBuilder 19 | // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 20 | // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen 21 | // (以 /str 作为命令选项),或重新生成 VS 项目。 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources 26 | { 27 | 28 | private static global::System.Resources.ResourceManager resourceMan; 29 | 30 | private static global::System.Globalization.CultureInfo resourceCulture; 31 | 32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 33 | internal Resources() 34 | { 35 | } 36 | 37 | /// 38 | /// 返回此类使用的缓存 ResourceManager 实例。 39 | /// 40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 41 | internal static global::System.Resources.ResourceManager ResourceManager 42 | { 43 | get 44 | { 45 | if ((resourceMan == null)) 46 | { 47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Thrift_HelloWorld_CSharp.Properties.Resources", typeof(Resources).Assembly); 48 | resourceMan = temp; 49 | } 50 | return resourceMan; 51 | } 52 | } 53 | 54 | /// 55 | /// 覆盖当前线程的 CurrentUICulture 属性 56 | /// 使用此强类型的资源类的资源查找。 57 | /// 58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 59 | internal static global::System.Globalization.CultureInfo Culture 60 | { 61 | get 62 | { 63 | return resourceCulture; 64 | } 65 | set 66 | { 67 | resourceCulture = value; 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Bidirection/HelloWorldBidirectionServer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Thrift; 7 | using Thrift.Protocol; 8 | using Thrift.Server; 9 | using Thrift.Transport; 10 | 11 | namespace Thrift_HelloWorld_CSharp 12 | { 13 | public class HelloWorldBidirectionServer : HelloWorldBidirectionService.Iface 14 | { 15 | public void Run(int port) 16 | { 17 | try 18 | { 19 | TServerTransport transport = new TServerSocket(port); 20 | 21 | TTransportFactory transportFac = new TTransportFactory(); 22 | 23 | TProtocolFactory inputProtocolFactory = new TBinaryProtocol.Factory(); 24 | TThreadPoolServer server = new TThreadPoolServer(getProcessorFactory(), transport, transportFac, inputProtocolFactory); 25 | 26 | server.Serve(); 27 | } 28 | catch (Exception ex) 29 | { 30 | Console.WriteLine(ex.Message); 31 | } 32 | } 33 | 34 | public static List TransportCollection = new List(); 35 | 36 | public void SayHello(string msg) 37 | { 38 | Console.WriteLine(string.Format("{0:yyyy/MM/dd hh:mm:ss} 服务端接收到消息: {1}", DateTime.Now, msg)); 39 | } 40 | 41 | public void SayToClient(string msg) 42 | { 43 | try 44 | { 45 | foreach (TTransport trans in TransportCollection) 46 | { 47 | TBinaryProtocol protocol = new TBinaryProtocol(trans); 48 | HelloWorldBidirectionService.Client client = new HelloWorldBidirectionService.Client(protocol); 49 | //Thread.Sleep(1000); 50 | client.SayHello(msg); 51 | Console.WriteLine("发给了客户端哟"); 52 | } 53 | } 54 | catch (Exception ex) 55 | { 56 | Console.WriteLine(ex.Message); 57 | } 58 | } 59 | 60 | public TProcessorFactory getProcessorFactory() 61 | { 62 | return new HelloWorldBidirectionProcessor(); 63 | } 64 | } 65 | 66 | public class HelloWorldBidirectionProcessor : TProcessorFactory 67 | { 68 | public TProcessor GetProcessor(TTransport trans, TServer server = null) 69 | { 70 | if (trans.IsOpen) 71 | { 72 | HelloWorldBidirectionServer.TransportCollection.Add(trans); 73 | Console.WriteLine("有客户端上船啦"); 74 | } 75 | 76 | HelloWorldBidirectionServer srv = new HelloWorldBidirectionServer(); 77 | return new global::HelloWorldBidirectionService.Processor(srv); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Thrift_HelloWorld_CSharp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {6914C101-5EF9-4549-B84F-867E8D75DA39} 8 | Exe 9 | Properties 10 | Thrift_HelloWorld_CSharp 11 | Thrift_HelloWorld_CSharp 12 | v4.5.2 13 | 512 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | ..\packages\thrift-csharp.0.10.0\lib\Thrift.dll 52 | True 53 | 54 | 55 | 56 | 57 | Form 58 | 59 | 60 | Form1.cs 61 | 62 | 63 | 64 | Form 65 | 66 | 67 | Form1.cs 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | Form1.cs 78 | 79 | 80 | Form1.cs 81 | 82 | 83 | ResXFileCodeGenerator 84 | Resources.Designer.cs 85 | Designer 86 | 87 | 88 | True 89 | Resources.resx 90 | 91 | 92 | 93 | SettingsSingleFileGenerator 94 | Settings.Designer.cs 95 | 96 | 97 | True 98 | Settings.settings 99 | True 100 | 101 | 102 | 103 | 104 | 105 | 106 | 113 | -------------------------------------------------------------------------------- /Properties/Resources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /Form1.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /Bidirection/Form1.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /Form1.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace Thrift_HelloWorld_CSharp 2 | { 3 | partial class Form1 4 | { 5 | /// 6 | /// 必需的设计器变量。 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// 清理所有正在使用的资源。 12 | /// 13 | /// 如果应释放托管资源,为 true;否则为 false。 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Windows 窗体设计器生成的代码 24 | 25 | /// 26 | /// 设计器支持所需的方法 - 不要修改 27 | /// 使用代码编辑器修改此方法的内容。 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.button1 = new System.Windows.Forms.Button(); 32 | this.button2 = new System.Windows.Forms.Button(); 33 | this.txbSrvPort = new System.Windows.Forms.TextBox(); 34 | this.txbMsg = new System.Windows.Forms.TextBox(); 35 | this.txbCliPort = new System.Windows.Forms.TextBox(); 36 | this.label1 = new System.Windows.Forms.Label(); 37 | this.label2 = new System.Windows.Forms.Label(); 38 | this.label3 = new System.Windows.Forms.Label(); 39 | this.button3 = new System.Windows.Forms.Button(); 40 | this.SuspendLayout(); 41 | // 42 | // button1 43 | // 44 | this.button1.Location = new System.Drawing.Point(201, 26); 45 | this.button1.Name = "button1"; 46 | this.button1.Size = new System.Drawing.Size(75, 23); 47 | this.button1.TabIndex = 0; 48 | this.button1.Text = "启动"; 49 | this.button1.UseVisualStyleBackColor = true; 50 | this.button1.Click += new System.EventHandler(this.button1_Click); 51 | // 52 | // button2 53 | // 54 | this.button2.Location = new System.Drawing.Point(366, 144); 55 | this.button2.Name = "button2"; 56 | this.button2.Size = new System.Drawing.Size(75, 23); 57 | this.button2.TabIndex = 1; 58 | this.button2.Text = "发送"; 59 | this.button2.UseVisualStyleBackColor = true; 60 | this.button2.Click += new System.EventHandler(this.button2_Click); 61 | // 62 | // txbSrvPort 63 | // 64 | this.txbSrvPort.Location = new System.Drawing.Point(95, 26); 65 | this.txbSrvPort.Name = "txbSrvPort"; 66 | this.txbSrvPort.Size = new System.Drawing.Size(100, 21); 67 | this.txbSrvPort.TabIndex = 2; 68 | this.txbSrvPort.Text = "9090"; 69 | // 70 | // txbMsg 71 | // 72 | this.txbMsg.Location = new System.Drawing.Point(95, 144); 73 | this.txbMsg.Name = "txbMsg"; 74 | this.txbMsg.Size = new System.Drawing.Size(265, 21); 75 | this.txbMsg.TabIndex = 3; 76 | this.txbMsg.Text = "HelloWorld"; 77 | // 78 | // txbCliPort 79 | // 80 | this.txbCliPort.Location = new System.Drawing.Point(95, 92); 81 | this.txbCliPort.Name = "txbCliPort"; 82 | this.txbCliPort.Size = new System.Drawing.Size(100, 21); 83 | this.txbCliPort.TabIndex = 4; 84 | this.txbCliPort.Text = "1234"; 85 | // 86 | // label1 87 | // 88 | this.label1.AutoSize = true; 89 | this.label1.Location = new System.Drawing.Point(12, 95); 90 | this.label1.Name = "label1"; 91 | this.label1.Size = new System.Drawing.Size(77, 12); 92 | this.label1.TabIndex = 5; 93 | this.label1.Text = "客户端端口:"; 94 | // 95 | // label2 96 | // 97 | this.label2.AutoSize = true; 98 | this.label2.Location = new System.Drawing.Point(12, 31); 99 | this.label2.Name = "label2"; 100 | this.label2.Size = new System.Drawing.Size(77, 12); 101 | this.label2.TabIndex = 6; 102 | this.label2.Text = "服务端端口:"; 103 | // 104 | // label3 105 | // 106 | this.label3.AutoSize = true; 107 | this.label3.Location = new System.Drawing.Point(12, 149); 108 | this.label3.Name = "label3"; 109 | this.label3.Size = new System.Drawing.Size(77, 12); 110 | this.label3.TabIndex = 7; 111 | this.label3.Text = "发送 消息:"; 112 | // 113 | // button3 114 | // 115 | this.button3.Location = new System.Drawing.Point(366, 26); 116 | this.button3.Name = "button3"; 117 | this.button3.Size = new System.Drawing.Size(75, 23); 118 | this.button3.TabIndex = 8; 119 | this.button3.Text = "双向demo"; 120 | this.button3.UseVisualStyleBackColor = true; 121 | this.button3.Click += new System.EventHandler(this.button3_Click); 122 | // 123 | // Form1 124 | // 125 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 126 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 127 | this.ClientSize = new System.Drawing.Size(453, 194); 128 | this.Controls.Add(this.button3); 129 | this.Controls.Add(this.label3); 130 | this.Controls.Add(this.label2); 131 | this.Controls.Add(this.label1); 132 | this.Controls.Add(this.txbCliPort); 133 | this.Controls.Add(this.txbMsg); 134 | this.Controls.Add(this.txbSrvPort); 135 | this.Controls.Add(this.button2); 136 | this.Controls.Add(this.button1); 137 | this.Name = "Form1"; 138 | this.Text = "Form1"; 139 | this.ResumeLayout(false); 140 | this.PerformLayout(); 141 | 142 | } 143 | 144 | #endregion 145 | 146 | private System.Windows.Forms.Button button1; 147 | private System.Windows.Forms.Button button2; 148 | private System.Windows.Forms.TextBox txbSrvPort; 149 | private System.Windows.Forms.TextBox txbMsg; 150 | private System.Windows.Forms.TextBox txbCliPort; 151 | private System.Windows.Forms.Label label1; 152 | private System.Windows.Forms.Label label2; 153 | private System.Windows.Forms.Label label3; 154 | private System.Windows.Forms.Button button3; 155 | } 156 | } 157 | 158 | -------------------------------------------------------------------------------- /Bidirection/HelloWorldBidirectionService.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * Autogenerated by Thrift Compiler (0.10.0) 3 | * 4 | * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING 5 | * @generated 6 | */ 7 | using System; 8 | using System.Collections; 9 | using System.Collections.Generic; 10 | using System.Text; 11 | using System.IO; 12 | using Thrift; 13 | using Thrift.Collections; 14 | using System.Runtime.Serialization; 15 | using Thrift.Protocol; 16 | using Thrift.Transport; 17 | 18 | public partial class HelloWorldBidirectionService { 19 | public interface ISync { 20 | void SayHello(string msg); 21 | } 22 | 23 | public interface Iface : ISync { 24 | #if SILVERLIGHT 25 | IAsyncResult Begin_SayHello(AsyncCallback callback, object state, string msg); 26 | void End_SayHello(IAsyncResult asyncResult); 27 | #endif 28 | } 29 | 30 | public class Client : IDisposable, Iface { 31 | public Client(TProtocol prot) : this(prot, prot) 32 | { 33 | } 34 | 35 | public Client(TProtocol iprot, TProtocol oprot) 36 | { 37 | iprot_ = iprot; 38 | oprot_ = oprot; 39 | } 40 | 41 | protected TProtocol iprot_; 42 | protected TProtocol oprot_; 43 | protected int seqid_; 44 | 45 | public TProtocol InputProtocol 46 | { 47 | get { return iprot_; } 48 | } 49 | public TProtocol OutputProtocol 50 | { 51 | get { return oprot_; } 52 | } 53 | 54 | 55 | #region " IDisposable Support " 56 | private bool _IsDisposed; 57 | 58 | // IDisposable 59 | public void Dispose() 60 | { 61 | Dispose(true); 62 | } 63 | 64 | 65 | protected virtual void Dispose(bool disposing) 66 | { 67 | if (!_IsDisposed) 68 | { 69 | if (disposing) 70 | { 71 | if (iprot_ != null) 72 | { 73 | ((IDisposable)iprot_).Dispose(); 74 | } 75 | if (oprot_ != null) 76 | { 77 | ((IDisposable)oprot_).Dispose(); 78 | } 79 | } 80 | } 81 | _IsDisposed = true; 82 | } 83 | #endregion 84 | 85 | 86 | 87 | #if SILVERLIGHT 88 | public IAsyncResult Begin_SayHello(AsyncCallback callback, object state, string msg) 89 | { 90 | return send_SayHello(callback, state, msg); 91 | } 92 | 93 | public void End_SayHello(IAsyncResult asyncResult) 94 | { 95 | oprot_.Transport.EndFlush(asyncResult); 96 | } 97 | 98 | #endif 99 | 100 | public void SayHello(string msg) 101 | { 102 | #if !SILVERLIGHT 103 | send_SayHello(msg); 104 | 105 | #else 106 | var asyncResult = Begin_SayHello(null, null, msg); 107 | 108 | #endif 109 | } 110 | #if SILVERLIGHT 111 | public IAsyncResult send_SayHello(AsyncCallback callback, object state, string msg) 112 | #else 113 | public void send_SayHello(string msg) 114 | #endif 115 | { 116 | oprot_.WriteMessageBegin(new TMessage("SayHello", TMessageType.Oneway, seqid_)); 117 | SayHello_args args = new SayHello_args(); 118 | args.Msg = msg; 119 | args.Write(oprot_); 120 | oprot_.WriteMessageEnd(); 121 | #if SILVERLIGHT 122 | return oprot_.Transport.BeginFlush(callback, state); 123 | #else 124 | oprot_.Transport.Flush(); 125 | #endif 126 | } 127 | 128 | } 129 | public class Processor : TProcessor { 130 | public Processor(ISync iface) 131 | { 132 | iface_ = iface; 133 | processMap_["SayHello"] = SayHello_Process; 134 | } 135 | 136 | protected delegate void ProcessFunction(int seqid, TProtocol iprot, TProtocol oprot); 137 | private ISync iface_; 138 | protected Dictionary processMap_ = new Dictionary(); 139 | 140 | public bool Process(TProtocol iprot, TProtocol oprot) 141 | { 142 | try 143 | { 144 | TMessage msg = iprot.ReadMessageBegin(); 145 | ProcessFunction fn; 146 | processMap_.TryGetValue(msg.Name, out fn); 147 | if (fn == null) { 148 | TProtocolUtil.Skip(iprot, TType.Struct); 149 | iprot.ReadMessageEnd(); 150 | TApplicationException x = new TApplicationException (TApplicationException.ExceptionType.UnknownMethod, "Invalid method name: '" + msg.Name + "'"); 151 | oprot.WriteMessageBegin(new TMessage(msg.Name, TMessageType.Exception, msg.SeqID)); 152 | x.Write(oprot); 153 | oprot.WriteMessageEnd(); 154 | oprot.Transport.Flush(); 155 | return true; 156 | } 157 | fn(msg.SeqID, iprot, oprot); 158 | } 159 | catch (IOException) 160 | { 161 | return false; 162 | } 163 | return true; 164 | } 165 | 166 | public void SayHello_Process(int seqid, TProtocol iprot, TProtocol oprot) 167 | { 168 | SayHello_args args = new SayHello_args(); 169 | args.Read(iprot); 170 | iprot.ReadMessageEnd(); 171 | try 172 | { 173 | iface_.SayHello(args.Msg); 174 | } 175 | catch (TTransportException) 176 | { 177 | throw; 178 | } 179 | catch (Exception ex) 180 | { 181 | Console.Error.WriteLine("Error occurred in processor:"); 182 | Console.Error.WriteLine(ex.ToString()); 183 | } 184 | } 185 | 186 | } 187 | 188 | 189 | #if !SILVERLIGHT 190 | [Serializable] 191 | #endif 192 | public partial class SayHello_args : TBase 193 | { 194 | private string _msg; 195 | 196 | public string Msg 197 | { 198 | get 199 | { 200 | return _msg; 201 | } 202 | set 203 | { 204 | __isset.msg = true; 205 | this._msg = value; 206 | } 207 | } 208 | 209 | 210 | public Isset __isset; 211 | #if !SILVERLIGHT 212 | [Serializable] 213 | #endif 214 | public struct Isset { 215 | public bool msg; 216 | } 217 | 218 | public SayHello_args() { 219 | } 220 | 221 | public void Read (TProtocol iprot) 222 | { 223 | iprot.IncrementRecursionDepth(); 224 | try 225 | { 226 | TField field; 227 | iprot.ReadStructBegin(); 228 | while (true) 229 | { 230 | field = iprot.ReadFieldBegin(); 231 | if (field.Type == TType.Stop) { 232 | break; 233 | } 234 | switch (field.ID) 235 | { 236 | case 1: 237 | if (field.Type == TType.String) { 238 | Msg = iprot.ReadString(); 239 | } else { 240 | TProtocolUtil.Skip(iprot, field.Type); 241 | } 242 | break; 243 | default: 244 | TProtocolUtil.Skip(iprot, field.Type); 245 | break; 246 | } 247 | iprot.ReadFieldEnd(); 248 | } 249 | iprot.ReadStructEnd(); 250 | } 251 | finally 252 | { 253 | iprot.DecrementRecursionDepth(); 254 | } 255 | } 256 | 257 | public void Write(TProtocol oprot) { 258 | oprot.IncrementRecursionDepth(); 259 | try 260 | { 261 | TStruct struc = new TStruct("SayHello_args"); 262 | oprot.WriteStructBegin(struc); 263 | TField field = new TField(); 264 | if (Msg != null && __isset.msg) { 265 | field.Name = "msg"; 266 | field.Type = TType.String; 267 | field.ID = 1; 268 | oprot.WriteFieldBegin(field); 269 | oprot.WriteString(Msg); 270 | oprot.WriteFieldEnd(); 271 | } 272 | oprot.WriteFieldStop(); 273 | oprot.WriteStructEnd(); 274 | } 275 | finally 276 | { 277 | oprot.DecrementRecursionDepth(); 278 | } 279 | } 280 | 281 | public override string ToString() { 282 | StringBuilder __sb = new StringBuilder("SayHello_args("); 283 | bool __first = true; 284 | if (Msg != null && __isset.msg) { 285 | if(!__first) { __sb.Append(", "); } 286 | __first = false; 287 | __sb.Append("Msg: "); 288 | __sb.Append(Msg); 289 | } 290 | __sb.Append(")"); 291 | return __sb.ToString(); 292 | } 293 | 294 | } 295 | 296 | } 297 | -------------------------------------------------------------------------------- /Bidirection/Form1.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace Thrift_HelloWorld_CSharp.Bidirection 2 | { 3 | partial class Form1 4 | { 5 | /// 6 | /// Required designer variable. 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// Clean up any resources being used. 12 | /// 13 | /// true if managed resources should be disposed; otherwise, false. 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Windows Form Designer generated code 24 | 25 | /// 26 | /// Required method for Designer support - do not modify 27 | /// the contents of this method with the code editor. 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.label3 = new System.Windows.Forms.Label(); 32 | this.label2 = new System.Windows.Forms.Label(); 33 | this.label1 = new System.Windows.Forms.Label(); 34 | this.txbCliPort = new System.Windows.Forms.TextBox(); 35 | this.txbMsg = new System.Windows.Forms.TextBox(); 36 | this.txbSrvPort = new System.Windows.Forms.TextBox(); 37 | this.button2 = new System.Windows.Forms.Button(); 38 | this.button1 = new System.Windows.Forms.Button(); 39 | this.button3 = new System.Windows.Forms.Button(); 40 | this.txbSrvMsg = new System.Windows.Forms.TextBox(); 41 | this.label4 = new System.Windows.Forms.Label(); 42 | this.button4 = new System.Windows.Forms.Button(); 43 | this.SuspendLayout(); 44 | // 45 | // label3 46 | // 47 | this.label3.AutoSize = true; 48 | this.label3.Location = new System.Drawing.Point(92, 184); 49 | this.label3.Name = "label3"; 50 | this.label3.Size = new System.Drawing.Size(77, 12); 51 | this.label3.TabIndex = 15; 52 | this.label3.Text = "发送 消息:"; 53 | // 54 | // label2 55 | // 56 | this.label2.AutoSize = true; 57 | this.label2.Location = new System.Drawing.Point(92, 34); 58 | this.label2.Name = "label2"; 59 | this.label2.Size = new System.Drawing.Size(77, 12); 60 | this.label2.TabIndex = 14; 61 | this.label2.Text = "服务端端口:"; 62 | // 63 | // label1 64 | // 65 | this.label1.AutoSize = true; 66 | this.label1.Location = new System.Drawing.Point(92, 135); 67 | this.label1.Name = "label1"; 68 | this.label1.Size = new System.Drawing.Size(77, 12); 69 | this.label1.TabIndex = 13; 70 | this.label1.Text = "客户端端口:"; 71 | // 72 | // txbCliPort 73 | // 74 | this.txbCliPort.Location = new System.Drawing.Point(175, 132); 75 | this.txbCliPort.Name = "txbCliPort"; 76 | this.txbCliPort.Size = new System.Drawing.Size(100, 21); 77 | this.txbCliPort.TabIndex = 12; 78 | this.txbCliPort.Text = "1234"; 79 | // 80 | // txbMsg 81 | // 82 | this.txbMsg.Location = new System.Drawing.Point(175, 179); 83 | this.txbMsg.Name = "txbMsg"; 84 | this.txbMsg.Size = new System.Drawing.Size(265, 21); 85 | this.txbMsg.TabIndex = 11; 86 | this.txbMsg.Text = "HelloWorld"; 87 | // 88 | // txbSrvPort 89 | // 90 | this.txbSrvPort.Location = new System.Drawing.Point(175, 29); 91 | this.txbSrvPort.Name = "txbSrvPort"; 92 | this.txbSrvPort.Size = new System.Drawing.Size(100, 21); 93 | this.txbSrvPort.TabIndex = 10; 94 | this.txbSrvPort.Text = "9090"; 95 | // 96 | // button2 97 | // 98 | this.button2.Location = new System.Drawing.Point(446, 179); 99 | this.button2.Name = "button2"; 100 | this.button2.Size = new System.Drawing.Size(103, 23); 101 | this.button2.TabIndex = 9; 102 | this.button2.Text = "发送到服务端"; 103 | this.button2.UseVisualStyleBackColor = true; 104 | this.button2.Click += new System.EventHandler(this.button2_Click); 105 | // 106 | // button1 107 | // 108 | this.button1.Location = new System.Drawing.Point(281, 29); 109 | this.button1.Name = "button1"; 110 | this.button1.Size = new System.Drawing.Size(75, 23); 111 | this.button1.TabIndex = 8; 112 | this.button1.Text = "启动"; 113 | this.button1.UseVisualStyleBackColor = true; 114 | this.button1.Click += new System.EventHandler(this.button1_Click); 115 | // 116 | // button3 117 | // 118 | this.button3.Location = new System.Drawing.Point(446, 83); 119 | this.button3.Name = "button3"; 120 | this.button3.Size = new System.Drawing.Size(103, 23); 121 | this.button3.TabIndex = 9; 122 | this.button3.Text = "发送到客户端"; 123 | this.button3.UseVisualStyleBackColor = true; 124 | this.button3.Click += new System.EventHandler(this.button3_Click); 125 | // 126 | // txbSrvMsg 127 | // 128 | this.txbSrvMsg.Location = new System.Drawing.Point(175, 83); 129 | this.txbSrvMsg.Name = "txbSrvMsg"; 130 | this.txbSrvMsg.Size = new System.Drawing.Size(265, 21); 131 | this.txbSrvMsg.TabIndex = 11; 132 | this.txbSrvMsg.Text = "HelloWorld"; 133 | // 134 | // label4 135 | // 136 | this.label4.AutoSize = true; 137 | this.label4.Location = new System.Drawing.Point(92, 88); 138 | this.label4.Name = "label4"; 139 | this.label4.Size = new System.Drawing.Size(77, 12); 140 | this.label4.TabIndex = 15; 141 | this.label4.Text = "发送 消息:"; 142 | // 143 | // button4 144 | // 145 | this.button4.Location = new System.Drawing.Point(281, 130); 146 | this.button4.Name = "button4"; 147 | this.button4.Size = new System.Drawing.Size(75, 23); 148 | this.button4.TabIndex = 16; 149 | this.button4.Text = "连接"; 150 | this.button4.UseVisualStyleBackColor = true; 151 | this.button4.Click += new System.EventHandler(this.button4_Click); 152 | // 153 | // Form1 154 | // 155 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 156 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 157 | this.ClientSize = new System.Drawing.Size(613, 262); 158 | this.Controls.Add(this.button4); 159 | this.Controls.Add(this.label4); 160 | this.Controls.Add(this.label3); 161 | this.Controls.Add(this.label2); 162 | this.Controls.Add(this.label1); 163 | this.Controls.Add(this.txbCliPort); 164 | this.Controls.Add(this.txbSrvMsg); 165 | this.Controls.Add(this.txbMsg); 166 | this.Controls.Add(this.button3); 167 | this.Controls.Add(this.txbSrvPort); 168 | this.Controls.Add(this.button2); 169 | this.Controls.Add(this.button1); 170 | this.Name = "Form1"; 171 | this.Text = "Form1"; 172 | this.ResumeLayout(false); 173 | this.PerformLayout(); 174 | 175 | } 176 | 177 | #endregion 178 | 179 | private System.Windows.Forms.Label label3; 180 | private System.Windows.Forms.Label label2; 181 | private System.Windows.Forms.Label label1; 182 | private System.Windows.Forms.TextBox txbCliPort; 183 | private System.Windows.Forms.TextBox txbMsg; 184 | private System.Windows.Forms.TextBox txbSrvPort; 185 | private System.Windows.Forms.Button button2; 186 | private System.Windows.Forms.Button button1; 187 | private System.Windows.Forms.Button button3; 188 | private System.Windows.Forms.TextBox txbSrvMsg; 189 | private System.Windows.Forms.Label label4; 190 | private System.Windows.Forms.Button button4; 191 | } 192 | } -------------------------------------------------------------------------------- /HelloWorldService.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * Autogenerated by Thrift Compiler (0.10.0) 3 | * 4 | * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING 5 | * @generated 6 | */ 7 | using System; 8 | using System.Collections; 9 | using System.Collections.Generic; 10 | using System.Text; 11 | using System.IO; 12 | using Thrift; 13 | using Thrift.Collections; 14 | using System.Runtime.Serialization; 15 | using Thrift.Protocol; 16 | using Thrift.Transport; 17 | 18 | public partial class HelloWorldService { 19 | public interface ISync { 20 | void SayHello(string msg); 21 | } 22 | 23 | public interface Iface : ISync { 24 | #if SILVERLIGHT 25 | IAsyncResult Begin_SayHello(AsyncCallback callback, object state, string msg); 26 | void End_SayHello(IAsyncResult asyncResult); 27 | #endif 28 | } 29 | 30 | public class Client : IDisposable, Iface { 31 | public Client(TProtocol prot) : this(prot, prot) 32 | { 33 | } 34 | 35 | public Client(TProtocol iprot, TProtocol oprot) 36 | { 37 | iprot_ = iprot; 38 | oprot_ = oprot; 39 | } 40 | 41 | protected TProtocol iprot_; 42 | protected TProtocol oprot_; 43 | protected int seqid_; 44 | 45 | public TProtocol InputProtocol 46 | { 47 | get { return iprot_; } 48 | } 49 | public TProtocol OutputProtocol 50 | { 51 | get { return oprot_; } 52 | } 53 | 54 | 55 | #region " IDisposable Support " 56 | private bool _IsDisposed; 57 | 58 | // IDisposable 59 | public void Dispose() 60 | { 61 | Dispose(true); 62 | } 63 | 64 | 65 | protected virtual void Dispose(bool disposing) 66 | { 67 | if (!_IsDisposed) 68 | { 69 | if (disposing) 70 | { 71 | if (iprot_ != null) 72 | { 73 | ((IDisposable)iprot_).Dispose(); 74 | } 75 | if (oprot_ != null) 76 | { 77 | ((IDisposable)oprot_).Dispose(); 78 | } 79 | } 80 | } 81 | _IsDisposed = true; 82 | } 83 | #endregion 84 | 85 | 86 | 87 | #if SILVERLIGHT 88 | public IAsyncResult Begin_SayHello(AsyncCallback callback, object state, string msg) 89 | { 90 | return send_SayHello(callback, state, msg); 91 | } 92 | 93 | public void End_SayHello(IAsyncResult asyncResult) 94 | { 95 | oprot_.Transport.EndFlush(asyncResult); 96 | recv_SayHello(); 97 | } 98 | 99 | #endif 100 | 101 | public void SayHello(string msg) 102 | { 103 | #if !SILVERLIGHT 104 | send_SayHello(msg); 105 | recv_SayHello(); 106 | 107 | #else 108 | var asyncResult = Begin_SayHello(null, null, msg); 109 | End_SayHello(asyncResult); 110 | 111 | #endif 112 | } 113 | #if SILVERLIGHT 114 | public IAsyncResult send_SayHello(AsyncCallback callback, object state, string msg) 115 | #else 116 | public void send_SayHello(string msg) 117 | #endif 118 | { 119 | oprot_.WriteMessageBegin(new TMessage("SayHello", TMessageType.Call, seqid_)); 120 | SayHello_args args = new SayHello_args(); 121 | args.Msg = msg; 122 | args.Write(oprot_); 123 | oprot_.WriteMessageEnd(); 124 | #if SILVERLIGHT 125 | return oprot_.Transport.BeginFlush(callback, state); 126 | #else 127 | oprot_.Transport.Flush(); 128 | #endif 129 | } 130 | 131 | public void recv_SayHello() 132 | { 133 | TMessage msg = iprot_.ReadMessageBegin(); 134 | if (msg.Type == TMessageType.Exception) { 135 | TApplicationException x = TApplicationException.Read(iprot_); 136 | iprot_.ReadMessageEnd(); 137 | throw x; 138 | } 139 | SayHello_result result = new SayHello_result(); 140 | result.Read(iprot_); 141 | iprot_.ReadMessageEnd(); 142 | return; 143 | } 144 | 145 | } 146 | public class Processor : TProcessor { 147 | public Processor(ISync iface) 148 | { 149 | iface_ = iface; 150 | processMap_["SayHello"] = SayHello_Process; 151 | } 152 | 153 | protected delegate void ProcessFunction(int seqid, TProtocol iprot, TProtocol oprot); 154 | private ISync iface_; 155 | protected Dictionary processMap_ = new Dictionary(); 156 | 157 | public bool Process(TProtocol iprot, TProtocol oprot) 158 | { 159 | try 160 | { 161 | TMessage msg = iprot.ReadMessageBegin(); 162 | ProcessFunction fn; 163 | processMap_.TryGetValue(msg.Name, out fn); 164 | if (fn == null) { 165 | TProtocolUtil.Skip(iprot, TType.Struct); 166 | iprot.ReadMessageEnd(); 167 | TApplicationException x = new TApplicationException (TApplicationException.ExceptionType.UnknownMethod, "Invalid method name: '" + msg.Name + "'"); 168 | oprot.WriteMessageBegin(new TMessage(msg.Name, TMessageType.Exception, msg.SeqID)); 169 | x.Write(oprot); 170 | oprot.WriteMessageEnd(); 171 | oprot.Transport.Flush(); 172 | return true; 173 | } 174 | fn(msg.SeqID, iprot, oprot); 175 | } 176 | catch (IOException) 177 | { 178 | return false; 179 | } 180 | return true; 181 | } 182 | 183 | public void SayHello_Process(int seqid, TProtocol iprot, TProtocol oprot) 184 | { 185 | SayHello_args args = new SayHello_args(); 186 | args.Read(iprot); 187 | iprot.ReadMessageEnd(); 188 | SayHello_result result = new SayHello_result(); 189 | try 190 | { 191 | iface_.SayHello(args.Msg); 192 | oprot.WriteMessageBegin(new TMessage("SayHello", TMessageType.Reply, seqid)); 193 | result.Write(oprot); 194 | } 195 | catch (TTransportException) 196 | { 197 | throw; 198 | } 199 | catch (Exception ex) 200 | { 201 | Console.Error.WriteLine("Error occurred in processor:"); 202 | Console.Error.WriteLine(ex.ToString()); 203 | TApplicationException x = new TApplicationException (TApplicationException.ExceptionType.InternalError," Internal error."); 204 | oprot.WriteMessageBegin(new TMessage("SayHello", TMessageType.Exception, seqid)); 205 | x.Write(oprot); 206 | } 207 | oprot.WriteMessageEnd(); 208 | oprot.Transport.Flush(); 209 | } 210 | 211 | } 212 | 213 | 214 | #if !SILVERLIGHT 215 | [Serializable] 216 | #endif 217 | public partial class SayHello_args : TBase 218 | { 219 | private string _msg; 220 | 221 | public string Msg 222 | { 223 | get 224 | { 225 | return _msg; 226 | } 227 | set 228 | { 229 | __isset.msg = true; 230 | this._msg = value; 231 | } 232 | } 233 | 234 | 235 | public Isset __isset; 236 | #if !SILVERLIGHT 237 | [Serializable] 238 | #endif 239 | public struct Isset { 240 | public bool msg; 241 | } 242 | 243 | public SayHello_args() { 244 | } 245 | 246 | public void Read (TProtocol iprot) 247 | { 248 | iprot.IncrementRecursionDepth(); 249 | try 250 | { 251 | TField field; 252 | iprot.ReadStructBegin(); 253 | while (true) 254 | { 255 | field = iprot.ReadFieldBegin(); 256 | if (field.Type == TType.Stop) { 257 | break; 258 | } 259 | switch (field.ID) 260 | { 261 | case 1: 262 | if (field.Type == TType.String) { 263 | Msg = iprot.ReadString(); 264 | } else { 265 | TProtocolUtil.Skip(iprot, field.Type); 266 | } 267 | break; 268 | default: 269 | TProtocolUtil.Skip(iprot, field.Type); 270 | break; 271 | } 272 | iprot.ReadFieldEnd(); 273 | } 274 | iprot.ReadStructEnd(); 275 | } 276 | finally 277 | { 278 | iprot.DecrementRecursionDepth(); 279 | } 280 | } 281 | 282 | public void Write(TProtocol oprot) { 283 | oprot.IncrementRecursionDepth(); 284 | try 285 | { 286 | TStruct struc = new TStruct("SayHello_args"); 287 | oprot.WriteStructBegin(struc); 288 | TField field = new TField(); 289 | if (Msg != null && __isset.msg) { 290 | field.Name = "msg"; 291 | field.Type = TType.String; 292 | field.ID = 1; 293 | oprot.WriteFieldBegin(field); 294 | oprot.WriteString(Msg); 295 | oprot.WriteFieldEnd(); 296 | } 297 | oprot.WriteFieldStop(); 298 | oprot.WriteStructEnd(); 299 | } 300 | finally 301 | { 302 | oprot.DecrementRecursionDepth(); 303 | } 304 | } 305 | 306 | public override string ToString() { 307 | StringBuilder __sb = new StringBuilder("SayHello_args("); 308 | bool __first = true; 309 | if (Msg != null && __isset.msg) { 310 | if(!__first) { __sb.Append(", "); } 311 | __first = false; 312 | __sb.Append("Msg: "); 313 | __sb.Append(Msg); 314 | } 315 | __sb.Append(")"); 316 | return __sb.ToString(); 317 | } 318 | 319 | } 320 | 321 | 322 | #if !SILVERLIGHT 323 | [Serializable] 324 | #endif 325 | public partial class SayHello_result : TBase 326 | { 327 | 328 | public SayHello_result() { 329 | } 330 | 331 | public void Read (TProtocol iprot) 332 | { 333 | iprot.IncrementRecursionDepth(); 334 | try 335 | { 336 | TField field; 337 | iprot.ReadStructBegin(); 338 | while (true) 339 | { 340 | field = iprot.ReadFieldBegin(); 341 | if (field.Type == TType.Stop) { 342 | break; 343 | } 344 | switch (field.ID) 345 | { 346 | default: 347 | TProtocolUtil.Skip(iprot, field.Type); 348 | break; 349 | } 350 | iprot.ReadFieldEnd(); 351 | } 352 | iprot.ReadStructEnd(); 353 | } 354 | finally 355 | { 356 | iprot.DecrementRecursionDepth(); 357 | } 358 | } 359 | 360 | public void Write(TProtocol oprot) { 361 | oprot.IncrementRecursionDepth(); 362 | try 363 | { 364 | TStruct struc = new TStruct("SayHello_result"); 365 | oprot.WriteStructBegin(struc); 366 | 367 | oprot.WriteFieldStop(); 368 | oprot.WriteStructEnd(); 369 | } 370 | finally 371 | { 372 | oprot.DecrementRecursionDepth(); 373 | } 374 | } 375 | 376 | public override string ToString() { 377 | StringBuilder __sb = new StringBuilder("SayHello_result("); 378 | __sb.Append(")"); 379 | return __sb.ToString(); 380 | } 381 | 382 | } 383 | 384 | } 385 | --------------------------------------------------------------------------------