David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1 | // |
| 2 | // TSimpleServer.cs |
| 3 | // |
| 4 | // Begin: Dec 3, 2007 |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 5 | // Authors: |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 6 | // 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 |
| 12 | using System; |
| 13 | using System.Collections.Generic; |
| 14 | using System.Text; |
| 15 | using Thrift.Transport; |
| 16 | using Thrift.Protocol; |
| 17 | |
| 18 | namespace Thrift.Server |
| 19 | { |
| 20 | /// <summary> |
| 21 | /// Simple single-threaded server for testing |
| 22 | /// </summary> |
David Reiss | 437c03b | 2008-04-02 22:10:06 +0000 | [diff] [blame] | 23 | public class TSimpleServer : TServer |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 24 | { |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 25 | private bool stop = false; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 26 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 27 | public TSimpleServer(TProcessor processor, |
| 28 | TServerTransport serverTransport) |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 29 | :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 Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 37 | { |
| 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 Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 48 | new TBinaryProtocol.Factory(), |
| 49 | DefaultLogDelegate) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 50 | { |
| 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 Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 62 | protocolFactory, |
| 63 | DefaultLogDelegate) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 64 | { |
| 65 | } |
| 66 | |
| 67 | public override void Serve() |
| 68 | { |
| 69 | try |
| 70 | { |
| 71 | serverTransport.Listen(); |
| 72 | } |
| 73 | catch (TTransportException ttx) |
| 74 | { |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 75 | logDelegate(ttx.ToString()); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 79 | while (!stop) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 80 | { |
| 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 Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 98 | catch (TTransportException ttx) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 99 | { |
| 100 | // Client died, just move on |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 101 | if (stop) |
| 102 | { |
| 103 | logDelegate("TSimpleServer was shutting down, caught " + ttx.GetType().Name); |
| 104 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 105 | } |
| 106 | catch (Exception x) |
| 107 | { |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 108 | logDelegate(x.ToString()); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | if (inputTransport != null) |
| 112 | { |
| 113 | inputTransport.Close(); |
| 114 | } |
| 115 | |
| 116 | if (outputTransport != null) |
| 117 | { |
| 118 | outputTransport.Close(); |
| 119 | } |
| 120 | } |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 121 | |
| 122 | if (stop) |
| 123 | { |
| 124 | try |
| 125 | { |
| 126 | serverTransport.Close(); |
| 127 | } |
| 128 | catch (TTransportException ttx) |
| 129 | { |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 130 | logDelegate("TServerTranport failed on close: " + ttx.Message); |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 131 | } |
| 132 | stop = false; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | public override void Stop() |
| 137 | { |
| 138 | stop = true; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 139 | serverTransport.Close(); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | } |