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 | { |
| 25 | public TSimpleServer(TProcessor processor, |
| 26 | TServerTransport serverTransport) |
| 27 | :base(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory()) |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | public TSimpleServer(TProcessor processor, |
| 32 | TServerTransport serverTransport, |
| 33 | TTransportFactory transportFactory) |
| 34 | :base(processor, |
| 35 | serverTransport, |
| 36 | transportFactory, |
| 37 | transportFactory, |
| 38 | new TBinaryProtocol.Factory(), |
| 39 | new TBinaryProtocol.Factory()) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | public TSimpleServer(TProcessor processor, |
| 44 | TServerTransport serverTransport, |
| 45 | TTransportFactory transportFactory, |
| 46 | TProtocolFactory protocolFactory) |
| 47 | :base(processor, |
| 48 | serverTransport, |
| 49 | transportFactory, |
| 50 | transportFactory, |
| 51 | protocolFactory, |
| 52 | protocolFactory) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | public override void Serve() |
| 57 | { |
| 58 | try |
| 59 | { |
| 60 | serverTransport.Listen(); |
| 61 | } |
| 62 | catch (TTransportException ttx) |
| 63 | { |
| 64 | Console.Error.WriteLine(ttx); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | while (true) |
| 69 | { |
| 70 | TTransport client = null; |
| 71 | TTransport inputTransport = null; |
| 72 | TTransport outputTransport = null; |
| 73 | TProtocol inputProtocol = null; |
| 74 | TProtocol outputProtocol = null; |
| 75 | try |
| 76 | { |
| 77 | client = serverTransport.Accept(); |
| 78 | if (client != null) |
| 79 | { |
| 80 | inputTransport = inputTransportFactory.GetTransport(client); |
| 81 | outputTransport = outputTransportFactory.GetTransport(client); |
| 82 | inputProtocol = inputProtocolFactory.GetProtocol(inputTransport); |
| 83 | outputProtocol = outputProtocolFactory.GetProtocol(outputTransport); |
| 84 | while (processor.Process(inputProtocol, outputProtocol)) { } |
| 85 | } |
| 86 | } |
| 87 | catch (TTransportException ttx) |
| 88 | { |
| 89 | // Client died, just move on |
| 90 | } |
| 91 | catch (Exception x) |
| 92 | { |
| 93 | Console.Error.WriteLine(x); |
| 94 | } |
| 95 | |
| 96 | if (inputTransport != null) |
| 97 | { |
| 98 | inputTransport.Close(); |
| 99 | } |
| 100 | |
| 101 | if (outputTransport != null) |
| 102 | { |
| 103 | outputTransport.Close(); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |