blob: 7717cfb7e1e73d5488e128592564039ca38cee4b [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 }
180 catch (SocketException)
181 {
182 // catch that away
183 }
184
185 if (hlp.DoCleanup)
186 {
187 asyncres.AsyncWaitHandle.Close();
188 if (hlp.Client is IDisposable)
189 ((IDisposable)hlp.Client).Dispose();
190 hlp.Client = null;
191 }
192 }
193 }
194
195 private class ConnectHelper
196 {
197 public object Mutex = new object();
198 public bool DoCleanup = false;
199 public bool CallbackDone = false;
200 public TcpClient Client;
201 public ConnectHelper(TcpClient client)
202 {
203 Client = client;
204 }
205 }
206
David Reiss7f42bcf2008-01-11 20:59:12 +0000207 public override void Close()
208 {
209 base.Close();
210 if (client != null)
211 {
212 client.Close();
213 client = null;
214 }
215 }
Roger Meierb1ec4cc2012-04-11 21:21:41 +0000216
217 #region " IDisposable Support "
218 private bool _IsDisposed;
219
220 // IDisposable
221 protected override void Dispose(bool disposing)
222 {
223 if (!_IsDisposed)
224 {
225 if (disposing)
226 {
227 if (client != null)
228 ((IDisposable)client).Dispose();
229 base.Dispose(disposing);
230 }
231 }
232 _IsDisposed = true;
233 }
234 #endregion
235 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000236}