blob: 912b4a34bd667f67c4c3224358de981acd7af3db [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;
David Reiss7f42bcf2008-01-11 20:59:12 +000013using Thrift.Transport;
14using Thrift.Protocol;
15
16namespace Thrift.Server
17{
18 /// <summary>
19 /// Simple single-threaded server for testing
20 /// </summary>
David Reiss437c03b2008-04-02 22:10:06 +000021 public class TSimpleServer : TServer
David Reiss7f42bcf2008-01-11 20:59:12 +000022 {
David Reissdc815f52008-03-02 00:58:04 +000023 private bool stop = false;
David Reiss63191332009-01-06 19:49:22 +000024
David Reiss7f42bcf2008-01-11 20:59:12 +000025 public TSimpleServer(TProcessor processor,
26 TServerTransport serverTransport)
David Reiss63191332009-01-06 19:49:22 +000027 :base(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DefaultLogDelegate)
28 {
29 }
30
31 public TSimpleServer(TProcessor processor,
32 TServerTransport serverTransport,
33 LogDelegate logDel)
34 : base(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), logDel)
David Reiss7f42bcf2008-01-11 20:59:12 +000035 {
36 }
37
38 public TSimpleServer(TProcessor processor,
39 TServerTransport serverTransport,
40 TTransportFactory transportFactory)
41 :base(processor,
42 serverTransport,
43 transportFactory,
44 transportFactory,
45 new TBinaryProtocol.Factory(),
David Reiss63191332009-01-06 19:49:22 +000046 new TBinaryProtocol.Factory(),
47 DefaultLogDelegate)
David Reiss7f42bcf2008-01-11 20:59:12 +000048 {
49 }
50
51 public TSimpleServer(TProcessor processor,
52 TServerTransport serverTransport,
53 TTransportFactory transportFactory,
54 TProtocolFactory protocolFactory)
55 :base(processor,
56 serverTransport,
57 transportFactory,
58 transportFactory,
59 protocolFactory,
David Reiss63191332009-01-06 19:49:22 +000060 protocolFactory,
61 DefaultLogDelegate)
David Reiss7f42bcf2008-01-11 20:59:12 +000062 {
63 }
64
65 public override void Serve()
66 {
67 try
68 {
69 serverTransport.Listen();
70 }
71 catch (TTransportException ttx)
72 {
David Reiss63191332009-01-06 19:49:22 +000073 logDelegate(ttx.ToString());
David Reiss7f42bcf2008-01-11 20:59:12 +000074 return;
75 }
76
David Reissdc815f52008-03-02 00:58:04 +000077 while (!stop)
David Reiss7f42bcf2008-01-11 20:59:12 +000078 {
79 TTransport client = null;
80 TTransport inputTransport = null;
81 TTransport outputTransport = null;
82 TProtocol inputProtocol = null;
83 TProtocol outputProtocol = null;
84 try
85 {
86 client = serverTransport.Accept();
87 if (client != null)
88 {
89 inputTransport = inputTransportFactory.GetTransport(client);
90 outputTransport = outputTransportFactory.GetTransport(client);
91 inputProtocol = inputProtocolFactory.GetProtocol(inputTransport);
92 outputProtocol = outputProtocolFactory.GetProtocol(outputTransport);
93 while (processor.Process(inputProtocol, outputProtocol)) { }
94 }
95 }
David Reiss63191332009-01-06 19:49:22 +000096 catch (TTransportException ttx)
David Reiss7f42bcf2008-01-11 20:59:12 +000097 {
98 // Client died, just move on
David Reiss63191332009-01-06 19:49:22 +000099 if (stop)
100 {
101 logDelegate("TSimpleServer was shutting down, caught " + ttx.GetType().Name);
102 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000103 }
104 catch (Exception x)
105 {
David Reiss63191332009-01-06 19:49:22 +0000106 logDelegate(x.ToString());
David Reiss7f42bcf2008-01-11 20:59:12 +0000107 }
108
109 if (inputTransport != null)
110 {
111 inputTransport.Close();
112 }
113
114 if (outputTransport != null)
115 {
116 outputTransport.Close();
117 }
118 }
David Reissdc815f52008-03-02 00:58:04 +0000119
120 if (stop)
121 {
122 try
123 {
124 serverTransport.Close();
125 }
126 catch (TTransportException ttx)
127 {
David Reiss63191332009-01-06 19:49:22 +0000128 logDelegate("TServerTranport failed on close: " + ttx.Message);
David Reissdc815f52008-03-02 00:58:04 +0000129 }
130 stop = false;
131 }
132 }
133
134 public override void Stop()
135 {
136 stop = true;
David Reiss63191332009-01-06 19:49:22 +0000137 serverTransport.Close();
David Reiss7f42bcf2008-01-11 20:59:12 +0000138 }
139 }
140}