public override void Serve() { try { serverTransport.Listen(); } catch (TTransportException ttx) { logDelegate(ttx.ToString()); return; } //Fire the preServe server event when server is up but before any client connections if (serverEventHandler != null) { serverEventHandler.preServe(); } while (!stop) { TTransport client = null; TTransport inputTransport = null; TTransport outputTransport = null; TProtocol inputProtocol = null; TProtocol outputProtocol = null; Object connectionContext = null; try { using (client = serverTransport.Accept()) { if (client != null) { using (inputTransport = inputTransportFactory.GetTransport(client)) { using (outputTransport = outputTransportFactory.GetTransport(client)) { inputProtocol = inputProtocolFactory.GetProtocol(inputTransport); outputProtocol = outputProtocolFactory.GetProtocol(outputTransport); //Recover event handler (if any) and fire createContext server event when a client connects if (serverEventHandler != null) { connectionContext = serverEventHandler.createContext(inputProtocol, outputProtocol); } //Process client requests until client disconnects while (true) { if (!inputTransport.Peek()) { break; } //Fire processContext server event //N.B. This is the pattern implemented in C++ and the event fires provisionally. //That is to say it may be many minutes between the event firing and the client request //actually arriving or the client may hang up without ever makeing a request. if (serverEventHandler != null) { serverEventHandler.processContext(connectionContext, inputTransport); } //Process client request (blocks until transport is readable) if (!processor.Process(inputProtocol, outputProtocol)) { break; } } } } } } } catch (TTransportException) { //Usually a client disconnect, expected } catch (Exception x) { //Unexpected logDelegate(x.ToString()); } //Fire deleteContext server event after client disconnects if (serverEventHandler != null) { serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol); } } }
private void ClientWorker(object context) { TTransport client = (TTransport)context; TTransport inputTransport = null; TTransport outputTransport = null; TProtocol inputProtocol = null; TProtocol outputProtocol = null; object connectionContext = null; try { using (inputTransport = inputTransportFactory.GetTransport(client)) { using (outputTransport = outputTransportFactory.GetTransport(client)) { inputProtocol = inputProtocolFactory.GetProtocol(inputTransport); outputProtocol = outputProtocolFactory.GetProtocol(outputTransport); //Recover event handler (if any) and fire createContext server event when a client connects if (serverEventHandler != null) { connectionContext = serverEventHandler.createContext(inputProtocol, outputProtocol); } //Process client requests until client disconnects while (!stop) { if (!inputTransport.Peek()) { break; } //Fire processContext server event //N.B. This is the pattern implemented in C++ and the event fires provisionally. //That is to say it may be many minutes between the event firing and the client request //actually arriving or the client may hang up without ever makeing a request. if (serverEventHandler != null) { serverEventHandler.processContext(connectionContext, inputTransport); } //Process client request (blocks until transport is readable) if (!processor.Process(inputProtocol, outputProtocol)) { break; } } } } } catch (TTransportException) { //Usually a client disconnect, expected } catch (Exception x) { //Unexpected logDelegate("Error: " + x); } //Fire deleteContext server event after client disconnects if (serverEventHandler != null) { serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol); } lock (clientLock) { clientThreads.Remove(Thread.CurrentThread); Monitor.Pulse(clientLock); } }
/// <summary> /// Loops on processing a client forever /// threadContext will be a TTransport instance /// </summary> /// <param name="threadContext"></param> private void Execute(Object threadContext) { using (TTransport client = (TTransport)threadContext) { TProcessor processor = processorFactory.GetProcessor(client, this); TTransport inputTransport = null; TTransport outputTransport = null; TProtocol inputProtocol = null; TProtocol outputProtocol = null; Object connectionContext = null; try { try { inputTransport = inputTransportFactory.GetTransport(client); outputTransport = outputTransportFactory.GetTransport(client); inputProtocol = inputProtocolFactory.GetProtocol(inputTransport); outputProtocol = outputProtocolFactory.GetProtocol(outputTransport); //Recover event handler (if any) and fire createContext server event when a client connects if (serverEventHandler != null) { connectionContext = serverEventHandler.createContext(inputProtocol, outputProtocol); } //Process client requests until client disconnects while (!stop) { if (!inputTransport.Peek()) { break; } //Fire processContext server event //N.B. This is the pattern implemented in C++ and the event fires provisionally. //That is to say it may be many minutes between the event firing and the client request //actually arriving or the client may hang up without ever makeing a request. if (serverEventHandler != null) { serverEventHandler.processContext(connectionContext, inputTransport); } //Process client request (blocks until transport is readable) if (!processor.Process(inputProtocol, outputProtocol)) { break; } } } catch (TTransportException) { //Usually a client disconnect, expected } catch (Exception x) { //Unexpected logDelegate("Error: " + x); } //Fire deleteContext server event after client disconnects if (serverEventHandler != null) { serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol); } } finally { //Close transports if (inputTransport != null) { inputTransport.Close(); } if (outputTransport != null) { outputTransport.Close(); } // disposable stuff should be disposed if (inputProtocol != null) { inputProtocol.Dispose(); } if (outputProtocol != null) { outputProtocol.Dispose(); } if (inputTransport != null) { inputTransport.Dispose(); } if (outputTransport != null) { outputTransport.Dispose(); } } } }