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> |
| 23 | class TSimpleServer : TServer |
| 24 | { |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame^] | 25 | private bool stop = false; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 26 | 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 Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame^] | 69 | while (!stop) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 70 | { |
| 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 Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame^] | 107 | |
| 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 Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | } |