blob: 95f39623d5c5c790acf9eec915025456025183b1 [file] [log] [blame]
David Reiss7f42bcf2008-01-11 20:59:12 +00001//
2// TSimpleServer.cs
3//
4// Begin: Dec 3, 2007
David Reiss0c90f6f2008-02-06 22:18:40 +00005// Authors:
David Reiss7f42bcf2008-01-11 20:59:12 +00006// Will Palmeri <wpalmeri@imeem.com>
7//
8// Distributed under the Thrift Software License
9//
10// See accompanying file LICENSE or visit the Thrift site at:
11// http://developers.facebook.com/thrift/using
12using System;
13using System.Collections.Generic;
14using System.Text;
15using Thrift.Transport;
16using Thrift.Protocol;
17
18namespace Thrift.Server
19{
20 /// <summary>
21 /// Simple single-threaded server for testing
22 /// </summary>
David Reiss437c03b2008-04-02 22:10:06 +000023 public class TSimpleServer : TServer
David Reiss7f42bcf2008-01-11 20:59:12 +000024 {
David Reissdc815f52008-03-02 00:58:04 +000025 private bool stop = false;
David Reiss63191332009-01-06 19:49:22 +000026
David Reiss7f42bcf2008-01-11 20:59:12 +000027 public TSimpleServer(TProcessor processor,
28 TServerTransport serverTransport)
David Reiss63191332009-01-06 19:49:22 +000029 :base(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DefaultLogDelegate)
30 {
31 }
32
33 public TSimpleServer(TProcessor processor,
34 TServerTransport serverTransport,
35 LogDelegate logDel)
36 : base(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), logDel)
David Reiss7f42bcf2008-01-11 20:59:12 +000037 {
38 }
39
40 public TSimpleServer(TProcessor processor,
41 TServerTransport serverTransport,
42 TTransportFactory transportFactory)
43 :base(processor,
44 serverTransport,
45 transportFactory,
46 transportFactory,
47 new TBinaryProtocol.Factory(),
David Reiss63191332009-01-06 19:49:22 +000048 new TBinaryProtocol.Factory(),
49 DefaultLogDelegate)
David Reiss7f42bcf2008-01-11 20:59:12 +000050 {
51 }
52
53 public TSimpleServer(TProcessor processor,
54 TServerTransport serverTransport,
55 TTransportFactory transportFactory,
56 TProtocolFactory protocolFactory)
57 :base(processor,
58 serverTransport,
59 transportFactory,
60 transportFactory,
61 protocolFactory,
David Reiss63191332009-01-06 19:49:22 +000062 protocolFactory,
63 DefaultLogDelegate)
David Reiss7f42bcf2008-01-11 20:59:12 +000064 {
65 }
66
67 public override void Serve()
68 {
69 try
70 {
71 serverTransport.Listen();
72 }
73 catch (TTransportException ttx)
74 {
David Reiss63191332009-01-06 19:49:22 +000075 logDelegate(ttx.ToString());
David Reiss7f42bcf2008-01-11 20:59:12 +000076 return;
77 }
78
David Reissdc815f52008-03-02 00:58:04 +000079 while (!stop)
David Reiss7f42bcf2008-01-11 20:59:12 +000080 {
81 TTransport client = null;
82 TTransport inputTransport = null;
83 TTransport outputTransport = null;
84 TProtocol inputProtocol = null;
85 TProtocol outputProtocol = null;
86 try
87 {
88 client = serverTransport.Accept();
89 if (client != null)
90 {
91 inputTransport = inputTransportFactory.GetTransport(client);
92 outputTransport = outputTransportFactory.GetTransport(client);
93 inputProtocol = inputProtocolFactory.GetProtocol(inputTransport);
94 outputProtocol = outputProtocolFactory.GetProtocol(outputTransport);
95 while (processor.Process(inputProtocol, outputProtocol)) { }
96 }
97 }
David Reiss63191332009-01-06 19:49:22 +000098 catch (TTransportException ttx)
David Reiss7f42bcf2008-01-11 20:59:12 +000099 {
100 // Client died, just move on
David Reiss63191332009-01-06 19:49:22 +0000101 if (stop)
102 {
103 logDelegate("TSimpleServer was shutting down, caught " + ttx.GetType().Name);
104 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000105 }
106 catch (Exception x)
107 {
David Reiss63191332009-01-06 19:49:22 +0000108 logDelegate(x.ToString());
David Reiss7f42bcf2008-01-11 20:59:12 +0000109 }
110
111 if (inputTransport != null)
112 {
113 inputTransport.Close();
114 }
115
116 if (outputTransport != null)
117 {
118 outputTransport.Close();
119 }
120 }
David Reissdc815f52008-03-02 00:58:04 +0000121
122 if (stop)
123 {
124 try
125 {
126 serverTransport.Close();
127 }
128 catch (TTransportException ttx)
129 {
David Reiss63191332009-01-06 19:49:22 +0000130 logDelegate("TServerTranport failed on close: " + ttx.Message);
David Reissdc815f52008-03-02 00:58:04 +0000131 }
132 stop = false;
133 }
134 }
135
136 public override void Stop()
137 {
138 stop = true;
David Reiss63191332009-01-06 19:49:22 +0000139 serverTransport.Close();
David Reiss7f42bcf2008-01-11 20:59:12 +0000140 }
141 }
142}