blob: ff59fc3a7340e163925a6e486dc2be289a313c48 [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>
23 class TSimpleServer : TServer
24 {
David Reissdc815f52008-03-02 00:58:04 +000025 private bool stop = false;
David Reiss7f42bcf2008-01-11 20:59:12 +000026 public TSimpleServer(TProcessor processor,
27 TServerTransport serverTransport)
28 :base(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory())
29 {
30 }
31
32 public TSimpleServer(TProcessor processor,
33 TServerTransport serverTransport,
34 TTransportFactory transportFactory)
35 :base(processor,
36 serverTransport,
37 transportFactory,
38 transportFactory,
39 new TBinaryProtocol.Factory(),
40 new TBinaryProtocol.Factory())
41 {
42 }
43
44 public TSimpleServer(TProcessor processor,
45 TServerTransport serverTransport,
46 TTransportFactory transportFactory,
47 TProtocolFactory protocolFactory)
48 :base(processor,
49 serverTransport,
50 transportFactory,
51 transportFactory,
52 protocolFactory,
53 protocolFactory)
54 {
55 }
56
57 public override void Serve()
58 {
59 try
60 {
61 serverTransport.Listen();
62 }
63 catch (TTransportException ttx)
64 {
65 Console.Error.WriteLine(ttx);
66 return;
67 }
68
David Reissdc815f52008-03-02 00:58:04 +000069 while (!stop)
David Reiss7f42bcf2008-01-11 20:59:12 +000070 {
71 TTransport client = null;
72 TTransport inputTransport = null;
73 TTransport outputTransport = null;
74 TProtocol inputProtocol = null;
75 TProtocol outputProtocol = null;
76 try
77 {
78 client = serverTransport.Accept();
79 if (client != null)
80 {
81 inputTransport = inputTransportFactory.GetTransport(client);
82 outputTransport = outputTransportFactory.GetTransport(client);
83 inputProtocol = inputProtocolFactory.GetProtocol(inputTransport);
84 outputProtocol = outputProtocolFactory.GetProtocol(outputTransport);
85 while (processor.Process(inputProtocol, outputProtocol)) { }
86 }
87 }
88 catch (TTransportException ttx)
89 {
90 // Client died, just move on
91 }
92 catch (Exception x)
93 {
94 Console.Error.WriteLine(x);
95 }
96
97 if (inputTransport != null)
98 {
99 inputTransport.Close();
100 }
101
102 if (outputTransport != null)
103 {
104 outputTransport.Close();
105 }
106 }
David Reissdc815f52008-03-02 00:58:04 +0000107
108 if (stop)
109 {
110 try
111 {
112 serverTransport.Close();
113 }
114 catch (TTransportException ttx)
115 {
116 Console.Error.WriteLine("TServerTrasnport failed on close: " + ttx.Message);
117 }
118 stop = false;
119 }
120 }
121
122 public override void Stop()
123 {
124 stop = true;
David Reiss7f42bcf2008-01-11 20:59:12 +0000125 }
126 }
127}