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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Contains some contributions under the Thrift Software License.
20 * Please see doc/old-thrift-license.txt in the Thrift distribution for
25 using System.Net.Sockets;
28 namespace Thrift.Transport
30 public class TServerSocket : TServerTransport
33 /// Underlying server with socket.
35 private TcpListener server = null;
38 /// Port to listen on.
43 /// Timeout for client sockets from accept.
45 private int clientTimeout = 0;
48 /// Whether or not to wrap new TSocket connections in buffers.
50 private bool useBufferedSockets = false;
53 /// Creates a server socket from underlying socket object.
55 public TServerSocket(TcpListener listener)
61 /// Creates a server socket from underlying socket object.
63 public TServerSocket(TcpListener listener, int clientTimeout)
65 this.server = listener;
66 this.clientTimeout = clientTimeout;
70 /// Creates just a port listening server socket.
72 public TServerSocket(int port)
78 /// Creates just a port listening server socket.
80 public TServerSocket(int port, int clientTimeout)
81 : this(port, clientTimeout, false)
85 public TServerSocket(int port, int clientTimeout, bool useBufferedSockets)
88 this.clientTimeout = clientTimeout;
89 this.useBufferedSockets = useBufferedSockets;
93 this.server = TSocketVersionizer.CreateTcpListener(this.port);
94 this.server.Server.NoDelay = true;
99 throw new TTransportException("Could not create ServerSocket on port " + this.port + ".", ex);
103 public override void Listen()
105 // Make sure not to block on accept
112 catch (SocketException sx)
114 throw new TTransportException("Could not accept on listening socket: " + sx.Message, sx);
119 protected override TTransport AcceptImpl()
123 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket.");
127 TSocket result2 = null;
128 TcpClient result = server.AcceptTcpClient();
131 result2 = new TSocket(result);
132 result2.Timeout = clientTimeout;
133 if (useBufferedSockets)
135 TBufferedTransport result3 = new TBufferedTransport(result2);
143 catch (System.Exception)
145 // If a TSocket was successfully created, then let
146 // it do proper cleanup of the TcpClient object.
149 else // Otherwise, clean it up ourselves.
150 ((IDisposable)result).Dispose();
156 throw new TTransportException(ex.ToString(), ex);
160 public override void Close()
170 throw new TTransportException("WARNING: Could not close server socket: " + ex, ex);