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 | namespace Thrift.Transport |
| 28 | { |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 29 | public class TSocket : TStreamTransport |
| 30 | { |
| 31 | private TcpClient client = null; |
| 32 | private string host = null; |
| 33 | private int port = 0; |
| 34 | private int timeout = 0; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 35 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 36 | public TSocket(TcpClient client) |
| 37 | { |
| 38 | this.client = client; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 39 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 40 | if (IsOpen) |
| 41 | { |
| 42 | inputStream = client.GetStream(); |
| 43 | outputStream = client.GetStream(); |
| 44 | } |
| 45 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 46 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 47 | public TSocket(string host, int port) |
| 48 | : this(host, port, 0) |
| 49 | { |
| 50 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 51 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 52 | public TSocket(string host, int port, int timeout) |
| 53 | { |
| 54 | this.host = host; |
| 55 | this.port = port; |
| 56 | this.timeout = timeout; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 57 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 58 | InitSocket(); |
| 59 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 60 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 61 | private void InitSocket() |
| 62 | { |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame^] | 63 | client = new TcpClient(); |
| 64 | client.ReceiveTimeout = client.SendTimeout = timeout; |
| 65 | client.Client.NoDelay = true; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 66 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 67 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 68 | public int Timeout |
| 69 | { |
| 70 | set |
| 71 | { |
| 72 | client.ReceiveTimeout = client.SendTimeout = timeout = value; |
| 73 | } |
| 74 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 75 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 76 | public TcpClient TcpClient |
| 77 | { |
| 78 | get |
| 79 | { |
| 80 | return client; |
| 81 | } |
| 82 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 83 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 84 | public string Host |
| 85 | { |
| 86 | get |
| 87 | { |
| 88 | return host; |
| 89 | } |
| 90 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 91 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 92 | public int Port |
| 93 | { |
| 94 | get |
| 95 | { |
| 96 | return port; |
| 97 | } |
| 98 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 99 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 100 | public override bool IsOpen |
| 101 | { |
| 102 | get |
| 103 | { |
| 104 | if (client == null) |
| 105 | { |
| 106 | return false; |
| 107 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 108 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 109 | return client.Connected; |
| 110 | } |
| 111 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 112 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 113 | public override void Open() |
| 114 | { |
| 115 | if (IsOpen) |
| 116 | { |
| 117 | throw new TTransportException(TTransportException.ExceptionType.AlreadyOpen, "Socket already connected"); |
| 118 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 119 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 120 | if (String.IsNullOrEmpty(host)) |
| 121 | { |
| 122 | throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open null host"); |
| 123 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 124 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 125 | if (port <= 0) |
| 126 | { |
| 127 | throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open without port"); |
| 128 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 129 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 130 | if (client == null) |
| 131 | { |
| 132 | InitSocket(); |
| 133 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 134 | |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame^] | 135 | if( timeout == 0) // no timeout -> infinite |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 136 | { |
| 137 | client.Connect(host, port); |
| 138 | } |
| 139 | else // we have a timeout -> use it |
| 140 | { |
Jens Geyer | 866c23b | 2013-07-05 19:20:27 +0200 | [diff] [blame] | 141 | ConnectHelper hlp = new ConnectHelper(client); |
| 142 | IAsyncResult asyncres = client.BeginConnect(host, port, new AsyncCallback(ConnectCallback), hlp); |
| 143 | bool bConnected = asyncres.AsyncWaitHandle.WaitOne(timeout) && client.Connected; |
| 144 | if (!bConnected) |
| 145 | { |
| 146 | lock (hlp.Mutex) |
| 147 | { |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame^] | 148 | if( hlp.CallbackDone) |
Jens Geyer | 866c23b | 2013-07-05 19:20:27 +0200 | [diff] [blame] | 149 | { |
| 150 | asyncres.AsyncWaitHandle.Close(); |
| 151 | client.Close(); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | hlp.DoCleanup = true; |
| 156 | client = null; |
| 157 | } |
| 158 | } |
| 159 | throw new TTransportException(TTransportException.ExceptionType.TimedOut, "Connect timed out"); |
| 160 | } |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 161 | } |
Jens Geyer | 866c23b | 2013-07-05 19:20:27 +0200 | [diff] [blame] | 162 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 163 | inputStream = client.GetStream(); |
| 164 | outputStream = client.GetStream(); |
| 165 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 166 | |
Jens Geyer | 866c23b | 2013-07-05 19:20:27 +0200 | [diff] [blame] | 167 | |
| 168 | static void ConnectCallback(IAsyncResult asyncres) |
| 169 | { |
| 170 | ConnectHelper hlp = asyncres.AsyncState as ConnectHelper; |
| 171 | lock (hlp.Mutex) |
| 172 | { |
| 173 | hlp.CallbackDone = true; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 174 | |
Jens Geyer | 866c23b | 2013-07-05 19:20:27 +0200 | [diff] [blame] | 175 | try |
| 176 | { |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame^] | 177 | if( hlp.Client.Client != null) |
Jens Geyer | 866c23b | 2013-07-05 19:20:27 +0200 | [diff] [blame] | 178 | hlp.Client.EndConnect(asyncres); |
| 179 | } |
Jens Geyer | d26f6fd | 2014-03-19 00:21:49 +0200 | [diff] [blame] | 180 | catch (Exception) |
Jens Geyer | 866c23b | 2013-07-05 19:20:27 +0200 | [diff] [blame] | 181 | { |
| 182 | // catch that away |
| 183 | } |
| 184 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 185 | if (hlp.DoCleanup) |
Jens Geyer | 866c23b | 2013-07-05 19:20:27 +0200 | [diff] [blame] | 186 | { |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame^] | 187 | try { |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 188 | asyncres.AsyncWaitHandle.Close(); |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame^] | 189 | } catch (Exception) {} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 190 | |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame^] | 191 | try { |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 192 | if (hlp.Client is IDisposable) |
| 193 | ((IDisposable)hlp.Client).Dispose(); |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame^] | 194 | } catch (Exception) {} |
Jens Geyer | 866c23b | 2013-07-05 19:20:27 +0200 | [diff] [blame] | 195 | hlp.Client = null; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | private class ConnectHelper |
| 201 | { |
| 202 | public object Mutex = new object(); |
| 203 | public bool DoCleanup = false; |
| 204 | public bool CallbackDone = false; |
| 205 | public TcpClient Client; |
| 206 | public ConnectHelper(TcpClient client) |
| 207 | { |
| 208 | Client = client; |
| 209 | } |
| 210 | } |
| 211 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 212 | public override void Close() |
| 213 | { |
| 214 | base.Close(); |
| 215 | if (client != null) |
| 216 | { |
| 217 | client.Close(); |
| 218 | client = null; |
| 219 | } |
| 220 | } |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 221 | |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame^] | 222 | #region " IDisposable Support " |
| 223 | private bool _IsDisposed; |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 224 | |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame^] | 225 | // IDisposable |
| 226 | protected override void Dispose(bool disposing) |
| 227 | { |
| 228 | if (!_IsDisposed) |
| 229 | { |
| 230 | if (disposing) |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 231 | { |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame^] | 232 | if (client != null) |
| 233 | ((IDisposable)client).Dispose(); |
| 234 | base.Dispose(disposing); |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 235 | } |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame^] | 236 | } |
| 237 | _IsDisposed = true; |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 238 | } |
Jens Geyer | 95717c9 | 2015-04-23 22:48:13 +0200 | [diff] [blame^] | 239 | #endregion |
| 240 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 241 | } |