blob: d8fa335ad0738847c2003393a8d46ceffb363cc0 [file] [log] [blame]
Jens Geyerb89316d2020-02-28 19:22:34 +01001/**
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 *
19 * Contains some contributions under the Thrift Software License.
20 * Please see doc/old-thrift-license.txt in the Thrift distribution for
21 * details.
22 */
23
24using System;
25using 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
47 public TSocket(string host, int port)
48 : this(host, port, 0)
49 {
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 this.client = TSocketVersionizer.CreateTcpClient();
64 this.client.ReceiveTimeout = client.SendTimeout = timeout;
65 this.client.Client.NoDelay = true;
66 }
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
84 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
100 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
135 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
163 inputStream = client.GetStream();
164 outputStream = client.GetStream();
165 }
166
167
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 (Exception)
181 {
182 // catch that away
183 }
184
185 if (hlp.DoCleanup)
186 {
187 try
188 {
189 asyncres.AsyncWaitHandle.Close();
190 }
191 catch (Exception) { }
192
193 try
194 {
195 if (hlp.Client is IDisposable)
196 ((IDisposable)hlp.Client).Dispose();
197 }
198 catch (Exception) { }
199 hlp.Client = null;
200 }
201 }
202 }
203
204 private class ConnectHelper
205 {
206 public object Mutex = new object();
207 public bool DoCleanup = false;
208 public bool CallbackDone = false;
209 public TcpClient Client;
210 public ConnectHelper(TcpClient client)
211 {
212 Client = client;
213 }
214 }
215
216 public override void Close()
217 {
218 base.Close();
219 if (client != null)
220 {
221 client.Close();
222 client = null;
223 }
224 }
225
226 #region " IDisposable Support "
227 private bool _IsDisposed;
228
229 // IDisposable
230 protected override void Dispose(bool disposing)
231 {
232 if (!_IsDisposed)
233 {
234 if (disposing)
235 {
236 if (client != null)
237 ((IDisposable)client).Dispose();
238 base.Dispose(disposing);
239 }
240 }
241 _IsDisposed = true;
242 }
243 #endregion
244 }
245}