]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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. | |
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. | |
22 | */ | |
23 | ||
24 | using System; | |
25 | using Thrift.Transport; | |
26 | using Thrift.Protocol; | |
27 | ||
28 | namespace Thrift.Server | |
29 | { | |
30 | /// <summary> | |
31 | /// Simple single-threaded server for testing. | |
32 | /// </summary> | |
33 | public class TSimpleServer : TServer | |
34 | { | |
35 | private bool stop = false; | |
36 | ||
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 | } | |
42 | ||
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 | } | |
49 | ||
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 | } | |
62 | ||
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 | } | |
76 | ||
77 | public TSimpleServer(TProcessorFactory processorFactory, | |
78 | TServerTransport serverTransport, | |
79 | TTransportFactory transportFactory, | |
80 | TProtocolFactory protocolFactory) | |
81 | : base(processorFactory, | |
82 | serverTransport, | |
83 | transportFactory, | |
84 | transportFactory, | |
85 | protocolFactory, | |
86 | protocolFactory, | |
87 | DefaultLogDelegate) | |
88 | { | |
89 | } | |
90 | ||
91 | public override void Serve() | |
92 | { | |
93 | try | |
94 | { | |
95 | serverTransport.Listen(); | |
96 | } | |
97 | catch (TTransportException ttx) | |
98 | { | |
99 | logDelegate(ttx.ToString()); | |
100 | return; | |
101 | } | |
102 | ||
103 | //Fire the preServe server event when server is up but before any client connections | |
104 | if (serverEventHandler != null) | |
105 | serverEventHandler.preServe(); | |
106 | ||
107 | while (!stop) | |
108 | { | |
109 | TProcessor processor = null; | |
110 | TTransport client = null; | |
111 | TTransport inputTransport = null; | |
112 | TTransport outputTransport = null; | |
113 | TProtocol inputProtocol = null; | |
114 | TProtocol outputProtocol = null; | |
115 | object connectionContext = null; | |
116 | try | |
117 | { | |
118 | using (client = serverTransport.Accept()) | |
119 | { | |
120 | processor = processorFactory.GetProcessor(client); | |
121 | if (client != null) | |
122 | { | |
123 | using (inputTransport = inputTransportFactory.GetTransport(client)) | |
124 | { | |
125 | using (outputTransport = outputTransportFactory.GetTransport(client)) | |
126 | { | |
127 | inputProtocol = inputProtocolFactory.GetProtocol(inputTransport); | |
128 | outputProtocol = outputProtocolFactory.GetProtocol(outputTransport); | |
129 | ||
130 | //Recover event handler (if any) and fire createContext server event when a client connects | |
131 | if (serverEventHandler != null) | |
132 | connectionContext = serverEventHandler.createContext(inputProtocol, outputProtocol); | |
133 | ||
134 | //Process client requests until client disconnects | |
135 | while (!stop) | |
136 | { | |
137 | if (!inputTransport.Peek()) | |
138 | break; | |
139 | ||
140 | //Fire processContext server event | |
141 | //N.B. This is the pattern implemented in C++ and the event fires provisionally. | |
142 | //That is to say it may be many minutes between the event firing and the client request | |
143 | //actually arriving or the client may hang up without ever makeing a request. | |
144 | if (serverEventHandler != null) | |
145 | serverEventHandler.processContext(connectionContext, inputTransport); | |
146 | //Process client request (blocks until transport is readable) | |
147 | if (!processor.Process(inputProtocol, outputProtocol)) | |
148 | break; | |
149 | } | |
150 | } | |
151 | } | |
152 | } | |
153 | } | |
154 | } | |
155 | catch (TTransportException ttx) | |
156 | { | |
157 | if (!stop || ttx.Type != TTransportException.ExceptionType.Interrupted) | |
158 | { | |
159 | logDelegate(ttx.ToString()); | |
160 | } | |
161 | } | |
162 | catch (Exception x) | |
163 | { | |
164 | //Unexpected | |
165 | logDelegate(x.ToString()); | |
166 | } | |
167 | ||
168 | //Fire deleteContext server event after client disconnects | |
169 | if (serverEventHandler != null) | |
170 | serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol); | |
171 | } | |
172 | } | |
173 | ||
174 | public override void Stop() | |
175 | { | |
176 | stop = true; | |
177 | serverTransport.Close(); | |
178 | } | |
179 | } | |
180 | } |