TProtocol outputProtocol = null;
try
{
- client = serverTransport.Accept();
- if (client != null)
- {
- inputTransport = inputTransportFactory.GetTransport(client);
- outputTransport = outputTransportFactory.GetTransport(client);
- inputProtocol = inputProtocolFactory.GetProtocol(inputTransport);
- outputProtocol = outputProtocolFactory.GetProtocol(outputTransport);
- while (processor.Process(inputProtocol, outputProtocol)) { }
- }
- }
- catch (TTransportException ttx)
- {
- // Client died, just move on
- if (stop)
- {
- logDelegate("TSimpleServer was shutting down, caught " + ttx.GetType().Name);
- }
- }
- catch (Exception x)
- {
- logDelegate(x.ToString());
- }
-
- if (inputTransport != null)
- {
- inputTransport.Close();
- }
-
- if (outputTransport != null)
- {
- outputTransport.Close();
- }
- }
+ 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);
+ while (processor.Process(inputProtocol, outputProtocol)) { }
+ }
+ }
+ }
+ }
+ }
+ catch (TTransportException ttx)
+ {
+ // Client died, just move on
+ if (stop)
+ {
+ logDelegate("TSimpleServer was shutting down, caught " + ttx.GetType().Name);
+ }
+ }
+ catch (Exception x)
+ {
+ logDelegate(x.ToString());
+ }
+ }
if (stop)
{
:base(processor, serverTransport, inputTransportFactory, outputTransportFactory,
inputProtocolFactory, outputProtocolFactory, logDel)
{
- if (!ThreadPool.SetMinThreads(minThreadPoolThreads, minThreadPoolThreads))
- {
- throw new Exception("Error: could not SetMinThreads in ThreadPool");
- }
- if (!ThreadPool.SetMaxThreads(maxThreadPoolThreads, maxThreadPoolThreads))
- {
- throw new Exception("Error: could not SetMaxThreads in ThreadPool");
+ lock (typeof(TThreadPoolServer))
+ {
+ if (!ThreadPool.SetMinThreads(minThreadPoolThreads, minThreadPoolThreads))
+ {
+ throw new Exception("Error: could not SetMinThreads in ThreadPool");
+ }
+ if (!ThreadPool.SetMaxThreads(maxThreadPoolThreads, maxThreadPoolThreads))
+ {
+ throw new Exception("Error: could not SetMaxThreads in ThreadPool");
+ }
}
}
+
/// <summary>
/// Use new ThreadPool thread for each new client connection
/// </summary>
TProtocol outputProtocol = null;
try
{
- inputTransport = inputTransportFactory.GetTransport(client);
- outputTransport = outputTransportFactory.GetTransport(client);
- inputProtocol = inputProtocolFactory.GetProtocol(inputTransport);
- outputProtocol = outputProtocolFactory.GetProtocol(outputTransport);
- while (processor.Process(inputProtocol, outputProtocol))
- {
- //keep processing requests until client disconnects
- }
+ using (inputTransport = inputTransportFactory.GetTransport(client))
+ {
+ using (outputTransport = outputTransportFactory.GetTransport(client))
+ {
+ inputProtocol = inputProtocolFactory.GetProtocol(inputTransport);
+ outputProtocol = outputProtocolFactory.GetProtocol(outputTransport);
+ while (processor.Process(inputProtocol, outputProtocol))
+ {
+ //keep processing requests until client disconnects
+ }
+ }
+ }
}
catch (TTransportException)
{
logDelegate("Error: " + x);
}
- if (inputTransport != null)
- {
- inputTransport.Close();
- }
- if (outputTransport != null)
- {
- outputTransport.Close();
- }
-
lock (clientLock)
{
clientThreads.Remove(Thread.CurrentThread);
namespace Thrift.Transport
{
- public class TBufferedTransport : TTransport
+ public class TBufferedTransport : TTransport, IDisposable
{
private BufferedStream inputBuffer;
private BufferedStream outputBuffer;
{
outputBuffer.Flush();
}
- }
+
+ #region " IDisposable Support "
+ private bool _IsDisposed;
+
+ // IDisposable
+ protected override void Dispose(bool disposing)
+ {
+ if (!_IsDisposed)
+ {
+ if (disposing)
+ {
+ if (inputBuffer != null)
+ inputBuffer.Dispose();
+ if (outputBuffer != null)
+ outputBuffer.Dispose();
+ }
+ }
+ _IsDisposed = true;
+ }
+ #endregion
+ }
}
* specific language governing permissions and limitations
* under the License.
*/
-
+using System;
using System.IO;
namespace Thrift.Transport
{
- public class TFramedTransport : TTransport
+ public class TFramedTransport : TTransport, IDisposable
{
protected TTransport transport = null;
protected MemoryStream writeBuffer;
((i32rd[2] & 0xff) << 8) |
((i32rd[3] & 0xff));
+
byte[] buff = new byte[size];
transport.ReadAll(buff, 0, size);
readBuffer = new MemoryStream(buff);
// Reserve space for message header to be put right before sending it out
writeBuffer.Write ( header_dummy, 0, header_size );
}
- }
+ #region " IDisposable Support "
+ private bool _IsDisposed;
+
+ // IDisposable
+ protected override void Dispose(bool disposing)
+ {
+ if (!_IsDisposed)
+ {
+ if (disposing)
+ {
+ if (readBuffer != null)
+ readBuffer.Dispose();
+ }
+ }
+ _IsDisposed = true;
+ }
+ #endregion
+ }
}
namespace Thrift.Transport
{
- public class THttpClient : TTransport
+ public class THttpClient : TTransport, IDisposable
{
private readonly Uri uri;
private Stream inputStream;
byte[] data = outputStream.ToArray();
connection.ContentLength = data.Length;
- Stream requestStream = connection.GetRequestStream();
- requestStream.Write(data, 0, data.Length);
- inputStream = connection.GetResponse().GetResponseStream();
+ using (Stream requestStream = connection.GetRequestStream())
+ {
+ requestStream.Write(data, 0, data.Length);
+ inputStream = connection.GetResponse().GetResponseStream();
+ }
}
catch (IOException iox)
{
}
}
#endif
- private HttpWebRequest CreateRequest()
+ private HttpWebRequest CreateRequest()
{
HttpWebRequest connection = (HttpWebRequest)WebRequest.Create(uri);
}
#endif
- }
+#region " IDisposable Support "
+ private bool _IsDisposed;
+
+ // IDisposable
+ protected override void Dispose(bool disposing)
+ {
+ if (!_IsDisposed)
+ {
+ if (disposing)
+ {
+ if (inputStream != null)
+ inputStream.Dispose();
+ if (outputStream != null)
+ outputStream.Dispose();
+ }
+ }
+ _IsDisposed = true;
+ }
+#endregion
+ }
}
{
TcpClient result = server.AcceptTcpClient();
TSocket result2 = new TSocket(result);
- result2.Timeout = clientTimeout;
- if (useBufferedSockets)
- {
- TBufferedTransport result3 = new TBufferedTransport(result2);
- return result3;
- }
- else
- {
- return result2;
- }
- }
- catch (Exception ex)
- {
- throw new TTransportException(ex.ToString());
- }
- }
+ try
+ {
+ result2 = new TSocket(result);
+ result2.Timeout = clientTimeout;
+ if (useBufferedSockets)
+ {
+ TBufferedTransport result3 = new TBufferedTransport(result2);
+ return result3;
+ }
+ else
+ {
+ return result2;
+ }
+ }
+ catch (System.Exception)
+ {
+ // If a TSocket was successfully created, then let
+ // it do proper cleanup of the TcpClient object.
+ if (result2 != null)
+ result2.Dispose();
+ else // Otherwise, clean it up ourselves.
+ ((IDisposable)result).Dispose();
+ throw;
+ }
+ }
+ catch (Exception ex)
+ {
+ throw new TTransportException(ex.ToString());
+ }
+ }
public override void Close()
{
client = null;
}
}
- }
+
+ #region " IDisposable Support "
+ private bool _IsDisposed;
+
+ // IDisposable
+ protected override void Dispose(bool disposing)
+ {
+ if (!_IsDisposed)
+ {
+ if (disposing)
+ {
+ if (client != null)
+ ((IDisposable)client).Dispose();
+ base.Dispose(disposing);
+ }
+ }
+ _IsDisposed = true;
+ }
+ #endregion
+ }
}
outputStream.Flush();
}
- }
+
+
+ #region " IDisposable Support "
+ private bool _IsDisposed;
+
+ // IDisposable
+ protected override void Dispose(bool disposing)
+ {
+ if (!_IsDisposed)
+ {
+ if (disposing)
+ {
+ if (InputStream != null)
+ InputStream.Dispose();
+ if (OutputStream != null)
+ OutputStream.Dispose();
+ }
+ }
+ _IsDisposed = true;
+ }
+ #endregion
+ }
}
namespace Thrift.Transport
{
- public abstract class TTransport
+ public abstract class TTransport : IDisposable
{
public abstract bool IsOpen
{
public virtual void EndFlush(IAsyncResult asyncResult)
{
}
- }
+
+ #region " IDisposable Support "
+ // IDisposable
+ protected abstract void Dispose(bool disposing);
+
+ public void Dispose()
+ {
+ // Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+ #endregion
+ }
}