blob: 218291afb2fd9b37678b32c385886f12035ba59f [file] [log] [blame]
Kevin Clarkab4460d2009-03-20 02:28:41 +00001/**
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 Lipcon53ae9f32009-12-07 00:42:38 +000018 *
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 Clarkab4460d2009-03-20 02:28:41 +000022 */
David Reiss7f42bcf2008-01-11 20:59:12 +000023
24using System;
David Reiss7f42bcf2008-01-11 20:59:12 +000025using System.Net.Sockets;
26
27
28namespace Thrift.Transport
29{
30 public class TServerSocket : TServerTransport
31 {
32 /**
33 * Underlying server with socket
34 */
35 private TcpListener server = null;
36
37 /**
38 * Port to listen on
39 */
40 private int port = 0;
41
42 /**
43 * Timeout for client sockets from accept
44 */
45 private int clientTimeout = 0;
46
47 /**
David Reiss63191332009-01-06 19:49:22 +000048 * Whether or not to wrap new TSocket connections in buffers
49 */
50 private bool useBufferedSockets = false;
51
52 /**
David Reiss7f42bcf2008-01-11 20:59:12 +000053 * Creates a server socket from underlying socket object
54 */
55 public TServerSocket(TcpListener listener)
56 :this(listener, 0)
57 {
58 }
59
60 /**
61 * Creates a server socket from underlying socket object
62 */
63 public TServerSocket(TcpListener listener, int clientTimeout)
64 {
65 this.server = listener;
66 this.clientTimeout = clientTimeout;
67 }
68
69 /**
70 * Creates just a port listening server socket
71 */
72 public TServerSocket(int port)
73 : this(port, 0)
74 {
75 }
76
77 /**
78 * Creates just a port listening server socket
79 */
80 public TServerSocket(int port, int clientTimeout)
David Reiss63191332009-01-06 19:49:22 +000081 :this(port, clientTimeout, false)
82 {
83 }
84
85 public TServerSocket(int port, int clientTimeout, bool useBufferedSockets)
David Reiss7f42bcf2008-01-11 20:59:12 +000086 {
87 this.port = port;
88 this.clientTimeout = clientTimeout;
David Reiss63191332009-01-06 19:49:22 +000089 this.useBufferedSockets = useBufferedSockets;
David Reiss7f42bcf2008-01-11 20:59:12 +000090 try
91 {
92 // Make server socket
93 server = new TcpListener(System.Net.IPAddress.Any, this.port);
94 }
David Reisscee1e082008-04-02 22:10:09 +000095 catch (Exception)
David Reiss7f42bcf2008-01-11 20:59:12 +000096 {
97 server = null;
98 throw new TTransportException("Could not create ServerSocket on port " + port + ".");
99 }
100 }
101
102 public override void Listen()
103 {
104 // Make sure not to block on accept
105 if (server != null)
106 {
107 try
108 {
109 server.Start();
110 }
111 catch (SocketException sx)
112 {
David Reiss63191332009-01-06 19:49:22 +0000113 throw new TTransportException("Could not accept on listening socket: " + sx.Message);
David Reiss7f42bcf2008-01-11 20:59:12 +0000114 }
115 }
116 }
117
118 protected override TTransport AcceptImpl()
119 {
120 if (server == null)
121 {
122 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket.");
123 }
124 try
125 {
126 TcpClient result = server.AcceptTcpClient();
127 TSocket result2 = new TSocket(result);
128 result2.Timeout = clientTimeout;
David Reiss63191332009-01-06 19:49:22 +0000129 if (useBufferedSockets)
130 {
131 TBufferedTransport result3 = new TBufferedTransport(result2);
132 return result3;
133 }
134 else
135 {
136 return result2;
137 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000138 }
139 catch (Exception ex)
140 {
141 throw new TTransportException(ex.ToString());
142 }
143 }
144
145 public override void Close()
146 {
147 if (server != null)
148 {
149 try
150 {
151 server.Stop();
152 }
153 catch (Exception ex)
154 {
David Reiss63191332009-01-06 19:49:22 +0000155 throw new TTransportException("WARNING: Could not close server socket: " + ex);
David Reiss7f42bcf2008-01-11 20:59:12 +0000156 }
157 server = null;
158 }
159 }
160 }
161}