blob: 2658fce89ef3ef31a4c0082aafa581e8d6307701 [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.
18 */
David Reiss7f42bcf2008-01-11 20:59:12 +000019
20using System;
David Reiss7f42bcf2008-01-11 20:59:12 +000021using System.Net.Sockets;
22
23
24namespace Thrift.Transport
25{
26 public class TServerSocket : TServerTransport
27 {
28 /**
29 * Underlying server with socket
30 */
31 private TcpListener server = null;
32
33 /**
34 * Port to listen on
35 */
36 private int port = 0;
37
38 /**
39 * Timeout for client sockets from accept
40 */
41 private int clientTimeout = 0;
42
43 /**
David Reiss63191332009-01-06 19:49:22 +000044 * Whether or not to wrap new TSocket connections in buffers
45 */
46 private bool useBufferedSockets = false;
47
48 /**
David Reiss7f42bcf2008-01-11 20:59:12 +000049 * Creates a server socket from underlying socket object
50 */
51 public TServerSocket(TcpListener listener)
52 :this(listener, 0)
53 {
54 }
55
56 /**
57 * Creates a server socket from underlying socket object
58 */
59 public TServerSocket(TcpListener listener, int clientTimeout)
60 {
61 this.server = listener;
62 this.clientTimeout = clientTimeout;
63 }
64
65 /**
66 * Creates just a port listening server socket
67 */
68 public TServerSocket(int port)
69 : this(port, 0)
70 {
71 }
72
73 /**
74 * Creates just a port listening server socket
75 */
76 public TServerSocket(int port, int clientTimeout)
David Reiss63191332009-01-06 19:49:22 +000077 :this(port, clientTimeout, false)
78 {
79 }
80
81 public TServerSocket(int port, int clientTimeout, bool useBufferedSockets)
David Reiss7f42bcf2008-01-11 20:59:12 +000082 {
83 this.port = port;
84 this.clientTimeout = clientTimeout;
David Reiss63191332009-01-06 19:49:22 +000085 this.useBufferedSockets = useBufferedSockets;
David Reiss7f42bcf2008-01-11 20:59:12 +000086 try
87 {
88 // Make server socket
89 server = new TcpListener(System.Net.IPAddress.Any, this.port);
90 }
David Reisscee1e082008-04-02 22:10:09 +000091 catch (Exception)
David Reiss7f42bcf2008-01-11 20:59:12 +000092 {
93 server = null;
94 throw new TTransportException("Could not create ServerSocket on port " + port + ".");
95 }
96 }
97
98 public override void Listen()
99 {
100 // Make sure not to block on accept
101 if (server != null)
102 {
103 try
104 {
105 server.Start();
106 }
107 catch (SocketException sx)
108 {
David Reiss63191332009-01-06 19:49:22 +0000109 throw new TTransportException("Could not accept on listening socket: " + sx.Message);
David Reiss7f42bcf2008-01-11 20:59:12 +0000110 }
111 }
112 }
113
114 protected override TTransport AcceptImpl()
115 {
116 if (server == null)
117 {
118 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket.");
119 }
120 try
121 {
122 TcpClient result = server.AcceptTcpClient();
123 TSocket result2 = new TSocket(result);
124 result2.Timeout = clientTimeout;
David Reiss63191332009-01-06 19:49:22 +0000125 if (useBufferedSockets)
126 {
127 TBufferedTransport result3 = new TBufferedTransport(result2);
128 return result3;
129 }
130 else
131 {
132 return result2;
133 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000134 }
135 catch (Exception ex)
136 {
137 throw new TTransportException(ex.ToString());
138 }
139 }
140
141 public override void Close()
142 {
143 if (server != null)
144 {
145 try
146 {
147 server.Stop();
148 }
149 catch (Exception ex)
150 {
David Reiss63191332009-01-06 19:49:22 +0000151 throw new TTransportException("WARNING: Could not close server socket: " + ex);
David Reiss7f42bcf2008-01-11 20:59:12 +0000152 }
153 server = null;
154 }
155 }
156 }
157}