Kevin Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame] | 1 | /** |
| 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 Lipcon | 53ae9f3 | 2009-12-07 00:42:38 +0000 | [diff] [blame] | 18 | * |
| 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 Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame] | 22 | */ |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 23 | |
| 24 | using System; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 25 | using System.Net.Sockets; |
| 26 | |
| 27 | |
| 28 | namespace Thrift.Transport |
| 29 | { |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame^] | 30 | public class TServerSocket : TServerTransport |
| 31 | { |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 32 | /** |
| 33 | * Underlying server with socket |
| 34 | */ |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame^] | 35 | private TcpListener server = null; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 36 | |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame^] | 37 | /** |
| 38 | * Port to listen on |
| 39 | */ |
| 40 | private int port = 0; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 41 | |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame^] | 42 | /** |
| 43 | * Timeout for client sockets from accept |
| 44 | */ |
| 45 | private int clientTimeout = 0; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 46 | |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame^] | 47 | /** |
| 48 | * Whether or not to wrap new TSocket connections in buffers |
| 49 | */ |
| 50 | private bool useBufferedSockets = false; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 51 | |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame^] | 52 | /** |
| 53 | * Creates a server socket from underlying socket object |
| 54 | */ |
| 55 | public TServerSocket(TcpListener listener) |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame] | 56 | :this(listener, 0) |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame^] | 57 | { |
| 58 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 59 | |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame^] | 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 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 68 | |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame^] | 69 | /** |
| 70 | * Creates just a port listening server socket |
| 71 | */ |
| 72 | public TServerSocket(int port) |
| 73 | : this(port, 0) |
| 74 | { |
| 75 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 76 | |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame^] | 77 | /** |
| 78 | * Creates just a port listening server socket |
| 79 | */ |
| 80 | public TServerSocket(int port, int clientTimeout) |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame] | 81 | :this(port, clientTimeout, false) |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame^] | 82 | { |
| 83 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 84 | |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame^] | 85 | public TServerSocket(int port, int clientTimeout, bool useBufferedSockets) |
| 86 | { |
| 87 | this.port = port; |
| 88 | this.clientTimeout = clientTimeout; |
| 89 | this.useBufferedSockets = useBufferedSockets; |
| 90 | try |
| 91 | { |
| 92 | // Make server socket |
| 93 | this.server = TSocketVersionizer.CreateTcpListener(port); |
| 94 | this.server.Server.NoDelay = true; |
| 95 | } |
| 96 | catch (Exception) |
| 97 | { |
| 98 | server = null; |
| 99 | throw new TTransportException("Could not create ServerSocket on port " + port + "."); |
| 100 | } |
| 101 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 102 | |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame^] | 103 | public override void Listen() |
| 104 | { |
| 105 | // Make sure not to block on accept |
| 106 | if (server != null) |
| 107 | { |
| 108 | try |
| 109 | { |
| 110 | server.Start(); |
| 111 | } |
| 112 | catch (SocketException sx) |
| 113 | { |
| 114 | throw new TTransportException("Could not accept on listening socket: " + sx.Message); |
| 115 | } |
| 116 | } |
| 117 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 118 | |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame^] | 119 | protected override TTransport AcceptImpl() |
| 120 | { |
| 121 | if (server == null) |
| 122 | { |
| 123 | throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket."); |
| 124 | } |
| 125 | try |
| 126 | { |
| 127 | TSocket result2 = null; |
| 128 | TcpClient result = server.AcceptTcpClient(); |
| 129 | try |
| 130 | { |
| 131 | result2 = new TSocket(result); |
| 132 | result2.Timeout = clientTimeout; |
| 133 | if (useBufferedSockets) |
| 134 | { |
| 135 | TBufferedTransport result3 = new TBufferedTransport(result2); |
| 136 | return result3; |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | return result2; |
| 141 | } |
| 142 | } |
| 143 | catch (System.Exception) |
| 144 | { |
| 145 | // If a TSocket was successfully created, then let |
| 146 | // it do proper cleanup of the TcpClient object. |
| 147 | if (result2 != null) |
| 148 | result2.Dispose(); |
| 149 | else // Otherwise, clean it up ourselves. |
| 150 | ((IDisposable)result).Dispose(); |
| 151 | throw; |
| 152 | } |
| 153 | } |
| 154 | catch (Exception ex) |
| 155 | { |
| 156 | throw new TTransportException(ex.ToString()); |
| 157 | } |
| 158 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 159 | |
Jens Geyer | feea478 | 2017-01-28 19:53:28 +0100 | [diff] [blame^] | 160 | public override void Close() |
| 161 | { |
| 162 | if (server != null) |
| 163 | { |
| 164 | try |
| 165 | { |
| 166 | server.Stop(); |
| 167 | } |
| 168 | catch (Exception ex) |
| 169 | { |
| 170 | throw new TTransportException("WARNING: Could not close server socket: " + ex); |
| 171 | } |
| 172 | server = null; |
| 173 | } |
| 174 | } |
| 175 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 176 | } |