Kevin Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
Todd Lipcon | 53ae9f3 | 2009-12-07 00:42:38 +0000 | [diff] [blame] | 18 | * |
| 19 | * Contains some contributions under the Thrift Software License. |
| 20 | * Please see doc/old-thrift-license.txt in the Thrift distribution for |
| 21 | * details. |
Kevin Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 24 | using System; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 25 | using Thrift.Transport; |
| 26 | using Thrift.Protocol; |
| 27 | |
| 28 | namespace Thrift.Server |
| 29 | { |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 30 | /// <summary> |
| 31 | /// Simple single-threaded server for testing |
| 32 | /// </summary> |
| 33 | public class TSimpleServer : TServer |
| 34 | { |
| 35 | private bool stop = false; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 36 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 37 | public TSimpleServer(TProcessor processor, |
| 38 | TServerTransport serverTransport) |
| 39 | : base(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DefaultLogDelegate) |
| 40 | { |
| 41 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 42 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 43 | public TSimpleServer(TProcessor processor, |
| 44 | TServerTransport serverTransport, |
| 45 | LogDelegate logDel) |
| 46 | : base(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), logDel) |
| 47 | { |
| 48 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 49 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 50 | public TSimpleServer(TProcessor processor, |
| 51 | TServerTransport serverTransport, |
| 52 | TTransportFactory transportFactory) |
| 53 | : base(processor, |
| 54 | serverTransport, |
| 55 | transportFactory, |
| 56 | transportFactory, |
| 57 | new TBinaryProtocol.Factory(), |
| 58 | new TBinaryProtocol.Factory(), |
| 59 | DefaultLogDelegate) |
| 60 | { |
| 61 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 62 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 63 | public TSimpleServer(TProcessor processor, |
| 64 | TServerTransport serverTransport, |
| 65 | TTransportFactory transportFactory, |
| 66 | TProtocolFactory protocolFactory) |
| 67 | : base(processor, |
| 68 | serverTransport, |
| 69 | transportFactory, |
| 70 | transportFactory, |
| 71 | protocolFactory, |
| 72 | protocolFactory, |
| 73 | DefaultLogDelegate) |
| 74 | { |
| 75 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 76 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 77 | public override void Serve() |
| 78 | { |
| 79 | try |
| 80 | { |
| 81 | serverTransport.Listen(); |
| 82 | } |
| 83 | catch (TTransportException ttx) |
| 84 | { |
| 85 | logDelegate(ttx.ToString()); |
| 86 | return; |
| 87 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 88 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 89 | //Fire the preServe server event when server is up but before any client connections |
| 90 | if (serverEventHandler != null) |
| 91 | serverEventHandler.preServe(); |
| 92 | |
| 93 | while (!stop) |
| 94 | { |
| 95 | TTransport client = null; |
| 96 | TTransport inputTransport = null; |
| 97 | TTransport outputTransport = null; |
| 98 | TProtocol inputProtocol = null; |
| 99 | TProtocol outputProtocol = null; |
| 100 | Object connectionContext = null; |
| 101 | try |
| 102 | { |
| 103 | using (client = serverTransport.Accept()) |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 104 | { |
| 105 | if (client != null) |
| 106 | { |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 107 | using (inputTransport = inputTransportFactory.GetTransport(client)) |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 108 | { |
| 109 | using (outputTransport = outputTransportFactory.GetTransport(client)) |
| 110 | { |
| 111 | inputProtocol = inputProtocolFactory.GetProtocol(inputTransport); |
| 112 | outputProtocol = outputProtocolFactory.GetProtocol(outputTransport); |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 113 | |
| 114 | //Recover event handler (if any) and fire createContext server event when a client connects |
| 115 | if (serverEventHandler != null) |
| 116 | connectionContext = serverEventHandler.createContext(inputProtocol, outputProtocol); |
| 117 | |
| 118 | //Process client requests until client disconnects |
| 119 | while (true) |
| 120 | { |
Jens Geyer | eb8e5ad | 2014-09-29 21:50:15 +0200 | [diff] [blame] | 121 | if (!inputTransport.Peek()) |
| 122 | break; |
| 123 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 124 | //Fire processContext server event |
| 125 | //N.B. This is the pattern implemented in C++ and the event fires provisionally. |
| 126 | //That is to say it may be many minutes between the event firing and the client request |
| 127 | //actually arriving or the client may hang up without ever makeing a request. |
| 128 | if (serverEventHandler != null) |
| 129 | serverEventHandler.processContext(connectionContext, inputTransport); |
| 130 | //Process client request (blocks until transport is readable) |
| 131 | if (!processor.Process(inputProtocol, outputProtocol)) |
| 132 | break; |
| 133 | } |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 139 | catch (TTransportException) |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 140 | { |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 141 | //Usually a client disconnect, expected |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 142 | } |
| 143 | catch (Exception x) |
| 144 | { |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 145 | //Unexpected |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 146 | logDelegate(x.ToString()); |
| 147 | } |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 148 | |
| 149 | //Fire deleteContext server event after client disconnects |
| 150 | if (serverEventHandler != null) |
| 151 | serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol); |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 152 | } |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 153 | } |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 154 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 155 | public override void Stop() |
| 156 | { |
| 157 | stop = true; |
| 158 | serverTransport.Close(); |
| 159 | } |
| 160 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 161 | } |