blob: 35fbe975dd45b8e146f870ed97a13a684011ff67 [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
27namespace Thrift.Transport
28{
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;
35
36 public TSocket(TcpClient client)
37 {
38 this.client = client;
39
40 if (IsOpen)
41 {
42 inputStream = client.GetStream();
43 outputStream = client.GetStream();
44 }
45 }
46
Jens Geyerf36fda22014-02-24 22:57:52 +010047 public TSocket(string host, int port)
48 : this(host, port, 0)
David Reiss7f42bcf2008-01-11 20:59:12 +000049 {
50 }
51
52 public TSocket(string host, int port, int timeout)
53 {
54 this.host = host;
55 this.port = port;
56 this.timeout = timeout;
57
58 InitSocket();
59 }
60
61 private void InitSocket()
62 {
63 client = new TcpClient();
64 client.ReceiveTimeout = client.SendTimeout = timeout;
Bryan Duxbury2d9dfdb2011-02-08 16:38:15 +000065 client.Client.NoDelay = true;
David Reiss7f42bcf2008-01-11 20:59:12 +000066 }
67
68 public int Timeout
69 {
70 set
71 {
72 client.ReceiveTimeout = client.SendTimeout = timeout = value;
73 }
74 }
75
76 public TcpClient TcpClient
77 {
78 get
79 {
80 return client;
81 }
82 }
83
David Reiss63191332009-01-06 19:49:22 +000084 public string Host
85 {
86 get
87 {
88 return host;
89 }
90 }
91
92 public int Port
93 {
94 get
95 {
96 return port;
97 }
98 }
99
David Reiss7f42bcf2008-01-11 20:59:12 +0000100 public override bool IsOpen
101 {
102 get
103 {
104 if (client == null)
105 {
106 return false;
107 }
108
109 return client.Connected;
110 }
111 }
112
113 public override void Open()
114 {
115 if (IsOpen)
116 {
117 throw new TTransportException(TTransportException.ExceptionType.AlreadyOpen, "Socket already connected");
118 }
119
120 if (String.IsNullOrEmpty(host))
121 {
122 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open null host");
123 }
124
125 if (port <= 0)
126 {
127 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open without port");
128 }
129
130 if (client == null)
131 {
132 InitSocket();
133 }
134
Jens Geyer866c23b2013-07-05 19:20:27 +0200135 if( timeout == 0) // no timeout -> infinite
136 {
137 client.Connect(host, port);
138 }
139 else // we have a timeout -> use it
140 {
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 {
148 if( hlp.CallbackDone)
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 }
161 }
162
David Reiss7f42bcf2008-01-11 20:59:12 +0000163 inputStream = client.GetStream();
164 outputStream = client.GetStream();
165 }
166
Jens Geyer866c23b2013-07-05 19:20:27 +0200167
168 static void ConnectCallback(IAsyncResult asyncres)
169 {
170 ConnectHelper hlp = asyncres.AsyncState as ConnectHelper;
171 lock (hlp.Mutex)
172 {
173 hlp.CallbackDone = true;
174
175 try
176 {
177 if( hlp.Client.Client != null)
178 hlp.Client.EndConnect(asyncres);
179 }
Jens Geyerd26f6fd2014-03-19 00:21:49 +0200180 catch (Exception)
Jens Geyer866c23b2013-07-05 19:20:27 +0200181 {
182 // catch that away
183 }
184
185 if (hlp.DoCleanup)
186 {
Jens Geyerd26f6fd2014-03-19 00:21:49 +0200187 try {
188 asyncres.AsyncWaitHandle.Close();
189 } catch (Exception) {}
190
191 try {
192 if (hlp.Client is IDisposable)
193 ((IDisposable)hlp.Client).Dispose();
194 } catch (Exception) {}
Jens Geyer866c23b2013-07-05 19:20:27 +0200195 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
David Reiss7f42bcf2008-01-11 20:59:12 +0000212 public override void Close()
213 {
214 base.Close();
215 if (client != null)
216 {
217 client.Close();
218 client = null;
219 }
220 }
Roger Meierb1ec4cc2012-04-11 21:21:41 +0000221
222 #region " IDisposable Support "
223 private bool _IsDisposed;
224
225 // IDisposable
226 protected override void Dispose(bool disposing)
227 {
228 if (!_IsDisposed)
229 {
230 if (disposing)
231 {
232 if (client != null)
233 ((IDisposable)client).Dispose();
234 base.Dispose(disposing);
235 }
236 }
237 _IsDisposed = true;
238 }
239 #endregion
240 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000241}